fix: improve code block handling and compatibility for non-Chromium browsers

fixes #312
// ignore-for-release
This commit is contained in:
Cell
2025-12-19 17:33:19 +08:00
parent 4d3b556684
commit 0a614e1763
6 changed files with 44 additions and 54 deletions
+34 -13
View File
@@ -2,36 +2,42 @@
{{- $result := transform.HighlightCodeBlock . -}}
{{- $wrapper := $result.Wrapped -}}
{{- /* Add code wrapper */ -}}
{{- $wrapper = partial "function/code-wrapper.html" $wrapper -}}
{{- if $config.wrapper -}}
{{- $mode := $config.mode | default "classic" -}}
{{- $lines := cond .Inner (len (split .Inner "\n")) 0 -}}
{{- $maxShownLines := $config.maxshownlines -}}
{{- $lineNoStart := cond (ne .Options.linenostart nil) .Options.linenostart 1 -}}
{{- $lineNosDigit := strings.RuneCount (add $lines $lineNoStart) -}}
{{- $class := "highlight" -}}
{{- with $config.class -}}{{ $class = add $class " " . }}{{- end -}}
{{- $targetClass := add "code-block " $class -}}
{{- with $config.wrapperclass -}}{{ $targetClass = add $targetClass " " . }}{{- end -}}
{{- /* add code wrapper */ -}}
{{- $wrapper = partial "function/code-wrapper.html" $wrapper -}}
{{- /* hide line numbers if linenos=false */ -}}
{{- /* Hide line numbers if linenos=false */ -}}
{{- if not (strings.Contains $wrapper `class="lnt"`) | and (not (strings.Contains $wrapper `class="ln"`)) -}}
{{- $targetClass = add $targetClass " line-nos-hidden" -}}
{{- end -}}
{{- /* add code expand button */ -}}
{{- $lines := cond .Inner (len (split .Inner "\n")) 0 -}}
{{- $maxShownLines := $config.maxshownlines -}}
{{- /* Add code expand button */ -}}
{{- if (gt $lines $maxShownLines) | and (gt $lines 0) | and (gt $maxShownLines 0) -}}
{{- $wrapper = dict "Wrapper" $wrapper | partial "function/code-expand-btn.html" -}}
{{- end -}}
{{- /* render for different modes */ -}}
{{- /* Render for different modes */ -}}
{{- if eq $mode "classic" -}}
{{- $wrapper = dict "Wrapper" $wrapper "Config" $config "Type" .Type | partial "function/code-header.html" -}}
{{- else -}}
{{- $wrapper = dict "Wrapper" $wrapper "Config" $config | partial "function/code-copy-btn.html" -}}
{{- end -}}
{{- /* add data attributes and class */ -}}
{{- /*
Add data attributes and class
Directly set CSS variables to ensure compatibility with non-Chromium-based browsers
See https://caniuse.com/css3-attr for attr() support status
*/ -}}
{{- if $config.editable -}}
{{- $wrapper =
replace $wrapper
@@ -49,17 +55,32 @@
{{- if ne .Options.linenostart nil -}}
{{- $wrapper =
replace $wrapper
(printf `class="%s"` $class)
(printf `class="%s" data-linenostart="%v"` $class .Options.linenostart)
`class="code-wrapper"`
(printf `class="code-wrapper" data-line-start="%v"` .)
-}}
{{- end -}}
{{- $wrapperStyle := printf "--fi-max-shown-lines:%v;--fi-line-digit:%v;--fi-line-start:%v;" $maxShownLines $lineNosDigit $lineNoStart -}}
{{- $wrapper =
replace $wrapper
`class="code-wrapper"`
(printf `class="code-wrapper" style="%s"` $wrapperStyle)
-}}
{{- $wrapper =
replace $wrapper
`class="code-wrapper"`
(printf
`class="code-wrapper" data-max="%v" data-line-digit="%d"`
$maxShownLines
$lineNosDigit
)
-}}
{{- $wrapper =
replace $wrapper
(printf `class="%s"` $class)
(printf `class="%s" data-mode="%s" data-max="%v" data-lines="%d"` $targetClass $mode $maxShownLines $lines)
(printf `class="%s" data-mode="%s"` $targetClass $mode)
-}}
{{- /* remove redundant attributes */ -}}
{{- /* Remove redundant attributes */ -}}
{{- $configKeys := slice -}}
{{- range $key, $_ := .Page.Param "codeblock" -}}
{{- $configKeys = $configKeys | append $key -}}