Merge pull request #51927 from Eundms/code-sample-fallback-missing-file

Add fallback to default language for code_sample shortcode
pull/51968/head^2
Kubernetes Prow Robot 2025-08-19 11:55:27 -07:00 committed by GitHub
commit 1ad50af3c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 7 deletions

View File

@ -3,20 +3,26 @@
{{ $codelang := .Get "language" | default (path.Ext $file | strings.TrimPrefix ".") }}
{{ $fileDir := path.Split $file }}
{{ $bundlePath := path.Join .Page.File.Dir $fileDir.Dir }}
{{ $filename := printf "/content/%s/examples/%s" .Page.Lang $file | safeURL }}
{{ $ghlink := printf "https://%s/%s%s" site.Params.githubwebsiteraw (default "main" site.Params.docsbranch) $filename | safeURL }}
{{ $.Scratch.Set "filename" (printf "/content/%s/examples/%s" .Page.Lang $file) }}
{{/* First assume this is a bundle and the file is inside it. */}}
{{ $resource := $p.Resources.GetMatch (printf "%s*" $file ) }}
{{ with $resource }}
{{ with $p.Resources.GetMatch (printf "%s*" $file) }}
{{ $.Scratch.Set "content" .Content }}
{{ else }}
{{ end }}
{{/* Read the file relative to the content root. */}}
{{ $resource := readFile $filename}}
{{ with $resource }}{{ $.Scratch.Set "content" . }}{{ end }}
{{ with readFile ($.Scratch.Get "filename")}}
{{ $.Scratch.Set "content" . }}
{{ end }}
{{/* If not found, try the default language */}}
{{ $defaultLang := (index (sort site.Languages "Weight") 0).Lang }}
{{ with readFile (printf "/content/%s/examples/%s" $defaultLang $file) }}
{{ $.Scratch.Set "content" . }}
{{ $.Scratch.Set "filename" (printf "/content/%s/examples/%s" $defaultLang $file) }}
{{ end }}
{{ if not ($.Scratch.Get "content") }}
{{ errorf "[%s] %q not found in %q" site.Language.Lang $fileDir.File $bundlePath }}
{{ end }}
{{ $filename := printf ($.Scratch.Get "filename") | safeURL }}
{{ $ghlink := printf "https://%s/%s%s" site.Params.githubwebsiteraw (default "main" site.Params.docsbranch) $filename | safeURL }}
{{ with $.Scratch.Get "content" }}
<div class="highlight code-sample">
<div class="copy-code-icon">