Repository: ajalt/mordant (kotlin)

Issue:
Handle images with reference full/short links
```
![img][1]

[1]: img.jpg
```

is not rendered properly. Render image link handles only INLINE_LINK type of references.
MarkdownRenderer.kt:357 renderImageLink()

Code is failing with 
```
java.lang.NullPointerException
        at com.github.ajalt.mordant.markdown.MarkdownRendererKt.firstChildOfType(MarkdownRenderer.kt:383)
        at com.github.ajalt.mordant.markdown.MarkdownRendererKt.access$firstChildOfType(MarkdownRenderer.kt:1)
        at com.github.ajalt.mordant.markdown.MarkdownRenderer.renderImageLink(MarkdownRenderer.kt:358)
        at com.github.ajalt.mordant.markdown.MarkdownRenderer.parseInlines(MarkdownRenderer.kt:226)
        at com.github.ajalt.mordant.markdown.MarkdownRenderer.innerInlines(MarkdownRenderer.kt:279)
        at com.github.ajalt.mordant.markdown.MarkdownRenderer.innerInlines$default(MarkdownRenderer.kt:277)
        at com.github.ajalt.mordant.markdown.MarkdownRenderer.parseStructure(MarkdownRenderer.kt:133)
        at com.github.ajalt.mordant.markdown.MarkdownRenderer.parseFile(MarkdownRenderer.kt:86)
        at com.github.ajalt.mordant.markdown.MarkdownRenderer.render(MarkdownRenderer.kt:80)
```

Pull request:
Add rendering for short and full reference links. 
Add small tweak for tests

Patch:
diff --git a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/markdown/MarkdownRenderer.kt b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/markdown/MarkdownRenderer.kt
index 95a5e30c2..008554926 100644
--- a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/markdown/MarkdownRenderer.kt
+++ b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/markdown/MarkdownRenderer.kt
@@ -356,13 +356,21 @@ internal class MarkdownRenderer(
     }
 
     private fun renderImageLink(node: ASTNode): Lines {
-        val text = findLinkText(node.firstChildOfType(MarkdownElementTypes.INLINE_LINK))
-            ?.takeUnless { it.isEmpty() }
-            ?: return EMPTY_LINES
-        return listOf(
-            parseText("🖼️ ", DEFAULT_STYLE),
-            text.replaceStyle(theme.style("markdown.img.alt-text"))
-        ).foldLines { it }
+        // Could be inline, full or short reference
+        val link = node.findChildOfType(MarkdownElementTypes.INLINE_LINK)
+            ?: node.findChildOfType(MarkdownElementTypes.FULL_REFERENCE_LINK)
+            ?: node.findChildOfType(MarkdownElementTypes.SHORT_REFERENCE_LINK)
+        if (link != null) {
+            val text = findLinkText(link)
+                ?.takeUnless { it.isEmpty() }
+                ?: return EMPTY_LINES
+            return listOf(
+                parseText("🖼️ ", DEFAULT_STYLE),
+                text.replaceStyle(theme.style("markdown.img.alt-text"))
+            ).foldLines { it }
+        } else {
+            return EMPTY_LINES
+        }
     }
 
     private fun findLinkLabel(node: ASTNode): String? {


Test patch:
diff --git a/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/markdown/MarkdownTest.kt b/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/markdown/MarkdownTest.kt
index da8461ea3..28a10d787 100644
--- a/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/markdown/MarkdownTest.kt
+++ b/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/markdown/MarkdownTest.kt
@@ -407,6 +407,10 @@ ${linkText("www.example.com/url")}
 ![an image](example.png)
 
 ![](example.png)
+
+![an image][1]
+
+[1]: example.png
 """, """
 🖼️ ${imgAlt("an image")}
 
@@ -415,6 +419,10 @@ ${linkText("www.example.com/url")}
 🖼️ ${imgAlt("an image")}
 
 
+
+🖼️ ${imgAlt("an image")}
+
+${linkDest("[1]: example.png")}
 """)
 
     @Test