Skip to content

Commit d186308

Browse files
committed
Add ghcode shortcode for rendering github content
1 parent e09d128 commit d186308

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ This theme contains the following custom shorcodes:
5252

5353
- openapi.html - Render an OpenAPI spec using ReDoc.
5454
- metrics.html - Imports data from a JSON file and presents it in table format.
55+
- ghcode.html - Rendering code from github
56+
57+
#### `ghcode` example
58+
To render code directly from github at build time, you can use the `ghcode` shortcode.
59+
60+
_Param 1_ - The url for the `raw` github file you want to render
61+
62+
_Param 2_ - Options for highlighting following Hugo's [Highlight shortcode](https://gohugo.io/content-management/syntax-highlighting/#highlight-shortcode)
63+
64+
```
65+
{{< ghcode "https://raw.githubusercontent.com/nginxinc/kubernetes-ingress/refs/heads/main/examples/custom-resources/access-control/access-control-policy-allow.yaml" "hl_lines=4" >}}
66+
```
67+
68+
5569
5670
### Includes
5771

layouts/shortcodes/ghcode.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{ $file := .Get 0 }}
2+
{{ $params := .Get 1 }}
3+
{{ with resources.GetRemote $file }}
4+
{{ with .Err }}
5+
{{ errorf "%s" . }}
6+
{{ else }}
7+
{{ $lang := path.Ext $file | strings.TrimPrefix "." }}
8+
{{ highlight .Content $lang $params }}
9+
{{ end }}
10+
{{ else }}
11+
{{ errorf "Unable to load github content from %s from %q" $file .Position}}
12+
{{ end }}

0 commit comments

Comments
 (0)