| 1 |
{{/* |
| 2 |
This render hook resolves internal destinations by looking for a matching: |
| 3 |
|
| 4 |
1. Content page |
| 5 |
2. Page resource (a file in the current page bundle) |
| 6 |
3. Section resource (a file in the current section) |
| 7 |
4. Global resource (a file in the assets directory) |
| 8 |
|
| 9 |
It skips the section resource lookup if the current page is a leaf bundle. |
| 10 |
|
| 11 |
External destinations are not modified. |
| 12 |
|
| 13 |
You must place global resources in the assets directory. If you have placed |
| 14 |
your resources in the static directory, and you are unable or unwilling to move |
| 15 |
them, you must mount the static directory to the assets directory by including |
| 16 |
both of these entries in your project configuration: |
| 17 |
|
| 18 |
[[module.mounts]] |
| 19 |
source = 'assets' |
| 20 |
target = 'assets' |
| 21 |
|
| 22 |
[[module.mounts]] |
| 23 |
source = 'static' |
| 24 |
target = 'assets' |
| 25 |
|
| 26 |
By default, if this render hook is unable to resolve a destination, including a |
| 27 |
fragment if present, it passes the destination through without modification. To |
| 28 |
emit a warning or error, set the error level in your project configuration: |
| 29 |
|
| 30 |
[params.render_hooks.link] |
| 31 |
errorLevel = 'warning' # ignore (default), warning, or error (fails the build) |
| 32 |
|
| 33 |
When you set the error level to warning, and you are in a development |
| 34 |
environment, you can visually highlight broken internal links: |
| 35 |
|
| 36 |
[params.render_hooks.link] |
| 37 |
errorLevel = 'warning' # ignore (default), warning, or error (fails the build) |
| 38 |
highlightBroken = true # true or false (default) |
| 39 |
|
| 40 |
This will add a "broken" class to anchor elements with invalid src attributes. |
| 41 |
Add a rule to your CSS targeting the broken links: |
| 42 |
|
| 43 |
a.broken { |
| 44 |
background: #ff0; |
| 45 |
border: 2px solid #f00; |
| 46 |
padding: 0.1em 0.2em; |
| 47 |
} |
| 48 |
|
| 49 |
This render hook may be unable to resolve destinations created with the ref and |
| 50 |
relref shortcodes. Unless you set the error level to ignore you should not use |
| 51 |
either of these shortcodes in conjunction with this render hook. |
| 52 |
|
| 53 |
@context {string} Destination The link destination. |
| 54 |
@context {page} Page A reference to the page containing the link. |
| 55 |
@context {string} PlainText The link description as plain text. |
| 56 |
@context {string} Text The link description. |
| 57 |
@context {string} Title The link title. |
| 58 |
|
| 59 |
@returns {template.html} |
| 60 |
*/}} |
| 61 |
|
| 62 |
{{- /* Initialize. */}} |
| 63 |
{{- $renderHookName := "link" }} |
| 64 |
|
| 65 |
{{- /* Verify minimum required version. */}} |
| 66 |
{{- $minHugoVersion := "0.141.0" }} |
| 67 |
{{- if lt hugo.Version $minHugoVersion }} |
| 68 |
{{- errorf "The %q render hook requires Hugo v%s or later." $renderHookName $minHugoVersion }} |
| 69 |
{{- end }} |
| 70 |
|
| 71 |
{{- /* Error level when unable to resolve destination: ignore, warning, or error. */}} |
| 72 |
{{- $errorLevel := or site.Params.render_hooks.link.errorLevel "ignore" | lower }} |
| 73 |
|
| 74 |
{{- /* If true, adds "broken" class to broken links. Applicable in development environment when errorLevel is warning. */}} |
| 75 |
{{- $highlightBrokenLinks := or site.Params.render_hooks.link.highlightBroken false }} |
| 76 |
|
| 77 |
{{- /* Validate error level. */}} |
| 78 |
{{- if not (in (slice "ignore" "warning" "error") $errorLevel) }} |
| 79 |
{{- errorf "The %q render hook is misconfigured. The errorLevel %q is invalid. Please check your project configuration." $renderHookName $errorLevel }} |
| 80 |
{{- end }} |
| 81 |
|
| 82 |
{{- /* Determine content path for warning and error messages. */}} |
| 83 |
{{- $contentPath := .Page.String }} |
| 84 |
|
| 85 |
{{- /* Parse destination. */}} |
| 86 |
{{- $u := urls.Parse .Destination }} |
| 87 |
|
| 88 |
{{- /* Set common message. */}} |
| 89 |
{{- $msg := printf "The %q render hook was unable to resolve the destination %q in %s" $renderHookName $u.String $contentPath }} |
| 90 |
|
| 91 |
{{- /* Set attributes for anchor element. */}} |
| 92 |
{{- $attrs := dict "href" $u.String }} |
| 93 |
{{- if eq $u.String "g" }} |
| 94 |
{{- /* Destination is a glossary term. */}} |
| 95 |
{{- $ctx := dict |
| 96 |
"contentPath" $contentPath |
| 97 |
"errorLevel" $errorLevel |
| 98 |
"renderHookName" $renderHookName |
| 99 |
"text" .Text |
| 100 |
}} |
| 101 |
{{- $attrs = partial "inline/h-rh-l/get-glossary-link-attributes.html" $ctx }} |
| 102 |
{{- else if $u.IsAbs }} |
| 103 |
{{- /* Destination is a remote resource. */}} |
| 104 |
{{- $attrs = merge $attrs (dict "rel" "external nofollow") }} |
| 105 |
{{- else }} |
| 106 |
{{- with $u.Path }} |
| 107 |
{{- with $p := or ($.PageInner.GetPage .) ($.PageInner.GetPage (strings.TrimRight "/" .)) }} |
| 108 |
{{- /* Destination is a page. */}} |
| 109 |
{{- $href := .RelPermalink }} |
| 110 |
{{- with $u.RawQuery }} |
| 111 |
{{- $href = printf "%s?%s" $href . }} |
| 112 |
{{- end }} |
| 113 |
{{- with $u.Fragment }} |
| 114 |
{{- $ctx := dict |
| 115 |
"contentPath" $contentPath |
| 116 |
"errorLevel" $errorLevel |
| 117 |
"page" $p |
| 118 |
"parsedURL" $u |
| 119 |
"renderHookName" $renderHookName |
| 120 |
}} |
| 121 |
{{- partial "inline/h-rh-l/validate-fragment.html" $ctx }} |
| 122 |
{{- $href = printf "%s#%s" $href . }} |
| 123 |
{{- end }} |
| 124 |
{{- $attrs = dict "href" $href }} |
| 125 |
{{- else with $.PageInner.Resources.Get $u.Path }} |
| 126 |
{{- /* Destination is a page resource; drop query and fragment. */}} |
| 127 |
{{- $attrs = dict "href" .RelPermalink }} |
| 128 |
{{- else with (and (ne $.Page.BundleType "leaf") ($.Page.CurrentSection.Resources.Get $u.Path)) }} |
| 129 |
{{- /* Destination is a section resource, and current page is not a leaf bundle. */}} |
| 130 |
{{- $attrs = dict "href" .RelPermalink }} |
| 131 |
{{- else with resources.Get $u.Path }} |
| 132 |
{{- /* Destination is a global resource; drop query and fragment. */}} |
| 133 |
{{- $attrs = dict "href" .RelPermalink }} |
| 134 |
{{- else }} |
| 135 |
{{- if eq $errorLevel "warning" }} |
| 136 |
{{- warnf $msg }} |
| 137 |
{{- if and $highlightBrokenLinks hugo.IsDevelopment }} |
| 138 |
{{- $attrs = merge $attrs (dict "class" "broken") }} |
| 139 |
{{- end }} |
| 140 |
{{- else if eq $errorLevel "error" }} |
| 141 |
{{- errorf $msg }} |
| 142 |
{{- end }} |
| 143 |
{{- end }} |
| 144 |
{{- else }} |
| 145 |
{{- with $u.Fragment }} |
| 146 |
{{- /* Destination is on the same page; prepend relative permalink. */}} |
| 147 |
{{- $ctx := dict |
| 148 |
"contentPath" $contentPath |
| 149 |
"errorLevel" $errorLevel |
| 150 |
"page" $.Page |
| 151 |
"parsedURL" $u |
| 152 |
"renderHookName" $renderHookName |
| 153 |
}} |
| 154 |
{{- partial "inline/h-rh-l/validate-fragment.html" $ctx }} |
| 155 |
{{- $attrs = dict "href" (printf "%s#%s" $.Page.RelPermalink .) }} |
| 156 |
{{- else }} |
| 157 |
{{- if eq $errorLevel "warning" }} |
| 158 |
{{- warnf $msg }} |
| 159 |
{{- if and $highlightBrokenLinks hugo.IsDevelopment }} |
| 160 |
{{- $attrs = merge $attrs (dict "class" "broken") }} |
| 161 |
{{- end }} |
| 162 |
{{- else if eq $errorLevel "error" }} |
| 163 |
{{- errorf $msg }} |
| 164 |
{{- end }} |
| 165 |
{{- end }} |
| 166 |
{{- end }} |
| 167 |
{{- end }} |
| 168 |
|
| 169 |
{{- /* Render anchor element. */ -}} |
| 170 |
<a |
| 171 |
{{- with .Title }}title="{{ . }}"{{- end }} |
| 172 |
{{- range $k, $v := $attrs }} |
| 173 |
{{- if $v }} |
| 174 |
{{- printf " %s=%q" $k ($v | transform.HTMLEscape) | safeHTMLAttr }} |
| 175 |
{{- end }} |
| 176 |
{{- end -}}>{{ .Text }}</a> |
| 177 |
|
| 178 |
{{- define "_partials/inline/h-rh-l/validate-fragment.html" }} |
| 179 |
{{- /* |
| 180 |
Validates the fragment portion of a link destination. |
| 181 |
|
| 182 |
@context {string} contentPath The page containing the link. |
| 183 |
@context {string} errorLevel The error level when unable to resolve destination; ignore (default), warning, or error. |
| 184 |
@context {page} page The page corresponding to the link destination |
| 185 |
@context {struct} parsedURL The link destination parsed by urls.Parse. |
| 186 |
@context {string} renderHookName The name of the render hook. |
| 187 |
*/}} |
| 188 |
|
| 189 |
{{- /* Initialize. */}} |
| 190 |
{{- $contentPath := .contentPath }} |
| 191 |
{{- $errorLevel := .errorLevel }} |
| 192 |
{{- $p := .page }} |
| 193 |
{{- $u := .parsedURL }} |
| 194 |
{{- $renderHookName := .renderHookName }} |
| 195 |
|
| 196 |
{{- /* Validate. */}} |
| 197 |
{{- with $u.Fragment }} |
| 198 |
{{- if $p.Fragments.Identifiers.Contains . }} |
| 199 |
{{- if gt ($p.Fragments.Identifiers.Count .) 1 }} |
| 200 |
{{- $msg := printf "The %q render hook detected duplicate heading IDs %q in %s" $renderHookName . $contentPath }} |
| 201 |
{{- if eq $errorLevel "warning" }} |
| 202 |
{{- warnf $msg }} |
| 203 |
{{- else if eq $errorLevel "error" }} |
| 204 |
{{- errorf $msg }} |
| 205 |
{{- end }} |
| 206 |
{{- end }} |
| 207 |
{{- else }} |
| 208 |
{{- /* Determine target path for warning and error message. */}} |
| 209 |
{{- $targetPath := "" }} |
| 210 |
{{- with $p.File }} |
| 211 |
{{- $targetPath = .Path }} |
| 212 |
{{- else }} |
| 213 |
{{- $targetPath = .Path }} |
| 214 |
{{- end }} |
| 215 |
{{- /* Set common message. */}} |
| 216 |
{{- $msg := printf "The %q render hook was unable to find heading ID %q in %s. See %s" $renderHookName . $targetPath $contentPath }} |
| 217 |
{{- if eq $targetPath $contentPath }} |
| 218 |
{{- $msg = printf "The %q render hook was unable to find heading ID %q in %s" $renderHookName . $targetPath }} |
| 219 |
{{- end }} |
| 220 |
{{- /* Throw warning or error. */}} |
| 221 |
{{- if eq $errorLevel "warning" }} |
| 222 |
{{- warnf $msg }} |
| 223 |
{{- else if eq $errorLevel "error" }} |
| 224 |
{{- errorf $msg }} |
| 225 |
{{- end }} |
| 226 |
{{- end }} |
| 227 |
{{- end }} |
| 228 |
{{- end }} |
| 229 |
|
| 230 |
{{- define "_partials/inline/h-rh-l/get-glossary-link-attributes.html" }} |
| 231 |
{{- /* |
| 232 |
Returns the anchor element attributes for a link to the given glossary term. |
| 233 |
|
| 234 |
It first checks for the existence of a glossary page for the given term. If |
| 235 |
no page is found, it then checks for a glossary page for the singular form of |
| 236 |
the term. If neither page exists it throws a warning or error dependent on |
| 237 |
the errorLevel setting |
| 238 |
|
| 239 |
The returned href attribute does not point to the glossary term page. |
| 240 |
Instead, via its fragment, it points to an entry on the glossary page. |
| 241 |
|
| 242 |
@context {string} contentPath The page containing the link. |
| 243 |
@context {string} errorLevel The error level when unable to resolve destination; ignore (default), warning, or error. |
| 244 |
@context {string} renderHookName The name of the render hook. |
| 245 |
@context {string} text The link text. |
| 246 |
*/}} |
| 247 |
|
| 248 |
{{- /* Get context.. */}} |
| 249 |
{{- $contentPath := .contentPath }} |
| 250 |
{{- $errorLevel := .errorLevel }} |
| 251 |
{{- $renderHookName := .renderHookName }} |
| 252 |
{{- $text := .text | transform.Plainify | strings.ToLower }} |
| 253 |
|
| 254 |
{{- /* Initialize. */}} |
| 255 |
{{- $glossaryPath := "/quick-reference/glossary" }} |
| 256 |
{{- $termGiven := $text }} |
| 257 |
{{- $termActual := "" }} |
| 258 |
{{- $termSingular := inflect.Singularize $termGiven }} |
| 259 |
|
| 260 |
{{- /* Verify that the glossary page exists. */}} |
| 261 |
{{- $glossaryPage := site.GetPage $glossaryPath }} |
| 262 |
{{- if not $glossaryPage }} |
| 263 |
{{- errorf "The %q render hook was unable to find %s: see %s" $renderHookName $glossaryPath $contentPath }} |
| 264 |
{{- end }} |
| 265 |
|
| 266 |
{{- /* There's a better way to handle this, but it works for now. */}} |
| 267 |
{{- $cheating := dict |
| 268 |
"chaining" "chain" |
| 269 |
"ci/cd" "cicd" |
| 270 |
"interleaved" "interleave" |
| 271 |
"localize" "localization" |
| 272 |
"localized" "localization" |
| 273 |
"paginating" "paginate" |
| 274 |
"walking" "walk" |
| 275 |
}} |
| 276 |
|
| 277 |
{{- /* Verify that a glossary term page exists for the given term. */}} |
| 278 |
{{- if site.GetPage (urls.JoinPath $glossaryPath ($termGiven | urlize)) }} |
| 279 |
{{- $termActual = $termGiven }} |
| 280 |
{{- else if site.GetPage (urls.JoinPath $glossaryPath ($termSingular | urlize)) }} |
| 281 |
{{- $termActual = $termSingular }} |
| 282 |
{{- else }} |
| 283 |
{{- $termToTest := index $cheating $termGiven }} |
| 284 |
{{- if site.GetPage (urls.JoinPath $glossaryPath ($termToTest | urlize)) }} |
| 285 |
{{- $termActual = $termToTest }} |
| 286 |
{{- end }} |
| 287 |
{{- end }} |
| 288 |
|
| 289 |
{{- if not $termActual }} |
| 290 |
{{- errorf "The %q render hook was unable to find a glossary page for either the singular or plural form of the term %q: see %s" $renderHookName $termGiven $contentPath }} |
| 291 |
{{- end }} |
| 292 |
|
| 293 |
{{- /* Create the href attribute. */}} |
| 294 |
{{- $href := "" }} |
| 295 |
{{- if $termActual }} |
| 296 |
{{- $href = fmt.Printf "%s#%s" $glossaryPage.RelPermalink (anchorize $termActual) }} |
| 297 |
{{- end }} |
| 298 |
|
| 299 |
{{- return (dict "href" $href) }} |
| 300 |
{{- end -}} |