feat: add code block render hook for RSS page (#651)

This commit is contained in:
Cell
2025-12-19 15:38:55 +08:00
parent c35dce56d0
commit 4d3b556684
5 changed files with 7 additions and 23 deletions
+1 -1
View File
@@ -1168,7 +1168,7 @@ twemoji = false
# set to true, images in the content will be shown as the gallery if the image has a title, e.g. ![alt](src "title")
# set to "force", images in the content will be forced to shown as the gallery regardless of the image has a title or not, e.g. ![alt](src)
# TODO change gallery engine in the future
lightgallery = true
lightgallery = false
# whether to enable the ruby extended syntax
ruby = true
# whether to enable the fraction extended syntax
+3
View File
@@ -0,0 +1,3 @@
{{- $result := transform.HighlightCodeBlock . -}}
<pre><code>{{ $result.Inner }}</code></pre>
{{- /* EOF */ -}}
+2 -2
View File
@@ -1,7 +1,7 @@
{{- /* Code header for classic mode code blocks */ -}}
{{- $config := .Config -}}
{{- $wrapper := .Wrapper -}}
{{- $type := lower .Type | default "fallback" -}}
{{- $lang := cond (transform.CanHighlight .Type) (lower .Type) "fallback" -}}
{{- $name := $config.name -}}
{{- $lineNosBtn := "" -}}
{{- $lineWrapBtn := "" -}}
@@ -33,7 +33,7 @@
{{- with $config.name -}}
{{- $titleEl = replace $titleEl `<span class="code-title">` (printf `<span class="code-title" data-name="%s">` .) -}}
{{- end -}}
{{- $codeHeader := printf `<div class="code-header language-%s">%s%s%s%s%s%s</div>` $type $titleEl $ellipsisBtn $lineNosBtn $lineWrapBtn $editBtn $copyBtn -}}
{{- $codeHeader := printf `<div class="code-header language-%s">%s%s%s%s%s%s</div>` $lang $titleEl $ellipsisBtn $lineNosBtn $lineWrapBtn $editBtn $copyBtn -}}
{{- $wrapper = replaceRE `(<div class="code-wrapper">)` (printf "%s$1" $codeHeader) $wrapper -}}
{{- return $wrapper -}}
@@ -4,25 +4,6 @@
{{- $content = dict "Content" .Content "Ruby" .Ruby "Fraction" .Fraction "Fontawesome" .Fontawesome | partial "function/content.html" }}
{{- /* Keep only the code content: remove highlight/chroma/lntable wrapper and line numbers */ -}}
{{- $content = $content | replaceRE `(?s)<div[^>]*class="highlight"[^>]*>.*?<div[^>]*class="chroma"[^>]*>.*?<table[^>]*class="lntable"[^>]*>.*?(?:<tbody[^>]*>\s*)?<tr[^>]*>\s*<td[^>]*class="lntd"[^>]*>.*?</td>\s*<td[^>]*class="lntd"[^>]*>(.*?)</td>\s*</tr>\s*(?:</tbody>\s*)?.*?</table>.*?</div>\s*</div>` "$1" -}}
{{- $codeBlocks := findRE `(?s)<code[^>]*>.*?</code>` $content -}}
{{- if gt (len $codeBlocks) 0 -}}
{{- range $i, $block := $codeBlocks -}}
{{- $newBlock := $block -}}
{{- $textMatches := findRE `>([^<]+)<` $block -}}
{{- if gt (len $textMatches) 0 -}}
{{- range $j, $m := $textMatches -}}
{{- $inner := $m | replaceRE `^>([^<]+)<$` "$1" -}}
{{- $newInner := replace $inner " " "&ensp;" -}}
{{- $newMatch := printf ">%s<" $newInner -}}
{{- $newBlock = replace $newBlock $m $newMatch -}}
{{- end -}}
{{- end -}}
{{- $content = replace $content $block $newBlock -}}
{{- end -}}
{{- end -}}
{{- /* 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 -}}
+1 -1
View File
@@ -1,4 +1,4 @@
{{- hugo.Store.Set "version" "v0.4.0-alpha.3-20251219061028-5dcb107d" -}}
{{- hugo.Store.Set "version" "v0.4.0-alpha.3-20251219073856-c35dce56" -}}
{{- .Store.Set "this" dict -}}
{{- partial "init/detection-env.html" . -}}