Skip to content

Commit 7e0be2d

Browse files
authored
feat: improve link handling (#3)
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. `<docName>/_index.md` 2. `<docName>.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: keptn/lifecycle-toolkit#1177 Signed-off-by: Simon Schrottner <[email protected]>
1 parent f26ddf6 commit 7e0be2d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{- if or (strings.HasPrefix .Destination "http") (strings.HasPrefix .Destination "#") -}}
2+
<a href="{{ .Destination | safeURL }}"{{ with .Title }} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
3+
{{- else -}}
4+
{{- $link := . -}}
5+
{{- $internal := urls.Parse .Destination -}}
6+
{{- if $internal.Path -}}
7+
{{- $fragment := "" }}
8+
{{- with $internal.Fragment }}{{ $fragment = printf "#%s" . }}{{ end -}}
9+
{{- with .Page.GetPage $internal.Path }}{{ $internal = printf "%s%s" .RelPermalink $fragment }}
10+
<a href="{{ $internal }}"{{ with .Title }} title="{{ . }}"{{ end }}>{{ $link.Text | safeHTML }}</a>
11+
{{- end -}}
12+
{{- end -}}
13+
{{- end -}}

0 commit comments

Comments
 (0)