mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 23:38:53 +00:00
Add glossary link shortcode
This commit is contained in:
@@ -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 }}
|
||||
@@ -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. */}}
|
||||
|
||||
@@ -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 }}
|
||||
|
||||
Reference in New Issue
Block a user