aboutsummaryrefslogtreecommitdiff
path: root/subprojects/analysis-markdown-jb/src
diff options
context:
space:
mode:
authorIgnat Beresnev <ignat.beresnev@jetbrains.com>2023-10-31 12:16:38 +0100
committerGitHub <noreply@github.com>2023-10-31 12:16:38 +0100
commit7951aff0650b4c50b82a987ba4f23879f18c9436 (patch)
tree9b75e0089f8d391f50e1bb0f6849475b12b9874d /subprojects/analysis-markdown-jb/src
parentedcd1fb24d01e11b5a8185328255f2005aadf037 (diff)
downloaddokka-7951aff0650b4c50b82a987ba4f23879f18c9436.tar.gz
dokka-7951aff0650b4c50b82a987ba4f23879f18c9436.tar.bz2
dokka-7951aff0650b4c50b82a987ba4f23879f18c9436.zip
Update org.jetbrains.markdown from 0.3.1 to 0.5.2 (#3231)
Diffstat (limited to 'subprojects/analysis-markdown-jb/src')
-rw-r--r--subprojects/analysis-markdown-jb/src/main/kotlin/org/jetbrains/dokka/analysis/markdown/jb/MarkdownParser.kt39
1 files changed, 39 insertions, 0 deletions
diff --git a/subprojects/analysis-markdown-jb/src/main/kotlin/org/jetbrains/dokka/analysis/markdown/jb/MarkdownParser.kt b/subprojects/analysis-markdown-jb/src/main/kotlin/org/jetbrains/dokka/analysis/markdown/jb/MarkdownParser.kt
index 256c7b98..130c6def 100644
--- a/subprojects/analysis-markdown-jb/src/main/kotlin/org/jetbrains/dokka/analysis/markdown/jb/MarkdownParser.kt
+++ b/subprojects/analysis-markdown-jb/src/main/kotlin/org/jetbrains/dokka/analysis/markdown/jb/MarkdownParser.kt
@@ -77,6 +77,44 @@ public open class MarkdownParser(
).flatMap { it.children }
)
+ /**
+ * Handler for [MarkdownTokenTypes.ATX_CONTENT], which is the content of the header
+ * elements like [MarkdownElementTypes.ATX_1], [MarkdownElementTypes.ATX_2] and so on.
+ *
+ * For example, a header line like `# Header text` is expected to be parsed into:
+ * - One [MarkdownTokenTypes.ATX_HEADER] with startOffset = 0, endOffset = 1 (only the `#` symbol)
+ * - Composite [MarkdownTokenTypes.ATX_CONTENT] with four children: WHITE_SPACE, TEXT, WHITE_SPACE, TEXT.
+ */
+ private fun headerContentHandler(node: ASTNode): List<DocTag> {
+ // ATX_CONTENT contains everything after the `#` symbol, so if there's a space
+ // in-between the `#` symbol and the text (like `# header`), it will be present here too.
+ // However, we don't need the leading space between the `#` symbol and the text, nor do we need trailing spaces,
+ // so we just skip it (otherwise the header text will be parsed as `<whitespace>header` instead of `header`).
+ // If there's more space between `#` and text, like `# header`, it will still be a single WHITE_SPACE
+ // element, but it will be wider, so the solution below should still hold. The same applies to trailing spaces.
+ val trimmedChildren = node.children.trimWhitespaceToken()
+
+ val children = trimmedChildren.evaluateChildren()
+ return DocTagsFromIElementFactory.getInstance(
+ MarkdownElementTypes.PARAGRAPH, // PARAGRAPH instead of TEXT to preserve compatibility with prev. versions
+ children = children
+ )
+ }
+
+ /**
+ * @return a sublist of [this] list that does not contain
+ * leading and trailing [MarkdownTokenTypes.WHITE_SPACE] elements
+ */
+ private fun List<ASTNode>.trimWhitespaceToken(): List<ASTNode> {
+ val firstNonWhitespaceIndex = this.indexOfFirst { it.type != MarkdownTokenTypes.WHITE_SPACE }
+ if (firstNonWhitespaceIndex == -1) {
+ return this
+ }
+ val lastNonWhitespaceIndex = this.indexOfLast { it.type != MarkdownTokenTypes.WHITE_SPACE }
+
+ return this.subList(firstNonWhitespaceIndex, lastNonWhitespaceIndex + 1)
+ }
+
private fun horizontalRulesHandler() =
DocTagsFromIElementFactory.getInstance(MarkdownTokenTypes.HORIZONTAL_RULE)
@@ -365,6 +403,7 @@ public open class MarkdownParser(
MarkdownElementTypes.ATX_5,
MarkdownElementTypes.ATX_6,
-> headersHandler(node)
+ MarkdownTokenTypes.ATX_CONTENT -> headerContentHandler(node)
MarkdownTokenTypes.HORIZONTAL_RULE -> horizontalRulesHandler()
MarkdownElementTypes.STRONG -> strongHandler(node)
MarkdownElementTypes.EMPH -> emphasisHandler(node)