|
| 1 | +{{/* prettier-ignore-start */}} |
| 2 | +{{/* |
| 3 | +Renders a highlighted code block using the given options and attributes. |
| 4 | + |
| 5 | +In addition to the options available to the transform.Highlight function, you |
| 6 | +may also specify the following parameters: |
| 7 | + |
| 8 | +@param {bool} [copy=false] Whether to display a copy-to-clipboard button. |
| 9 | +@param {string} [file] The file name to display above the rendered code. |
| 10 | +@param {bool} [details=false] Whether to wrap the highlighted code block within a details element. |
| 11 | +@param {bool} [open=false] Whether to initially display the content of the details element. |
| 12 | +@param {string} [summary=Details] The content of the details summary element rendered from Markdown to HTML. |
| 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 | +{{/* prettier-ignore-end */}} |
| 27 | + |
| 28 | +{{- $copy := false }} |
| 29 | +{{- $file := or .Attributes.file "" }} |
| 30 | +{{- $details := false }} |
| 31 | +{{- $open := "" }} |
| 32 | +{{- $summary := or .Attributes.summary "Details" | .Page.RenderString }} |
| 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 | +{{- end }} |
| 38 | +{{- if eq $lang "md" }} |
| 39 | + {{- $lang = "text" }} |
| 40 | +{{- end }} |
| 41 | + |
| 42 | +{{- with .Attributes.copy }} |
| 43 | + {{- if in (slice true "true" 1) . }} |
| 44 | + {{- $copy = true }} |
| 45 | + {{- else if in (slice false "false" 0) . }} |
| 46 | + {{- $copy = false }} |
| 47 | + {{- end }} |
| 48 | +{{- end }} |
| 49 | + |
| 50 | +{{- with .Attributes.details }} |
| 51 | + {{- if in (slice true "true" 1) . }} |
| 52 | + {{- $details = true }} |
| 53 | + {{- else if in (slice false "false" 0) . }} |
| 54 | + {{- $details = false }} |
| 55 | + {{- end }} |
| 56 | +{{- end }} |
| 57 | + |
| 58 | +{{- with .Attributes.open }} |
| 59 | + {{- if in (slice true "true" 1) . }} |
| 60 | + {{- $open = "open" }} |
| 61 | + {{- else if in (slice false "false" 0) . }} |
| 62 | + {{- $open = "" }} |
| 63 | + {{- end }} |
| 64 | +{{- end }} |
| 65 | + |
| 66 | +{{- if $details }} |
| 67 | + <details class="cursor-pointer" {{ $open }}> |
| 68 | + <summary>{{ $summary }}</summary> |
| 69 | +{{- end }} |
| 70 | + |
| 71 | +<div |
| 72 | + x-data |
| 73 | + 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"> |
| 74 | + {{- $fileSelectClass := "select-none" }} |
| 75 | + {{- if $copy }} |
| 76 | + {{- $fileSelectClass = "select-text" }} |
| 77 | + <svg |
| 78 | + 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" |
| 79 | + @click="$copy($refs.code)"> |
| 80 | + <use href="#icon--copy"></use> |
| 81 | + </svg> |
| 82 | + {{- end }} |
| 83 | + {{- with $file }} |
| 84 | + <div |
| 85 | + class="san-serif text-sm inline-block leading-none pl-2 py-3 bg-gray-300 dark:bg-slate-700 w-full {{ $fileSelectClass }} |
| 86 | +"> |
| 87 | + {{ . }} |
| 88 | + </div> |
| 89 | + {{- end }} |
| 90 | + |
| 91 | + <div x-ref="code"> |
| 92 | + {{- transform.Highlight (strings.TrimSpace .Inner) $lang .Options }} |
| 93 | + </div> |
| 94 | +</div> |
| 95 | + |
| 96 | +{{- if $details }} |
| 97 | + </details> |
| 98 | +{{- end }} |
0 commit comments