{{/* Copyright 2026 The Agent Substrate Authors 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. */ -}} {{/* Rewrites relative Markdown links in .content, resolved against .dir (the directory of the repository file the text came from): - a file that some page includes via repo_source links to that page (or its Markdown output when .markdown is true); - a path under params.repo_static_dirs links to the mounted static copy; - anything else links to the file on GitHub. Set .absolute to emit full URLs instead of site-relative paths. */ -}} {{- $content := .content -}} {{- $dir := .dir -}} {{- $absolute := .absolute | default false -}} {{- $markdown := .markdown | default false -}} {{- $repo := site.Params.github_repo -}} {{- $branch := site.Params.github_branch | default "main" -}} {{- range findRESubmatch `(?:\]\(([^)\s]+)\)|href="([^"]+)")` $content -}} {{- $mdTarget := index . 1 -}} {{- $hrefTarget := index . 2 -}} {{- $target := or $mdTarget $hrefTarget -}} {{- $isRelative := not (or (strings.HasPrefix $target "#") (strings.HasPrefix $target "/") (strings.Contains $target ":")) -}} {{- if $isRelative -}} {{- $parts := split $target "#" -}} {{- $rel := index $parts 0 -}} {{- $anchor := "" -}} {{- if gt (len $parts) 1 -}} {{- $anchor = printf "#%s" (index $parts 1) -}} {{- end -}} {{- $repoPath := path.Join $dir $rel -}} {{- $url := "" -}} {{- $matchedPages := or (where site.Pages "Params.repo_source" $repoPath) (where site.Pages "Params.repo_sources" "intersect" (slice $repoPath)) -}} {{- with $matchedPages -}} {{- $p := index . 0 -}} {{- if $markdown -}} {{- with $p.OutputFormats.Get "markdown" -}} {{- $url = cond $absolute .Permalink .RelPermalink -}} {{- end -}} {{- end -}} {{- if not $url -}} {{- $url = cond $absolute $p.Permalink $p.RelPermalink -}} {{- end -}} {{- else -}} {{- range site.Params.repo_static_dirs -}} {{- if and (not $url) (strings.HasPrefix $repoPath (printf "%s/" .)) -}} {{- $url = cond $absolute (absURL $repoPath) (relURL $repoPath) -}} {{- end -}} {{- end -}} {{- end -}} {{- if not $url -}} {{- $kind := cond (or (strings.HasSuffix $rel "/") (eq (path.Ext $repoPath) "")) "tree" "blob" -}} {{- $url = printf "%s/%s/%s/%s" $repo $kind $branch $repoPath -}} {{- end -}} {{- if $mdTarget -}} {{- $content = replace $content (printf "](%s)" $target) (printf "](%s%s)" $url $anchor) -}} {{- else -}} {{- $content = replace $content (printf "href=%q" $target) (printf "href=\"%s%s\"" $url $anchor) -}} {{- end -}} {{- end -}} {{- end -}} {{- return $content -}}