diff --git a/assets/css/_partials/_single/_code.scss b/assets/css/_partials/_single/_code.scss index 71ec443c..3723390f 100644 --- a/assets/css/_partials/_single/_code.scss +++ b/assets/css/_partials/_single/_code.scss @@ -69,18 +69,19 @@ pre:not([data-processed]) { .highlight { margin: 0.5rem 0; position: relative; - counter-reset: codeNos calc(var(#{$rootPrefix}linenostart) - 1); - @include border-radius($global-border-radius); - // attr() for arbitrary properties is currently only supported in Chromium-based browsers (see https://caniuse.com/css3-attr); - // non-supporting browsers fall back to values set via initCodeCompatibility() in JavaScript. - #{$rootPrefix}max-shown-lines: attr(data-max number, 10); - #{$rootPrefix}linenostart: attr(data-linenostart number, 1); .code-wrapper { overflow: hidden; height: auto; + counter-reset: codeNos calc(var(#{$rootPrefix}line-start) - 1); + @include border-radius($global-border-radius); @include transition(height 0.5s ease-out); @include border-radius($global-border-radius); + // attr() for arbitrary properties is currently only supported in Chromium-based browsers (see https://caniuse.com/css3-attr); + // non-supporting browsers fall back to values set via Hugo templating in render-codeblock.html + #{$rootPrefix}max-shown-lines: attr(data-max number, 10); + #{$rootPrefix}line-start: attr(data-line-start number, 1); + #{$rootPrefix}line-digit: attr(data-line-digit number, 1); } > .chroma { @@ -359,7 +360,7 @@ pre:not([data-processed]) { content: counter(codeNos); text-wrap: nowrap; text-align: right; - min-width: var(#{$rootPrefix}line-nos-width, 1ch); + min-width: calc(var(#{$rootPrefix}line-digit) * 1ch); padding-inline: 0.75rem; flex-shrink: 0; color: var(#{$rootPrefix}global-font-secondary-color); diff --git a/assets/js/theme.js b/assets/js/theme.js index 25b639fd..95797d6c 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -451,23 +451,6 @@ class FixIt { }, false); } - /** - * Browsers compatible with non-Chromium engines (https://caniuse.com/css3-attr) - * @param {HTMLElement} codeBlock code block wrapper element - */ - initCodeCompatibility(codeBlock) { - if (navigator.userAgent.toLowerCase().includes('chrome')) return; - codeBlock.style.setProperty('--fi-max-shown-lines', Number(codeBlock.dataset.max)); - codeBlock.style.setProperty('--fi-linenostart', Number(codeBlock.dataset.linenostart)); - } - - initLineNosWidth(codeBlock) { - const digit = this.util.digitCount(codeBlock.dataset.lines); - if (digit > 1) { - codeBlock.style.setProperty('--fi-line-nos-width', `${digit}ch`); - } - } - /** * init code wrapper */ @@ -481,8 +464,6 @@ class FixIt { this.initCopyCode($codeBlock, $codePreEl); this.initCodeExpandBtn($codeBlock); - this.initCodeCompatibility($codeBlock); - this.initLineNosWidth($codeBlock); // classic mode code block interactions if ($codeBlock.dataset.mode === 'classic') { diff --git a/assets/js/util.js b/assets/js/util.js index 7949080e..57c9344e 100644 --- a/assets/js/util.js +++ b/assets/js/util.js @@ -133,17 +133,4 @@ export default class Util { } return false; } - - /** - * count digits of a number - * @param {Number} n integer number - * @returns {Number} digit count - */ - digitCount(n) { - const t = Number(n); - if (!Number.isInteger(t)) { - throw new Error('The parameter must be an integer number.'); - } - return Math.abs(t).toString().length; - } } diff --git a/hugo.toml b/hugo.toml index e8329253..b435b75f 100644 --- a/hugo.toml +++ b/hugo.toml @@ -289,7 +289,7 @@ privacyEnhanced = true [mediaTypes] # [mediaTypes."application/feed+json"] -# suffixes = ["json"] +# suffixes = ["json"] # ------------------------------------------------------------------------------------- # Output Format Configuration diff --git a/layouts/_markup/render-codeblock.html b/layouts/_markup/render-codeblock.html index 6615a8db..d56071f4 100644 --- a/layouts/_markup/render-codeblock.html +++ b/layouts/_markup/render-codeblock.html @@ -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 -}} diff --git a/layouts/_partials/init/index.html b/layouts/_partials/init/index.html index 02ed4bc0..5a5464a2 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-20251219073856-c35dce56" -}} +{{- hugo.Store.Set "version" "v0.4.0-alpha.3-20251219093450-0a7f8698" -}} {{- .Store.Set "this" dict -}} {{- partial "init/detection-env.html" . -}}