Skip to content

Commit ccea35f

Browse files
authored
Merge pull request #42242 from mengjiao-liu/add-code-shortcode
Add code shortcode to replace codenew
2 parents 06b02fb + f5388a1 commit ccea35f

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

content/en/docs/contribute/style/hugo-shortcodes/index.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,29 +273,31 @@ Renders to:
273273

274274
### Source code files
275275

276-
You can use the `{{%/* codenew */%}}` shortcode to embed the contents of file in a code block to allow users to download or copy its content to their clipboard. This shortcode is used when the contents of the sample file is generic and reusable, and you want the users to try it out themselves.
276+
`{{%/* codenew */%}}` shortcode is replaced by `{{%/* code */%}}`.
277+
278+
You can use the `{{%/* code */%}}` shortcode to embed the contents of file in a code block to allow users to download or copy its content to their clipboard. This shortcode is used when the contents of the sample file is generic and reusable, and you want the users to try it out themselves.
277279

278280
This shortcode takes in two named parameters: `language` and `file`. The mandatory parameter `file` is used to specify the path to the file being displayed. The optional parameter `language` is used to specify the programming language of the file. If the `language` parameter is not provided, the shortcode will attempt to guess the language based on the file extension.
279281

280282
For example:
281283

282284
```none
283-
{{%/* codenew language="yaml" file="application/deployment-scale.yaml" */%}}
285+
{{%/* code language="yaml" file="application/deployment-scale.yaml" */%}}
284286
```
285287

286288
The output is:
287289

288-
{{% codenew language="yaml" file="application/deployment-scale.yaml" %}}
290+
{{% code language="yaml" file="application/deployment-scale.yaml" %}}
289291

290-
When adding a new sample file, such as a YAML file, create the file in one of the `<LANG>/examples/` subdirectories where `<LANG>` is the language for the page. In the markdown of your page, use the `codenew` shortcode:
292+
When adding a new sample file, such as a YAML file, create the file in one of the `<LANG>/examples/` subdirectories where `<LANG>` is the language for the page. In the markdown of your page, use the `code` shortcode:
291293

292294
```none
293-
{{%/* codenew file="<RELATIVE-PATH>/example-yaml>" */%}}
295+
{{%/* code file="<RELATIVE-PATH>/example-yaml>" */%}}
294296
```
295297
where `<RELATIVE-PATH>` is the path to the sample file to include, relative to the `examples` directory. The following shortcode references a YAML file located at `/content/en/examples/configmap/configmaps.yaml`.
296298

297299
```none
298-
{{%/* codenew file="configmap/configmaps.yaml" */%}}
300+
{{%/* code file="configmap/configmaps.yaml" */%}}
299301
```
300302

301303
## Third party content marker

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 .HasShortcode "codenew" -}}
8+
{{- if or (.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.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)