diff options
author | Mike Sinkovsky <msink@users.noreply.github.com> | 2021-05-27 19:47:23 +0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-27 16:47:23 +0200 |
commit | 50350aa305d6831a1298932b59504a626aecdcc5 (patch) | |
tree | b653a9c7f3f2bf37899f33b015471fef0b450a19 /plugins/gfm/src | |
parent | 5c000c86238523e5dbd4a36a1ab9fe51f123893a (diff) | |
download | dokka-50350aa305d6831a1298932b59504a626aecdcc5.tar.gz dokka-50350aa305d6831a1298932b59504a626aecdcc5.tar.bz2 dokka-50350aa305d6831a1298932b59504a626aecdcc5.zip |
GFM renderer: sanitize line ends (#1936)
* GFM renderer: sanitize line ends
* GFM renderer: sanitize line ends in table cell
Diffstat (limited to 'plugins/gfm/src')
5 files changed, 57 insertions, 47 deletions
diff --git a/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/renderer/CommonmarkRenderer.kt b/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/renderer/CommonmarkRenderer.kt index aa54af97..ac6cdaa8 100644 --- a/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/renderer/CommonmarkRenderer.kt +++ b/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/renderer/CommonmarkRenderer.kt @@ -28,7 +28,7 @@ open class CommonmarkRenderer( return when { node.hasStyle(TextStyle.Block) -> { childrenCallback() - buildNewLine() + buildParagraph() } node.hasStyle(TextStyle.Paragraph) -> { buildParagraph() @@ -43,7 +43,7 @@ open class CommonmarkRenderer( buildParagraph() append("#".repeat(level) + " ") content() - buildNewLine() + appendNewLine() } override fun StringBuilder.buildLink(address: String, content: StringBuilder.() -> Unit) { @@ -101,11 +101,17 @@ open class CommonmarkRenderer( } override fun StringBuilder.buildNewLine() { - append(" \n") + append("\\") + appendNewLine() + } + + private fun StringBuilder.appendNewLine() { + append("\n") } private fun StringBuilder.buildParagraph() { - append("\n\n") + appendNewLine() + appendNewLine() } override fun StringBuilder.buildPlatformDependent( @@ -131,8 +137,8 @@ open class CommonmarkRenderer( distinct.filter { it.key.isNotBlank() }.forEach { (text, platforms) -> append(" ") buildSourceSetTags(platforms.toSet()) - append(" $text ") - buildNewLine() + append(" $text") + appendNewLine() } } } @@ -149,18 +155,18 @@ open class CommonmarkRenderer( pageContext: ContentPage, sourceSetRestriction: Set<DisplaySourceSet>? ) { - buildNewLine() + appendNewLine() if (node.dci.kind == ContentKind.Sample || node.dci.kind == ContentKind.Parameters) { node.sourceSets.forEach { sourcesetData -> append(sourcesetData.name) - buildNewLine() + appendNewLine() buildTable( node.copy( children = node.children.filter { it.sourceSets.contains(sourcesetData) }, dci = node.dci.copy(kind = ContentKind.Main) ), pageContext, sourceSetRestriction ) - buildNewLine() + appendNewLine() } } else { val size = node.header.firstOrNull()?.children?.size ?: node.children.firstOrNull()?.children?.size ?: 0 @@ -177,11 +183,17 @@ open class CommonmarkRenderer( } } else { append("| ".repeat(size)) - if (size > 0) append("|\n") + if (size > 0) { + append("|") + appendNewLine() + } } append("|---".repeat(size)) - if (size > 0) append("|\n") + if (size > 0) { + append("|") + appendNewLine() + } node.children.forEach { val builder = StringBuilder() @@ -225,7 +237,7 @@ open class CommonmarkRenderer( override fun buildPage(page: ContentPage, content: (StringBuilder, ContentPage) -> Unit): String = buildString { content(this, page) - } + }.trim().replace("\n[\n]+".toRegex(), "\n\n") override fun buildError(node: ContentNode) { context.logger.warn("Markdown renderer has encountered problem. The unmatched node is $node") @@ -248,8 +260,9 @@ open class CommonmarkRenderer( val (instance, sourceSets) = entry.getInstanceAndSourceSets() buildSourceSetTags(sourceSets) - buildNewLine() + instance.before?.let { + buildNewLine() append("Brief description") buildNewLine() buildContentNode( @@ -257,14 +270,14 @@ open class CommonmarkRenderer( pageContext, sourceSets.first() ) // It's workaround to render content only once - buildNewLine() } - append("Content") buildNewLine() + append("Content") entry.groupBy { buildString { buildContentNode(it.first.divergent, pageContext, setOf(it.second)) } } .values.forEach { innerEntry -> val (innerInstance, innerSourceSets) = innerEntry.getInstanceAndSourceSets() + buildNewLine() if (sourceSets.size > 1) { buildSourceSetTags(innerSourceSets) buildNewLine() @@ -274,10 +287,10 @@ open class CommonmarkRenderer( pageContext, setOf(innerSourceSets.first()) ) // It's workaround to render content only once - buildNewLine() } instance.after?.let { + buildNewLine() append("More info") buildNewLine() buildContentNode( @@ -285,7 +298,6 @@ open class CommonmarkRenderer( pageContext, sourceSets.first() ) // It's workaround to render content only once - buildNewLine() } buildParagraph() @@ -332,7 +344,10 @@ open class CommonmarkRenderer( } } - private fun String.withEntersAsHtml(): String = replace("\n", "<br>") + private fun String.withEntersAsHtml(): String = this + .replace("\\\n", "\n\n") + .replace("\n[\n]+".toRegex(), "<br>") + .replace("\n", " ") private fun List<Pair<ContentDivergentInstance, DisplaySourceSet>>.getInstanceAndSourceSets() = this.let { Pair(it.first().first, it.map { it.second }.toSet()) } diff --git a/plugins/gfm/src/test/kotlin/renderers/gfm/DivergentTest.kt b/plugins/gfm/src/test/kotlin/renderers/gfm/DivergentTest.kt index b0eead1b..d9ae5e99 100644 --- a/plugins/gfm/src/test/kotlin/renderers/gfm/DivergentTest.kt +++ b/plugins/gfm/src/test/kotlin/renderers/gfm/DivergentTest.kt @@ -41,7 +41,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[js] \nContent \na \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[js]\\\nContent\\\na" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -57,7 +57,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[js] \nContent \na \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[js]\\\nContent\\\na" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -83,7 +83,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[js, jvm, native] \nContent \n[js] \na \n[jvm] \nb \n[native] \nc \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[js, jvm, native]\\\nContent\\\n[js]\\\na\\\n[jvm]\\\nb\\\n[native]\\\nc" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -109,7 +109,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[js] \nContent \na \nb \nc \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[js]\\\nContent\\\na\\\nb\\\nc" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -145,7 +145,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[native, js, jvm] \nContent \n[native] \na \n[js] \nb \n[jvm] \nc \n[js] \nd \n[native] \ne \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[native, js, jvm]\\\nContent\\\n[native]\\\na\\\n[js]\\\nb\\\n[jvm]\\\nc\\\n[js]\\\nd\\\n[native]\\\ne" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -193,7 +193,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[native] \nContent \na \nMore info \na+ \n\n\n[js] \nContent \nb \nd \nMore info \nbd+ \n\n\n[jvm] \nContent \nc \n\n\n[native] \nContent \ne \nMore info \ne+ \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[native]\\\nContent\\\na\\\nMore info\\\na+\n\n[js]\\\nContent\\\nb\\\nd\\\nMore info\\\nbd+\n\n[jvm]\\\nContent\\\nc\n\n[native]\\\nContent\\\ne\\\nMore info\\\ne+" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -220,7 +220,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[native] \nBrief description \nab- \nContent \na \nb \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[native]\\\nBrief description\\\nab-\\\nContent\\\na\\\nb" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -247,7 +247,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[native] \nContent \na \nb \nMore info \nab+ \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[native]\\\nContent\\\na\\\nb\\\nMore info\\\nab+" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -280,7 +280,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[native] \nBrief description \nab- \nContent \na \nb \nMore info \nab+ \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[native]\\\nBrief description\\\nab-\\\nContent\\\na\\\nb\\\nMore info\\\nab+" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -313,7 +313,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[native] \nBrief description \na- \nContent \na \nMore info \nab+ \n\n\n[native] \nBrief description \nb- \nContent \nb \nMore info \nab+ \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[native]\\\nBrief description\\\na-\\\nContent\\\na\\\nMore info\\\nab+\n\n[native]\\\nBrief description\\\nb-\\\nContent\\\nb\\\nMore info\\\nab+" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } @@ -364,7 +364,7 @@ class DivergentTest : GfmRenderingOnlyTestBase() { } } } - val expect = "//[testPage](test-page.md)\n\n[native] \nContent \na \nMore info \na+ \n\n\n[js, jvm] \nContent \n[js] \nb \n[jvm] \nc \n[js] \nd \nMore info \nbd+ \n\n\n[native] \nContent \ne \nMore info \ne+ \n\n\n" + val expect = "//[testPage](test-page.md)\n\n[native]\\\nContent\\\na\\\nMore info\\\na+\n\n[js, jvm]\\\nContent\\\n[js]\\\nb\\\n[jvm]\\\nc\\\n[js]\\\nd\\\nMore info\\\nbd+\n\n[native]\\\nContent\\\ne\\\nMore info\\\ne+" CommonmarkRenderer(context).render(page) assert(renderedContent == expect) } diff --git a/plugins/gfm/src/test/kotlin/renderers/gfm/GroupWrappingTest.kt b/plugins/gfm/src/test/kotlin/renderers/gfm/GroupWrappingTest.kt index 93aee5e1..11105ebd 100644 --- a/plugins/gfm/src/test/kotlin/renderers/gfm/GroupWrappingTest.kt +++ b/plugins/gfm/src/test/kotlin/renderers/gfm/GroupWrappingTest.kt @@ -34,7 +34,7 @@ class GroupWrappingTest : GfmRenderingOnlyTestBase() { CommonmarkRenderer(context).render(page) - assert(renderedContent == "//[testPage](test-page.md)\n\n\n\nab\n\nc") + assert(renderedContent == "//[testPage](test-page.md)\n\nab\n\nc") } @Test @@ -48,8 +48,7 @@ class GroupWrappingTest : GfmRenderingOnlyTestBase() { } CommonmarkRenderer(context).render(page) - - assert(renderedContent == "//[testPage](test-page.md)\n\nab \nc") + assert(renderedContent == "//[testPage](test-page.md)\n\nab\n\nc") } @Test @@ -70,7 +69,7 @@ class GroupWrappingTest : GfmRenderingOnlyTestBase() { CommonmarkRenderer(context).render(page) // renderedContent.match(Div("a", Div(Div("bc")), "d")) - assert(renderedContent == "//[testPage](test-page.md)\n\nabc \n \nd \n") + assert(renderedContent == "//[testPage](test-page.md)\n\nabc\n\nd") } } diff --git a/plugins/gfm/src/test/kotlin/renderers/gfm/SimpleElementsTest.kt b/plugins/gfm/src/test/kotlin/renderers/gfm/SimpleElementsTest.kt index ef85d777..1a28a9a8 100644 --- a/plugins/gfm/src/test/kotlin/renderers/gfm/SimpleElementsTest.kt +++ b/plugins/gfm/src/test/kotlin/renderers/gfm/SimpleElementsTest.kt @@ -15,7 +15,7 @@ class SimpleElementsTest : GfmRenderingOnlyTestBase() { val page = testPage { header(1, "The Hobbit or There and Back Again") } - val expect = "//[testPage](test-page.md)\n\n\n\n# The Hobbit or There and Back Again \n" + val expect = "//[testPage](test-page.md)\n\n# The Hobbit or There and Back Again" CommonmarkRenderer(context).render(page) assertEquals(expect, renderedContent) } @@ -123,12 +123,10 @@ class SimpleElementsTest : GfmRenderingOnlyTestBase() { val expect = """\//[testPage](test-page.md) \ - \ \| Col1 | Col2 | Col3 | \|---|---|---| \| <a name="////PointingToDeclaration/"></a>Text1| <a name="////PointingToDeclaration/"></a>Text2| <a name="////PointingToDeclaration/"></a>Text3| - \| <a name="////PointingToDeclaration/"></a>Text4| <a name="////PointingToDeclaration/"></a>Text5| <a name="////PointingToDeclaration/"></a>Text6| - \""".trimMargin("\\") + \| <a name="////PointingToDeclaration/"></a>Text4| <a name="////PointingToDeclaration/"></a>Text5| <a name="////PointingToDeclaration/"></a>Text6|""".trimMargin("\\") CommonmarkRenderer(context).render(page) assertEquals(expect, renderedContent) @@ -153,12 +151,10 @@ class SimpleElementsTest : GfmRenderingOnlyTestBase() { val expect = """\//[testPage](test-page.md) \ - \ \| | | | \|---|---|---| \| <a name="////PointingToDeclaration/"></a>Text1| <a name="////PointingToDeclaration/"></a>Text2| <a name="////PointingToDeclaration/"></a>Text3| - \| <a name="////PointingToDeclaration/"></a>Text4| <a name="////PointingToDeclaration/"></a>Text5| <a name="////PointingToDeclaration/"></a>Text6| - \""".trimMargin("\\") + \| <a name="////PointingToDeclaration/"></a>Text4| <a name="////PointingToDeclaration/"></a>Text5| <a name="////PointingToDeclaration/"></a>Text6|""".trimMargin("\\") CommonmarkRenderer(context).render(page) assertEquals(expect, renderedContent) diff --git a/plugins/gfm/src/test/kotlin/renderers/gfm/SourceSetDependentHintTest.kt b/plugins/gfm/src/test/kotlin/renderers/gfm/SourceSetDependentHintTest.kt index 4d05c602..254832de 100644 --- a/plugins/gfm/src/test/kotlin/renderers/gfm/SourceSetDependentHintTest.kt +++ b/plugins/gfm/src/test/kotlin/renderers/gfm/SourceSetDependentHintTest.kt @@ -41,7 +41,7 @@ class SourceSetDependentHintTest : GfmRenderingOnlyTestBase() { } CommonmarkRenderer(context).render(page) - assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1, pl2, pl3] abc \n \n") + assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1, pl2, pl3] abc") } @Test @@ -55,7 +55,7 @@ class SourceSetDependentHintTest : GfmRenderingOnlyTestBase() { } CommonmarkRenderer(context).render(page) - assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1] a \n \n [pl2] b \n \n [pl3] c \n \n") + assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1] a\n\n [pl2] b\n\n [pl3] c") } @Test @@ -69,7 +69,7 @@ class SourceSetDependentHintTest : GfmRenderingOnlyTestBase() { } CommonmarkRenderer(context).render(page) - assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1] ab \n \n [pl2] bc \n \n") + assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1] ab\n\n [pl2] bc") } @Test @@ -83,7 +83,7 @@ class SourceSetDependentHintTest : GfmRenderingOnlyTestBase() { } CommonmarkRenderer(context).render(page) - assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1, pl2] ab \n \n") + assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1, pl2] ab") } @Test @@ -99,7 +99,7 @@ class SourceSetDependentHintTest : GfmRenderingOnlyTestBase() { } CommonmarkRenderer(context).render(page) - assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1] ab \n \n \n [pl2] a \nb \n \n") + assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1] ab\n\n [pl2] a\n\nb") } @Test @@ -115,7 +115,7 @@ class SourceSetDependentHintTest : GfmRenderingOnlyTestBase() { } CommonmarkRenderer(context).render(page) - assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1, pl2] ab \n") + assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1, pl2] ab") } @Test @@ -129,6 +129,6 @@ class SourceSetDependentHintTest : GfmRenderingOnlyTestBase() { } CommonmarkRenderer(context).render(page) - assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1, pl2] a \n \n [pl3] b \n \n") + assert(renderedContent == "//[testPage](test-page.md)\n\n [pl1, pl2] a\n\n [pl3] b") } } |