deno.land / x / lume@v2.1.4 / plugins / redirects.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { merge } from "../core/utils/object.ts";import { Page } from "../core/file.ts";import { log } from "../core/utils/log.ts";
import type Site from "../core/site.ts";
export interface Options { /** The redirects output format */ output?: "html" | "json" | "netlify" | "vercel" | OutputStrategy;
/** The default status code to use */ defaultStatus?: Status;}
type Status = 301 | 302 | 307 | 308;type Redirect = [string, string, Status];type OutputStrategy = ( redirects: Redirect[], site: Site,) => Promise<void> | void;
export const defaults: Options = { output: "html", defaultStatus: 301,};
/** Predefined output strategies */const outputs: Record<string, OutputStrategy> = { html, json, netlify, vercel,};
export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return (site: Site) => { site.process("*", (pages) => { const redirects: Redirect[] = [];
pages.forEach((page) => { const { url, oldUrl } = page.data;
if (url && oldUrl) { const oldUrls = Array.isArray(oldUrl) ? oldUrl : [oldUrl];
for (const old of oldUrls) { const redirect = parseRedirection(url, old, options.defaultStatus); if (redirect) { redirects.push(redirect); } } } });
if (!redirects.length) { return; }
const outputFn = typeof options.output === "string" ? outputs[options.output] : options.output;
if (!outputFn) { log.error(`[redirects] Invalid output format: ${options.output}`); throw new Error(`Invalid output format: ${options.output}`); }
redirects.sort((a, b) => a[0].localeCompare(b[0]));
return outputFn(redirects, site); }); };}
const validStatusCodes = [301, 302, 303, 307, 308];
function parseRedirection( newUrl: string, oldUrl: string, defaultCode: Status,): [string, string, Status] | undefined { const [from, code] = oldUrl.split(/\s+/); const parsedCode = code ? parseInt(code) : defaultCode;
if (!validStatusCodes.includes(parsedCode)) { log.error( `Invalid status code for redirection from ${from} to ${newUrl} (${code}).`, ); return; }
return [from, newUrl, parsedCode as Status];}
/** HTML redirect */function html(redirects: Redirect[], site: Site): void { for (const [url, to, statusCode] of redirects) { const timeout = (statusCode === 301 || statusCode === 308) ? 0 : 1; const content = `<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <title>Redirecting…</title> <meta http-equiv="refresh" content="${timeout}; url=${to}"></head><body> <h1>Redirecting…</h1> <a href="${to}">Click here if you are not redirected.</a></body></html>`; const page = Page.create({ url, content }); site.pages.push(page); }}
/** JSON redirect (to use with redirect middleware) */function json(redirects: Redirect[], site: Site): void { const obj = Object.fromEntries( redirects.map(( [from, to, code], ) => [from, code === 301 ? to : { to, code }]), ); const page = Page.create({ url: "_redirects.json", content: JSON.stringify(obj, null, 2), }); site.pages.push(page);}
/** Netlify redirect */async function netlify(redirects: Redirect[], site: Site): Promise<void> { const content = redirects.map(([from, to, code]) => `${from} ${to} ${code}`) .join("\n"); const page = await site.getOrCreatePage("_redirects"); page.content = content;}
/** Vercel redirect */async function vercel(redirects: Redirect[], site: Site): Promise<void> { const config = { redirects: redirects.map(([source, destination, statusCode]) => ({ source, destination, statusCode, })), };
const page = await site.getOrCreatePage("vercel.json"); const content = JSON.parse(page.content as string | undefined || "{}"); Object.assign(content, config); page.content = JSON.stringify(content, null, 2);}
/** Extends Data interface */declare global { namespace Lume { export interface Data { /** * The old url(s) of a page * @see https://lume.land/plugins/redirects/ */ oldUrl?: string | string[]; } }}
lume

Version Info

Tagged at
7 months ago