Skip to content

Commit

Permalink
Add a test for multiple whitespaces between hash and header
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnatBeresnev committed Oct 18, 2023
1 parent 8c5415b commit 97b2db2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 17 additions & 1 deletion plugins/base/src/test/kotlin/markdown/ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,23 @@ class ParserTest : KDocTest() {
P(listOf(Text(" sdsdsds sdd"))),
P(listOf(Text(" eweww ")))
)
print(expectedDocumentationNode)
assertEquals(actualDocumentationNode, expectedDocumentationNode)
}

@Test // exists due to #3231
fun `should ignore the first whitespace in header in-between the hash symbol and header text`() {
val markdown = """
| # first header
| ## second header
| ### third header
|
""".trimMargin()
val actualDocumentationNode = parseMarkdownToDocNode(markdown).children
val expectedDocumentationNode = listOf(
H1(listOf(Text("first header"))),
H2(listOf(Text("second header"))),
H3(listOf(Text("third header"))),
)
assertEquals(actualDocumentationNode, expectedDocumentationNode)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public open class MarkdownParser(
// in-between the `#` symbol and the text (like `# header`), it will be present here too.
// However, we don't need the first space between the `#` symbol and the text,
// 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.
val textStartsWithWhitespace = node.children.firstOrNull()?.type == MarkdownTokenTypes.WHITE_SPACE
val children = if (textStartsWithWhitespace) node.children.subList(1, node.children.size) else node.children

Expand Down

0 comments on commit 97b2db2

Please sign in to comment.