Skip to content

Commit ae7bfba

Browse files
authored
fix: don't escape markdown within doc preformat blocks (#643)
1 parent 36b3e02 commit ae7bfba

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "88581b9f-34b0-4509-8eaf-481efd2cbdf1",
3+
"type": "bugfix",
4+
"description": "Don't escape markdown within preformat blocks in documentation."
5+
}

smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/lang/DocumentationPreprocessor.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,19 @@ private fun Node.hasAncestor(predicate: (Node) -> Boolean): Boolean =
200200
private fun Node.isList() =
201201
nodeName().let { it == "ul" || it == "ol" }
202202

203-
private fun TextNode.markdownText() =
204-
text()
205-
// Replace square brackets with escaped equivalents so that they are not rendered as invalid Markdown
206-
// links.
203+
private fun Node.isPreformat() =
204+
nodeName().let { it == "code" || it == "pre" }
205+
206+
private fun TextNode.markdownText() = when {
207+
// If we're inside a preformat block, everything is literal, ie. no escapes required.
208+
hasAncestor(Node::isPreformat) -> text()
209+
210+
// Replace square brackets with escaped equivalents so that they are not rendered as invalid Markdown
211+
// links.
212+
else -> text()
207213
.replace("[", "[")
208214
.replace("]", "]")
215+
}
209216

210217
/**
211218
* Operates on all substrings that fall within the provided section delimiters. Returns a new string where all

smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/lang/DocumentationPreprocessorTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ class DocumentationPreprocessorTest {
161161
inputTest(input, expected)
162162
}
163163

164+
@Test
165+
fun `it renders markdown text`() {
166+
val input = "<p>This [should be escaped because it's in formatland].</p>" +
167+
"<p><code>this should [not]</code></p>" +
168+
"<p><pre>nor [should] this</pre></p>"
169+
val expected = """
170+
This &#91;should be escaped because it's in formatland&#93;.
171+
172+
`this should [not]`
173+
174+
`nor [should] this`
175+
""".trimIndent()
176+
inputTest(input, expected)
177+
}
178+
164179
@Test
165180
fun `it fully renders S3 CreateMultipartUpload`() {
166181
val input = """

0 commit comments

Comments
 (0)