@unbase-app/pages 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/astro.d.mts +7 -0
- package/astro.mjs +3 -2
- package/binding.mjs +97 -7
- package/core.mjs +1148 -198
- package/package.json +2 -2
- package/pages-address.mjs +162 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unbase-app/pages",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Keeps draft pages out of a site's production build and applies the redirects a moved page leaves: an Astro integration and a Vite plugin, for Unbase.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
},
|
|
37
37
|
"./package.json": "./package.json"
|
|
38
38
|
},
|
|
39
|
-
"files": ["core.mjs", "binding.mjs", "node.mjs", "astro.mjs", "astro.d.mts", "vite.mjs", "vite.d.mts", "README.md", "LICENSE"],
|
|
39
|
+
"files": ["core.mjs", "pages-address.mjs", "binding.mjs", "node.mjs", "astro.mjs", "astro.d.mts", "vite.mjs", "vite.d.mts", "README.md", "LICENSE"],
|
|
40
40
|
"engines": {
|
|
41
41
|
"node": ">=18.17"
|
|
42
42
|
},
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// @unbase-app/pages — addresses (docs/plan-page-engine.md, PE4a)
|
|
3
|
+
//
|
|
4
|
+
// The one module for what an address is: normalising one, the slug a name
|
|
5
|
+
// makes, whether an address can be given to a page, and how a route's pattern
|
|
6
|
+
// (`/blog/[slug]`, `/blog/:slug`, `/docs/[[...path]]`, `/files/*`) matches an
|
|
7
|
+
// address. The core (core.mjs) imports it; the editor imports it from here;
|
|
8
|
+
// the Edge Functions import their copy beside their core. Exact copies in
|
|
9
|
+
// supabase/functions/_shared/pages-address.mjs, kit/vite/pages/pages-address.mjs
|
|
10
|
+
// and kit/next/pages-address.mjs; tests fail when they differ.
|
|
11
|
+
//
|
|
12
|
+
// A pattern matcher is named for the rule it keeps, because the places that
|
|
13
|
+
// match differ in one thing, a catch-all with nothing after it:
|
|
14
|
+
//
|
|
15
|
+
// routeTest a framework's routing: `[...x]` and `*` one segment or
|
|
16
|
+
// more, `[[...x]]` and `:x?` none or more (the editor
|
|
17
|
+
// opening a route; a CMS's detail page)
|
|
18
|
+
// templateMatches a draft's or a page action's template: a catch-all at
|
|
19
|
+
// the end takes none or more segments, any other dynamic
|
|
20
|
+
// segment one
|
|
21
|
+
// redirectMatches a redirect's source: `[...x]` and `:x*` any characters
|
|
22
|
+
// (none too), `[x]` and `:x` one segment
|
|
23
|
+
// templateTest the page actions' entry for a template: a catch-all or
|
|
24
|
+
// `*` one character or more, slashes included
|
|
25
|
+
//
|
|
26
|
+
// Pure: no file system, no network, no clock.
|
|
27
|
+
// ============================================================================
|
|
28
|
+
|
|
29
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
30
|
+
|
|
31
|
+
/** "about/" → "/about"; "" → "/". */
|
|
32
|
+
export function normalisePath(p) {
|
|
33
|
+
const s = `/${String(p)}`.replace(/\/{2,}/g, "/").replace(/\/+$/, "");
|
|
34
|
+
return s || "/";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** "Pricing plans" → "pricing-plans"; "Één agenda" → "een-agenda". */
|
|
38
|
+
export function slugOf(name) {
|
|
39
|
+
return String(name)
|
|
40
|
+
.normalize("NFKD")
|
|
41
|
+
.replace(/[̀-ͯ]/g, "")
|
|
42
|
+
.toLowerCase()
|
|
43
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
44
|
+
.replace(/^-+|-+$/g, "")
|
|
45
|
+
.slice(0, 80);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const SEGMENT = /^[A-Za-z0-9][A-Za-z0-9._~-]*$/;
|
|
49
|
+
|
|
50
|
+
/** Whether `p` is an address a page can be given: plain segments, no template, not too long. */
|
|
51
|
+
export function validAddress(p) {
|
|
52
|
+
const at = normalisePath(p);
|
|
53
|
+
if (at.length > 200) return false;
|
|
54
|
+
if (at === "/") return true;
|
|
55
|
+
return at
|
|
56
|
+
.slice(1)
|
|
57
|
+
.split("/")
|
|
58
|
+
.every((s) => SEGMENT.test(s) && s !== "index" && !/^(404|500)$/.test(s));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Whether an address is a pattern (a template, a route) rather than one page: it has `[`, `]`, `:` or `*`. */
|
|
62
|
+
export const isPattern = (path) => /[[\]:*]/.test(path);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A dynamic segment of a route as frameworks write it: `[id]`, `:id` and `:id?` name a value;
|
|
66
|
+
* `[...path]`, `[[...path]]` and `*` stand for any depth (`rest`). Null for a fixed segment.
|
|
67
|
+
* @param {string} segment
|
|
68
|
+
* @returns {{ name: string, rest: boolean } | null}
|
|
69
|
+
*/
|
|
70
|
+
export function dynamicSegment(segment) {
|
|
71
|
+
if (segment === "*") return { name: "", rest: true };
|
|
72
|
+
const bracket = segment.match(/^\[{1,2}(\.\.\.)?([^\]]+?)\]{1,2}$/);
|
|
73
|
+
if (bracket) return { name: /** @type {string} */ (bracket[2]), rest: !!bracket[1] };
|
|
74
|
+
const colon = segment.match(/^:([^?]+)\??$/);
|
|
75
|
+
return colon ? { name: /** @type {string} */ (colon[1]), rest: false } : null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A route's pattern as a test of an address, as frameworks route: `[id]` and `:id` one segment,
|
|
80
|
+
* `[...x]` and `*` one or more, `[[...x]]` and `:x?` none or more. A trailing slash is the same address.
|
|
81
|
+
* @param {string} pattern
|
|
82
|
+
* @returns {RegExp}
|
|
83
|
+
*/
|
|
84
|
+
export function routeTest(pattern) {
|
|
85
|
+
let re = "";
|
|
86
|
+
for (const segment of pattern.split("/").filter(Boolean)) {
|
|
87
|
+
const d = dynamicSegment(segment);
|
|
88
|
+
if (!d) {
|
|
89
|
+
re += `/${escape(segment)}`;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const optional = segment.startsWith("[[") || segment.endsWith("?");
|
|
93
|
+
if (d.rest) re += optional ? "(?:/[^/]+)*" : "(?:/[^/]+)+";
|
|
94
|
+
else re += optional ? "(?:/[^/]+)?" : "/[^/]+";
|
|
95
|
+
}
|
|
96
|
+
return new RegExp(`^${re || "/"}/?$`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* "/blog/:slug" or "/blog/[slug]" against "/blog/hello": a draft's or a page action's template. A
|
|
101
|
+
* catch-all at the end (`[...x]`, `*`) takes none or more segments.
|
|
102
|
+
* @param {string} template
|
|
103
|
+
* @param {string} path
|
|
104
|
+
*/
|
|
105
|
+
export function templateMatches(template, path) {
|
|
106
|
+
const t = template.split("/");
|
|
107
|
+
const p = path.split("/");
|
|
108
|
+
if (t.at(-1)?.startsWith("[...") || t.at(-1) === "*") return p.length >= t.length - 1 && t.slice(0, -1).every((s, i) => s === p[i] || /^(\[|:)/.test(s));
|
|
109
|
+
return t.length === p.length && t.every((s, i) => s === p[i] || /^(\[|:)/.test(s));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Whether a redirect's source pattern takes `path`: `[x]` and `:x` one segment, `[...x]` and `:x*`
|
|
114
|
+
* any characters. False for a source with no pattern in it.
|
|
115
|
+
* @param {string} source
|
|
116
|
+
* @param {string} path
|
|
117
|
+
*/
|
|
118
|
+
export function redirectMatches(source, path) {
|
|
119
|
+
if (!/\[|:/.test(source)) return false;
|
|
120
|
+
const re = new RegExp(`^${source.split(/(\[\.\.\.[^\]]+\]|\[[^\]]+\]|:[A-Za-z_]\w*\*?)/).map((part, i) => (i % 2 ? (part.startsWith("[...") || part.endsWith("*") ? ".*" : "[^/]+") : escape(part))).join("")}$`);
|
|
121
|
+
return re.test(path);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The addresses a template's pattern makes, as the page actions find a template's entry:
|
|
126
|
+
* "/nieuws/[slug]" or "/nieuws/:slug" any one segment, "[...rest]", "[[...rest]]" or "*" one
|
|
127
|
+
* character or more.
|
|
128
|
+
* @param {string} pattern
|
|
129
|
+
* @returns {RegExp}
|
|
130
|
+
*/
|
|
131
|
+
export function templateTest(pattern) {
|
|
132
|
+
const segment = (s) => (/^\[\[?\.\.\./.test(s) || s === "*" ? ".+" : /^\[.+\]$/.test(s) || s.startsWith(":") ? "[^/]+" : escape(s));
|
|
133
|
+
return new RegExp(`^${pattern.split("/").map(segment).join("/")}$`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The page a file is by file routing (an Astro or pages-router page in `pages/`, a Next.js app
|
|
138
|
+
* router `page.*`), without knowing the site's framework; null for any other file or a route with a
|
|
139
|
+
* parameter. Where the framework is known, the core's discovery says it better: a React Router
|
|
140
|
+
* page's address is its route's, never its file name's.
|
|
141
|
+
* @param {string} file
|
|
142
|
+
* @returns {string | null}
|
|
143
|
+
*/
|
|
144
|
+
export function fileRouteOf(file) {
|
|
145
|
+
let m = /^src\/pages\/(.*)\.(astro|mdx?|html)$/.exec(file);
|
|
146
|
+
if (!m) m = /^(?:src\/)?pages\/(?!api\/|_)(.*)\.(tsx|jsx|ts|js)$/.exec(file);
|
|
147
|
+
if (m) {
|
|
148
|
+
const route = "/" + /** @type {string} */ (m[1]).replace(/(^|\/)index$/, "");
|
|
149
|
+
return route.includes("[") ? null : route.replace(/\/$/, "") || "/";
|
|
150
|
+
}
|
|
151
|
+
const a = /^(?:src\/)?app\/(.*?)\/?page\.(tsx|jsx|ts|js|mdx)$/.exec(file);
|
|
152
|
+
if (a) {
|
|
153
|
+
const route =
|
|
154
|
+
"/" +
|
|
155
|
+
/** @type {string} */ (a[1])
|
|
156
|
+
.split("/")
|
|
157
|
+
.filter((s) => s && !/^\(.*\)$/.test(s) && !s.startsWith("@"))
|
|
158
|
+
.join("/");
|
|
159
|
+
return route.includes("[") ? null : route;
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|