From cf1306a97452f2cfbaac602a6dc210365269154e Mon Sep 17 00:00:00 2001 From: Giorgio Garofalo Date: Fri, 9 Aug 2024 01:06:03 +0200 Subject: [PATCH] Add page numbers to TOC in paged mode --- .../html/QuarkdownHtmlNodeRenderer.kt | 19 ++++++++++++------- .../main/resources/render/theme/global.css | 8 +++++++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/QuarkdownHtmlNodeRenderer.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/QuarkdownHtmlNodeRenderer.kt index b510f133..f53170bd 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/QuarkdownHtmlNodeRenderer.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/QuarkdownHtmlNodeRenderer.kt @@ -133,17 +133,22 @@ class QuarkdownHtmlNodeRenderer(context: Context) : BaseHtmlNodeRenderer(context +visit(tableOfContentsItemsToList(node.items)) } - override fun visit(node: TableOfContents.Item) = - buildTag("a") { - +node.text + override fun visit(node: TableOfContents.Item): CharSequence { + val link = + buildTag("a") { + +node.text - if (node.children.isNotEmpty()) { - +visit(tableOfContentsItemsToList(node.subItems)) + // Link to the target anchor (e.g. a heading). + attribute("href", "#" + HtmlIdentifierProvider.of(this@QuarkdownHtmlNodeRenderer).getId(node.target)) } - // Link to the target anchor (e.g. a heading). - attribute("href", "#" + HtmlIdentifierProvider.of(this@QuarkdownHtmlNodeRenderer).getId(node.target)) + // Recursively render sub-items. + return if (node.subItems.isNotEmpty()) { + link + visit(tableOfContentsItemsToList(node.subItems)) + } else { + link } + } // Inline diff --git a/core/src/main/resources/render/theme/global.css b/core/src/main/resources/render/theme/global.css index 50dd80c8..38b8e064 100644 --- a/core/src/main/resources/render/theme/global.css +++ b/core/src/main/resources/render/theme/global.css @@ -138,4 +138,10 @@ h1, h2, h3, h4, h5, h6 { .stack > p, .clip > p { margin: 0; -} \ No newline at end of file +} + +.table-of-contents li a::after { + /* TOC page counter on supported targets */ + content: target-counter(attr(href), page); + float: right; +}