+{{ .Text }}+{{ end -}} diff --git a/layouts/_default/_markup/render-link.html b/layouts/_default/_markup/render-link.html new file mode 100644 index 0000000000..3788cfbee6 --- /dev/null +++ b/layouts/_default/_markup/render-link.html @@ -0,0 +1,64 @@ +{{- /* + Link render hook: resolves internal Markdown links to their published + permalink, recreating the behaviour of the `relref` shortcode so that + `[text](/path)` can replace `[text]({{< relref "/path" >}})`. + + Prototype from DOC-6909 (investigate alternatives to Hugo shortcodes). + Verified against a `relref` baseline: internal links render byte-identically; + pre-existing plain links are normalised (relative -> absolute, `.md` stripped, + trailing slash added). See HUGO_DEPENDENCY_ASSESSMENT.md. + + Resolution uses .PageInner, not .Page, so relative links resolve against the + page whose Markdown contains the link even when content is transcluded. + + NOTE: remove the placeholder transition guard once every `relref` has been + migrated to a plain Markdown link. +*/ -}} +{{- $dest := .Destination -}} +{{- $text := .Text -}} +{{- $title := .Title -}} +{{- if strings.Contains $dest "HAHAHUGOSHORTCODE" -}} + {{- /* A relref/shortcode has not been substituted yet; leave the placeholder + for Hugo to fill in and do not attempt to resolve or warn. */ -}} + {{ $text | safeHTML }} +{{- else if strings.HasPrefix $dest "#" -}} + {{- /* Fragment-only: a same-page internal link, emitted unchanged. */ -}} + {{ $text | safeHTML }} +{{- else if or (findRE "^[a-zA-Z][a-zA-Z0-9+.\\-]*:" $dest) (strings.HasPrefix $dest "//") -}} + {{- /* External: a URL scheme or protocol-relative link. Detected with findRE + rather than urls.Parse, which hard-errors on malformed destinations. */ -}} + {{ $text | safeHTML }} +{{- else -}} + {{- $path := $dest -}} + {{- $anchor := "" -}} + {{- /* Split on the first `#` only, so an anchor that itself contains `#` + (malformed but present in the corpus) is preserved intact. */ -}} + {{- if strings.Contains $path "#" -}} + {{- $parts := split $path "#" -}} + {{- $path = index $parts 0 -}} + {{- $anchor = printf "#%s" (delimit (after 1 $parts) "#") -}} + {{- end -}} + {{- /* Strip a leading `./` so explicit same-directory links resolve; leave + `../` alone, which GetPage resolves relative to the current page. */ -}} + {{- $path = strings.TrimPrefix "./" $path -}} + {{- /* Normalise source-relative Markdown links so they resolve in the repo + (VS Code, GitHub) and here: drop the `.md`, and a trailing `/_index` + or `/index` from links that point at a section or leaf-bundle file. */ -}} + {{- $lookup := strings.TrimSuffix ".md" $path -}} + {{- $lookup = strings.TrimSuffix "/_index" $lookup -}} + {{- $lookup = strings.TrimSuffix "/index" $lookup -}} + {{- $target := .PageInner.GetPage $lookup -}} + {{- if $target -}} + {{ $text | safeHTML }} + {{- else -}} + {{- /* Not a content page: try a page-bundle resource (e.g. the source + files linked with [source](cache.rs) in the use-case demos). */ -}} + {{- $res := .PageInner.Resources.GetMatch $path -}} + {{- if $res -}} + {{ $text | safeHTML }} + {{- else -}} + {{- warnf "render-link: unresolved link %q on page %q" $dest .PageInner.Path -}} + {{ $text | safeHTML }} + {{- end -}} + {{- end -}} +{{- end -}}