Skip to content

Commit

Permalink
Localize ToC title
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Sep 25, 2024
1 parent fadf4ed commit 88e8a8c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import eu.iamgio.quarkdown.ast.base.block.OrderedList
import eu.iamgio.quarkdown.ast.base.inline.CodeSpan
import eu.iamgio.quarkdown.ast.base.inline.Image
import eu.iamgio.quarkdown.ast.base.inline.Link
import eu.iamgio.quarkdown.ast.base.inline.Text
import eu.iamgio.quarkdown.ast.dsl.buildInline
import eu.iamgio.quarkdown.ast.id.getId
import eu.iamgio.quarkdown.ast.quarkdown.FunctionCallNode
import eu.iamgio.quarkdown.ast.quarkdown.block.Aligned
Expand Down Expand Up @@ -187,12 +187,15 @@ class QuarkdownHtmlNodeRenderer(context: Context) : BaseHtmlNodeRenderer(context
val tableOfContents = context.attributes.tableOfContents ?: return ""

return buildMultiTag {
// Title
// Localized title.
val titleText = context.localizeOrNull("tableofcontents")

// Title heading. Its content is either the node's user-set title or a default localized one.
+Heading(
depth = 1,
text = node.title ?: listOf(Text("Table of Contents")),
text = node.title ?: buildInline { titleText?.let { text(it) } },
customId = "table-of-contents",
) // In the future, the default title should be localized.
)
// Content
+buildTag("nav") {
+tableOfContentsItemsToList(tableOfContents.items, node)
Expand Down
4 changes: 3 additions & 1 deletion stdlib/src/main/resources/lib/localization.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
- tip: Tip
- warning: Warning
- error: Error
- tableofcontents: Table of Contents
- Italian
- note: Nota
- tip: Consiglio
- warning: Attenzione
- error: Errore
- error: Errore
- tableofcontents: Indice
61 changes: 61 additions & 0 deletions test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,67 @@ class FullPipelineTest {

@Test
fun `table of contents`() {
execute(
"""
.tableofcontents
# ABC
Hi
# DEF
Hello
""".trimIndent(),
MutableContextOptions(),
) {
assertEquals(
"<div class=\"page-break\" data-hidden=\"\"></div>" +
"<h1 id=\"table-of-contents\"></h1>" +
"<nav><ol>" +
"<li><a href=\"#abc\">ABC</a></li>" +
"<li><a href=\"#def\">DEF</a></li>" +
"</ol></nav>" +
"<div class=\"page-break\" data-hidden=\"\"></div>" +
"<h1 id=\"abc\">ABC</h1><p>Hi</p>" +
"<div class=\"page-break\" data-hidden=\"\"></div>" +
"<h1 id=\"def\">DEF</h1>" +
"<p>Hello</p>",
it,
)
}

execute(
"""
.doclang {english}
.tableofcontents
# ABC
Hi
# DEF
Hello
""".trimIndent(),
MutableContextOptions(),
) {
assertEquals(
"<div class=\"page-break\" data-hidden=\"\"></div>" +
"<h1 id=\"table-of-contents\">Table of Contents</h1>" + // Localized name
"<nav><ol>" +
"<li><a href=\"#abc\">ABC</a></li>" +
"<li><a href=\"#def\">DEF</a></li>" +
"</ol></nav>" +
"<div class=\"page-break\" data-hidden=\"\"></div>" +
"<h1 id=\"abc\">ABC</h1><p>Hi</p>" +
"<div class=\"page-break\" data-hidden=\"\"></div>" +
"<h1 id=\"def\">DEF</h1>" +
"<p>Hello</p>",
it,
)
}

execute(
"""
.tableofcontents title:{_TOC_}
Expand Down

0 comments on commit 88e8a8c

Please sign in to comment.