mirror of
https://github.com/hugo-fixit/FixIt.git
synced 2026-08-24 07:18:57 +00:00
fix(layouts): fix toggle code block YAML conversion for identical values
- Add explicit format detection fallback when auto-detection fails - Strip YAML anchors/aliases created by Go yaml.v3 for identical values
This commit is contained in:
@@ -1,27 +1,43 @@
|
||||
{{- $raw := strings.TrimSpace .Inner -}}
|
||||
{{- $parsed := dict -}}
|
||||
{{- $ok := false -}}
|
||||
{{- with try (transform.Unmarshal .Inner) -}}
|
||||
{{- $activeFormat := "" -}}
|
||||
{{- /* Try auto-detect first, then explicit format detection */ -}}
|
||||
{{- with try (transform.Unmarshal $raw) -}}
|
||||
{{- with .Err -}}
|
||||
{{- warnf "Toggle code block: unable to parse content at %s" $.Position -}}
|
||||
{{- /* Auto-detect failed, try each format explicitly */ -}}
|
||||
{{- range $candidate := slice "json" "toml" "yaml" -}}
|
||||
{{- if not $ok -}}
|
||||
{{- with try (transform.Unmarshal (dict "format" $candidate) $raw) -}}
|
||||
{{- if not .Err -}}
|
||||
{{- $parsed = .Value -}}
|
||||
{{- $ok = true -}}
|
||||
{{- $activeFormat = $candidate -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if not $ok -}}
|
||||
{{- warnf "Toggle code block: unable to parse content at %s" $.Position -}}
|
||||
{{- end -}}
|
||||
{{- else with .Value -}}
|
||||
{{- $parsed = . -}}
|
||||
{{- $ok = true -}}
|
||||
{{- /* Detect the active format for tab highlighting */ -}}
|
||||
{{- range $candidate := slice "json" "toml" "yaml" -}}
|
||||
{{- if not $activeFormat -}}
|
||||
{{- with try (transform.Unmarshal (dict "format" $candidate) $raw) -}}
|
||||
{{- if not .Err -}}
|
||||
{{- $activeFormat = $candidate -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if $ok -}}
|
||||
{{- $groupId := printf "tab-%s" (md5 (dict "Page" .Page | partial "function/id.html")) -}}
|
||||
{{- $activeFormat := "" -}}
|
||||
{{- $raw := strings.TrimSpace .Inner -}}
|
||||
{{- range $candidate := slice "json" "toml" "yaml" -}}
|
||||
{{- if not $activeFormat -}}
|
||||
{{- with try (transform.Unmarshal (dict "format" $candidate) $raw) -}}
|
||||
{{- if not .Err -}}
|
||||
{{- $activeFormat = $candidate -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- $commonAttrsStr := printf ", group=%q" $groupId -}}
|
||||
{{- if ne .Attributes.copyable nil -}}
|
||||
{{- $commonAttrsStr = printf "%s, copyable=%v" $commonAttrsStr .Attributes.copyable -}}
|
||||
@@ -52,9 +68,15 @@
|
||||
{{- end -}}
|
||||
{{- $renderText := "" -}}
|
||||
{{- range $format := slice "toml" "yaml" "json" -}}
|
||||
{{- $content := transform.Remarshal $format $parsed -}}
|
||||
{{- $content := "" -}}
|
||||
{{- if eq $format "toml" -}}
|
||||
{{- $content = transform.Remarshal $format $parsed -}}
|
||||
{{- $content = replaceRE `(?m)^[\t ]+` "" $content -}}
|
||||
{{- else if eq $format "yaml" -}}
|
||||
{{- $content = transform.Remarshal $format $parsed -}}
|
||||
{{- /* Strip YAML anchors/aliases created for identical values (e.g. empty arrays) */ -}}
|
||||
{{- $content = replaceRE `(?m)(\w[\w\s]*:\s+)&(\w+)\n(\S+)` "$1$3" $content -}}
|
||||
{{- $content = replaceRE `(?m)^(\s*\w[\w\s]*:\s+)&\w+\s+\*\w+$` "${1}[]" $content -}}
|
||||
{{- else if eq $format "json" -}}
|
||||
{{- $content = $parsed | jsonify (dict "indent" " ") -}}
|
||||
{{- end -}}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{{- hugo.Store.Set "version" "v1.0.0-mroxqxk7" -}}
|
||||
{{- hugo.Store.Set "version" "v1.0.0-mrp7tfov" -}}
|
||||
{{- .Store.Set "this" dict -}}
|
||||
|
||||
{{- partial "init/detection-env.html" . -}}
|
||||
|
||||
Reference in New Issue
Block a user