layouts/shortcodes/githubcode.html
{{- $repository := (.Get 0) -}} {{- $path := (.Get 1) -}} {{- $codeURL := printf "https://api.github.com/repos/%s/contents/%s" $repository $path -}} {{- $dataJSON := false -}} {{- $headers := dict -}} {{- with $github_token := (os.Getenv "HUGO_GITHUB_TOKEN" | default false ) -}} {{- $headers = dict "Authorization" (printf "Bearer %s" .) -}} {{- end -}} {{- with try (resources.GetRemote $codeURL (dict "headers" $headers)) -}} {{- with .Err -}} {{- errorf "Error fetching GitHub API: %s" . -}} {{- else with .Value -}} {{- $dataJSON = . | transform.Unmarshal -}} {{- end -}} {{- else -}} {{- errorf "Unable to fetch remote resource %q" $codeURL -}} {{- end }} {{ with $dataJSON }} {{- .content | base64Decode | safeHTML -}} {{ else }} {{ $.Page.Scratch.Set "github_error" true }} {{ end }}