aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plugins/base/src/main/kotlin/parsers/MarkdownParser.kt35
-rw-r--r--plugins/base/src/main/resources/dokka/styles/style.css4
-rw-r--r--plugins/base/src/test/kotlin/markdown/ParserTest.kt23
3 files changed, 50 insertions, 12 deletions
diff --git a/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt
index 0fa4e360..8c7f9257 100644
--- a/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt
+++ b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt
@@ -1,7 +1,9 @@
package org.jetbrains.dokka.base.parsers
import com.intellij.psi.PsiElement
+import org.intellij.markdown.MarkdownElementType
import org.intellij.markdown.MarkdownElementTypes
+import org.intellij.markdown.MarkdownElementTypes.LINK_DESTINATION
import org.intellij.markdown.MarkdownTokenTypes
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.CompositeASTNode
@@ -213,17 +215,24 @@ class MarkdownParser(
false
}
- private fun imagesHandler(node: ASTNode): DocTag {
- val linkNode =
- node.children.last().children.find { it.type == MarkdownElementTypes.LINK_LABEL }!!.children[1]
- val link = text.substring(linkNode.startOffset, linkNode.endOffset)
- val src = mapOf("src" to link)
- return DocTagsFromIElementFactory.getInstance(
- node.type,
- params = src,
- children = listOf(visitNode(node.children.last().children.find { it.type == MarkdownElementTypes.LINK_TEXT }!!))
- )
- }
+ private fun imagesHandler(node: ASTNode): DocTag =
+ with(node.children.last().children) {
+ val destination = find { it.type == MarkdownElementTypes.LINK_DESTINATION }
+ val description = find { it.type == MarkdownElementTypes.LINK_TEXT }
+
+ val src = destination?.let {
+ mapOf("href" to text.substring(it.startOffset, it.endOffset))
+ } ?: emptyMap()
+
+ val alt = description?.let {
+ mapOf("alt" to text.substring(it.startOffset + 1, it.endOffset - 1))
+ } ?: emptyMap()
+
+ return DocTagsFromIElementFactory.getInstance(
+ node.type,
+ params = src + alt
+ )
+ }
private fun codeSpansHandler(node: ASTNode): DocTag =
DocTagsFromIElementFactory.getInstance(
@@ -380,7 +389,9 @@ class MarkdownParser(
private fun mergedLeafNode(nodes: List<ASTNode>, index: Int, startOffset: Int, sIndex: Int): LeafASTNode? {
val endOffset = nodes[index].endOffset
if (text.substring(startOffset, endOffset).transform().trim().isNotEmpty()) {
- val type = if (nodes.subList(sIndex, index).any { it.type == MarkdownTokenTypes.CODE_LINE }) MarkdownTokenTypes.CODE_LINE else MarkdownTokenTypes.TEXT
+ val type = if (nodes.subList(sIndex, index)
+ .any { it.type == MarkdownTokenTypes.CODE_LINE }
+ ) MarkdownTokenTypes.CODE_LINE else MarkdownTokenTypes.TEXT
return LeafASTNode(type, startOffset, endOffset)
}
return null
diff --git a/plugins/base/src/main/resources/dokka/styles/style.css b/plugins/base/src/main/resources/dokka/styles/style.css
index f5a1e65b..41e2536f 100644
--- a/plugins/base/src/main/resources/dokka/styles/style.css
+++ b/plugins/base/src/main/resources/dokka/styles/style.css
@@ -543,6 +543,10 @@ th {
color: #444;
}
+p.paragraph img {
+ display: block;
+}
+
img {
max-width: 100%;
}
diff --git a/plugins/base/src/test/kotlin/markdown/ParserTest.kt b/plugins/base/src/test/kotlin/markdown/ParserTest.kt
index 838233eb..e127afc2 100644
--- a/plugins/base/src/test/kotlin/markdown/ParserTest.kt
+++ b/plugins/base/src/test/kotlin/markdown/ParserTest.kt
@@ -1167,5 +1167,28 @@ class ParserTest : KDocTest() {
)
executeTest(kdoc, expectedDocumentationNode)
}
+
+ @Test
+ fun image(){
+ val kdoc = "![Sample image](https://www.google.pl/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png)"
+ val expectedDocumentationNode = DocumentationNode(
+ listOf(
+ Description(
+ P(
+ listOf(
+ Img(
+ emptyList(),
+ mapOf(
+ "href" to "https://www.google.pl/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
+ "alt" to "Sample image"
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ executeTest(kdoc, expectedDocumentationNode)
+ }
}