From 40de39117b5e30a5ee0c40fb724e240ec43882a4 Mon Sep 17 00:00:00 2001 From: Simon Schrottner Date: Tue, 4 Apr 2023 12:39:10 +0200 Subject: [PATCH] feat: improve link handling Hugo could be better when linking markdown files if they are outside their own folder. With this improvement, we will fetch all relative links, even with `.md,` and generate their url different than the [originial version](https://gohugo.io/templates/render-hooks/#link-with-title-markdown-example). This change now also allows links to `.md` files within the links, so we don't need to care about this anymore. In Hugo you can write documentation with two different naming/structuring approaches: 1. `/_index.md` 2. `.md` The problem is that links from files within the second approach could be simpler. Locally and in the Markdown space, they're in the same folder. But when Hugo renders them, there is a folder level in between. With this approach, we're changing how hugo renders relative files. Rather than blindly using the link for pages within the documentation, we search for that page, and take the link from this page, generate via Hugo. This way we will always have the right directory levels, for markdown and for hugo relates: https://github.com/keptn/lifecycle-toolkit/pull/1177 Signed-off-by: Simon Schrottner --- layouts/_default/_markup/render-link.html | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 layouts/_default/_markup/render-link.html diff --git a/layouts/_default/_markup/render-link.html b/layouts/_default/_markup/render-link.html new file mode 100644 index 0000000..89d0b0d --- /dev/null +++ b/layouts/_default/_markup/render-link.html @@ -0,0 +1,13 @@ +{{- if or (strings.HasPrefix .Destination "http") (strings.HasPrefix .Destination "#") -}} +{{ .Text | safeHTML }} +{{- else -}} +{{- $link := . -}} +{{- $internal := urls.Parse .Destination -}} +{{- if $internal.Path -}} +{{- $fragment := "" }} +{{- with $internal.Fragment }}{{ $fragment = printf "#%s" . }}{{ end -}} +{{- with .Page.GetPage $internal.Path }}{{ $internal = printf "%s%s" .RelPermalink $fragment }} +{{ $link.Text | safeHTML }} +{{- end -}} +{{- end -}} +{{- end -}}