Skip to content

Commit e771c88

Browse files
authored
Refactor layouts/index.redirects (#4900)
1 parent 4b3e58f commit e771c88

9 files changed

+105
-73
lines changed

layouts/index.redirects

+6-70
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,14 @@
11
# Netlify redirects. See https://www.netlify.com/docs/redirects/
2-
{{/* cSpell:ignore cond wordmark */ -}}
3-
4-
{{ range $p := .Site.Pages -}}
5-
6-
{{ range $p.Params.redirects -}}
7-
{{ $from := cond (strings.HasPrefix .from "/")
8-
.from
9-
(print $p.RelPermalink .from) -}}
10-
{{ $to := cond (strings.HasPrefix .to "/")
11-
.to
12-
(print $p.RelPermalink .to) -}}
13-
{{ $from | printf "%-35s" }} {{ $to }}
14-
{{ end -}}
15-
16-
{{ range $p.Aliases -}}
17-
{{/* Temporary workaround for semconv alias errors */ -}}
18-
{{ if strings.HasPrefix . "docs/specs/semconv/general" -}}
19-
{{ . | printf "%-35s" }} {{ $p.RelPermalink }}
20-
{{ else -}}
21-
{{ $alias := cond (strings.HasPrefix . "/")
22-
.
23-
(partial "relative-redirects-alias" (dict "alias" . "p" $p.Parent)) -}}
24-
{{ $alias | printf "%-35s" }} {{ $p.RelPermalink }}
25-
{{ end -}}
26-
{{ end -}}
27-
28-
{{ with $p.Params.redirect -}}
29-
{{ $p.RelPermalink | printf "%-35s" }} {{ . }}
30-
{{ end -}}
31-
32-
{{ end }}{{/* range $p */ -}}
33-
34-
{{ $languages := (.Site.GetPage "/docs/instrumentation").Pages -}}
35-
{{ range $languages -}}
36-
{{ $lang := .File.ContentBaseName -}}
37-
{{ if ne $lang "other" -}}
38-
/docs/{{ $lang }} /docs/instrumentation/{{ $lang }}
39-
/docs/{{ $lang }}/* /docs/instrumentation/{{ $lang }}/:splat
40-
{{ end -}}
41-
{{ end -}}
2+
{{ partial "redirects/pages.txt" . | partial "func/trim-lines.html" -}}
3+
{{ partial "redirects/languages.txt" . | partial "func/trim-lines.html" -}}
424

5+
{{/* TODO: move the following into the spec index file. */ -}}
436
/docs/reference/specification /docs/specs/otel
447
/docs/reference/specification/* /docs/specs/otel/:splat
458
/docs/specification/otel/* /docs/specs/otel/:splat
469

47-
{{ $schemaFiles := partial "schema-file-list" . -}}
48-
{{ $latestSchemaFile := index $schemaFiles 0 -}}
49-
50-
/schemas/latest /schemas/{{ $latestSchemaFile.Name }}
51-
52-
{{/*
53-
Social-media image redirects. As mentioned in
54-
https://developers.facebook.com/docs/sharing/webmasters/images, we need to
55-
preserve og:image (and other social media image) URLs forever.
56-
*/ -}}
57-
58-
{{ $og_image_current := `/img/social/logo-wordmark-001.png` -}}
59-
60-
/featured-background.jpg {{ $og_image_current }} {{- /* homepage og:image used prior to 2022/08 */}}
61-
62-
{{- define "partials/relative-redirects-alias" -}}
63-
{{ $result := "" }}
64-
{{ if strings.HasPrefix .alias "../" }}
65-
{{ $result = (partial "relative-redirects-alias"
66-
(dict
67-
"alias" (strings.TrimPrefix "../" .alias)
68-
"p" .p.Parent ))
69-
}}
70-
{{ else }}
71-
{{ $result = path.Join .p.RelPermalink .alias }}
72-
{{ end }}
73-
{{ return $result }}
74-
{{ end }}
10+
{{ partial "redirects/schemas.txt" . }}
7511

76-
{{/* Multilingual support */ -}}
12+
{{ partial "redirects/social-media.txt" . }}
7713

78-
{{ partial "redirects/sites.redirects" . | partial "func/trim-lines.html" -}}
14+
{{ partial "redirects/localization.txt" . | partial "func/trim-lines.html" }}

