From c9f7ebf0043c3e7a0f0236e9f5aa6513f4761923 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Tue, 20 Feb 2024 17:57:51 -0800 Subject: [PATCH] tpl/tplimpl: Resolve fragments in link render hook Fixes #12084 --- .../_default/_markup/render-link.html | 4 +- tpl/tplimpl/render_hook_integration_test.go | 129 ++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 tpl/tplimpl/render_hook_integration_test.go diff --git a/tpl/tplimpl/embedded/templates/_default/_markup/render-link.html b/tpl/tplimpl/embedded/templates/_default/_markup/render-link.html index bd64b204b35..cfc95ab3a2d 100644 --- a/tpl/tplimpl/embedded/templates/_default/_markup/render-link.html +++ b/tpl/tplimpl/embedded/templates/_default/_markup/render-link.html @@ -1,6 +1,8 @@ {{- $u := urls.Parse .Destination -}} {{- $href := $u.String -}} -{{- if not $u.IsAbs -}} +{{- if strings.HasPrefix $u.String "#" }} + {{- $href = printf "%s#%s" .Page.RelPermalink $u.Fragment }} +{{- else if not $u.IsAbs -}} {{- with or ($.Page.GetPage $u.Path) ($.Page.Resources.Get $u.Path) diff --git a/tpl/tplimpl/render_hook_integration_test.go b/tpl/tplimpl/render_hook_integration_test.go new file mode 100644 index 00000000000..607e06059cc --- /dev/null +++ b/tpl/tplimpl/render_hook_integration_test.go @@ -0,0 +1,129 @@ +// Copyright 2024 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tplimpl_test + +import ( + "testing" + + "github.com/gohugoio/hugo/hugolib" +) + +func TestEmbeddedLinkRenderHook(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +disableKinds = ['rss','sitemap','taxonomy','term'] +[markup.goldmark.renderHooks.link] +enableDefault = true +-- layouts/_default/list.html -- +{{ .Content }} +-- layouts/_default/single.html -- +{{ .Content }} +-- assets/a.txt -- +irrelevant +-- content/_index.md -- +--- +title: home +--- +-- content/s1/_index.md -- +--- +title: s1 +--- +-- content/s1/p1.md -- +--- +title: s1/p1 +--- +-- content/s1/p2/index.md -- +--- +title: s1/p2 +--- +[500](a.txt) // global resource +[600](b.txt) // page resource +-- content/s1/p2/b.txt -- +irrelevant +-- content/s1/p3.md -- +--- +title: s1/p3 +--- +// Remote +[10](https://a.org) + +// fragment +[100](/#foo) +[110](#foo) +[120](p1#foo) +[130](p1/#foo) + +// section page +[200](s1) +[210](/s1) +[220](../s1) +[230](s1/) +[240](/s1/) +[250](../s1/) + +// regular page +[300](p1) +[310](/s1/p1) +[320](../s1/p1) +[330](p1/) +[340](/s1/p1/) +[350](../s1/p1/) + +// leaf bundle +[400](p2) +[410](/s1/p2) +[420](../s1/p2) +[430](p2/) +[440](/s1/p2/) +[450](../s1/p2/) +` + + b := hugolib.Test(t, files) + + b.AssertFileContent("public/s1/p3/index.html", + `10`, + `100`, + `110`, + `120`, + `130`, + + `200`, + `210`, + `220`, + `230`, + `240`, + `250`, + + `300`, + `310`, + `320`, + `330`, + `340`, + `350`, + + `400`, + `410`, + `420`, + `430`, + `440`, + `450`, + ) + + b.AssertFileContent("public/s1/p2/index.html", + `500`, + `600`, + ) +}