mirror of
https://github.com/tiennm99/hugoDocs.git
synced 2026-10-14 04:16:13 +00:00
theme: Create code block render hook
This commit is contained in:
1 parent
4cdde66496
commit
71d8426ee6
5 files changed
+75
-9
No files matched your search
@@ -0,0 +1,65 @@
|
||||
{{/* prettier-ignore-start */}}
|
||||
{{/*
|
||||
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.
|
||||
|
||||
@returns {template.HTML}
|
||||
|
||||
@examples
|
||||
|
||||
```go
|
||||
fmt.Println("Hello world!")
|
||||
```
|
||||
|
||||
```go {linenos=true copy=true file="layouts/index.html"}
|
||||
fmt.Println("Hello world!")
|
||||
```
|
||||
*/}}
|
||||
{{/* prettier-ignore-end */}}
|
||||
|
||||
{{- $copy := false }}
|
||||
{{- $file := or .Attributes.file "" }}
|
||||
{{- $ext := strings.TrimPrefix "." (path.Ext $file) }}
|
||||
{{- $lang := or .Type $ext "text" }}
|
||||
{{- if eq $lang "html" }}
|
||||
{{- $lang = "go-html-template" }}
|
||||
{{- end }}
|
||||
|
||||
{{- with .Attributes.copy }}
|
||||
{{- if in (slice true "true" 1) . }}
|
||||
{{- $copy = true }}
|
||||
{{- else if in (slice false "false" 0) . }}
|
||||
{{- $copy = false }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
|
||||
<div
|
||||
x-data
|
||||
class="render-hook-codeblock font-mono not-prose relative mt-6 mb-8 border-1 border-gray-200 dark:border-gray-800 bg-light dark:bg-dark">
|
||||
{{- $fileSelectClass := "select-none" }}
|
||||
{{- if $copy }}
|
||||
{{- $fileSelectClass = "select-text" }}
|
||||
<svg
|
||||
class="absolute right-3 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">
|
||||
{{- transform.Highlight (strings.TrimSpace .Inner) $lang .Options }}
|
||||
</div>
|
||||
</div>
|
||||
Reference in new issue
Block a user