Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor layouts/index.redirects #4900

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 6 additions & 70 deletions layouts/index.redirects
Original file line number Diff line number Diff line change
@@ -1,78 +1,14 @@
# Netlify redirects. See https://www.netlify.com/docs/redirects/
{{/* cSpell:ignore cond wordmark */ -}}

{{ range $p := .Site.Pages -}}

{{ range $p.Params.redirects -}}
{{ $from := cond (strings.HasPrefix .from "/")
.from
(print $p.RelPermalink .from) -}}
{{ $to := cond (strings.HasPrefix .to "/")
.to
(print $p.RelPermalink .to) -}}
{{ $from | printf "%-35s" }} {{ $to }}
{{ end -}}

{{ range $p.Aliases -}}
{{/* Temporary workaround for semconv alias errors */ -}}
{{ if strings.HasPrefix . "docs/specs/semconv/general" -}}
{{ . | printf "%-35s" }} {{ $p.RelPermalink }}
{{ else -}}
{{ $alias := cond (strings.HasPrefix . "/")
.
(partial "relative-redirects-alias" (dict "alias" . "p" $p.Parent)) -}}
{{ $alias | printf "%-35s" }} {{ $p.RelPermalink }}
{{ end -}}
{{ end -}}

{{ with $p.Params.redirect -}}
{{ $p.RelPermalink | printf "%-35s" }} {{ . }}
{{ end -}}

{{ end }}{{/* range $p */ -}}

{{ $languages := (.Site.GetPage "/docs/instrumentation").Pages -}}
{{ range $languages -}}
{{ $lang := .File.ContentBaseName -}}
{{ if ne $lang "other" -}}
/docs/{{ $lang }} /docs/instrumentation/{{ $lang }}
/docs/{{ $lang }}/* /docs/instrumentation/{{ $lang }}/:splat
{{ end -}}
{{ end -}}
{{ partial "redirects/pages.txt" . | partial "func/trim-lines.html" -}}
{{ partial "redirects/languages.txt" . | partial "func/trim-lines.html" -}}

{{/* TODO: move the following into the spec index file. */ -}}
/docs/reference/specification /docs/specs/otel
/docs/reference/specification/* /docs/specs/otel/:splat
/docs/specification/otel/* /docs/specs/otel/:splat

{{ $schemaFiles := partial "schema-file-list" . -}}
{{ $latestSchemaFile := index $schemaFiles 0 -}}

/schemas/latest /schemas/{{ $latestSchemaFile.Name }}

{{/*
Social-media image redirects. As mentioned in
https://developers.facebook.com/docs/sharing/webmasters/images, we need to
preserve og:image (and other social media image) URLs forever.
*/ -}}

{{ $og_image_current := `/img/social/logo-wordmark-001.png` -}}

/featured-background.jpg {{ $og_image_current }} {{- /* homepage og:image used prior to 2022/08 */}}

{{- define "partials/relative-redirects-alias" -}}
{{ $result := "" }}
{{ if strings.HasPrefix .alias "../" }}
{{ $result = (partial "relative-redirects-alias"
(dict
"alias" (strings.TrimPrefix "../" .alias)
"p" .p.Parent ))
}}
{{ else }}
{{ $result = path.Join .p.RelPermalink .alias }}
{{ end }}
{{ return $result }}
{{ end }}
{{ partial "redirects/schemas.txt" . }}

{{/* Multilingual support */ -}}
{{ partial "redirects/social-media.txt" . }}

{{ partial "redirects/sites.redirects" . | partial "func/trim-lines.html" -}}
{{ partial "redirects/localization.txt" . | partial "func/trim-lines.html" }}
4 changes: 3 additions & 1 deletion layouts/partials/func/trim-lines.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{{ range split . "\n" -}}
{{ trim . " \t" }}
{{ with trim . " \t" -}}
{{ . }}
{{ end -}}
{{ end -}}
29 changes: 29 additions & 0 deletions layouts/partials/redirects/aliases.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{/* Generate rules for `aliases` page param. */ -}}
{{/* cSpell:ignore cond */ -}}

{{ $p := . -}}
{{ range $p.Aliases -}}
{{/* Temporary workaround for semconv alias errors */ -}}
{{ if strings.HasPrefix . "docs/specs/semconv/general" -}}
{{ . | printf "%-35s" }} {{ $p.RelPermalink }}
{{ else -}}
{{ $alias := cond (strings.HasPrefix . "/")
.
(partial "relative-redirects-alias" (dict "alias" . "p" $p.Parent)) -}}
{{ $alias | printf "%-35s" }} {{ $p.RelPermalink }}
{{ end -}}
{{ end -}}

