Improve consistency of handling shrotcode attributes

This commit is contained in:
Alex Shpak
2025-10-12 13:27:05 +02:00
parent 6c9e2a1918
commit 836a8a82d6
15 changed files with 140 additions and 34 deletions
+1
View File
@@ -0,0 +1 @@
{{- range $key, $value := . -}} {{- $key | htmlEscape -}}="{{- $value | safeHTMLAttr -}}" {{- end -}}
+36
View File
@@ -0,0 +1,36 @@
{{- /*
This is generic function the helps modifying and merging dictionaries, usually used for html attributes
Logic is executed in order defined below.
partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"keep" (slice "id" "class" "tabindex" "alt" "title" "loading")
"set" (dict "href" "https://")
"merge" (dict "class" "a-class")
"delimiter" " " // Used when merging values
"delete" (slice "src")
)
*/ -}}
{{- $store := newScratch -}}
{{- $allowlist := or (.keep) (slice "id" "class" "tabindex" "dir")}}
{{- range $key, $value := .attributes -}}
{{- if in $allowlist $key -}}
{{- $store.Set $key $value -}}
{{- end -}}
{{- end -}}
{{- range $key, $value := .set -}}
{{- $store.Set $key (print $value) -}}
{{- end -}}
{{- range $key, $value := .merge -}}
{{- $merged := (slice) | append $value | append (or (index $.attributes $key) (slice)) -}}
{{- $delimiter := or ($.delimiter) (" ") }}
{{- $store.Set $key (delimit $merged $delimiter) -}}
{{- end -}}
{{- range $key := .delete -}}
{{- $store.Delete $key -}}
{{- end -}}
{{- return $store.Values -}}
+15 -7
View File
@@ -7,10 +7,18 @@
<script defer src="{{ "asciinema/asciinema-auto.js" | relURL }}"></script>
{{- .Page.Store.Set "asciinema" true -}}
{{- end -}}
{{- with (partial "docs/links/portable-image" (dict "Page" .Page "Destination" (index .Params "cast"))) -}}
{{- $opts := newScratch -}}
{{- $opts.Set "asciinema" $.Params -}}
{{- $opts.DeleteInMap "asciinema" "cast" -}}
<div class="asciinema book-asciinema" id="asciinema-{{ $.Ordinal }}" x-data-cast="{{ . | safeHTMLAttr }}" x-data-opts="{{ $opts.Get "asciinema" | jsonify | safeHTMLAttr }}"></div>
{{- end -}}
{{- $href := (partial "docs/links/portable-image" (dict "Page" .Page "Destination" (.Get "cast"))) -}}
{{- .Store.Set "asciinema" .Params -}}
{{- .Store.DeleteInMap "asciinema" "cast" -}}
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (dict)
"set" (dict
"id" (printf "asciinema-%d" .Ordinal)
"x-data-cast" $href
"x-data-opts" (.Store.Get "asciinema" | jsonify)
)
"merge" (dict
"class" (slice "asciinema" "book-asciinema")
)
) -}}
<div {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}></div>
+10 -4
View File
@@ -1,6 +1,12 @@
{{- $style := default "default" (.Get "style") -}}
<span class="book-badge {{ $style }}">
{{- with .Get "title" -}}<span class="book-badge-title">{{ . | plainify }}</span>{{- end -}}
{{- with .Get "value" -}}<span class="book-badge-value">{{ . | plainify }}</span>{{- end -}}
{{- $style := or (.Get "style") (.Get 0) "default" -}}
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"merge" (dict
"class" (slice "book-badge" $style)
)
) -}}
<span {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>
{{- with or (.Get "title") (.Get 1) -}}<span class="book-badge-title">{{ . | plainify }}</span>{{- end -}}
{{- with or (.Get "value") (.Get 2) -}}<span class="book-badge-value">{{ . | plainify }}</span>{{- end -}}
</span>
{{- /* remove whitespaces */ -}}
+13 -1
View File
@@ -1,6 +1,18 @@
{{- $href := partial "docs/links/portable-link" (dict "Page" .Page "Destination" (.Get "href")) -}}
{{- $isRemote := (urls.Parse $href).IsAbs -}}
<a {{ with $href }}href="{{ $href }}"{{ end }} {{ with $isRemote }}target="_blank" rel="noopener"{{ end }} class="book-btn{{ with .Get "class" }} {{ . }}{{ end }}">
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"set" (dict
"href" $href
"target" "_blank"
"rel" "noopener"
)
"merge" (dict
"class" "book-btn"
)
"delete" (cond $isRemote (slice) (slice "target" "rel"))
) -}}
<a {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>
{{- .InnerDeindent -}}
</a>
{{- /* remove whitespaces */ -}}
+14 -2
View File
@@ -1,7 +1,13 @@
{{- $ratio := (split (.Get "ratio") ":") -}}
{{- if strings.Contains .InnerDeindent "<--->" -}}
{{- warnf "Columns shortcode seprator '<--->' is deprecated, use markdown list instead." -}}
<div class="book-columns flex flex-wrap{{ with .Get "class" }} {{ . }}{{ end }}">
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"merge" (dict
"class" (slice "book-columns" "flex" "flex-wrap")
)
) -}}
<div {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>
{{- range $index, $content := split .InnerDeindent "<--->" -}}
{{- $grow := default 1 (index $ratio $index) -}}
<div class="flex-even markdown-inner" style="flex-grow: {{ $grow }};">
@@ -15,7 +21,13 @@
flex-grow: {{ default 1 $grow }};
}
{{- end -}}</style>{{- end -}}
<div class="book-columns book-columns-{{ .Ordinal }}{{ with .Get "class" }} {{ . }}{{ end }}">
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"merge" (dict
"class" (slice "book-columns")
)
) -}}
<div {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>
{{ .Inner | safeHTML -}}
</div>
{{- end -}}
+7 -2
View File
@@ -1,5 +1,10 @@
{{- /* remove whitespaces */ -}}
<details {{ if or (.Get "open") (in .Params "open") }}open{{ end }}>
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"merge" (dict
"open" (cond .IsNamedParams (.Get "open") (in .Params "open"))
)
) -}}
<details {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>
{{- $summary := cond .IsNamedParams (.Get "title") (.Get 0) -}}
<summary>{{ $summary | .Page.RenderString }}</summary>
{{- /* remove whitespaces */ -}}
+8 -2
View File
@@ -1,5 +1,11 @@
{{- /* remove whitespaces */ -}}
<blockquote class="book-hint {{ default "default" (.Get 0) }}">
{{- warnf "Hint shortcode is deprecated, use markdown alerts format: '> [!NOTE] ...'" -}}
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"merge" (dict
"class" (slice "book-hint" (or (.Get 0) "default"))
)
) -}}
<blockquote {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>
{{ .Inner | safeHTML -}}
</blockquote>
{{- /* remove whitespaces */ -}}
+5 -6
View File
@@ -1,11 +1,10 @@
{{- $destination := partial "docs/links/portable-image" (dict "Page" .Page "Destination" (.Get "src")) -}}
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"keep" (slice "id" "class" "tabindex" "alt" "title" "loading")
) -}}
<label class="book-image" for="book-image-toggle-{{ .Ordinal }}">
<input class="hidden toggle" type="checkbox" id="book-image-toggle-{{ .Ordinal }}" />
<img src="{{ $destination | safeURL }}"
{{- with .Get "alt"}} alt="{{ . }}"{{ end -}}
{{- with .Get "title"}} title="{{ . }}"{{ end -}}
{{- with .Get "loading"}} loading="{{ . }}"{{ end -}}
{{- with .Get "class"}} class="{{ . }}"{{ end -}}
/>
<img src="{{ $destination | safeURL }}" {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }} />
</label>
{{- /* remove whitespaces */ -}}
+8 -2
View File
@@ -1,8 +1,14 @@
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"merge" (dict
"class" "book-katex"
)
) -}}
{{- if .Inner -}}
{{- if .Get "display" -}}
<div>{{ printf "\\[ %s \\]" .Inner }}</div>
<div {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>{{ printf "\\[ %s \\]" .Inner }}</div>
{{- else -}}
<span>{{ printf "\\( %s \\)" .Inner }}</span>
<span {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>{{ printf "\\( %s \\)" .Inner }}</span>
{{- end -}}
{{- end -}}
{{- if not (.Page.Store.Get "katex") -}}
+7 -2
View File
@@ -1,5 +1,10 @@
{{- /* remove whitespaces */ -}}
<pre class="mermaid{{ with .Get "class" }} {{ . }}{{ end }}">
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"merge" (dict
"class" "mermaid"
)
) -}}
<pre {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>
{{- .Inner | htmlEscape | safeHTML -}}
</pre>
{{- if not (.Page.Store.Get "mermaid") -}}
+7 -2
View File
@@ -1,5 +1,10 @@
{{- /* remove whitespaces */ -}}
<div class="book-steps {{ with .Get "class" }}{{ . }}{{ end }}">
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"merge" (dict
"class" "book-steps"
)
) -}}
<div {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>
{{ .Inner -}}
</div>
{{- /* remove whitespaces */ -}}
+1 -1
View File
@@ -1,4 +1,4 @@
{{- $group := printf "tabs-%v" (default .Parent.Ordinal (.Parent.Get 0)) -}}
{{- $group := printf "tabs-%v" .Parent.Ordinal -}}
{{- $tab := printf "%s-%d" $group .Ordinal -}}
<input type="radio" class="toggle" name="{{ $group }}" id="{{ $tab }}" {{ if not .Ordinal }}checked="checked"{{ end }} />
{{- /* remove whitespaces */ -}}
+7 -2
View File
@@ -1,5 +1,10 @@
{{- /* remove whitespaces */ -}}
<div class="book-tabs">
{{- $attributes := partial "docs/text/mapper" (dict
"attributes" (cond .IsNamedParams .Params dict)
"merge" (dict
"class" "book-tabs"
)
) -}}
<div {{ partial "docs/html-attrs" $attributes | safeHTMLAttr }}>
{{- .Inner -}}
</div>
{{- /* remove whitespaces */ -}}