Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#1145 from wzshiming/doc/shortcode
Browse files Browse the repository at this point in the history
[doc] Add shortcode `code-sample`
  • Loading branch information
wzshiming authored Jun 27, 2024
2 parents d5db9e2 + 586f536 commit c969456
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions site/layouts/shortcodes/code-sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{ $page := .Page }}
{{ $file := .Get "file" }}
{{ $anchor := .Get "anchor" }}
{{ $codelang := .Get "language" | default (path.Ext $file | strings.TrimPrefix ".") }}

{{ $filename := $file }}
{{ if not (hasPrefix $filename "/") }}
{{ $filename = printf "%s/%s" $page.Path $file }}
{{ end }}
{{ $filename = safeURL $filename }}

{{ $resource := readFile $filename}}

{{ with $resource }}
{{ if eq $anchor "true" }}
<h6 id="{{ $file | anchorize }}">{{ $file }}
<a class="anchor" href="#{{ $file | anchorize }}">#</a>
</h6>
{{ end }}
<div class="highlight">
{{- highlight . $codelang "" -}}
</div>
{{ else }}
{{ errorf "%q not found" $filename }}
{{- end -}}

0 comments on commit c969456

Please sign in to comment.