Skip to content

Commit

Permalink
Strip headings' rich content from ToC entries
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Nov 23, 2024
1 parent ff24394 commit 2b25176
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import eu.iamgio.quarkdown.ast.quarkdown.block.list.FocusListItemVariant
import eu.iamgio.quarkdown.ast.quarkdown.block.list.LocationTargetListItemVariant
import eu.iamgio.quarkdown.context.toc.TableOfContents
import eu.iamgio.quarkdown.document.numbering.DocumentNumbering
import eu.iamgio.quarkdown.util.stripRichContent
import eu.iamgio.quarkdown.visitor.node.NodeVisitor

/**
* Converts a table of contents to a renderable [OrderedList].
* @param renderer renderer to use to render items
* @param items ToC items [this] view should contain
* @param linkUrlMapper function that obtains the URL to send to when a ToC item is interacted with
*/
fun TableOfContentsView.convertToListNode(
renderer: NodeVisitor<CharSequence>,
items: List<TableOfContents.Item>,
linkUrlMapper: (TableOfContents.Item) -> String,
): OrderedList =
Expand All @@ -25,15 +29,15 @@ fun TableOfContentsView.convertToListNode(
// A link to the target heading.
this +=
Link(
item.text,
item.text.stripRichContent(renderer), // Rich content is ignored.
url = linkUrlMapper(item),
title = null,
)

// Recursively include sub-items.
item.subItems.filter { it.depth <= view.maxDepth }
.takeIf { it.isNotEmpty() }
?.let { this += convertToListNode(it, linkUrlMapper) }
?.let { this += convertToListNode(renderer, it, linkUrlMapper) }
}

return OrderedList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ class QuarkdownHtmlNodeRenderer(context: Context) : BaseHtmlNodeRenderer(context
// Content
+buildTag("nav") {
+node.convertToListNode(
this@QuarkdownHtmlNodeRenderer,
tableOfContents.items,
linkUrlMapper = { item ->
"#" + HtmlIdentifierProvider.of(this@QuarkdownHtmlNodeRenderer).getId(item.target)
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/kotlin/eu/iamgio/quarkdown/util/NodeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import eu.iamgio.quarkdown.ast.NestableNode
import eu.iamgio.quarkdown.ast.Node
import eu.iamgio.quarkdown.ast.base.inline.CriticalContent
import eu.iamgio.quarkdown.ast.base.inline.PlainTextNode
import eu.iamgio.quarkdown.ast.dsl.buildInline
import eu.iamgio.quarkdown.ast.quarkdown.inline.TextSymbol
import eu.iamgio.quarkdown.visitor.node.NodeVisitor

Expand Down Expand Up @@ -87,3 +88,12 @@ fun InlineContent.toPlainText(renderer: NodeVisitor<CharSequence>? = null): Stri

return builder.toString()
}

/**
* Strips rich content from [this] inline content and returns a new inline content with only one [eu.iamgio.quarkdown.ast.base.inline.Text] child,
* which contains the plain text representation of [this] inline content.
* @param renderer optional renderer to use to render critical content and text symbols
* @return inline content with only plain text
* @see toPlainText
*/
fun InlineContent.stripRichContent(renderer: NodeVisitor<CharSequence>? = null): InlineContent = buildInline { text(toPlainText(renderer)) }
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ class FullPipelineTest {
"<h1 id=\"table-of-contents\"><em>TOC</em></h1>" +
"<nav><ol>" +
"<li><a href=\"#abc\">ABC</a>" +
"<ol><li><a href=\"#abc1\"><em>ABC/1</em></a></li></ol></li>" +
"<ol><li><a href=\"#abc1\">ABC/1</a></li></ol></li>" +
"<li><a href=\"#def\">DEF</a>" +
"<ol><li><a href=\"#def1\">DEF/1</a>" +
"<ol><li><a href=\"#def2\">DEF/2</a></li>" +
Expand Down Expand Up @@ -1645,8 +1645,8 @@ class FullPipelineTest {
"<div class=\"page-break\" data-hidden=\"\"></div>" +
"<h1 id=\"table-of-contents\"><em><strong>TOC</strong></em></h1>" +
"<nav><ol>" +
"<li><a href=\"#marker-1\"><em>Marker 1</em></a></li>" +
"<li><a href=\"#marker-2\"><em>Marker 2</em></a></li>" +
"<li><a href=\"#marker-1\">Marker 1</a></li>" +
"<li><a href=\"#marker-2\">Marker 2</a></li>" +
"</ol></nav>" +
"<div class=\"marker\" data-hidden=\"\" id=\"marker-1\"></div>" +
"<div class=\"page-break\" data-hidden=\"\"></div>" +
Expand Down

0 comments on commit 2b25176

Please sign in to comment.