hugo/layouts/shortcodes/code-toggle.html
Bjørn Erik Pedersen 45e6fdb315 Squashed 'docs/' changes from 316cec249..4eb10c1a9
4eb10c1a9 Set a sensible maxAge on the image cache
2c37f4f89 Update lists.md
8690c018f Update introduction.md
0458c3d88 Update index.md
150982f5e Update index.md
50dc0b6b9 Update multilingual.md
648598090 Configure image cache to use :cacheDir/images
4dfb523c3 Update introduction.md
b94d42e6f Add text to social images
d3253d2aa Add resources/_gen/images/ to .gitignore
8a56e1dfd Delete resources/_gen/images
1521e79b6 Update introduction.md
c7110a597 Update introduction.md
f7c4d220f Update introduction.md
c14ecf1ff Update introduction.md
c4b388c20 Fix shortcode in hugo-pipes/introduction (#1605)
d9876bd68 Update introduction.md
a39a532d5 Update introduction.md
fa8758122 netlify: Hugo 0.90.1
50cc40570 Update Contribute to Hugo Development
fc98fc82c Add .vscode dir to .gitignore (#1602)
8f8bcdefc Add frontmatter param to code-toggle shortcode (#1601)
f3b432c7c Add markdownlint configuration file (#1597)
7ea3aea92 netlify: Bump to 0.90.0
e2b6f990b docs: Regenerate docs helper
c02259e33 Merge commit '8d9511a08f14260cbfb73119e4afae50e5a9966d'
2e3573899 Add custom font support to images.Text
3186e6458 images: Text filter that draws text with the given options (#9239)
424f219eb tpl/transform: Optional options for highlight func
7993f17d0 Implement XML data support
1eb9d34cf Make resources.Get use a file cache for remote resources
8a0fffd56 Add remote support to resources.Get

git-subtree-dir: docs
git-subtree-split: 4eb10c1a9689aebf652011a6260a14bf8a3dcef4
2021-12-13 21:04:12 +01:00

108 lines
3.5 KiB
HTML

{{- /*
Renders syntax-highlighted configuration data in JSON, TOML, and YAML formats.
@param {string} config
Section of site.Data.docs.config to render.
Example: markup.highlight
Default: ""
@param {bool} copy
Display a copy button.
Default: true
@param {string} file
File name to display above the rendered code.
Default: ""
@param {bool} fm
Does Inner represent front matter?
Default: false
@param {string} Inner
Content between opening and closing shortcode tags.
Default: ""
@param {bool} skipHeader
Omit top level key(s) when rendering a section of site.Data.docs.config.
Default: false
@returns {template.HTML}
*/ -}}
{{- /* Initialize. */ -}}
{{- $config := "" -}}
{{- $copy := true -}}
{{- $file := "" -}}
{{- $fm := false -}}
{{- $skipHeader := false -}}
{{- /* Get parameters, defend against string booleans. */ -}}
{{- if .Params -}}
{{- $config = .Get "config" -}}
{{- $file = .Get "file" -}}
{{- if (isset .Params "copy") -}}
{{- if in (slice true "true") (.Get "copy") -}}
{{- $copy = true -}}
{{- else -}}
{{- $copy = false -}}
{{- end -}}
{{- end -}}
{{- if (isset .Params "fm") -}}
{{- if in (slice true "true") (.Get "fm") -}}
{{- $fm = true -}}
{{- else -}}
{{- $fm = false -}}
{{- end -}}
{{- end -}}
{{- if (isset .Params "skipHeader") -}}
{{- if in (slice true "true") (.Get "skipHeader") -}}
{{- $skipHeader = true -}}
{{- else -}}
{{- $skipHeader = false -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- /* Define constants. */ -}}
{{- $delimiters := dict "toml" "+++" "yaml" "---" -}}
{{- $langs := slice "yaml" "toml" "json" -}}
{{- $placeHolder := "#-hugo-placeholder-#" -}}
{{- /* Render. */ -}}
{{- $code := "" -}}
{{- with $config -}}
{{- $file = $file | default "config" -}}
{{- $sections := (split . ".") -}}
{{- $configSection := index $.Site.Data.docs.config $sections -}}
{{- $code = dict $sections $configSection -}}
{{- if $skipHeader -}}
{{- $code = $configSection -}}
{{- end -}}
{{- else -}}
{{- $code = $.Inner -}}
{{- end }}
<div class="code relative" {{ with $file }}id="{{ . | urlize }}"{{ end }}>
<div class="code-nav flex flex-nowrap items-stretch">
{{- with $file }}
<div class="san-serif f6 dib lh-solid pl2 pv2 mr2">
{{- . -}}{{- if not $fm -}}.{{- end -}}
</div>
{{- end -}}
{{- range $langs }}
<button data-toggle-tab="{{ . }}" class="tab-button {{ cond (eq . "yaml") "active" "" }} ba san-serif f6 dib lh-solid ph2 pv2">
{{ . }}
</button>
&nbsp;
{{- end }}
</div>
<div class="tab-content">
{{- range $langs }}
<div data-pane="{{ . }}" class="code-copy-content nt3 tab-pane {{ cond (eq . "yaml") "active" "" }}">
{{- $hCode := $code | transform.Remarshal . -}}
{{- if and $fm (in (slice "toml" "yaml") .) -}}
{{- $hCode = printf "%s\n%s\n%s" $placeHolder $hCode $placeHolder -}}
{{- end -}}
{{- $hCode = $hCode | replaceRE `\n+` "\n" }}
{{ highlight $hCode . "" | replaceRE $placeHolder (index $delimiters .) | safeHTML }}
</div>
{{- if $copy }}
<button class="needs-js copy copy-toggle bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button"></button>
{{- /* Functionality located within filesaver.js The copy here is located in the css with .copy class so it can be replaced with JS on success */ -}}
{{- end -}}
{{- end }}
</div>
</div>