layouts/partials/func/trim-lines.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{{ range split . "\n" -}}
2-
{{ trim . " \t" }}
2+
{{ with trim . " \t" -}}
3+
{{ . }}
4+
{{ end -}}
35
{{ end -}}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{/* Generate rules for `aliases` page param. */ -}}
2+
{{/* cSpell:ignore cond */ -}}
3+
4+
{{ $p := . -}}
5+
{{ range $p.Aliases -}}
6+
{{/* Temporary workaround for semconv alias errors */ -}}
7+
{{ if strings.HasPrefix . "docs/specs/semconv/general" -}}
8+
{{ . | printf "%-35s" }} {{ $p.RelPermalink }}
9+
{{ else -}}
10+
{{ $alias := cond (strings.HasPrefix . "/")
11+
.
12+
(partial "relative-redirects-alias" (dict "alias" . "p" $p.Parent)) -}}
13+
{{ $alias | printf "%-35s" }} {{ $p.RelPermalink }}
14+
{{ end -}}
15+
{{ end -}}
16+
17+
{{- define "partials/relative-redirects-alias" -}}
18+
{{ $result := "" }}
19+
{{ if strings.HasPrefix .alias "../" }}
20+
{{ $result = (partial "relative-redirects-alias"
21+
(dict
22+
"alias" (strings.TrimPrefix "../" .alias)
23+
"p" .p.Parent ))
24+
}}
25+
{{ else }}
26+
{{ $result = path.Join .p.RelPermalink .alias }}
27+
{{ end }}
28+
{{ return $result }}
29+
{{ end -}}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{/* Generate rules for all languages. */ -}}
2+
3+
{{/* FIXME - redirect to the new canonical links */ -}}
4+
5+
{{ $languages := (.Site.GetPage "/docs/instrumentation").Pages -}}
6+
{{ range $languages -}}
7+
{{ $lang := .File.ContentBaseName -}}
8+
{{ if ne $lang "other" -}}
9+
/docs/{{ $lang }} /docs/instrumentation/{{ $lang }}
10+
/docs/{{ $lang }}/* /docs/instrumentation/{{ $lang }}/:splat
11+
{{ end -}}
12+
{{ end -}}

layouts/partials/redirects/sites.redirects layouts/partials/redirects/localization.txt

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
{{/* Redirect for default language when .LanguagePrefix is empty. */ -}}
1+
{{/* Generate Netlify redirect rules for non-default .Site.Sites */ -}}
2+
{{/* cSpell:ignore cond */ -}}
3+
4+
{{/*
5+
Determine what the default language code is, `en` in most cases. Redirect
6+
paths that start with the default language code by effectively stripping the
7+
language code from the start of the path. Hugo handles this for the
8+
language-code root. The rules below handle it for all other paths using a
9+
wildcard. */ -}}
210

311
{{ $defaultLang := "" -}}
412
{{ with .Site.Sites.Default -}}
@@ -11,7 +19,7 @@
1119
{{ end -}}
1220
{{ end -}}
1321

14-
{{/* Process non-default languages. */ -}}
22+
{{/* Add redirect rules for non-default languages. */ -}}
1523

1624
{{ range after 1 .Sites -}}
1725

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

31+
{{/* Enable after refactoring is complete:
32+
{{ partial "redirects/redirect.txt" $p -}}
33+
*/ -}}
34+
2335
{{ range $p.Params.redirects -}}
2436
{{ $fallbackPage := partial "i18n/fallback-page.html" $p -}}
2537
{{ if or (eq $siteLang $defaultLang) (not $fallbackPage) -}}

layouts/partials/redirects/pages.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{{/* Generate redirects for all given pages */ -}}
2+
{{/* cSpell:ignore cond */ -}}
3+
4+
{{ range $p := .Site.Pages -}}
5+
6+
{{ range $p.Params.redirects -}}
7+
{{ $from := cond (strings.HasPrefix .from "/")
8+
.from
9+
(print $p.RelPermalink .from) -}}
10+
{{ $to := cond (strings.HasPrefix .to "/")
11+
.to
12+
(print $p.RelPermalink .to) -}}
13+
{{ $from | printf "%-35s" }} {{ $to }}
14+
{{ end -}}
15+
16+
{{ partial "redirects/aliases.txt" $p -}}
17+
{{ partial "redirects/redirect.txt" $p -}}
18+
19+
{{ end -}}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{/* Generate a Netlify redirect rule for pages with a `redirect` param */ -}}
2+
3+
{{ $p := . -}}
4+
{{ with $p.Params.redirect -}}
5+
{{ $p.RelPermalink | printf "%-35s" }} {{ . }}
6+
{{ end -}}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{ $schemaFiles := partial "schema-file-list" . -}}
2+
{{ $latestSchemaFile := index $schemaFiles 0 -}}
3+
4+
/schemas/latest /schemas/{{ $latestSchemaFile.Name -}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{/*
2+
Social-media image redirects. As mentioned in
3+
https://developers.facebook.com/docs/sharing/webmasters/images, we need to
4+
preserve og:image (and other social media image) URLs forever.
5+
6+
cSpell:ignore wordmark
7+
*/ -}}
8+
9+
{{ $og_image_current := `/img/social/logo-wordmark-001.png` -}}
10+
11+
{{/* homepage og:image used prior to 2022/08 */ -}}
12+
/featured-background.jpg {{ $og_image_current -}}

0 commit comments

Comments
 (0)