{{- /* GitHub file content. @param {string} file Path to file on GitHub. @param {string} ref Branch, tag or commit. @param {string} lang Syntax highlighting. If not specified, the syntax is determined by the file extension. @returns {template.HTML} @examples {{< gh-file file="github/docs/README.md" lang="md" >}} */ -}} {{- if (.IsNamedParams) -}} {{ $file := (.Get "file") }} {{ $ref := ((.Get "ref") | default "main") }} {{ $lang := (.Get "lang") }} {{ $api := (site.Params.api) }} {{ $ts := (now.Unix) }} {{ $headers := (dict "User-Agent" ($api.user_agent) "Authorization" (printf "Bearer %s" (($api.github.token) | base64Decode)) ) }} {{ $data := (newScratch) }} {{ range ((split $file "/") | first 2) }}{{ ($data.Add "repo" (slice .)) }}{{ end }} {{ range ((split $file "/") | after 2) }}{{ ($data.Add "path" (slice .)) }}{{ end }} {{ $file = (getJSON ($api.github.url) "/repos/" (delimit ($data.Get "repo") "/") "/contents/" (delimit ($data.Get "path") "/") "?ref=" ($ref) "&ts=" ($ts) ($headers)) }} {{ $cp := ((printf "clipboard-%s-%s" ($.Name) (delimit (shuffle (seq 11 99)) "")) | md5) }}
{{ ($file.path) }}
    {{ with ($file.size) }}
  • {{ (div . 1000) }} {{ (i18n "sc.github.file.kB") }}
  • {{ end }}
  • {{ with ($file.download_url) }}
  • {{ end }} {{ with ($file.html_url) }}
  • {{ end }}
{{ if (eq (($lang) | len) 0) }} {{ $lang = ((replace (path.Ext (delimit ($data.Get "path") "/")) "." "") | default "plaintext") }} {{ end }} {{ (highlight ((($file.content) | base64Decode) | chomp) ($lang)) }}
{{- else -}} {{ (partial "alerts/no_params" .) }} {{- end -}}