Add a gh shortcode

This commit is contained in:
XhmikosR 2020-09-26 16:31:26 +03:00
parent f95004f654
commit e3624bb986

View file

@ -0,0 +1,25 @@
{{- /*
Usage: gh "foo", where foo can be:
* "pr 1" -> <a href="https://github.com/twbs/bootstrap/pull/1">#1</a>
* "issue 2" -> <a href="https://github.com/twbs/bootstrap/issues/2">#2</a>
* "@XhmikosR" -> <a href="https://github.com/XhmikosR">@XhmikosR</a>
* "a3b09530fb31cfe6bd14d49965c19e8f89c1b023" -> <a href="https://github.com/twbs/bootstrap/commit/a3b09530fb31cfe6bd14d49965c19e8f89c1b023">a3b0953</a>
* "a3b0953" -> <a href="https://github.com/twbs/bootstrap/commit/a3b09530fb31cfe6bd14d49965c19e8f89c1b023">a3b0953</a>
Quotes are not always needed, but Hugo can choke on some characters like @, in which case wrap the parameter with quotes. Also, they are needed for PR and Issue
*/ -}}
{{ range .Params -}}
{{- if hasPrefix . "@" -}}
<a href="https://github.com/{{ substr . 1 }}">{{ . }}</a>
{{- else if hasPrefix (. | lower) "pr" -}}
<a href="{{ $.Site.Params.repo }}/pull/{{ trim (substr . 2) " " }}">#{{ trim (substr . 2) " " }}</a>
{{- else if hasPrefix (. | lower) "issue" -}}
<a href="{{ $.Site.Params.repo }}/issues/{{ trim (substr . 5) " " }}">#{{ trim (substr . (len "issue")) " " }}</a>
{{- else if or (eq (len .) 7) (eq (len .) 40) -}}
<a href="{{ $.Site.Params.repo }}/commit/{{ trim . " " }}">{{ trim (substr . 0 7) " " }}</a>
{{- else -}}
{{ errorf `Wrong parameter passed in %q shortcode: %s` $.Name $.Position }}
{{- end -}}
{{- end -}}