Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GFM renderer: sanitize line ends #1936

Merged
merged 2 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ open class CommonmarkRenderer(
return when {
node.hasStyle(TextStyle.Block) -> {
childrenCallback()
buildNewLine()
buildParagraph()
}
node.hasStyle(TextStyle.Paragraph) -> {
buildParagraph()
Expand All @@ -43,7 +43,7 @@ open class CommonmarkRenderer(
buildParagraph()
append("#".repeat(level) + " ")
content()
buildNewLine()
appendNewLine()
}

override fun StringBuilder.buildLink(address: String, content: StringBuilder.() -> Unit) {
Expand Down Expand Up @@ -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(
Expand All @@ -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()
}
}
}
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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")
Expand All @@ -248,23 +260,24 @@ open class CommonmarkRenderer(
val (instance, sourceSets) = entry.getInstanceAndSourceSets()

buildSourceSetTags(sourceSets)
buildNewLine()

instance.before?.let {
buildNewLine()
append("Brief description")
buildNewLine()
buildContentNode(
it,
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()
Expand All @@ -274,18 +287,17 @@ 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(
it,
pageContext,
sourceSets.first()
) // It's workaround to render content only once
buildNewLine()
}

buildParagraph()
Expand Down Expand Up @@ -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()) }
Expand Down
22 changes: 11 additions & 11 deletions plugins/gfm/src/test/kotlin/renderers/gfm/DivergentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
}

}
10 changes: 3 additions & 7 deletions plugins/gfm/src/test/kotlin/renderers/gfm/SimpleElementsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
}
}