refactor: add code block render hooks for xml feed content (#651)

This commit is contained in:
Cell
2025-12-20 14:38:12 +08:00
parent f2b6218edc
commit 4d95fd8d30
7 changed files with 27 additions and 59 deletions
-3
View File
@@ -1,3 +0,0 @@
{{- $result := transform.HighlightCodeBlock . -}}
<pre><code>{{ $result.Inner }}</code></pre>
{{- /* EOF */ -}}
+11
View File
@@ -0,0 +1,11 @@
{{- if eq .Type "timeline" -}}
{{- dict
"Options" .Attributes
"Inner" .Inner
"Resources" .Page.Resources
"Position" .Position
| partial "plugin/timeline.html"
-}}
{{- else -}}
<pre><code>{{ .Inner }}</code></pre>
{{- end -}}
@@ -4,41 +4,6 @@
{{- $content = dict "Content" .Content "Ruby" .Ruby "Fraction" .Fraction "Fontawesome" .Fontawesome | partial "function/content.html" }}
{{- /* Keep only the first <pre class="mermaid">...</pre> inside a diagram-container */ -}}
{{- $content = $content | replaceRE `(?s)<div[^>]*class="diagram-container"[^>]*>.*?(<pre[^>]*class="mermaid"[^>]*>.*?</pre>).*?</div>\s*(?:<template[^>]*>.*?</template>\s*)?` "$1" }}
{{- $mermaidMatches := findRE `(?s)<pre[^>]*class="mermaid"[^>]*>.*?</pre>` $content -}}
{{- if gt (len $mermaidMatches) 0 -}}
{{- range $i, $m := $mermaidMatches -}}
{{- $attrs := $m | replaceRE `(?s)<pre([^>]*)>.*?</pre>` "$1" -}}
{{- $inner := $m | replaceRE `(?s)<pre[^>]*>(.*?)</pre>` "$1" -}}
{{- $inner = replace $inner " " "&ensp;" -}}
{{- $newPre := printf "<pre%s>%s</pre>" $attrs $inner -}}
{{- $content = replace $content $m $newPre -}}
{{- end -}}
{{- end -}}
{{- /* Remove echarts diagram containers entirely */ -}}
{{- $content = $content | replaceRE `(?s)<div[^>]*class="diagram-container"[^>]*>.*?<div[^>]*class="echarts"[^>]*>.*?</div>\s*(?:<template[^>]*>.*?</template>\s*)?.*?</div>` "" -}}
{{- /* Replace json-viewer components with JSON code blocks */ -}}
{{- $matches := findRE `(?s)<json-viewer\b[^>]*\bvalue="[^"]*"[^>]*>(?:</json-viewer>)?` $content -}}
{{- if gt (len $matches) 0 -}}
{{- range $i, $m := $matches -}}
{{- /* extract the value="...": use replaceRE on the full match to get the captured group */ -}}
{{- $rawVal := $m | replaceRE `(?s).*value="([^"]*)".*` "$1" -}}
{{- /* decode HTML entities, strip outer quotes if present, unescape JSON-style escapes */ -}}
{{- $jsonStr := $rawVal | htmlUnescape | replaceRE `^"(.*)"$` "$1" | replaceRE `\\\\` `\` | replaceRE `\\\"` `"` | replaceRE `\\n` "\n" | replaceRE `\\r` "" | replaceRE `\\t` "\t" -}}
{{- /* try to unmarshal into object */ -}}
{{- $obj := transform.Unmarshal $jsonStr | default dict -}}
{{- /* serialize back to JSON */ -}}
{{- $pretty := $obj | jsonify (dict "indent" "&ensp;&ensp;") -}}
{{- /* build replacement pre/code block */ -}}
{{- $replacement := printf "<pre><code>%s</code></pre>" $pretty -}}
{{- /* replace the full original match with the rendered block */ -}}
{{- $content = replace $content $m $replacement -}}
{{- end -}}
{{- end -}}
{{- with .FeaturedImage }}
{{- $content = add (printf "<img src=%q alt=%q referrerpolicy=%q>" . "featured image" "no-referrer") $content }}
{{- end }}
+1 -1
View File
@@ -1,4 +1,4 @@
{{- hugo.Store.Set "version" "v0.4.0-alpha.3-20251219153119-0a614e17" -}}
{{- hugo.Store.Set "version" "v0.4.0-alpha.3-20251220063813-f2b6218e" -}}
{{- .Store.Set "this" dict -}}
{{- partial "init/detection-env.html" . -}}
+2
View File
@@ -0,0 +1,2 @@
<pre><code>{{ strings.TrimSpace .Inner }}</code></pre>
{{- /* EOF */ -}}
+2
View File
@@ -0,0 +1,2 @@
<pre><code>{{ strings.TrimSpace .Inner }}</code></pre>
{{- /* EOF */ -}}