deno.land / x / lume@v2.1.4 / core / utils / page_url.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
import { posix } from "../../deps/path.ts";
import type { Page, RawData } from "../file.ts";
/** Returns the final URL assigned to a page */export function getPageUrl( page: Page, prettyUrls: boolean, parentPath: string,): string | false { const data = page.data as RawData; let { url } = data;
if (url === false) { return false; }
if (typeof url === "function") { page.data.url = getDefaultUrl(page, parentPath, prettyUrls); url = url(page); }
if (url === false) { return false; }
if (typeof url === "string") { // Relative URL if (url.startsWith("./") || url.startsWith("../")) { return normalizeUrl(posix.join(parentPath, url)); }
if (url.startsWith("/")) { return normalizeUrl(url); }
throw new Error( `The url variable for the page ${page.sourcePath} (${url}) must start with "/", "./" or "../" `, ); }
// If the user has provided a value which hasn't yielded a string then it is an invalid url. if (url !== undefined) { throw new Error( `The url variable for the page ${page.sourcePath} is not correct. If specified, it should either be a string, or a function which returns a string. The provided url is of type: ${typeof url}.`, ); }
return getDefaultUrl(page, parentPath, prettyUrls);}
/** Returns the default URL for a page */function getDefaultUrl( page: Page, parentPath: string, prettyUrls: boolean,): string { // Calculate the URL from the path const url = posix.join(parentPath, page.data.basename);
if (page.src.asset) { return url + page.src.ext; }
// Pretty URLs affects to all pages but 404 if (prettyUrls && url !== "/404") { if (posix.basename(url) === "index") { return posix.join(posix.dirname(url), "/"); } return posix.join(url, "/"); }
return `${url}.html`;}
/** Remove the /index.html part if exist and replace spaces */function normalizeUrl(url: string): string { url = encodeURI(url);
if (url.endsWith("/index.html")) { return url.slice(0, -10); }
return url;}
lume

Version Info

Tagged at
7 months ago