diff options
author | Don Cross <cosinekitty@users.noreply.github.com> | 2022-05-04 12:33:21 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-04 19:33:21 +0300 |
commit | 3fb60bc74a4ed4e4c795f2f14fa112321a3c94cc (patch) | |
tree | 8ca1c8f5147a1a15fa577898594d496a2f330c83 /plugins/gfm/src | |
parent | 6046abccab85f2cfde5e20476d3b9ea1b1ab1ff9 (diff) | |
download | dokka-3fb60bc74a4ed4e4c795f2f14fa112321a3c94cc.tar.gz dokka-3fb60bc74a4ed4e4c795f2f14fa112321a3c94cc.tar.bz2 dokka-3fb60bc74a4ed4e4c795f2f14fa112321a3c94cc.zip |
Support code blocks and inline code for GFM format (#2485)
Fixes #2477.
Inline code, text that is nested within a pair of backquotes,
is now converted into GitHub Flavored Markdown (gfm) without
stripping out the backquotes. For example:
The parameter `sum` must be a non-negative real number.
Code blocks, which are any number of lines of literal text
between triple-backquotes, and an optional programming language
name, are now preserved. If absent, the programming language
is assumed to be "kotlin". This follows the behavior of the
html renderer. For example:
Here is an example of calling the function:
```kotlin
val sum = addThemUp(left, right)
```
Diffstat (limited to 'plugins/gfm/src')
-rw-r--r-- | plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/renderer/CommonmarkRenderer.kt | 16 | ||||
-rw-r--r-- | plugins/gfm/src/test/kotlin/renderers/gfm/CodeWrappingTest.kt | 48 |
2 files changed, 64 insertions, 0 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 13322944..d192b9a6 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 @@ -311,6 +311,22 @@ open class CommonmarkRenderer( } } + override fun StringBuilder.buildCodeBlock(code: ContentCodeBlock, pageContext: ContentPage) { + append("```") + append(code.language.ifEmpty { "kotlin" }) + buildNewLine() + code.children.forEach { it.build(this, pageContext) } + buildNewLine() + append("```") + buildNewLine() + } + + override fun StringBuilder.buildCodeInline(code: ContentCodeInline, pageContext: ContentPage) { + append("`") + code.children.forEach { it.build(this, pageContext) } + append("`") + } + private fun decorators(styles: Set<Style>) = buildString { styles.forEach { when (it) { diff --git a/plugins/gfm/src/test/kotlin/renderers/gfm/CodeWrappingTest.kt b/plugins/gfm/src/test/kotlin/renderers/gfm/CodeWrappingTest.kt new file mode 100644 index 00000000..ce0c3d0c --- /dev/null +++ b/plugins/gfm/src/test/kotlin/renderers/gfm/CodeWrappingTest.kt @@ -0,0 +1,48 @@ +package renderers.gfm + +import org.jetbrains.dokka.gfm.renderer.CommonmarkRenderer +import org.jetbrains.dokka.pages.TextStyle +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import renderers.* + +class CodeWrappingTest : GfmRenderingOnlyTestBase() { + @Test + fun wrappedCodeBlock() { + val page = testPage { + codeBlock { + text("fun myCode(): String") + } + } + val expect = """|//[testPage](test-page.md) + | + |```kotlin + |fun myCode(): String + |```""".trimMargin() + + CommonmarkRenderer(context).render(page) + assertEquals(expect, renderedContent) + } + + @Test + fun wrappedInlineCode() { + val page = testPage { + text("This function adds the values of ") + codeInline("") { + text("left") + } + text(" and ") + codeInline("") { + text("right") + } + text(".\nBoth numbers must be finite, or an exception occurs.\n") + } + val expect = """|//[testPage](test-page.md) + | + |This function adds the values of `left` and `right`. + |Both numbers must be finite, or an exception occurs.""".trimMargin() + + CommonmarkRenderer(context).render(page) + assertEquals(expect, renderedContent) + } +} |