Skip to content

Commit

Permalink
Merge pull request #534 from jp7677/markdown-links
Browse files Browse the repository at this point in the history
Add trailing slash to site links within markdown
  • Loading branch information
jp7677 authored Jun 19, 2024
2 parents 784136e + 3413099 commit 3536634
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/example/workspace-docs/02-markdown-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
<h2>header</h2>
<p>Link: <a href="decisions/1/">Some decision</a>,</p>
""".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(
"""
<h2>header</h2>
<p>Link: <a href="document.md">Some document</a>,</p>
""".trimIndent()
)
}
}

0 comments on commit 3536634

Please sign in to comment.