1{{- /* Last modified: 2023-06-30T12:24:14-07:00 */}}
2
3{{- /*
4Copyright 2023 Veriphor LLC
5
6Licensed under the Apache License, Version 2.0 (the "License"); you may not
7use this file except in compliance with the License. You may obtain a copy of
8the License at
9
10https://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15License for the specific language governing permissions and limitations under
16the License.
17*/}}
18
19{{- /*
20Renders an SVG image of a diagram from a textual description using the Kroki service.
21
22References:
23
24- https://kroki.io/
25- https://kroki.io/#examples
26
27@context {map} Attributes The markdown attributes from the info string.
28@context {string} Inner The content between the leading and trailing code fences, excluding the info string.
29@context {map} Options The highlighting options from the info string.
30@context {int} Ordinal The zero-based ordinal of the code block on the page.
31@context {page} Page A reference to the page containing the code block.
32@context {text.Position} Position The position of the code block within the page content.
33@context {string} Type The first word of the info string.
34
35@param {string} Attributes.type The type of diagram to render
36
37@returns {template.html}
38*/}}
39
40{{- /* Initialize. */}}
41{{- $renderHookName := "kroki" }}
42
43{{- /* Verify minimum required version. */}}
44{{- $minHugoVersion := "0.114.0" }}
45{{- if lt hugo.Version $minHugoVersion }}
46 {{- errorf "The %q code block render hook requires Hugo v%s or later." $renderHookName $minHugoVersion }}
47{{- end }}
48
49{{- /* Get context. */}}
50{{- $attrs := .Attributes }}
51{{- $inner := trim .Inner "\n\r" }}
52{{- $ordinal := .Ordinal }}
53{{- $position := .Position }}
54
55{{- /* Initialize. */}}
56{{- $apiEndpoint := "https://kroki.io/" }}
57{{- $diagramType := $attrs.type | lower }}
58
59{{- /* Validate diagram type. */}}
60{{- $supportedTypes := slice
61 "actdiag" "blockdiag" "bpmn" "bytefield" "ditaa" "d2" "dbml" "erd" "graphviz"
62 "mermaid" "nomnoml" "nwdiag" "packetdiag" "pikchr" "plantuml" "rackdiag"
63 "seqdiag" "structurizr" "svgbob" "umlet" "vega" "vegalite" "wavedrom"
64 "wireviz"
65}}
66{{- $typesDelimited := delimit $supportedTypes ", " ", and " }}
67{{- if not (in $supportedTypes $diagramType) }}
68 {{- errorf "The %q code block render hook does not support diagram type %q. Valid types are %s. See %s" $renderHookName $attrs.type $typesDelimited $position }}
69{{- end }}
70
71{{- /* Determine class attribute. */}}
72{{- $class := printf "diagram diagram-kroki diagram-kroki-%s" $diagramType }}
73{{- with $attrs.class }}
74 {{- $class = printf "%s %s" $class . }}
75{{- end }}
76
77{{- /* Determine id attribute. */}}
78{{- $id := printf "h-rh-cb-kroki-%d" $ordinal }}
79{{- with $attrs.id }}
80 {{- $id = . }}
81{{- end }}
82
83{{- /* Merge class and id attributes. */}}
84{{- $attrs = merge $attrs (dict "class" $class "id" $id "alt" "diagram") }}
85
86{{- $diagram_opts := dict "theme" $attrs.d2theme }}
87{{- if $attrs.d2sketch }}
88 {{- $diagram_opts = merge $diagram_opts (dict "sketch" "") }}
89{{- end }}
90
91{{- /* Get diagram. */}}
92{{- $body := dict "diagram_source" $inner "diagram_type" $diagramType "output_format" "SVG" "diagram_options" $diagram_opts | jsonify }}
93{{- $opts := dict "method" "post" "body" $body }}
94{{- with resources.GetRemote $apiEndpoint $opts }}
95 {{- with .Err }}
96 {{- errorf "The %q code block render hook was unable to get the remote diagram. See %s. %s" $renderHookName $position . }}
97 {{- else }}
98 {{- $attrs = merge $attrs (dict "src" .RelPermalink) }}
99 {{- end }}
100{{- else }}
101 {{- errorf "The %q code block render hook was unable to get the remote diagram. See %s" $renderHookName $position }}
102{{- end }}
103
104{{- /* Render. */}}
105<a href="{{ $attrs.src | safeURL }}">
106<img
107 {{- range $k, $v := $attrs }}
108 {{- if not (eq $k "type") }}
109 {{- if $v }}
110 {{- printf " %s=%q" $k (string $v) | safeHTMLAttr }}
111 {{- end }}
112 {{- end }}
113 {{- end -}}
114>
115</a>
116{{- /**/ -}}