Debian Codegraph @

docs/layouts/_markup/render-codeblock.html

raw
1 {{/*
2 Renders a highlighted code block using the given options and attributes.
3
4 In addition to the options available to the transform.Highlight function, you
5 may also specify the following parameters:
6
7 @param {bool} [copy=false] Whether to display a copy-to-clipboard button.
8 @param {string} [file] The file name to display above the rendered code.
9 @param {bool} [details=false] Whether to wrap the highlighted code block within a details element.
10 @param {bool} [open=false] Whether to initially display the content of the details element.
11 @param {string} [summary=Details] The content of the details summary element rendered from Markdown to HTML.
12 @param {bool} [trim=true] Whether to trim leading and trailing whitespace from the content of the code block.
13
14 @returns {template.HTML}
15
16 @examples
17
18 ```go
19 fmt.Println("Hello world!")
20 ```
21
22 ```go {linenos=true file="layouts/index.html" copy=true}
23 fmt.Println("Hello world!")
24 ```
25 */}}
26
27 {{- $copy := false }}
28 {{- $file := or .Attributes.file "" }}
29 {{- $details := false }}
30 {{- $open := "" }}
31 {{- $summary := or .Attributes.summary "Details" | .Page.RenderString }}
32 {{- $trim := true }}
33 {{- $ext := strings.TrimPrefix "." (path.Ext $file) }}
34 {{- $lang := or .Type $ext "text" }}
35 {{- if in (slice "html" "gotmpl") $lang }}
36 {{- $lang = "go-html-template" }}
37 {{- else if in (slice "bash" "ksh" "sh" "shell" "zsh") $lang }}
38 {{- $lang = "text" }}
39 {{- else if in (slice "md" "mkd") $lang }}
40 {{- $lang = "text" }}
41 {{- else if eq $lang "tree" }}
42 {{- $lang = "text" }}
43 {{- end }}
44
45 {{- if collections.IsSet .Attributes "copy" }}
46 {{- if in (slice true "true" 1) .Attributes.copy }}
47 {{- $copy = true }}
48 {{- else if in (slice false "false" 0) .Attributes.copy }}
49 {{- $copy = false }}
50 {{- end }}
51 {{- end }}
52
53 {{- if collections.IsSet .Attributes "details" }}
54 {{- if in (slice true "true" 1) .Attributes.details }}
55 {{- $details = true }}
56 {{- else if in (slice false "false" 0) .Attributes.details }}
57 {{- $details = false }}
58 {{- end }}
59 {{- end }}
60
61 {{- if collections.IsSet .Attributes "open" }}
62 {{- if in (slice true "true" 1) .Attributes.open }}
63 {{- $open = "open" }}
64 {{- else if in (slice false "false" 0) .Attributes.open }}
65 {{- $open = "" }}
66 {{- end }}
67 {{- end }}
68
69 {{- if collections.IsSet .Attributes "trim" }}
70 {{- if in (slice true "true" 1) .Attributes.trim }}
71 {{- $trim = true }}
72 {{- else if in (slice false "false" 0) .Attributes.trim }}
73 {{- $trim = false }}
74 {{- end }}
75 {{- end }}
76
77 {{- if $details }}
78 <details class="cursor-pointer" {{ $open }}>
79 <summary>{{ $summary }}</summary>
80 {{- end }}
81
82 <div
83 x-data
84 class="render-hook-codeblock font-mono not-prose relative mt-6 mb-8 border border-gray-200 dark:border-gray-800 bg-light dark:bg-dark">
85 {{- $fileSelectClass := "select-none" }}
86 {{- if $copy }}
87 {{- $fileSelectClass = "select-text" }}
88 <svg
89 class="absolute right-4 top-2 z-30 text-blue-600 hover:text-blue-500 dark:text-gray-400 dark:hover:text-gray-300 cursor-pointer w-6 h-6"
90 @click="$copy($refs.code)">
91 <use href="#icon--copy"></use>
92 </svg>
93 {{- end }}
94 {{- with $file }}
95 <div
96 class="san-serif text-sm inline-block leading-none pl-2 py-3 bg-gray-300 dark:bg-slate-700 w-full {{ $fileSelectClass }}
97 ">
98 {{ . }}
99 </div>
100 {{- end }}
101
102 <div x-ref="code">
103 {{- $content := .Inner }}
104 {{- if $trim }}
105 {{- $content = strings.TrimSpace .Inner }}
106 {{- end }}
107 {{- transform.Highlight $content $lang .Options }}
108 </div>
109 </div>
110
111 {{- if $details }}
112 </details>
113 {{- end }}