Skip to content

Commit 234458f

Browse files
author
Mengjiao Liu
committed
Add code_sample shortcode to replace code shortcode
1 parent a8c3b3f commit 234458f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

layouts/partials/hooks/body-end.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<!-- scripts for algolia docsearch -->
66
{{ end }}
77
{{/* copy-and-paste helper for codenew shortcode */}}
8-
{{- if or (.HasShortcode "code") (.HasShortcode "codenew") -}}
8+
{{- if or (.HasShortcode "code_sample") (.HasShortcode "code") (.HasShortcode "codenew") -}}
99
<script async src="{{ "js/sweetalert-2.1.2.min.js" | relURL }}"></script>
1010
<script type="text/javascript">
1111
function copyCode(elem){

layouts/shortcodes/code_sample.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{ $p := .Page }}
2+
{{ $file := .Get "file" }}
3+
{{ $codelang := .Get "language" | default (path.Ext $file | strings.TrimPrefix ".") }}
4+
{{ $fileDir := path.Split $file }}
5+
{{ $bundlePath := path.Join .Page.File.Dir $fileDir.Dir }}
6+
{{ $filename := printf "/content/%s/examples/%s" .Page.Lang $file | safeURL }}
7+
{{ $ghlink := printf "https://%s/%s%s" site.Params.githubwebsiteraw (default "main" site.Params.docsbranch) $filename | safeURL }}
8+
{{/* First assume this is a bundle and the file is inside it. */}}
9+
{{ $resource := $p.Resources.GetMatch (printf "%s*" $file ) }}
10+
{{ with $resource }}
11+
{{ $.Scratch.Set "content" .Content }}
12+
{{ else }}
13+
{{/* Read the file relative to the content root. */}}
14+
{{ $resource := readFile $filename}}
15+
{{ with $resource }}{{ $.Scratch.Set "content" . }}{{ end }}
16+
{{ end }}
17+
{{ if not ($.Scratch.Get "content") }}
18+
{{ errorf "[%s] %q not found in %q" site.Language.Lang $fileDir.File $bundlePath }}
19+
{{ end }}
20+
{{ with $.Scratch.Get "content" }}
21+
<div class="highlight">
22+
<div class="copy-code-icon" style="text-align:right">
23+
{{ with $ghlink }}<a href="{{ . }}" download="{{ $file }}">{{ end }}<code>{{ $file }}</code>
24+
{{ if $ghlink }}</a>{{ end }}
25+
<img src="{{ "images/copycode.svg" | relURL }}" style="max-height:24px; cursor: pointer" onclick="copyCode('{{ $file | anchorize }}')" title="Copy {{ $file }} to clipboard">
26+
</img>
27+
</div>
28+
<div class="includecode" id="{{ $file | anchorize }}">
29+
{{- highlight . $codelang "" -}}
30+
</div>
31+
</div>
32+
{{ end }}

0 commit comments

Comments
 (0)