| 1 |
{{/* |
| 2 |
Renders syntax-highlighted configuration data in JSON, TOML, and YAML formats. |
| 3 |
|
| 4 |
@param {string} [config] The section of hugo.Data.docs.config to render. |
| 5 |
@param {bool} [copy=false] Whether to display a copy-to-clipboard button. |
| 6 |
@param {string} [dataKey] The section of hugo.Data.docs to render. |
| 7 |
@param {string} [file] The file name to display above the rendered code. |
| 8 |
@param {bool} [fm=false] Whether to render the code as front matter. |
| 9 |
@param {bool} [skipHeader=false] Whether to omit top level key(s) when rendering a section of hugo.Data.docs.config. |
| 10 |
|
| 11 |
@example {{< code-toggle file=hugo config=build />}} |
| 12 |
|
| 13 |
@example {{< code-toggle file=content/example.md fm="true" }} |
| 14 |
title='Example' |
| 15 |
draft='false |
| 16 |
{{< /code-toggle }} |
| 17 |
*/}} |
| 18 |
|
| 19 |
{{- /* Initialize. */}} |
| 20 |
{{- $config := "" }} |
| 21 |
{{- $copy := false }} |
| 22 |
{{- $dataKey := "" }} |
| 23 |
{{- $file := "" }} |
| 24 |
{{- $fm := false }} |
| 25 |
{{- $skipHeader := false }} |
| 26 |
|
| 27 |
{{- /* Get parameters. */}} |
| 28 |
{{- $config = .Get "config" }} |
| 29 |
{{- $dataKey = .Get "dataKey" }} |
| 30 |
{{- $file = .Get "file" }} |
| 31 |
{{- if in (slice "false" false 0) (.Get "copy") }} |
| 32 |
{{- $copy = false }} |
| 33 |
{{- else if in (slice "true" true 1) (.Get "copy") }} |
| 34 |
{{- $copy = true }} |
| 35 |
{{- end }} |
| 36 |
{{- if in (slice "false" false 0) (.Get "fm") }} |
| 37 |
{{- $fm = false }} |
| 38 |
{{- else if in (slice "true" true 1) (.Get "fm") }} |
| 39 |
{{- $fm = true }} |
| 40 |
{{- end }} |
| 41 |
{{- if in (slice "false" false 0) (.Get "skipHeader") }} |
| 42 |
{{- $skipHeader = false }} |
| 43 |
{{- else if in (slice "true" true 1) (.Get "skipHeader") }} |
| 44 |
{{- $skipHeader = true }} |
| 45 |
{{- end }} |
| 46 |
|
| 47 |
{{- /* Define constants. */}} |
| 48 |
{{- $delimiters := dict "toml" "+++" "yaml" "---" }} |
| 49 |
{{- $langs := slice "yaml" "toml" "json" }} |
| 50 |
{{- $placeHolder := "#-hugo-placeholder-#" }} |
| 51 |
|
| 52 |
{{- /* Render. */}} |
| 53 |
{{- $code := "" }} |
| 54 |
{{- if $config }} |
| 55 |
{{- $file = $file | default "hugo" }} |
| 56 |
{{- $sections := (split $config ".") }} |
| 57 |
{{- $configSection := index hugo.Data.docs.config $sections }} |
| 58 |
{{- $code = dict $sections $configSection }} |
| 59 |
{{- if $skipHeader }} |
| 60 |
{{- $code = $configSection }} |
| 61 |
{{- end }} |
| 62 |
{{- else if $dataKey }} |
| 63 |
{{- $file = $file | default $dataKey }} |
| 64 |
{{- $sections := (split $dataKey ".") }} |
| 65 |
{{- $code = index hugo.Data.docs $sections }} |
| 66 |
{{- else }} |
| 67 |
{{- $code = $.Inner }} |
| 68 |
{{- end }} |
| 69 |
|
| 70 |
|
| 71 |
<div x-data class="shortcode-code not-prose relative p-0 mt-6 mb-8"> |
| 72 |
{{- if $copy }} |
| 73 |
<svg |
| 74 |
class="absolute right-4 top-12 z-30 text-blue-600 hover:text-blue-500 cursor-pointer w-6 h-6" |
| 75 |
@click="$copy($refs[$store.nav.userSettings.settings.configFileType])"> |
| 76 |
<use href="#icon--copy"></use> |
| 77 |
</svg> |
| 78 |
{{- end }} |
| 79 |
<nav class="relative flex" aria-label="Tabs"> |
| 80 |
{{- with $file }} |
| 81 |
<div |
| 82 |
class="select-none flex-none text-sm px-2 content-center border-b border-gray-300 dark:border-gray-700" |
| 83 |
aria-label="Filename"> |
| 84 |
{{ . }}{{ if not $fm }}.{{ end }} |
| 85 |
</div> |
| 86 |
{{- end }} |
| 87 |
{{- range $i, $lang := $langs }} |
| 88 |
{{- $isLast := eq (add $i 1) (len $langs) }} |
| 89 |
<button |
| 90 |
x-on:click="$store.nav.userSettings.settings.configFileType = '{{ index $langs $i }}'" |
| 91 |
aria-label="{{ printf `Toggle %s` . }}" |
| 92 |
class="px-3 py-2 font-semibold text-black dark:text-slate-200 border-l border-t {{ if $isLast }} |
| 93 |
border-r |
| 94 |
{{ end }} border-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800 dark:border-gray-700 cursor-pointer relative min-w-0 flex-1 overflow-hidden text-sm no-underline text-center focus:z-10 overflow-x-auto" |
| 95 |
:class="$store.nav.userSettings.settings.configFileType === '{{ index $langs $i }}' ? 'border-b-0 bg-light dark:bg-dark' : 'border-b'"> |
| 96 |
<span class="select-none"> |
| 97 |
{{ . }} |
| 98 |
</span> |
| 99 |
</button> |
| 100 |
{{- end }} |
| 101 |
</nav> |
| 102 |
{{- if $code }} |
| 103 |
{{- range $i, $lang := $langs }} |
| 104 |
<div |
| 105 |
class="max-h-96 overflow-y-auto border-l border-b border-r border-gray-300 dark:border-gray-700" |
| 106 |
x-ref="{{ $lang }}" |
| 107 |
x-cloak |
| 108 |
x-transition:enter.opacity.duration.300ms |
| 109 |
x-show="$store.nav.userSettings.settings.configFileType === '{{ index $langs $i }}'"> |
| 110 |
{{- $hCode := $code | transform.Remarshal . }} |
| 111 |
{{- if and $fm (in (slice "toml" "yaml") .) }} |
| 112 |
{{- $hCode = printf "%s\n%s\n%s" $placeHolder $hCode $placeHolder }} |
| 113 |
{{- end }} |
| 114 |
{{- $hCode = $hCode | replaceRE `\n+` "\n" }} |
| 115 |
{{- highlight $hCode . "" | replaceRE $placeHolder (index $delimiters .) | safeHTML }} |
| 116 |
</div> |
| 117 |
{{- end }} |
| 118 |
{{- end }} |
| 119 |
</div> |