From 4d95fd8d30c9d27ece609a8fd2d1cb8096070e1c Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Sat, 20 Dec 2025 14:38:12 +0800 Subject: [PATCH] refactor: add code block render hooks for xml feed content (#651) --- assets/css/_partials/_single/_code.scss | 31 +++++++----------- layouts/_markup/render-codeblock.rss.xml | 3 -- layouts/_markup/render-codeblock.xml | 11 +++++++ layouts/_partials/function/xml-content.html | 35 --------------------- layouts/_partials/init/index.html | 2 +- layouts/_shortcodes/echarts.xml | 2 ++ layouts/_shortcodes/mermaid.xml | 2 ++ 7 files changed, 27 insertions(+), 59 deletions(-) delete mode 100644 layouts/_markup/render-codeblock.rss.xml create mode 100644 layouts/_markup/render-codeblock.xml create mode 100644 layouts/_shortcodes/echarts.xml create mode 100644 layouts/_shortcodes/mermaid.xml diff --git a/assets/css/_partials/_single/_code.scss b/assets/css/_partials/_single/_code.scss index 07c413e8..6334b737 100644 --- a/assets/css/_partials/_single/_code.scss +++ b/assets/css/_partials/_single/_code.scss @@ -66,6 +66,7 @@ pre:not([data-processed]) { margin: 0.5rem 0; position: relative; + // lineNumbersInTable=true > .chroma { position: relative; background-color: var(#{$rootPrefix}code-block-background-color); @@ -91,24 +92,15 @@ pre:not([data-processed]) { @include border-radius(0); } - // ========== lineNumbersInTable ========== // // line numbers .lntd:first-child { position: sticky; left: 0; } - // code content - .lntd:last-child { - width: 100%; - code { - width: 100%; - } - } - // ========== lineNumbersInTable ========== // } } - // lineNumbersInTable=false + // line numbers (lineNumbersInTable=false) .ln { padding-right: 0.75rem; display: inline-block; @@ -363,18 +355,20 @@ pre:not([data-processed]) { } .lntd:last-child pre.chroma, .code-wrapper > pre.chroma { + max-width: 100%; + box-sizing: border-box; + code { text-wrap: wrap; - white-space: pre-wrap; - @include overflow-wrap(break-word); + word-break: break-all; + overflow-wrap: break-word; } } } - // line numbers (pseudo-elements) for code block with line wrapping or lineNos=false + // line numbers (pseudo-elements) for code blocks &.line-wrapping:not(.line-nos-hidden) .lntd:last-child pre.chroma, &:not(.line-nos-hidden) .code-wrapper > pre.chroma:not(:has(.ln)) { - padding-left: 0; > code > .line { display: flex; @@ -405,12 +399,9 @@ pre:not([data-processed]) { // when line numbers shown &:not(.line-nos-hidden) { - > .chroma { - .lntable { - .lntd:last-child pre.chroma { - padding-left: 0; - } - } + .lntd:last-child pre.chroma, + .code-wrapper > pre.chroma:not(:has(.ln)) { + padding-left: 0; } } diff --git a/layouts/_markup/render-codeblock.rss.xml b/layouts/_markup/render-codeblock.rss.xml deleted file mode 100644 index 79577aa2..00000000 --- a/layouts/_markup/render-codeblock.rss.xml +++ /dev/null @@ -1,3 +0,0 @@ -{{- $result := transform.HighlightCodeBlock . -}} -
{{ $result.Inner }}
-{{- /* EOF */ -}} diff --git a/layouts/_markup/render-codeblock.xml b/layouts/_markup/render-codeblock.xml new file mode 100644 index 00000000..05237a06 --- /dev/null +++ b/layouts/_markup/render-codeblock.xml @@ -0,0 +1,11 @@ +{{- if eq .Type "timeline" -}} + {{- dict + "Options" .Attributes + "Inner" .Inner + "Resources" .Page.Resources + "Position" .Position + | partial "plugin/timeline.html" + -}} +{{- else -}} +
{{ .Inner }}
+{{- end -}} diff --git a/layouts/_partials/function/xml-content.html b/layouts/_partials/function/xml-content.html index 70543fd5..c8d6f745 100644 --- a/layouts/_partials/function/xml-content.html +++ b/layouts/_partials/function/xml-content.html @@ -4,41 +4,6 @@ {{- $content = dict "Content" .Content "Ruby" .Ruby "Fraction" .Fraction "Fontawesome" .Fontawesome | partial "function/content.html" }} - {{- /* Keep only the first
...
inside a diagram-container */ -}} - {{- $content = $content | replaceRE `(?s)]*class="diagram-container"[^>]*>.*?(]*class="mermaid"[^>]*>.*?).*?\s*(?:]*>.*?\s*)?` "$1" }} - {{- $mermaidMatches := findRE `(?s)]*class="mermaid"[^>]*>.*?` $content -}} - {{- if gt (len $mermaidMatches) 0 -}} - {{- range $i, $m := $mermaidMatches -}} - {{- $attrs := $m | replaceRE `(?s)]*)>.*?` "$1" -}} - {{- $inner := $m | replaceRE `(?s)]*>(.*?)` "$1" -}} - {{- $inner = replace $inner " " " " -}} - {{- $newPre := printf "%s" $attrs $inner -}} - {{- $content = replace $content $m $newPre -}} - {{- end -}} - {{- end -}} - - {{- /* Remove echarts diagram containers entirely */ -}} - {{- $content = $content | replaceRE `(?s)]*class="diagram-container"[^>]*>.*?]*class="echarts"[^>]*>.*?\s*(?:]*>.*?\s*)?.*?` "" -}} - - {{- /* Replace json-viewer components with JSON code blocks */ -}} - {{- $matches := findRE `(?s)]*\bvalue="[^"]*"[^>]*>(?:)?` $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" "  ") -}} - {{- /* build replacement pre/code block */ -}} - {{- $replacement := printf "
%s
" $pretty -}} - {{- /* replace the full original match with the rendered block */ -}} - {{- $content = replace $content $m $replacement -}} - {{- end -}} - {{- end -}} - {{- with .FeaturedImage }} {{- $content = add (printf "%q" . "featured image" "no-referrer") $content }} {{- end }} diff --git a/layouts/_partials/init/index.html b/layouts/_partials/init/index.html index 40c00f0a..d717a3ff 100644 --- a/layouts/_partials/init/index.html +++ b/layouts/_partials/init/index.html @@ -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" . -}} diff --git a/layouts/_shortcodes/echarts.xml b/layouts/_shortcodes/echarts.xml new file mode 100644 index 00000000..6dcdebda --- /dev/null +++ b/layouts/_shortcodes/echarts.xml @@ -0,0 +1,2 @@ +
{{ strings.TrimSpace .Inner }}
+{{- /* EOF */ -}} diff --git a/layouts/_shortcodes/mermaid.xml b/layouts/_shortcodes/mermaid.xml new file mode 100644 index 00000000..6dcdebda --- /dev/null +++ b/layouts/_shortcodes/mermaid.xml @@ -0,0 +1,2 @@ +
{{ strings.TrimSpace .Inner }}
+{{- /* EOF */ -}}