Skip to content

Commit

Permalink
feat: improve link handling (#3)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
aepfli authored Apr 4, 2023
1 parent f26ddf6 commit 7e0be2d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions layouts/_default/_markup/render-link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{- if or (strings.HasPrefix .Destination "http") (strings.HasPrefix .Destination "#") -}}
<a href="{{ .Destination | safeURL }}"{{ with .Title }} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>
{{- 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 }}
<a href="{{ $internal }}"{{ with .Title }} title="{{ . }}"{{ end }}>{{ $link.Text | safeHTML }}</a>
{{- end -}}
{{- end -}}
{{- end -}}

0 comments on commit 7e0be2d

Please sign in to comment.