Skip to content

Commit

Permalink
Nit: Change "toText" to "nestedLiteral" for more explicit intention
Browse files Browse the repository at this point in the history
  • Loading branch information
bitspittle committed Dec 29, 2024
1 parent 0eb5c3d commit e66bb22
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
}
}

0 comments on commit e66bb22

Please sign in to comment.