aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/src/main/kotlin/Formats/MarkdownFormatService.kt16
-rw-r--r--core/src/main/kotlin/Kotlin/ContentBuilder.kt8
-rw-r--r--core/src/test/kotlin/format/MarkdownFormatTest.kt4
-rw-r--r--core/testdata/format/backtickInCodeBlock.kt9
-rw-r--r--core/testdata/format/backtickInCodeBlock.md12
5 files changed, 48 insertions, 1 deletions
diff --git a/core/src/main/kotlin/Formats/MarkdownFormatService.kt b/core/src/main/kotlin/Formats/MarkdownFormatService.kt
index 6f2ab327..2e3c9f6d 100644
--- a/core/src/main/kotlin/Formats/MarkdownFormatService.kt
+++ b/core/src/main/kotlin/Formats/MarkdownFormatService.kt
@@ -19,6 +19,7 @@ open class MarkdownOutputBuilder(to: StringBuilder,
protected var inTableCell = false
protected var inCodeBlock = false
private var lastTableCellStart = -1
+ private var maxBackticksInCodeBlock = 0
private fun appendNewline() {
while (to.endsWith(' ')) {
@@ -64,9 +65,14 @@ open class MarkdownOutputBuilder(to: StringBuilder,
to.append(" / ")
}
+ private val backTickFindingRegex = """(`+)""".toRegex()
+
override fun appendText(text: String) {
if (inCodeBlock) {
to.append(text)
+ val backTicks = backTickFindingRegex.findAll(text)
+ val longestBackTickRun = backTicks.map { it.value.length }.max() ?: 0
+ maxBackticksInCodeBlock = maxBackticksInCodeBlock.coerceAtLeast(longestBackTickRun)
}
else {
to.append(text.htmlEscape())
@@ -75,7 +81,17 @@ open class MarkdownOutputBuilder(to: StringBuilder,
override fun appendCode(body: () -> Unit) {
inCodeBlock = true
+ val codeBlockStart = to.length
+ maxBackticksInCodeBlock = 0
+
wrapIfNotEmpty("`", "`", body, checkEndsWith = true)
+
+ if (maxBackticksInCodeBlock > 0) {
+ val extraBackticks = "`".repeat(maxBackticksInCodeBlock)
+ to.insert(codeBlockStart, extraBackticks)
+ to.append(extraBackticks)
+ }
+
inCodeBlock = false
}
diff --git a/core/src/main/kotlin/Kotlin/ContentBuilder.kt b/core/src/main/kotlin/Kotlin/ContentBuilder.kt
index c16b19c5..7bee6936 100644
--- a/core/src/main/kotlin/Kotlin/ContentBuilder.kt
+++ b/core/src/main/kotlin/Kotlin/ContentBuilder.kt
@@ -43,7 +43,12 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri
MarkdownElementTypes.EMPH -> appendNodeWithChildren(ContentEmphasis())
MarkdownElementTypes.STRONG -> appendNodeWithChildren(ContentStrong())
MarkdownElementTypes.CODE_SPAN -> {
- appendNodeWithChildren(ContentCode())
+ val startDelimiter = node.child(MarkdownTokenTypes.BACKTICK)?.text
+ if (startDelimiter != null) {
+ val text = node.text.substring(startDelimiter.length).removeSuffix(startDelimiter)
+ val codeSpan = ContentCode().apply { append(ContentText(text)) }
+ parent.append(codeSpan)
+ }
}
MarkdownElementTypes.CODE_BLOCK,
MarkdownElementTypes.CODE_FENCE -> appendNodeWithChildren(ContentBlockCode())
@@ -126,6 +131,7 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri
MarkdownTokenTypes.LBRACKET,
MarkdownTokenTypes.RBRACKET,
MarkdownTokenTypes.EXCLAMATION_MARK,
+ MarkdownTokenTypes.BACKTICK,
MarkdownTokenTypes.CODE_FENCE_CONTENT -> {
parent.append(ContentText(node.text))
}
diff --git a/core/src/test/kotlin/format/MarkdownFormatTest.kt b/core/src/test/kotlin/format/MarkdownFormatTest.kt
index 00bbdd61..864fb9d1 100644
--- a/core/src/test/kotlin/format/MarkdownFormatTest.kt
+++ b/core/src/test/kotlin/format/MarkdownFormatTest.kt
@@ -214,6 +214,10 @@ class MarkdownFormatTest {
verifyMarkdownNodeByName("exclInCodeBlock", "foo")
}
+ @Test fun backtickInCodeBlock() {
+ verifyMarkdownNodeByName("backtickInCodeBlock", "foo")
+ }
+
private fun verifyMarkdownPackage(fileName: String, withKotlinRuntime: Boolean = false) {
verifyOutput("testdata/format/$fileName.kt", ".package.md", withKotlinRuntime = withKotlinRuntime) { model, output ->
markdownService.createOutputBuilder(output, tempLocation).appendNodes(model.members)
diff --git a/core/testdata/format/backtickInCodeBlock.kt b/core/testdata/format/backtickInCodeBlock.kt
new file mode 100644
index 00000000..b457efbd
--- /dev/null
+++ b/core/testdata/format/backtickInCodeBlock.kt
@@ -0,0 +1,9 @@
+/**
+ * bt : `` ` ``
+ *
+ * bt+ : ``prefix ` postfix``
+ *
+ * backslash: `\`
+ */
+fun foo() {
+}
diff --git a/core/testdata/format/backtickInCodeBlock.md b/core/testdata/format/backtickInCodeBlock.md
new file mode 100644
index 00000000..fc244630
--- /dev/null
+++ b/core/testdata/format/backtickInCodeBlock.md
@@ -0,0 +1,12 @@
+[test](test/index) / [foo](test/foo)
+
+# foo
+
+`fun foo(): Unit`
+
+bt : `` ` ``
+
+bt+ : ``prefix ` postfix``
+
+backslash: `\`
+