From acbe2f8c47a81c8e6046214f600ffdef24890fa6 Mon Sep 17 00:00:00 2001 From: Alex Waters Date: Fri, 28 Apr 2017 14:56:44 +0930 Subject: Fix missing whitespace around links and tokens in Markdown formatted text --- core/src/main/kotlin/Kotlin/ContentBuilder.kt | 13 +++++++++--- core/src/test/kotlin/format/MarkdownFormatTest.kt | 24 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/Kotlin/ContentBuilder.kt b/core/src/main/kotlin/Kotlin/ContentBuilder.kt index 9c081d17..c124821e 100644 --- a/core/src/main/kotlin/Kotlin/ContentBuilder.kt +++ b/core/src/main/kotlin/Kotlin/ContentBuilder.kt @@ -87,9 +87,16 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri parent.append(link) } } - MarkdownTokenTypes.WHITE_SPACE, + MarkdownTokenTypes.WHITE_SPACE -> { + // Don't append first space if start of header (it is added during formatting later) + // v + // #### Some Heading + if (nodeStack.peek() !is ContentHeading || node.parent?.children?.first() != node) { + parent.append(ContentText(node.text)) + } + } MarkdownTokenTypes.EOL -> { - if (keepWhitespace(nodeStack.peek()) && node.parent?.children?.last() != node) { + if (keepEol(nodeStack.peek()) && node.parent?.children?.last() != node) { parent.append(ContentText(node.text)) } } @@ -148,7 +155,7 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri private fun MarkdownNode.getLabelText() = children.filter { it.type == MarkdownTokenTypes.TEXT || it.type == MarkdownTokenTypes.EMPH }.joinToString("") { it.text } -private fun keepWhitespace(node: ContentNode) = node is ContentParagraph || node is ContentSection || node is ContentBlockCode +private fun keepEol(node: ContentNode) = node is ContentParagraph || node is ContentSection || node is ContentBlockCode fun buildInlineContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (String) -> ContentBlock) { val inlineContent = tree.children.singleOrNull { it.type == MarkdownElementTypes.PARAGRAPH }?.children ?: listOf(tree) diff --git a/core/src/test/kotlin/format/MarkdownFormatTest.kt b/core/src/test/kotlin/format/MarkdownFormatTest.kt index 16a81165..999d739b 100644 --- a/core/src/test/kotlin/format/MarkdownFormatTest.kt +++ b/core/src/test/kotlin/format/MarkdownFormatTest.kt @@ -332,6 +332,30 @@ class MarkdownFormatTest { } } + @Test fun linksInEmphasis() { + verifyMarkdownNode("linksInEmphasis") + } + + @Test fun linksInStrong() { + verifyMarkdownNode("linksInStrong") + } + + @Test fun linksInHeaders() { + verifyMarkdownNode("linksInHeaders") + } + + @Test fun tokensInEmphasis() { + verifyMarkdownNode("tokensInEmphasis") + } + + @Test fun tokensInStrong() { + verifyMarkdownNode("tokensInStrong") + } + + @Test fun tokensInHeaders() { + verifyMarkdownNode("tokensInHeaders") + } + private fun buildMultiplePlatforms(path: String): DocumentationModule { val module = DocumentationModule("test") val options = DocumentationOptions("", "html", generateIndexPages = false) -- cgit