hugo/docs/layouts/shortcodes/code.html
2023-05-22 16:47:07 +02:00

40 lines
1.3 KiB
HTML

{{- /*
Renders syntax highlighted code.
@param {bool} [copy=true] If true, display a copy to clipboard button.
@param {string} [file] The file name to display above the rendered code.
@param {string} [lang] The code language of the inner content.
@returns {template.HTML}
*/}}
{{- /* Initialize. */}}
{{- $copy := true }}
{{- $file := " " }}
{{- $lang := "" }}
{{- /* Get parameters. */}}
{{- $file = .Get "file" }}
{{- $lang = or (.Get "lang") (path.Ext $file | strings.TrimPrefix ".") "text" }}
{{- if in (slice "false" false 0) (.Get "copy") }}
{{- $copy = false }}
{{- else if in (slice "true" true 1) (.Get "copy")}}
{{- $copy = true }}
{{- end }}
{{- /* Use the go-html-template Chroma lexer for HTML. */}}
{{- if eq $lang "html" }}
{{- $lang = "go-html-template" }}
{{- end }}
{{- /* Render. */}}
<div class="code relative" id="{{ $file | urlize }}">
<div class="f6 dib lh-solid pl2 pv2">{{ or $file "nbsp;" }}</div>
{{- if $copy }}
<button class="needs-js copy 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>
{{- end }}
<div class="code-copy-content nt3">
{{- highlight (trim .Inner "\n\r") $lang }}
</div>
</div>