Update code-toggle shortcode

Simplify bool param logic
This commit is contained in:
Joe Mooring 2023-03-27 13:09:24 -07:00 committed by Joe Mooring
parent 6ddeab4f80
commit fb33bf59bd

View File

@ -1,87 +1,66 @@
{{- /* {{- /*
Renders syntax-highlighted configuration data in JSON, TOML, and YAML formats. Renders syntax-highlighted configuration data in JSON, TOML, and YAML formats.
@param {string} config @param {string} [config] The section of site.Data.docs.config to render.
Section of site.Data.docs.config to render. @param {bool} [copy=true] If true, display a copy to clipboard button.
Example: markup.highlight @param {string} [file] The file name to display above the rendered code.
Default: "" @param {bool} [fm=false] If true, render the code as front matter.
@param {bool} copy @param {bool} [skipHeader=false] If false, omit top level key(s) when rendering a section of site.Data.docs.config.
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} @returns {template.HTML}
*/ -}} */}}
{{- /* Initialize. */ -}} {{- /* Initialize. */}}
{{- $config := "" -}} {{- $config := "" }}
{{- $copy := true -}} {{- $copy := true }}
{{- $file := "" -}} {{- $file := "" }}
{{- $fm := false -}} {{- $fm := false }}
{{- $skipHeader := false -}} {{- $skipHeader := false }}
{{- /* Get parameters, defend against string booleans. */ -}} {{- /* Get parameters. */}}
{{- if .Params -}} {{- $config = .Get "config" }}
{{- $config = .Get "config" -}} {{- $file = .Get "file" }}
{{- $file = .Get "file" -}} {{- if in (slice "false" false 0) (.Get "copy") }}
{{- if (isset .Params "copy") -}} {{- $copy = false }}
{{- if in (slice true "true") (.Get "copy") -}} {{- else if in (slice "true" true 1) (.Get "copy")}}
{{- $copy = true -}} {{- $copy = true }}
{{- else -}} {{- end }}
{{- $copy = false -}} {{- if in (slice "false" false 0) (.Get "fm") }}
{{- end -}} {{- $fm = false }}
{{- end -}} {{- else if in (slice "true" true 1) (.Get "fm")}}
{{- if (isset .Params "fm") -}} {{- $fm = true }}
{{- if in (slice true "true") (.Get "fm") -}} {{- end }}
{{- $fm = true -}} {{- if in (slice "false" false 0) (.Get "skipHeader") }}
{{- else -}} {{- $skipHeader = false }}
{{- $fm = false -}} {{- else if in (slice "true" true 1) (.Get "skipHeader")}}
{{- end -}} {{- $skipHeader = true }}
{{- end -}} {{- end }}
{{- if (isset .Params "skipHeader") -}}
{{- if in (slice true "true") (.Get "skipHeader") -}}
{{- $skipHeader = true -}}
{{- else -}}
{{- $skipHeader = false -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- /* Define constants. */ -}} {{- /* Define constants. */}}
{{- $delimiters := dict "toml" "+++" "yaml" "---" -}} {{- $delimiters := dict "toml" "+++" "yaml" "---" }}
{{- $langs := slice "yaml" "toml" "json" -}} {{- $langs := slice "yaml" "toml" "json" }}
{{- $placeHolder := "#-hugo-placeholder-#" -}} {{- $placeHolder := "#-hugo-placeholder-#" }}
{{- /* Render. */ -}} {{- /* Render. */}}
{{- $code := "" -}} {{- $code := "" }}
{{- with $config -}} {{- with $config }}
{{- $file = $file | default "config" -}} {{- $file = $file | default "config" }}
{{- $sections := (split . ".") -}} {{- $sections := (split . ".") }}
{{- $configSection := index $.Site.Data.docs.config $sections -}} {{- $configSection := index $.Site.Data.docs.config $sections }}
{{- $code = dict $sections $configSection -}} {{- $code = dict $sections $configSection }}
{{- if $skipHeader -}} {{- if $skipHeader }}
{{- $code = $configSection -}} {{- $code = $configSection }}
{{- end -}} {{- end }}
{{- else -}} {{- else }}
{{- $code = $.Inner -}} {{- $code = $.Inner }}
{{- end }} {{- end }}
<div class="code relative" {{ with $file }}id="{{ . | urlize }}"{{ end }}> <div class="code relative" {{ with $file }}id="{{ . | urlize }}"{{ end }}>
<div class="code-nav flex flex-nowrap items-stretch"> <div class="code-nav flex flex-nowrap items-stretch">
{{- with $file }} {{- with $file }}
<div class="san-serif f6 dib lh-solid pl2 pv2 mr2"> <div class="san-serif f6 dib lh-solid pl2 pv2 mr2">
{{- . -}}{{- if not $fm -}}.{{- end -}} {{ . }}{{ if not $fm }}.{{ end }}
</div> </div>
{{- end -}} {{- end }}
{{- range $langs }} {{- range $langs }}
<button data-toggle-tab="{{ . }}" class="tab-button {{ cond (eq . "yaml") "active" "" }} ba san-serif f6 dib lh-solid ph2 pv2"> <button data-toggle-tab="{{ . }}" class="tab-button {{ cond (eq . "yaml") "active" "" }} ba san-serif f6 dib lh-solid ph2 pv2">
{{ . }} {{ . }}
@ -92,17 +71,17 @@ Renders syntax-highlighted configuration data in JSON, TOML, and YAML formats.
<div class="tab-content"> <div class="tab-content">
{{- range $langs }} {{- range $langs }}
<div data-pane="{{ . }}" class="code-copy-content nt3 tab-pane {{ cond (eq . "yaml") "active" "" }}"> <div data-pane="{{ . }}" class="code-copy-content nt3 tab-pane {{ cond (eq . "yaml") "active" "" }}">
{{- $hCode := $code | transform.Remarshal . -}} {{- $hCode := $code | transform.Remarshal . }}
{{- if and $fm (in (slice "toml" "yaml") .) -}} {{- if and $fm (in (slice "toml" "yaml") .) }}
{{- $hCode = printf "%s\n%s\n%s" $placeHolder $hCode $placeHolder -}} {{- $hCode = printf "%s\n%s\n%s" $placeHolder $hCode $placeHolder }}
{{- end -}} {{- end }}
{{- $hCode = $hCode | replaceRE `\n+` "\n" }} {{- $hCode = $hCode | replaceRE `\n+` "\n" }}
{{ highlight $hCode . "" | replaceRE $placeHolder (index $delimiters .) | safeHTML }} {{ highlight $hCode . "" | replaceRE $placeHolder (index $delimiters .) | safeHTML }}
</div> </div>
{{- if $copy }} {{- 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> <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 */ -}} {{- /* 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 }}
{{- end }} {{- end }}
</div> </div>
</div> </div>