aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrzej Ratajczak <andrzej.ratajczak98@gmail.com>2020-08-27 12:47:31 +0200
committerAndrzej Ratajczak <32793002+BarkingBad@users.noreply.github.com>2020-09-14 13:44:34 +0200
commitba05c354a353ac3145c775e190b96b2bbe3c1227 (patch)
tree9a9b432cf5784b0ac0e8a9b35ecdce727d249aa6
parent30d6e6c3d82993e76c3942fc9faf102f36736673 (diff)
downloaddokka-ba05c354a353ac3145c775e190b96b2bbe3c1227.tar.gz
dokka-ba05c354a353ac3145c775e190b96b2bbe3c1227.tar.bz2
dokka-ba05c354a353ac3145c775e190b96b2bbe3c1227.zip
Refactor DocTags and add markdown tests
-rw-r--r--core/src/main/kotlin/model/doc/DocTag.kt446
-rw-r--r--plugins/base/src/main/kotlin/parsers/MarkdownParser.kt83
-rw-r--r--plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt4
-rw-r--r--plugins/base/src/test/kotlin/markdown/ParserTest.kt119
4 files changed, 518 insertions, 134 deletions
diff --git a/core/src/main/kotlin/model/doc/DocTag.kt b/core/src/main/kotlin/model/doc/DocTag.kt
index 0d3b0612..b2530ca6 100644
--- a/core/src/main/kotlin/model/doc/DocTag.kt
+++ b/core/src/main/kotlin/model/doc/DocTag.kt
@@ -3,111 +3,353 @@ package org.jetbrains.dokka.model.doc
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.WithChildren
-sealed class DocTag(
- override val children: List<DocTag>,
- val params: Map<String, String>
-) : WithChildren<DocTag> {
- override fun equals(other: Any?): Boolean =
- (
- other != null &&
- other::class == this::class &&
- this.children == (other as DocTag).children &&
- this.params == other.params
- )
-
- override fun hashCode(): Int = children.hashCode() + params.hashCode()
+sealed class DocTag : WithChildren<DocTag> {
+ abstract val params: Map<String, String>
}
-class A(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Big(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class B(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class BlockQuote(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) :
- DocTag(children, params)
-
-object Br : DocTag(emptyList(), emptyMap())
-class Cite(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-sealed class Code(children: List<DocTag>, params: Map<String, String>) : DocTag(children, params)
-class CodeInline(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) :
- Code(children, params)
-
-class CodeBlock(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : Code(children, params)
-class Dd(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Dfn(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Dir(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Div(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Dl(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Dt(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Em(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Font(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Footer(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Frame(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class FrameSet(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) :
- DocTag(children, params)
-
-class H1(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class H2(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class H3(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class H4(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class H5(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class H6(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Head(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Header(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Html(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class I(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class IFrame(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Img(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Input(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Li(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Link(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Listing(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Main(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Menu(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Meta(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Nav(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class NoFrames(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) :
- DocTag(children, params)
-
-class NoScript(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) :
- DocTag(children, params)
-
-class Ol(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class P(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Pre(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Script(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Section(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Small(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Span(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Strikethrough(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) :
- DocTag(children, params)
-
-class Strong(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Sub(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Sup(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Table(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Text(val body: String = "", children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) :
- DocTag(children, params) {
- override fun equals(other: Any?): Boolean = super.equals(other) && this.body == (other as Text).body
- override fun hashCode(): Int = super.hashCode() + body.hashCode()
+data class A(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Big(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class B(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class BlockQuote(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+object Br : DocTag() {
+ override val params = emptyMap<String, String>()
+ override val children = emptyList<DocTag>()
}
-class TBody(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Td(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class TFoot(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Th(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class THead(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Title(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Tr(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Tt(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class U(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Ul(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Var(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class DocumentationLink(val dri: DRI, children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) :
- DocTag(children, params) {
- override fun equals(other: Any?): Boolean = super.equals(other) && this.dri == (other as DocumentationLink).dri
- override fun hashCode(): Int = super.hashCode() + dri.hashCode()
+data class Cite(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+sealed class Code : DocTag()
+
+data class CodeInline(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : Code()
+
+data class CodeBlock(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : Code()
+
+data class CustomDocTag(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Dd(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Dfn(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Dir(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Div(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Dl(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class DocumentationLink(
+ val dri: DRI,
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Dt(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Em(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Font(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Footer(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Frame(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class FrameSet(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class H1(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class H2(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class H3(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class H4(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class H5(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class H6(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Head(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Header(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+object HorizontalRule : DocTag() {
+ override val params = emptyMap<String, String>()
+ override val children = emptyList<DocTag>()
}
-object HorizontalRule : DocTag(emptyList(), emptyMap())
-class Index(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class CustomDocTag(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) :
- DocTag(children, params)
+data class Html(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class I(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class IFrame(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Img(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Index(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Input(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Li(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Link(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Listing(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Main(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Menu(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Meta(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Nav(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class NoFrames(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class NoScript(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Ol(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class P(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Pre(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Script(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Section(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Small(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Span(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Strikethrough(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Strong(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Sub(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Sup(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Table(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Text(
+ val body: String = "",
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class TBody(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Td(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class TFoot(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Th(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class THead(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Title(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Tr(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Tt(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class U(override val children: List<DocTag> = emptyList(), override val params: Map<String, String> = emptyMap()) :
+ DocTag()
+
+data class Ul(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
+
+data class Var(
+ override val children: List<DocTag> = emptyList(),
+ override val params: Map<String, String> = emptyMap()
+) : DocTag()
diff --git a/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt
index 8c7f9257..a74f2b05 100644
--- a/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt
+++ b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt
@@ -1,9 +1,7 @@
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
@@ -208,6 +206,46 @@ class MarkdownParser(
}
}
+ private fun codeLineHandler(node: ASTNode) = DocTagsFromIElementFactory.getInstance(
+ MarkdownElementTypes.CODE_BLOCK,
+ body = text.substring(node.startOffset, node.endOffset)
+ )
+
+ private fun textHandler(node: ASTNode) = DocTagsFromIElementFactory.getInstance(
+ MarkdownTokenTypes.TEXT,
+ body = text.substring(node.startOffset, node.endOffset).transform()
+ )
+
+ private fun markdownFileHandler(node: ASTNode) = if (node.children.size == 1)
+ visitNode(node.children.first())
+ else
+ defaultHandler(node)
+
+ private fun strikeThroughHandler(node: ASTNode) = DocTagsFromIElementFactory.getInstance(
+ GFMElementTypes.STRIKETHROUGH,
+ body = text.substring(node.startOffset, node.endOffset).transform()
+ )
+
+ private fun tableHandler(node: ASTNode) = DocTagsFromIElementFactory.getInstance(
+ GFMElementTypes.TABLE,
+ children = node.children.filterTabSeparators().evaluateChildren()
+ )
+
+ private fun headerHandler(node: ASTNode) = DocTagsFromIElementFactory.getInstance(
+ GFMElementTypes.HEADER,
+ children = node.children.filterTabSeparators().evaluateChildren()
+ )
+
+ private fun rowHandler(node: ASTNode) = DocTagsFromIElementFactory.getInstance(
+ GFMElementTypes.ROW,
+ children = node.children.filterTabSeparators().evaluateChildren()
+ )
+
+ private fun cellHandler(node: ASTNode) = DocTagsFromIElementFactory.getInstance(
+ GFMTokenTypes.CELL,
+ children = node.children.filterTabSeparators().evaluateChildren().trimSurroundingTokensIfText()
+ )
+
private fun String.isRemoteLink() = try {
URL(this)
true
@@ -304,34 +342,14 @@ class MarkdownParser(
MarkdownElementTypes.IMAGE -> imagesHandler(node)
MarkdownTokenTypes.HARD_LINE_BREAK -> DocTagsFromIElementFactory.getInstance(node.type)
MarkdownTokenTypes.CODE_FENCE_CONTENT,
- MarkdownTokenTypes.CODE_LINE -> DocTagsFromIElementFactory.getInstance(
- MarkdownElementTypes.CODE_BLOCK,
- body = text.substring(node.startOffset, node.endOffset)
- )
- MarkdownTokenTypes.TEXT -> DocTagsFromIElementFactory.getInstance(
- MarkdownTokenTypes.TEXT,
- body = text.substring(node.startOffset, node.endOffset).transform()
- )
- MarkdownElementTypes.MARKDOWN_FILE -> if (node.children.size == 1) visitNode(node.children.first()) else defaultHandler(
- node
- )
- GFMElementTypes.STRIKETHROUGH -> DocTagsFromIElementFactory.getInstance(
- GFMElementTypes.STRIKETHROUGH,
- body = text
- .substring(node.startOffset, node.endOffset).transform()
- )
- GFMElementTypes.TABLE -> DocTagsFromIElementFactory.getInstance(
- GFMElementTypes.TABLE,
- children = node.children.filterTabSeparators().evaluateChildren()
- )
- GFMElementTypes.HEADER -> DocTagsFromIElementFactory.getInstance(
- GFMElementTypes.HEADER,
- children = node.children.filterTabSeparators().evaluateChildren()
- )
- GFMElementTypes.ROW -> DocTagsFromIElementFactory.getInstance(
- GFMElementTypes.ROW,
- children = node.children.filterTabSeparators().evaluateChildren()
- )
+ MarkdownTokenTypes.CODE_LINE -> codeLineHandler(node)
+ MarkdownTokenTypes.TEXT -> textHandler(node)
+ MarkdownElementTypes.MARKDOWN_FILE -> markdownFileHandler(node)
+ GFMElementTypes.STRIKETHROUGH -> strikeThroughHandler(node)
+ GFMElementTypes.TABLE -> tableHandler(node)
+ GFMElementTypes.HEADER -> headerHandler(node)
+ GFMElementTypes.ROW -> rowHandler(node)
+ GFMTokenTypes.CELL -> cellHandler(node)
else -> defaultHandler(node)
}
@@ -349,6 +367,11 @@ class MarkdownParser(
))
}
+ private fun List<DocTag>.trimSurroundingTokensIfText() = mapIndexed { index, elem ->
+ val elemTransformed = if (index == 0 && elem is Text) elem.copy(elem.body.trimStart()) else elem
+ if (index == size - 1 && elemTransformed is Text) elemTransformed.copy(elemTransformed.body.trimEnd()) else elemTransformed
+ }
+
private val notLeafNodes = listOf(MarkdownTokenTypes.HORIZONTAL_RULE, MarkdownTokenTypes.HARD_LINE_BREAK)
private fun List<ASTNode>.isNotLeaf(index: Int): Boolean =
diff --git a/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt
index 277fd35e..58b4b223 100644
--- a/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt
+++ b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt
@@ -5,6 +5,7 @@ import org.intellij.markdown.IElementType
import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.MarkdownTokenTypes
import org.intellij.markdown.flavours.gfm.GFMElementTypes
+import org.intellij.markdown.flavours.gfm.GFMTokenTypes
import org.jetbrains.dokka.links.DRI
import java.lang.NullPointerException
@@ -38,6 +39,7 @@ object DocTagsFromIElementFactory {
GFMElementTypes.TABLE -> Table(children, params)
GFMElementTypes.HEADER -> Th(children, params)
GFMElementTypes.ROW -> Tr(children, params)
+ GFMTokenTypes.CELL -> Td(children, params)
else -> CustomDocTag(children, params)
}
-} \ No newline at end of file
+}
diff --git a/plugins/base/src/test/kotlin/markdown/ParserTest.kt b/plugins/base/src/test/kotlin/markdown/ParserTest.kt
index e127afc2..268af405 100644
--- a/plugins/base/src/test/kotlin/markdown/ParserTest.kt
+++ b/plugins/base/src/test/kotlin/markdown/ParserTest.kt
@@ -1169,7 +1169,7 @@ class ParserTest : KDocTest() {
}
@Test
- fun image(){
+ fun image() {
val kdoc = "![Sample image](https://www.google.pl/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png)"
val expectedDocumentationNode = DocumentationNode(
listOf(
@@ -1190,5 +1190,122 @@ class ParserTest : KDocTest() {
)
executeTest(kdoc, expectedDocumentationNode)
}
+
+ @Test
+ fun `Bold + italic + link`() {
+ val kdoc = "It's very easy to make some words **bold** and other words *italic* with Markdown.\n" +
+ "You can even [link to Google!](http://google.com)"
+ val expectedDocumentationNode = DocumentationNode(
+ listOf(
+ Description(
+ P(
+ listOf(
+ Text("It's very easy to make some words "),
+ B(listOf(Text("bold"))),
+ Text(" and other words "),
+ I(listOf(Text("italic"))),
+ Text(" with Markdown. You can even "),
+ A(listOf(Text("link to Google!")), mapOf("href" to "http://google.com"))
+ )
+ )
+ )
+ )
+ )
+ executeTest(kdoc, expectedDocumentationNode)
+ }
+
+ @Test
+ fun `Codeblock from indent`() {
+ val kdoc = "Here is some example how to use conditional instructions:\n\n" +
+ " val x = 1\n" +
+ " val y = 2\n" +
+ " if (x == 1) {\n" +
+ " println(y)\n" +
+ " }"
+ val expectedDocumentationNode = DocumentationNode(
+ listOf(
+ Description(
+ P(
+ listOf(
+ P(listOf(Text("Here is some example how to use conditional instructions:"))),
+ CodeBlock(
+ listOf(
+ Text(
+ " val x = 1\n" +
+ " val y = 2\n" +
+ " if (x == 1) {\n" +
+ " println(y)\n" +
+ " }"
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ executeTest(kdoc, expectedDocumentationNode)
+ }
+
+ @Test
+ fun `Table`() {
+ val kdoc = "First Header | Second Header\n" +
+ "------------ | -------------\n" +
+ "Content from cell 1 | Content from cell 2\n" +
+ "Content in the first column | Content in the second column"
+ val expectedDocumentationNode = DocumentationNode(
+ listOf(
+ Description(
+ Table(
+ listOf(
+ Th(
+ listOf(
+ Td(
+ listOf(
+ Text("First Header")
+ )
+ ),
+ Td(
+ listOf(
+ Text("Second Header")
+ )
+ )
+ )
+ ),
+ Tr(
+ listOf(
+ Td(
+ listOf(
+ Text("Content from cell 1")
+ )
+ ),
+ Td(
+ listOf(
+ Text("Content from cell 2")
+ )
+ )
+ )
+ ),
+ Tr(
+ listOf(
+ Td(
+ listOf(
+ Text("Content in the first column")
+ )
+ ),
+ Td(
+ listOf(
+ Text("Content in the second column")
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ executeTest(kdoc, expectedDocumentationNode)
+ }
}