{{- define "partials/relative-redirects-alias" -}}
{{ $result := "" }}
{{ if strings.HasPrefix .alias "../" }}
{{ $result = (partial "relative-redirects-alias"
(dict
"alias" (strings.TrimPrefix "../" .alias)
"p" .p.Parent ))
}}
{{ else }}
{{ $result = path.Join .p.RelPermalink .alias }}
{{ end }}
{{ return $result }}
{{ end -}}
12 changes: 12 additions & 0 deletions layouts/partials/redirects/languages.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{/* Generate rules for all languages. */ -}}

{{/* FIXME - redirect to the new canonical links */ -}}

{{ $languages := (.Site.GetPage "/docs/instrumentation").Pages -}}
{{ range $languages -}}
{{ $lang := .File.ContentBaseName -}}
{{ if ne $lang "other" -}}
/docs/{{ $lang }} /docs/instrumentation/{{ $lang }}
/docs/{{ $lang }}/* /docs/instrumentation/{{ $lang }}/:splat
{{ end -}}
{{ end -}}
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{{/* Redirect for default language when .LanguagePrefix is empty. */ -}}
{{/* Generate Netlify redirect rules for non-default .Site.Sites */ -}}
{{/* cSpell:ignore cond */ -}}

{{/*
Determine what the default language code is, `en` in most cases. Redirect
paths that start with the default language code by effectively stripping the
language code from the start of the path. Hugo handles this for the
language-code root. The rules below handle it for all other paths using a
wildcard. */ -}}

{{ $defaultLang := "" -}}
{{ with .Site.Sites.Default -}}
Expand All @@ -11,7 +19,7 @@
{{ end -}}
{{ end -}}

{{/* Process non-default languages. */ -}}
{{/* Add redirect rules for non-default languages. */ -}}

{{ range after 1 .Sites -}}

Expand All @@ -20,6 +28,10 @@
# Site localization {{ $siteLang }}
{{ range $p := .Pages -}}

{{/* Enable after refactoring is complete:
{{ partial "redirects/redirect.txt" $p -}}
*/ -}}

{{ range $p.Params.redirects -}}
{{ $fallbackPage := partial "i18n/fallback-page.html" $p -}}
{{ if or (eq $siteLang $defaultLang) (not $fallbackPage) -}}
Expand Down
19 changes: 19 additions & 0 deletions layouts/partials/redirects/pages.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{/* Generate redirects for all given pages */ -}}
{{/* cSpell:ignore cond */ -}}

{{ range $p := .Site.Pages -}}

{{ range $p.Params.redirects -}}
{{ $from := cond (strings.HasPrefix .from "/")
.from
(print $p.RelPermalink .from) -}}
{{ $to := cond (strings.HasPrefix .to "/")
.to
(print $p.RelPermalink .to) -}}
{{ $from | printf "%-35s" }} {{ $to }}
{{ end -}}

{{ partial "redirects/aliases.txt" $p -}}
{{ partial "redirects/redirect.txt" $p -}}

{{ end -}}
6 changes: 6 additions & 0 deletions layouts/partials/redirects/redirect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{/* Generate a Netlify redirect rule for pages with a `redirect` param */ -}}

{{ $p := . -}}
{{ with $p.Params.redirect -}}
{{ $p.RelPermalink | printf "%-35s" }} {{ . }}
{{ end -}}
4 changes: 4 additions & 0 deletions layouts/partials/redirects/schemas.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ $schemaFiles := partial "schema-file-list" . -}}
{{ $latestSchemaFile := index $schemaFiles 0 -}}

/schemas/latest /schemas/{{ $latestSchemaFile.Name -}}
12 changes: 12 additions & 0 deletions layouts/partials/redirects/social-media.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{/*
Social-media image redirects. As mentioned in
https://developers.facebook.com/docs/sharing/webmasters/images, we need to
preserve og:image (and other social media image) URLs forever.

cSpell:ignore wordmark
*/ -}}

{{ $og_image_current := `/img/social/logo-wordmark-001.png` -}}

{{/* homepage og:image used prior to 2022/08 */ -}}
/featured-background.jpg {{ $og_image_current -}}