Files
hugo/layouts/_markup/render-codeblock.html
T
Bjørn Erik Pedersen c86d9f4aa8 Squashed 'docs/' changes from 1f8ddb8a52..e17426e2b6
e17426e2b6 content: Adjust Cloudflare hosting guide
02edafd770 content: Add build caching instructions for Cloudflare Workers
3430b3c154 content: Update version references
ed8f6930e6 content: Fix examples
3e1a3030ab content: Miscellaneous edits
77f12f75d7 Update HUGO_VERSION to 0.163.2
943417e8f2 content: Remove Hugo SFTP Upload from community tools
62d04f0440 content: Add --global to git config command in CI/CD examples
b9fd5b6cad content: Document GitInfo limitation
9d28aa9e0e content: Fix link references
66bba81f82 content: Fix link references
b08128c0aa content: Fix function reference
2da3083578 content: Miscellaneous edits
40e048f605 content: Use consistent terminology for configuration settings
1b8d67c3fe content: Add HUGO_ENVIRONMENT to the configuration introduction
a931241abe content: Remove erroneous config setting
d621e2c38b Update HUGO_VERSION to 0.163.1
a23a5a0465 content: Fix fenced code block
1122f0e8b5 content: Fix typo
076184e983 content: Miscellaneous standardization edits
b53b801061 theme: Adjust code language substitutions
6fe0cc5f77 content: Fix formatting
f6fc643110 content: Fix link
8e9cdfb9e9 content: Fix link
ae2e15a51e content: Fix links
c73fefee7a content: Fix formatting
9d0390e927 content: Fix link destinations
938a76e00f content: Update comments.md
01d66b670c content: Fix typos
d1e70b81cb content: Improve segmentation documentation
a2a2f89f7f content: Clarify applicability of format-specific imaging config keys
e6c487eb80 content: Remove outdated feature badge
39aebec026 content: Updates for v0.163.0
e9bd8eca6b Update HUGO_VERSION to 0.163.0
f088cf8d19 content: Update hosting guides
ce481a8798 content: Fix broken links
e1f09a8783 content: Remove hidden showcases
54f3a7651e content: Standardize link references
344f8d660e theme: Add nofollow to external links
a83ccec106 theme: Remove the qr transition
5e73f514b4 theme: Hide the search modal on pageswap
57f89ce22d theme: Trim https://gohugo.io from search results when running the server
f12aa1d1d0 content: Fix formatting in Defer
5ab496dd2a content: Add FastComments to the list of commenting systems
1819171668 theme: Remove some unused code
218754408b theme: Replace Turbo with Speculation Rules
bddc24beff content: Fix typos
1a3b527595 content: Fix typo
b72ccefbd9 content: Fix typo
4bfd6c937f content: Miscellaneous link edits
4b57ca8555 content: Clarify :filename slug inference in front matter config
5b1a03ff68 content: Remove outdated new-in badges
bcc6647be8 content: Fix link
263efb81ec content: Add front matter to core-methods.md
41e27e2e54 theme: Render function/method link titles as inline code
8875ef7fe6 content: Replace "scratch pad" with clearer data structure terminology
6e186e591d Update HUGO_VERSION to 0.162.1
30a90c961d content: Add missing configuration key data types
c8e5b9fd2d content: Replace h3 method sequences with description lists
8b44afbe31 content: Fix typo
04109b0921 content: Change collections.Dictionary note regarding nil map
9de560ca01 content: Remove Commento from comments integration list
d2d1506598 content: Update PostCSS examples
6c4bd3a096 content: Updates for v0.162.0
46e6e2c66d Update HUGO_VERSION to 0.162.0
6900d27eea content: Remove date from content file
2c1e04da4a theme: Darken inline code elements in content for better readability
1c5bd6a4b4 content: Clarify resources.Copy publication path
911c1c7549 content: Wrap relevant description list terms in backticks
740e887e05 misc: Remove VS Code extension suggestions

git-subtree-dir: docs
git-subtree-split: e17426e2b63dafe06354e7a204ce61506dc79dc3
2026-06-18 16:28:21 +02:00

114 lines
3.4 KiB
HTML

{{/*
Renders a highlighted code block using the given options and attributes.
In addition to the options available to the transform.Highlight function, you
may also specify the following parameters:
@param {bool} [copy=false] Whether to display a copy-to-clipboard button.
@param {string} [file] The file name to display above the rendered code.
@param {bool} [details=false] Whether to wrap the highlighted code block within a details element.
@param {bool} [open=false] Whether to initially display the content of the details element.
@param {string} [summary=Details] The content of the details summary element rendered from Markdown to HTML.
@param {bool} [trim=true] Whether to trim leading and trailing whitespace from the content of the code block.
@returns {template.HTML}
@examples
```go
fmt.Println("Hello world!")
```
```go {linenos=true file="layouts/index.html" copy=true}
fmt.Println("Hello world!")
```
*/}}
{{- $copy := false }}
{{- $file := or .Attributes.file "" }}
{{- $details := false }}
{{- $open := "" }}
{{- $summary := or .Attributes.summary "Details" | .Page.RenderString }}
{{- $trim := true }}
{{- $ext := strings.TrimPrefix "." (path.Ext $file) }}
{{- $lang := or .Type $ext "text" }}
{{- if in (slice "html" "gotmpl") $lang }}
{{- $lang = "go-html-template" }}
{{- else if in (slice "bash" "ksh" "sh" "shell" "zsh") $lang }}
{{- $lang = "text" }}
{{- else if in (slice "md" "mkd") $lang }}
{{- $lang = "text" }}
{{- else if eq $lang "tree" }}
{{- $lang = "text" }}
{{- end }}
{{- if collections.IsSet .Attributes "copy" }}
{{- if in (slice true "true" 1) .Attributes.copy }}
{{- $copy = true }}
{{- else if in (slice false "false" 0) .Attributes.copy }}
{{- $copy = false }}
{{- end }}
{{- end }}
{{- if collections.IsSet .Attributes "details" }}
{{- if in (slice true "true" 1) .Attributes.details }}
{{- $details = true }}
{{- else if in (slice false "false" 0) .Attributes.details }}
{{- $details = false }}
{{- end }}
{{- end }}
{{- if collections.IsSet .Attributes "open" }}
{{- if in (slice true "true" 1) .Attributes.open }}
{{- $open = "open" }}
{{- else if in (slice false "false" 0) .Attributes.open }}
{{- $open = "" }}
{{- end }}
{{- end }}
{{- if collections.IsSet .Attributes "trim" }}
{{- if in (slice true "true" 1) .Attributes.trim }}
{{- $trim = true }}
{{- else if in (slice false "false" 0) .Attributes.trim }}
{{- $trim = false }}
{{- end }}
{{- end }}
{{- if $details }}
<details class="cursor-pointer" {{ $open }}>
<summary>{{ $summary }}</summary>
{{- end }}
<div
x-data
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">
{{- $fileSelectClass := "select-none" }}
{{- if $copy }}
{{- $fileSelectClass = "select-text" }}
<svg
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"
@click="$copy($refs.code)">
<use href="#icon--copy"></use>
</svg>
{{- end }}
{{- with $file }}
<div
class="san-serif text-sm inline-block leading-none pl-2 py-3 bg-gray-300 dark:bg-slate-700 w-full {{ $fileSelectClass }}
">
{{ . }}
</div>
{{- end }}
<div x-ref="code">
{{- $content := .Inner }}
{{- if $trim }}
{{- $content = strings.TrimSpace .Inner }}
{{- end }}
{{- transform.Highlight $content $lang .Options }}
</div>
</div>
{{- if $details }}
</details>
{{- end }}