Merge pull request #51927 from Eundms/code-sample-fallback-missing-file
Add fallback to default language for code_sample shortcodepull/51968/head^2
commit
1ad50af3c0
|
@ -3,20 +3,26 @@
|
||||||
{{ $codelang := .Get "language" | default (path.Ext $file | strings.TrimPrefix ".") }}
|
{{ $codelang := .Get "language" | default (path.Ext $file | strings.TrimPrefix ".") }}
|
||||||
{{ $fileDir := path.Split $file }}
|
{{ $fileDir := path.Split $file }}
|
||||||
{{ $bundlePath := path.Join .Page.File.Dir $fileDir.Dir }}
|
{{ $bundlePath := path.Join .Page.File.Dir $fileDir.Dir }}
|
||||||
{{ $filename := printf "/content/%s/examples/%s" .Page.Lang $file | safeURL }}
|
{{ $.Scratch.Set "filename" (printf "/content/%s/examples/%s" .Page.Lang $file) }}
|
||||||
{{ $ghlink := printf "https://%s/%s%s" site.Params.githubwebsiteraw (default "main" site.Params.docsbranch) $filename | safeURL }}
|
|
||||||
{{/* First assume this is a bundle and the file is inside it. */}}
|
{{/* First assume this is a bundle and the file is inside it. */}}
|
||||||
{{ $resource := $p.Resources.GetMatch (printf "%s*" $file ) }}
|
{{ with $p.Resources.GetMatch (printf "%s*" $file) }}
|
||||||
{{ with $resource }}
|
|
||||||
{{ $.Scratch.Set "content" .Content }}
|
{{ $.Scratch.Set "content" .Content }}
|
||||||
{{ else }}
|
{{ end }}
|
||||||
{{/* Read the file relative to the content root. */}}
|
{{/* Read the file relative to the content root. */}}
|
||||||
{{ $resource := readFile $filename}}
|
{{ with readFile ($.Scratch.Get "filename")}}
|
||||||
{{ with $resource }}{{ $.Scratch.Set "content" . }}{{ end }}
|
{{ $.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 }}
|
{{ end }}
|
||||||
{{ if not ($.Scratch.Get "content") }}
|
{{ if not ($.Scratch.Get "content") }}
|
||||||
{{ errorf "[%s] %q not found in %q" site.Language.Lang $fileDir.File $bundlePath }}
|
{{ errorf "[%s] %q not found in %q" site.Language.Lang $fileDir.File $bundlePath }}
|
||||||
{{ end }}
|
{{ 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" }}
|
{{ with $.Scratch.Get "content" }}
|
||||||
<div class="highlight code-sample">
|
<div class="highlight code-sample">
|
||||||
<div class="copy-code-icon">
|
<div class="copy-code-icon">
|
||||||
|
|
Loading…
Reference in New Issue