Add glossary link shortcode

This commit is contained in:
Joe Mooring
2025-01-18 15:20:16 -08:00
committed by Joe Mooring
parent 4171c0eb7d
commit 6a06a8de75
3 changed files with 47 additions and 4 deletions
+42
View File
@@ -0,0 +1,42 @@
{{- /*
Renders a link to the glossary entry for the given term.
It first checks for the existence of a glossary page for the given term. If no
page is found, it then checks for a glossary page for the singular form of the
term. If neither page exists, the shortcode raises an error.
The rendered link displays the given term as the link text. However, the link
itself does not point directly to the glossary term page. Instead, via its
fragment, it points to an entry on the glossary page.
@param {string} (.Get 0) The glossary term.
@returns {template.HTML}
@example {{% gl float %}}
@example {{% gl "floating point" %}}
*/}}
{{- with $termGiven := .Get 0 }}
{{- $basePath := "/getting-started/glossary" }}
{{- $termActual := "" }}
{{- $termSingular := inflect.Singularize $termGiven }}
{{- if site.GetPage (urls.JoinPath $basePath $termGiven) }}
{{- $termActual = $termGiven }}
{{- else if site.GetPage (urls.JoinPath $basePath $termSingular) }}
{{- $termActual = $termSingular}}
{{- else }}
{{- errorf "The glossary link (%s) shortcode was unable to find a defintion for either the singular or plural form of the term %q: see %s" $.Name $termGiven $.Position }}
{{- end }}
{{- if $termActual }}
{{- with site.GetPage $basePath }}
{{- $href := fmt.Printf "%s#%s" .RelPermalink (anchorize $termActual) }}
[{{ $termGiven }}]({{ $href }}){{/* Do not indent. */}}
{{- else }}
{{- errorf "The glossary link (%s) shortcode was unable to find %s: see %s" $.Name $basePath $.Position }}
{{- end }}
{{- end }}
{{- else }}
{{- errorf "The glossary link (%s) shortcode requires one positional parameter: see %s" $.Name $.Position }}
{{- end }}
+1 -1
View File
@@ -25,7 +25,7 @@ shortcode.
{{- continue }}
{{- end }}
{{- $anchor := path.BaseName .Path | anchorize }}
{{- $m = merge $m (dict $k ($anchor | anchorize)) }}
{{- $m = merge $m (dict $k $anchor) }}
{{- end }}
{{- range $k, $v := $m }}
[{{ $k }}](#{{ $v }}) {{/* Do not indent. */}}
+4 -3
View File
@@ -1,5 +1,5 @@
{{- /*
Renders a glossary term.
Renders the definition of the given glossary term.
@param {string} (.Get 0) The glossary term.
@returns {template.HTML}
@@ -7,13 +7,14 @@ Renders a glossary term.
@example {{% gt float %}}
@example {{% gt "floating point" %}}
*/}}
{{- with .Get 0 }}
{{- $path := printf "/getting-started/glossary/%s" (urlize .) }}
{{- with site.GetPage $path }}
{{ .RenderShortcodes }}{{/* Do not indent this line. */}}
{{ .RenderShortcodes }}{{/* Do not indent. */}}
{{- else }}
{{- errorf "The glossary term (%s) shortcode was unable to find %s: see %s" $.Name $path $.Position }}
{{- end }}
{{- else }}
{{- errorf "The glossary term (%s) shortcode requires one positional parameter: see %s" .Name .Position }}
{{- errorf "The glossary term (%s) shortcode requires one positional parameter: see %s" $.Name $.Position }}
{{- end }}