diff --git a/tools/gradle-plugins/extensions/markdown/src/main/kotlin/com/varabyte/kobwebx/gradle/markdown/handlers/MarkdownHandlers.kt b/tools/gradle-plugins/extensions/markdown/src/main/kotlin/com/varabyte/kobwebx/gradle/markdown/handlers/MarkdownHandlers.kt index baee0ce6a..5848b95ff 100644 --- a/tools/gradle-plugins/extensions/markdown/src/main/kotlin/com/varabyte/kobwebx/gradle/markdown/handlers/MarkdownHandlers.kt +++ b/tools/gradle-plugins/extensions/markdown/src/main/kotlin/com/varabyte/kobwebx/gradle/markdown/handlers/MarkdownHandlers.kt @@ -11,6 +11,7 @@ import com.varabyte.kobwebx.gradle.markdown.children import com.varabyte.kobwebx.gradle.markdown.util.escapeDollars import com.varabyte.kobwebx.gradle.markdown.util.escapeQuotes import com.varabyte.kobwebx.gradle.markdown.util.escapeTripleQuotes +import com.varabyte.kobwebx.gradle.markdown.util.nestedLiteral import org.commonmark.ext.gfm.tables.TableBlock import org.commonmark.ext.gfm.tables.TableBody import org.commonmark.ext.gfm.tables.TableCell @@ -65,20 +66,6 @@ class NodeScope(val reporter: Reporter, val data: TypedMap, private val indentCo fun indent(indentCount: Int) = " ".repeat(indentCountBase + indentCount) } -/** - * Convert a [Node] to any text it contains. - * - * This is useful if you are pretty sure that a node contains some text but it might be deep down in its children. For - * example, if a node represents a link, then the text value is contained as a child node. - */ -private fun Node.toText(): String { - return when (this) { - is Text -> this.literal - is Code -> this.literal - else -> this.children().joinToString { it.toText() } - } -} - /** * Register custom handlers for various Markdown elements. * @@ -289,7 +276,7 @@ abstract class MarkdownHandlers @Inject constructor(project: Project) { buildString { append("$JB_DOM.H${heading.level}") if (generateHeaderIds.get()) { - val text = heading.toText() + val text = heading.nestedLiteral val headingIds = data.computeIfAbsent(DataKeys.HeadingIds) { mutableMapOf() } val id = run { val baseId = idGenerator.get().invoke(text) diff --git a/tools/gradle-plugins/extensions/markdown/src/main/kotlin/com/varabyte/kobwebx/gradle/markdown/util/NodeUtils.kt b/tools/gradle-plugins/extensions/markdown/src/main/kotlin/com/varabyte/kobwebx/gradle/markdown/util/NodeUtils.kt new file mode 100644 index 000000000..d7d8247c1 --- /dev/null +++ b/tools/gradle-plugins/extensions/markdown/src/main/kotlin/com/varabyte/kobwebx/gradle/markdown/util/NodeUtils.kt @@ -0,0 +1,20 @@ +package com.varabyte.kobwebx.gradle.markdown.util + +import com.varabyte.kobwebx.gradle.markdown.children +import org.commonmark.node.Code +import org.commonmark.node.Node +import org.commonmark.node.Text + +/** + * Convert a [Node] to any literal value it contains. + * + * This is useful if you are pretty sure that a node contains some text but it might be deep down in its children. For + * example, if a node represents a link, then the text value is contained as a child node. + */ +internal val Node.nestedLiteral: String get() { + return when (this) { + is Text -> this.literal + is Code -> this.literal + else -> this.children().joinToString { it.nestedLiteral } + } +}