Current scope: kotmark-renderer-kotlinx-html| all classes
|
io.heapy.kotmark.renderer.kotlinxhtml.adapters
Coverage Summary for Class: HeadingAnchorAdapterKt (io.heapy.kotmark.renderer.kotlinxhtml.adapters)
| Class | Method, % | Branch, % | Line, % | Instruction, % |
|---|---|---|---|---|
| HeadingAnchorAdapterKt | 100% (5/5) | 75% (3/4) | 100% (10/10) | 100% (95/95) |
| HeadingAnchorAdapterKt$headingAnchorAdapter$1$1$1$title$1$1 | 66.7% (2/3) | 25% (1/4) | 66.7% (2/3) | 45.5% (10/22) |
| Total | 87.5% (7/8) | 50% (4/8) | 92.3% (12/13) | 89.7% (105/117) |
package io.heapy.kotmark.renderer.kotlinxhtml.adapters
import io.heapy.kotmark.ext.heading.anchor.idGenerator
import io.heapy.kotmark.node.AbstractVisitor
import io.heapy.kotmark.node.Code
import io.heapy.kotmark.node.Heading
import io.heapy.kotmark.node.Text
import io.heapy.kotmark.renderer.html.AttributeProvider
import io.heapy.kotmark.renderer.kotlinxhtml.KotlinxHtmlAdapter
/** Generates heading IDs with a fresh duplicate counter per document render. */
fun headingAnchorAdapter(defaultId: String = "id", idPrefix: String = "", idSuffix: String = ""): KotlinxHtmlAdapter =
KotlinxHtmlAdapter { configuration ->
configuration.attributeProviderFactory {
val generator = idGenerator { this.defaultId = defaultId; prefix = idPrefix; suffix = idSuffix }
AttributeProvider { node, tag, attributes ->
if (node is Heading && tag == "h${node.level}") {
val title = buildString {
node.accept(object : AbstractVisitor() {
override fun visit(text: Text) { append(text.literal.orEmpty()) }
override fun visit(code: Code) { append(code.literal.orEmpty()) }
})
}
attributes["id"] = generator.generateId(title.trim().lowercase())
}
}
}
}