diff --git a/assets/css/_page/_single/_code.scss b/assets/css/_page/_single/_code.scss index 9a8009e2..7825b4c6 100644 --- a/assets/css/_page/_single/_code.scss +++ b/assets/css/_page/_single/_code.scss @@ -532,11 +532,6 @@ pre { padding-block: 0.875rem; } } - - // for code tabs, only show the first tab content before JS loaded - &[data-hidden] { - display: none; - } } } diff --git a/assets/js/theme.js b/assets/js/theme.js index 5312f811..ec29497c 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -601,7 +601,7 @@ class FixIt { * init code tabs */ initCodeTabs() { - const $codeBlocks = document.querySelectorAll('.code-block[group]'); + const $codeBlocks = document.querySelectorAll('.code-block[group]:not([data-tab-init])'); const processed = new Set(); Util.forEach($codeBlocks, ($block) => { @@ -613,7 +613,6 @@ class FixIt { // collect consecutive blocks with same group while ($curr && $curr.classList?.contains('code-block') && $curr.getAttribute('group') === groupName) { - delete $curr.dataset.hidden; $tabs.push($curr); processed.add($curr); $curr = $curr.nextElementSibling; @@ -701,6 +700,8 @@ class FixIt { // move block to content $tab.classList.toggle('active', activeTabIndex === index || defaultActiveTab); $tab.classList.remove('is-collapsed'); + $tab.classList.remove('d-none'); + $tab.dataset.tabInit = 'true'; $content.appendChild($tab); }); @@ -1606,6 +1607,7 @@ class FixIt { } const $codeBlocks = $codeTabs.querySelectorAll('.code-block'); $codeBlocks.forEach(($codeBlock) => { + delete $codeBlock.dataset.tabInit; $codeTabs.parentElement.insertBefore($codeBlock, $codeTabs); }); $codeTabs.parentElement.removeChild($codeTabs); diff --git a/layouts/_markup/render-codeblock-toggle.html b/layouts/_markup/render-codeblock-toggle.html index b849d7e5..bb3eb682 100644 --- a/layouts/_markup/render-codeblock-toggle.html +++ b/layouts/_markup/render-codeblock-toggle.html @@ -10,9 +10,7 @@ {{- end -}} {{- if $ok -}} - {{- $pageID := .Page.RelPermalink | default .Page.File.Path -}} - {{- $seed := printf "%s|%s|%s" $pageID .Position .Inner -}} - {{- $group := printf "toggle-%s" (md5 $seed) -}} + {{- $groupId := printf "tab-%s" (md5 (dict "Page" .Page | partial "function/id.html")) -}} {{- $activeFormat := "" -}} {{- $raw := strings.TrimSpace .Inner -}} {{- range $candidate := slice "json" "toml" "yaml" -}} @@ -24,7 +22,7 @@ {{- end -}} {{- end -}} {{- end -}} - {{- $commonAttrsStr := printf ", group=%q" $group -}} + {{- $commonAttrsStr := printf ", group=%q" $groupId -}} {{- if ne .Attributes.copyable nil -}} {{- $commonAttrsStr = printf "%s, copyable=%v" $commonAttrsStr .Attributes.copyable -}} {{- end -}} @@ -43,8 +41,8 @@ {{- if ne .Attributes.editable nil -}} {{- $commonAttrsStr = printf "%s, editable=%v" $commonAttrsStr .Attributes.editable -}} {{- end -}} - {{- if ne .Attributes.before_tabs nil -}} - {{- $commonAttrsStr = printf "%s, before_tabs=%q" $commonAttrsStr .Attributes.before_tabs -}} + {{- with .Attributes.before_tabs -}} + {{- $commonAttrsStr = printf "%s, before_tabs=%q" $commonAttrsStr . -}} {{- end -}} {{- $renderText := "" -}} {{- range $format := slice "toml" "yaml" "json" -}} @@ -56,7 +54,8 @@ {{- end -}} {{- $active := cond (eq $format $activeFormat) ", .active" "" -}} {{- $jsonViewer := cond (eq $format "json") ", enable=false" "" -}} - {{- $block := printf "```%s {name=%q%s%s%s}\n%s\n```\n\n" $format $format $active $jsonViewer $commonAttrsStr $content -}} + {{- $labelName := cond (ne $.Attributes.before_tabs nil) $format (upper $format) -}} + {{- $block := printf "```%s {name=%q%s%s%s}\n%s\n```\n\n" $format $labelName $active $jsonViewer $commonAttrsStr $content -}} {{- $renderText = add $renderText $block -}} {{- end -}} {{- $renderText | .Page.RenderString -}} diff --git a/layouts/_partials/init/index.html b/layouts/_partials/init/index.html index 73dc8141..1916e573 100644 --- a/layouts/_partials/init/index.html +++ b/layouts/_partials/init/index.html @@ -1,4 +1,4 @@ -{{- hugo.Store.Set "version" "v0.4.5-20260320034416-9e384b66" -}} +{{- hugo.Store.Set "version" "v0.4.5-20260320160308-b0955598" -}} {{- .Store.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} diff --git a/layouts/_partials/plugin/code-block-wrapper.html b/layouts/_partials/plugin/code-block-wrapper.html index e6f0e5e4..f50d2c50 100644 --- a/layouts/_partials/plugin/code-block-wrapper.html +++ b/layouts/_partials/plugin/code-block-wrapper.html @@ -71,12 +71,14 @@ {{- $isFirst := not (.Page.Store.Get $groupKey) -}} {{- if $isFirst -}} {{- .Page.Store.Set $groupKey true -}} + {{- else -}} + {{- $targetClass = add $targetClass " d-none" -}} {{- end -}} - {{- $hidden := cond $isFirst "" " data-hidden" -}} {{- $wrapper = replace $wrapper (printf `class="%s"` $class) - (printf `class="%s" data-tab-group="%s" data-tab-title="%s"%s` $class $config.group $tabTitle $hidden) -}} + (printf `class="%s" data-tab-title="%s"` $class $tabTitle) + -}} {{- end -}} {{- if ne .Options.linenostart nil -}} {{- $wrapper =