From 43f52f1aea6fb106eac0ece957ab1d7446601ba7 Mon Sep 17 00:00:00 2001 From: Bridget McErlean Date: Thu, 11 Mar 2021 19:01:51 -0500 Subject: [PATCH] Correctly render links with fragments on docs pages Our previous render hook to create links would drop the fragment when linking to headings within the current page or within other markdown pages on the site. This change parses the URL and formats the link correctly if it includes a fragment. If the link is a header on the current page, it is rendered as `http:///#header`. If the link is a header on a different page (e.g. page.md#header), it is rendered as `http:///#header`. This change is taken from the following Hugo community support post: https://discourse.gohugo.io/t/markdown-render-hooks-github-and-hugo-compatible-links/22543/14 Signed-off-by: Bridget McErlean --- site/layouts/_default/_markup/render-link.html | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/site/layouts/_default/_markup/render-link.html b/site/layouts/_default/_markup/render-link.html index d69b894aa..416465d96 100644 --- a/site/layouts/_default/_markup/render-link.html +++ b/site/layouts/_default/_markup/render-link.html @@ -1,5 +1,10 @@ {{ $link := .Destination }} {{ if not (strings.HasPrefix $link "http") }} - {{ $link = (.Page.GetPage .Destination).RelPermalink }} + {{ $url := urls.Parse .Destination }} + {{- if $url.Path -}} + {{ $fragment := "" }} + {{- with $url.Fragment }}{{ $fragment = printf "#%s" . }}{{ end -}} + {{- with .Page.GetPage $url.Path }}{{ $link = printf "%s%s" .RelPermalink $fragment }}{{ end }} + {{ end }} {{ end }} {{ .Text | safeHTML }} \ No newline at end of file