From 3413099730753cf293db4e0916a74bbcec46f743 Mon Sep 17 00:00:00 2001 From: Jens Peters Date: Fri, 7 Jun 2024 08:50:47 +0200 Subject: [PATCH] Add trailing slash to site links within markdown --- .../workspace-docs/02-markdown-features.md | 8 ++++ .../site/generatr/site/model/ToHtml.kt | 10 +++- .../generatr/site/model/MarkdownToHtmlTest.kt | 46 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/docs/example/workspace-docs/02-markdown-features.md b/docs/example/workspace-docs/02-markdown-features.md index 2b89dc48..884e8408 100644 --- a/docs/example/workspace-docs/02-markdown-features.md +++ b/docs/example/workspace-docs/02-markdown-features.md @@ -301,3 +301,11 @@ will be rendered as ```DSL "generatr.markdown.flexmark.extensions" "GfmTaskList" ``` + +### Links to Markdown documents + +Placing links on markdown pages is easy. You can link to: + +* an overview page like [Workspace decisions](/decisions/), +* an individual decision [ADR-0001. record architecture decisions](/decisions/1/) +* or some system documentation like [Internet Banking](/internet-banking-system/). diff --git a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ToHtml.kt b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ToHtml.kt index 6203359f..6379d09f 100644 --- a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ToHtml.kt +++ b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ToHtml.kt @@ -14,6 +14,7 @@ import kotlinx.html.stream.createHTML import net.sourceforge.plantuml.FileFormat import net.sourceforge.plantuml.FileFormatOption import net.sourceforge.plantuml.SourceStringReader +import nl.avisi.structurizr.site.generatr.site.asUrlToDirectory import nl.avisi.structurizr.site.generatr.site.asUrlToFile import nl.avisi.structurizr.site.generatr.site.views.diagram import org.asciidoctor.Options @@ -134,7 +135,14 @@ private class CustomLinkResolver(private val pageViewModel: PageViewModel) : Lin return link return link.withStatus(LinkStatus.VALID) - .withUrl("/${link.url.dropWhile { it == '/' }}".asUrlToFile(pageViewModel.url)) + .withUrl("/${link.url.dropWhile { it == '/' }}" + .let { + if (it.endsWith('/')) + it.asUrlToDirectory(pageViewModel.url) + else + it.asUrlToFile(pageViewModel.url) + } + ) } class Factory(private val viewModel: PageViewModel) : LinkResolverFactory { diff --git a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/model/MarkdownToHtmlTest.kt b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/model/MarkdownToHtmlTest.kt index 242a7010..7f664657 100644 --- a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/model/MarkdownToHtmlTest.kt +++ b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/model/MarkdownToHtmlTest.kt @@ -334,4 +334,50 @@ class MarkdownToHtmlTest : ViewModelTest() { """.trimIndent() ) } + + @Test + fun `links to pages`() { + val generatorContext = generatorContext() + val viewModel = HomePageViewModel(generatorContext) + + val html = toHtml( + viewModel, + """ + ## header + Link: [Some decision](/decisions/1/), + """.trimIndent(), + Format.Markdown, + svgFactory + ) + + assertThat(html).isEqualTo( + """ +

header

+

Link: Some decision,

+ """.trimIndent() + ) + } + + @Test + fun `links to files`() { + val generatorContext = generatorContext() + val viewModel = HomePageViewModel(generatorContext) + + val html = toHtml( + viewModel, + """ + ## header + Link: [Some document](/document.md), + """.trimIndent(), + Format.Markdown, + svgFactory + ) + + assertThat(html).isEqualTo( + """ +

header

+

Link: Some document,

+ """.trimIndent() + ) + } }