kotmark

A Markdown engine
Kotlin Multiplatform

Extension 02 / kotmark-ext-footnotes

Footnotes

Footnote nodes are numbered by first reference and rendered as a list with back-references.

Artifact
io.heapy.kotmark:kotmark-ext-footnotes:0.1.0
Package
io.heapy.kotmark.ext.footnotes

Usage

Register the extension with both parser and renderer; enable inline notes only if needed.

Footnotes with inline notes enabled
import io.heapy.kotmark.ext.footnotes.footnotes
import io.heapy.kotmark.extensions
import io.heapy.kotmark.parser.parser
import io.heapy.kotmark.renderer.html.htmlRenderer

fun renderWithFootnotes(markdown: String): String {
    val extensions = extensions {
        footnotes { inlineFootnotes = true }
    }
    val document = parser { extensions(extensions) }.parse(markdown)
    return htmlRenderer { extensions(extensions) }.render(document)
}

Example

INPUT · Markdown
Kotmark ports commonmark-java.[^port] Inline notes work too.^[Like this one.]

[^port]: Revisions are recorded upstream.
OUTPUT · HTML
<p>Kotmark ports commonmark-java.<sup class="footnote-ref"><a href="#fn-port" id="fnref-port" data-footnote-ref>1</a></sup> Inline notes work too.<sup class="footnote-ref"><a href="#fn2" id="fnref2" data-footnote-ref>2</a></sup></p>
<section class="footnotes" data-footnotes>
<ol>
<li id="fn-port">
<p>Revisions are recorded upstream. <a href="#fnref-port" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
</li>
<li id="fn2">
<p>Like this one. <a href="#fnref2" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="2" aria-label="Back to reference 2">↩</a></p>
</li>
</ol>
</section>

Syntax

Form Meaning
[^label] A reference to a footnote definition.
[^label]: text A footnote definition block.
^[text] An inline footnote when inlineFootnotes = true.