From 88e8a8c3a36d72996932832ad72df9150dfba122 Mon Sep 17 00:00:00 2001 From: Giorgio Garofalo Date: Wed, 25 Sep 2024 14:29:34 +0200 Subject: [PATCH] Localize ToC title --- .../html/QuarkdownHtmlNodeRenderer.kt | 11 ++-- .../src/main/resources/lib/localization.qmd | 4 +- .../iamgio/quarkdown/test/FullPipelineTest.kt | 61 +++++++++++++++++++ 3 files changed, 71 insertions(+), 5 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 bd683942..1be17059 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 @@ -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 @@ -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) diff --git a/stdlib/src/main/resources/lib/localization.qmd b/stdlib/src/main/resources/lib/localization.qmd index 922557dc..f3b24826 100644 --- a/stdlib/src/main/resources/lib/localization.qmd +++ b/stdlib/src/main/resources/lib/localization.qmd @@ -6,8 +6,10 @@ - tip: Tip - warning: Warning - error: Error + - tableofcontents: Table of Contents - Italian - note: Nota - tip: Consiglio - warning: Attenzione - - error: Errore \ No newline at end of file + - error: Errore + - tableofcontents: Indice \ No newline at end of file diff --git a/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt b/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt index a725024b..32af1a02 100644 --- a/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt +++ b/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt @@ -877,6 +877,67 @@ class FullPipelineTest { @Test fun `table of contents`() { + execute( + """ + .tableofcontents + + # ABC + + Hi + + # DEF + + Hello + """.trimIndent(), + MutableContextOptions(), + ) { + assertEquals( + "
" + + "

" + + "" + + "
" + + "

ABC

Hi

" + + "
" + + "

DEF

" + + "

Hello

", + it, + ) + } + + execute( + """ + .doclang {english} + .tableofcontents + + # ABC + + Hi + + # DEF + + Hello + """.trimIndent(), + MutableContextOptions(), + ) { + assertEquals( + "
" + + "

Table of Contents

" + // Localized name + "" + + "
" + + "

ABC

Hi

" + + "
" + + "

DEF

" + + "

Hello

", + it, + ) + } + execute( """ .tableofcontents title:{_TOC_}