react-cheminfo 0.4.1 → 0.6.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.
Files changed (99) hide show
  1. package/README.md +182 -34
  2. package/lib/ecosystem/core/sites.d.ts +1 -1
  3. package/lib/ecosystem/core/sites.d.ts.map +1 -1
  4. package/lib/ecosystem/core/sites.js +10 -0
  5. package/lib/ecosystem/core/sites.js.map +1 -1
  6. package/lib/ecosystem/ui/glyphs.d.ts.map +1 -1
  7. package/lib/ecosystem/ui/glyphs.js +3 -0
  8. package/lib/ecosystem/ui/glyphs.js.map +1 -1
  9. package/lib/orbital/ui/AtomicOrbitalCanvas.d.ts +5 -0
  10. package/lib/orbital/ui/AtomicOrbitalCanvas.d.ts.map +1 -1
  11. package/lib/orbital/ui/AtomicOrbitalCanvas.js +4 -3
  12. package/lib/orbital/ui/AtomicOrbitalCanvas.js.map +1 -1
  13. package/lib/orbital/ui/AtomicOrbitalViewer.d.ts +5 -0
  14. package/lib/orbital/ui/AtomicOrbitalViewer.d.ts.map +1 -1
  15. package/lib/orbital/ui/AtomicOrbitalViewer.js.map +1 -1
  16. package/lib/orbital/ui/axesGeometry.d.ts +27 -0
  17. package/lib/orbital/ui/axesGeometry.d.ts.map +1 -0
  18. package/lib/orbital/ui/axesGeometry.js +74 -0
  19. package/lib/orbital/ui/axesGeometry.js.map +1 -0
  20. package/lib/orbital/ui/camera.d.ts +7 -0
  21. package/lib/orbital/ui/camera.d.ts.map +1 -1
  22. package/lib/orbital/ui/camera.js +8 -1
  23. package/lib/orbital/ui/camera.js.map +1 -1
  24. package/lib/orbital/ui/renderAxes.d.ts +53 -0
  25. package/lib/orbital/ui/renderAxes.d.ts.map +1 -0
  26. package/lib/orbital/ui/renderAxes.js +110 -0
  27. package/lib/orbital/ui/renderAxes.js.map +1 -0
  28. package/lib/orbital/ui/viewer.d.ts +16 -0
  29. package/lib/orbital/ui/viewer.d.ts.map +1 -1
  30. package/lib/orbital/ui/viewer.js +24 -2
  31. package/lib/orbital/ui/viewer.js.map +1 -1
  32. package/lib/seo/core/documentMeta.d.ts +2 -2
  33. package/lib/seo/core/documentMeta.js +4 -3
  34. package/lib/seo/core/documentMeta.js.map +1 -1
  35. package/lib/seo/core/index.d.ts +13 -4
  36. package/lib/seo/core/index.d.ts.map +1 -1
  37. package/lib/seo/core/index.js +8 -3
  38. package/lib/seo/core/index.js.map +1 -1
  39. package/lib/seo/core/noscript.d.ts +97 -0
  40. package/lib/seo/core/noscript.d.ts.map +1 -0
  41. package/lib/seo/core/noscript.js +93 -0
  42. package/lib/seo/core/noscript.js.map +1 -0
  43. package/lib/seo/core/pageMeta.d.ts +30 -14
  44. package/lib/seo/core/pageMeta.d.ts.map +1 -1
  45. package/lib/seo/core/pageMeta.js +40 -43
  46. package/lib/seo/core/pageMeta.js.map +1 -1
  47. package/lib/seo/core/robots.d.ts +55 -0
  48. package/lib/seo/core/robots.d.ts.map +1 -0
  49. package/lib/seo/core/robots.js +70 -0
  50. package/lib/seo/core/robots.js.map +1 -0
  51. package/lib/seo/core/routes.d.ts +73 -5
  52. package/lib/seo/core/routes.d.ts.map +1 -1
  53. package/lib/seo/core/routes.js +142 -16
  54. package/lib/seo/core/routes.js.map +1 -1
  55. package/lib/seo/core/siteFiles.d.ts +39 -43
  56. package/lib/seo/core/siteFiles.d.ts.map +1 -1
  57. package/lib/seo/core/siteFiles.js +53 -69
  58. package/lib/seo/core/siteFiles.js.map +1 -1
  59. package/lib/seo/core/startDocumentMeta.d.ts +44 -0
  60. package/lib/seo/core/startDocumentMeta.d.ts.map +1 -0
  61. package/lib/seo/core/startDocumentMeta.js +47 -0
  62. package/lib/seo/core/startDocumentMeta.js.map +1 -0
  63. package/lib/seo/core/structuredData.d.ts +48 -0
  64. package/lib/seo/core/structuredData.d.ts.map +1 -0
  65. package/lib/seo/core/structuredData.js +41 -0
  66. package/lib/seo/core/structuredData.js.map +1 -0
  67. package/lib/seo/core/template.d.ts +48 -0
  68. package/lib/seo/core/template.d.ts.map +1 -0
  69. package/lib/seo/core/template.js +53 -0
  70. package/lib/seo/core/template.js.map +1 -0
  71. package/lib/seo/vite/ogCard.d.ts +9 -1
  72. package/lib/seo/vite/ogCard.d.ts.map +1 -1
  73. package/lib/seo/vite/ogCard.js +14 -4
  74. package/lib/seo/vite/ogCard.js.map +1 -1
  75. package/lib/seo/vite/prerender.d.ts +38 -7
  76. package/lib/seo/vite/prerender.d.ts.map +1 -1
  77. package/lib/seo/vite/prerender.js +68 -30
  78. package/lib/seo/vite/prerender.js.map +1 -1
  79. package/package.json +1 -1
  80. package/src/ecosystem/core/sites.ts +11 -0
  81. package/src/ecosystem/ui/glyphs.tsx +19 -0
  82. package/src/orbital/ui/AtomicOrbitalCanvas.tsx +9 -2
  83. package/src/orbital/ui/AtomicOrbitalViewer.tsx +5 -0
  84. package/src/orbital/ui/axesGeometry.ts +91 -0
  85. package/src/orbital/ui/camera.ts +9 -1
  86. package/src/orbital/ui/renderAxes.ts +190 -0
  87. package/src/orbital/ui/viewer.ts +32 -2
  88. package/src/seo/core/documentMeta.ts +5 -5
  89. package/src/seo/core/index.ts +19 -12
  90. package/src/seo/core/noscript.ts +195 -0
  91. package/src/seo/core/pageMeta.ts +54 -53
  92. package/src/seo/core/robots.ts +114 -0
  93. package/src/seo/core/routes.ts +181 -14
  94. package/src/seo/core/siteFiles.ts +58 -96
  95. package/src/seo/core/startDocumentMeta.ts +77 -0
  96. package/src/seo/core/structuredData.ts +80 -0
  97. package/src/seo/core/template.ts +54 -0
  98. package/src/seo/vite/ogCard.ts +15 -5
  99. package/src/seo/vite/prerender.ts +105 -58
@@ -4,18 +4,23 @@
4
4
  * Googlebot renders JavaScript, but Bing, a Slack unfurl, an LMS preview and
5
5
  * every academic indexer read the HTML that came off the wire — so the title,
6
6
  * the description and the canonical of a page must already be in it. A site
7
- * with a server rewrites them per request; a static one writes one file per
7
+ * with a server writes them per request; a static one writes one file per
8
8
  * address at build time. Both call this, which is pure string work: no
9
9
  * `window`, no `node:fs`.
10
+ *
11
+ * The page it is given is the template, which declares where its head goes and
12
+ * carries none of its own, so this only ever writes — see `./template.ts`.
10
13
  */
11
14
 
12
- import { siteById, siteDisplayName } from '../../ecosystem/core/lookup.ts';
15
+ import { siteDisplayName } from '../../ecosystem/core/lookup.ts';
13
16
  import type { EcosystemSite, SiteId } from '../../ecosystem/core/sites.ts';
14
17
  import { escapeAttribute, escapeText } from '../../share/core/escape.ts';
15
18
 
16
19
  import type { DocumentMeta } from './documentMeta.ts';
17
20
  import type { RouteMeta } from './routes.ts';
18
- import { pageMetaFor, trimTrailingSlash } from './routes.ts';
21
+ import { pageMetaFor } from './routes.ts';
22
+ import { mountPathOf, originOf, resolveSite } from './siteFiles.ts';
23
+ import { PAGE_HEAD_MARKER, fill } from './template.ts';
19
24
 
20
25
  /** Which site is being served, and what it answers. */
21
26
  export interface PageMetaOptions {
@@ -23,11 +28,18 @@ export interface PageMetaOptions {
23
28
  site: EcosystemSite | SiteId;
24
29
  /** Every address it answers, each with its title and description. */
25
30
  routes: readonly RouteMeta[];
26
- /** The address being written, query string included. */
31
+ /**
32
+ * The address being written, query string included: a path, or the absolute
33
+ * address an app reads off the page it is on.
34
+ */
27
35
  url: string;
28
36
  /**
29
- * Origin every absolute address is built on. A server passes the one the
30
- * request arrived on; a build leaves it out and the site's own host is used.
37
+ * Where the site is served, mount path included, e.g.
38
+ * `https://learn.cheminfo.org/surge`. A server passes the one the request
39
+ * arrived on; a build leaves it out and the site's own host is used. Every
40
+ * address written here is composed on it, so the mount is carried by the
41
+ * origin rather than applied a second time. It is an absolute address, or it
42
+ * is refused.
31
43
  * @default `https://<the site's host>`
32
44
  */
33
45
  origin?: string;
@@ -41,33 +53,44 @@ export interface PageMetaOptions {
41
53
  /**
42
54
  * Give a page the title, the description and the canonical address of the route
43
55
  * it answers, plus the card a link to it unfurls into.
44
- * @param html - The built page.
56
+ * @param html - The built template.
45
57
  * @param options - Which site, which address, and where it is served from.
46
- * @returns The page, with its head rewritten for that route.
58
+ * @returns The page, with its head written for that route.
59
+ * @throws {Error} When the page carries no `<!--cheminfo:head-->`, when the
60
+ * site answers no route, or when it names an origin that is not an absolute
61
+ * address.
47
62
  */
48
63
  export function injectPageMeta(html: string, options: PageMetaOptions): string {
49
- const site = resolveSite(options.site);
50
- const meta = pageMetaFor(options.routes, options.url);
51
- const name = siteDisplayName(site);
52
- const origin = trimTrailingSlash(options.origin ?? `https://${site.host}`);
64
+ return fill(html, PAGE_HEAD_MARKER, pageHeadTags(options));
65
+ }
66
+
67
+ /**
68
+ * The head a route is indexed and shared under, for a caller writing more into
69
+ * the same place — a structured-data block, a tracking snippet.
70
+ * @param options - Which site, which address, and where it is served from.
71
+ * @returns The tags, one per line.
72
+ * @throws {Error} When the site answers no route, or names an origin that is
73
+ * not an absolute address.
74
+ */
75
+ export function pageHeadTags(options: PageMetaOptions): string {
76
+ const name = siteDisplayName(resolveSite(options.site));
77
+ const description = routeMetaOf(options).description;
78
+ const origin = originOf(options);
53
79
  const { title, canonical } = pageDocumentMeta(options);
54
80
  const image = absolute(options.image ?? '/og.png', origin);
55
81
 
56
- const head = [
82
+ return [
83
+ `<title>${escapeText(title)}</title>`,
84
+ `<meta name="description" content="${escapeAttribute(description)}" />`,
57
85
  `<link rel="canonical" href="${escapeAttribute(canonical)}" />`,
58
86
  '<meta property="og:type" content="website" />',
59
87
  `<meta property="og:site_name" content="${escapeAttribute(name)}" />`,
60
88
  `<meta property="og:title" content="${escapeAttribute(title)}" />`,
61
- `<meta property="og:description" content="${escapeAttribute(meta.description)}" />`,
89
+ `<meta property="og:description" content="${escapeAttribute(description)}" />`,
62
90
  `<meta property="og:url" content="${escapeAttribute(canonical)}" />`,
63
91
  `<meta property="og:image" content="${escapeAttribute(image)}" />`,
64
92
  '<meta name="twitter:card" content="summary_large_image" />',
65
93
  ].join('\n');
66
-
67
- return insertBeforeHeadEnd(
68
- replaceDescription(replaceTitle(html, title), meta.description),
69
- head,
70
- );
71
94
  }
72
95
 
73
96
  /**
@@ -78,50 +101,28 @@ export function injectPageMeta(html: string, options: PageMetaOptions): string {
78
101
  * click that changes the page cannot disagree with the page a crawler fetched.
79
102
  * @param options - Which site, which address, and where it is served from.
80
103
  * @returns The title, the description and the canonical of that address.
104
+ * @throws {Error} When the site answers no route, or names an origin that is
105
+ * not an absolute address.
81
106
  */
82
- export function pageDocumentMeta(options: PageMetaOptions): Required<DocumentMeta> {
107
+ export function pageDocumentMeta(
108
+ options: PageMetaOptions,
109
+ ): Required<DocumentMeta> {
83
110
  const site = resolveSite(options.site);
84
- const meta = pageMetaFor(options.routes, options.url);
85
- const origin = trimTrailingSlash(options.origin ?? `https://${site.host}`);
111
+ const meta = routeMetaOf(options);
86
112
  return {
87
113
  title: `${meta.title} — ${siteDisplayName(site)}`,
88
114
  description: meta.description,
89
- canonical: `${origin}${meta.path}`,
115
+ canonical: `${originOf(options)}${meta.path}`,
90
116
  };
91
117
  }
92
118
 
93
- /**
94
- * Put an addition at the end of the head, where a tracking snippet and a
95
- * structured-data block both belong.
96
- * @param html - The page.
97
- * @param addition - The markup to add, taken as written.
98
- * @returns The page, with the addition before `</head>`.
99
- */
100
- export function insertBeforeHeadEnd(html: string, addition: string): string {
101
- const head = html.lastIndexOf('</head>');
102
- if (head === -1) return `${html}\n${addition}\n`;
103
- return `${html.slice(0, head)}${addition}\n${html.slice(head)}`;
104
- }
105
-
106
- function resolveSite(site: EcosystemSite | SiteId): EcosystemSite {
107
- return typeof site === 'string' ? siteById(site) : site;
119
+ // A server behind a mount is handed the address the browser asked for, and the
120
+ // route table is written from the site's own root, so the mount the origin
121
+ // carries is taken off it before the table is read.
122
+ function routeMetaOf(options: PageMetaOptions): RouteMeta {
123
+ return pageMetaFor(options.routes, options.url, mountPathOf(options));
108
124
  }
109
125
 
110
126
  function absolute(target: string, origin: string): string {
111
127
  return target.startsWith('/') ? `${origin}${target}` : target;
112
128
  }
113
-
114
- function replaceTitle(html: string, title: string): string {
115
- const replacement = `<title>${escapeText(title)}</title>`;
116
- return html.includes('<title>')
117
- ? html.replace(/<title>[\s\S]*?<\/title>/, replacement)
118
- : insertBeforeHeadEnd(html, replacement);
119
- }
120
-
121
- function replaceDescription(html: string, description: string): string {
122
- const replacement = `<meta name="description" content="${escapeAttribute(description)}" />`;
123
- const existing = /<meta[^>]*name="description"[^>]*>/;
124
- return existing.test(html)
125
- ? html.replace(existing, replacement)
126
- : insertBeforeHeadEnd(html, replacement);
127
- }
@@ -0,0 +1,114 @@
1
+ /**
2
+ * The crawl policy.
3
+ *
4
+ * Our tools are meant to be found, so only the endpoints are kept out of the
5
+ * index — an API prefix and its documentation are not pages — and each one may
6
+ * say in a comment why, because a policy nobody can read is a policy nobody
7
+ * maintains.
8
+ *
9
+ * Every line of this file is a directive, so nothing an author writes may
10
+ * become one they did not: a path is a single line that says something, and
11
+ * carries no fragment, or it is refused; and a comment is folded onto the one
12
+ * line it is written as.
13
+ */
14
+
15
+ import { joinBasePath } from '../../router/core/basePath.ts';
16
+
17
+ import type { SiteFilesOptions } from './siteFiles.ts';
18
+ import { mountPathOf, originOf } from './siteFiles.ts';
19
+
20
+ /** One address kept out of the index, and why. */
21
+ export interface RobotsDisallow {
22
+ /**
23
+ * The address prefix, from the site's own root, e.g. `/v1/`. It is a path,
24
+ * on one line, saying something, carrying no `#`, and padded by nothing: a
25
+ * blank one would read as `Disallow: /` and keep the whole site out of the
26
+ * index — RFC 9309 eats the trailing whitespace of a line, so `" "` is read
27
+ * as nothing at all — one carrying a line break would write whatever follows
28
+ * it as a directive of its own, everything from a `#` onwards is read as a
29
+ * comment, which truncates the path silently, and a padded one is not the
30
+ * address it looks like: `" /v1/"` does not start at the site root, so it is
31
+ * written `Disallow: / /v1/` and matches no address at all, leaving the
32
+ * endpoint crawled by the very line meant to keep it out.
33
+ */
34
+ path: string;
35
+ /**
36
+ * One sentence written as a `#` line above the directive. The `#` is added
37
+ * when it is not already there, every run of whitespace is folded to a single
38
+ * space so the sentence stays on its own line, and a blank one is written as
39
+ * no line at all.
40
+ * @default undefined — the directive is written on its own
41
+ */
42
+ comment?: string;
43
+ }
44
+
45
+ const NEWLINE = /[\n\r]/;
46
+
47
+ const WHITESPACE = /\s+/g;
48
+
49
+ /**
50
+ * The crawl policy of the address the site is served at.
51
+ *
52
+ * Every path is written under the mount, so a build published as one tool among
53
+ * several on a shared host allows what it actually answers rather than claiming
54
+ * the whole host. The sitemap is named only because this module also writes it:
55
+ * a `Sitemap:` line pointing at a 404 is reported as an error on every fetch.
56
+ * @param options - The site, its routes, and where it is served.
57
+ * @param disallow - Addresses to keep out of the index, each optionally with
58
+ * the sentence saying why.
59
+ * @returns The `robots.txt` document.
60
+ * @throws {Error} When a disallowed address is blank, spans more than one line,
61
+ * carries a `#` or is padded with whitespace, or when the deployment named an
62
+ * origin that is not an absolute address.
63
+ */
64
+ export function robotsTxt(
65
+ options: SiteFilesOptions,
66
+ disallow: ReadonlyArray<string | RobotsDisallow> = [],
67
+ ): string {
68
+ const mount = mountPathOf(options);
69
+ const lines = ['User-agent: *', `Allow: ${joinBasePath(mount, '/')}`];
70
+
71
+ for (const entry of disallow) {
72
+ const rule = typeof entry === 'string' ? { path: entry } : entry;
73
+ const comment =
74
+ rule.comment === undefined ? undefined : commentLine(rule.comment);
75
+ if (comment !== undefined) lines.push(comment);
76
+ lines.push(`Disallow: ${joinBasePath(mount, disallowPath(rule.path))}`);
77
+ }
78
+
79
+ lines.push('', `Sitemap: ${originOf(options)}/sitemap.xml`, '');
80
+ return lines.join('\n');
81
+ }
82
+
83
+ function disallowPath(path: string): string {
84
+ if (path === '') {
85
+ throw new Error('a disallowed address is a path, never the empty string');
86
+ }
87
+ if (path.trim() === '') {
88
+ throw new Error(
89
+ `a disallowed address is a path, never blank: ${JSON.stringify(path)}`,
90
+ );
91
+ }
92
+ if (NEWLINE.test(path)) {
93
+ throw new Error(
94
+ `a disallowed address is written on one line: ${JSON.stringify(path)}`,
95
+ );
96
+ }
97
+ if (path.includes('#')) {
98
+ throw new Error(
99
+ `a disallowed address carries no fragment: ${JSON.stringify(path)}`,
100
+ );
101
+ }
102
+ if (path !== path.trim()) {
103
+ throw new Error(
104
+ `a disallowed address is written without padding: ${JSON.stringify(path)}`,
105
+ );
106
+ }
107
+ return path;
108
+ }
109
+
110
+ function commentLine(comment: string): string | undefined {
111
+ const text = comment.replaceAll(WHITESPACE, ' ').trim();
112
+ if (text === '') return undefined;
113
+ return text.startsWith('#') ? text : `# ${text}`;
114
+ }
@@ -8,6 +8,17 @@
8
8
  * the table is a page a search engine only ever sees as the home page.
9
9
  */
10
10
 
11
+ import { stripBasePath } from '../../router/core/basePath.ts';
12
+
13
+ const QUERY_OR_FRAGMENT = /[?#]/;
14
+
15
+ const TRAILING_SLASHES = /\/+$/;
16
+
17
+ // A scheme and an authority: what `location.href` hands out, and the one shape
18
+ // that cannot be confused with a path. `//host/path` is left as a path, because
19
+ // a route table is free to name one.
20
+ const ABSOLUTE_URL = /^[a-z][\d+.a-z-]*:\/\//i;
21
+
11
22
  /** A page, as a crawler and a shared card see it. */
12
23
  export interface RouteMeta {
13
24
  /** Absolute path, without a trailing slash and without a query string. */
@@ -16,10 +27,34 @@ export interface RouteMeta {
16
27
  title: string;
17
28
  /** One sentence, in the words someone would search for. */
18
29
  description: string;
30
+ /**
31
+ * The label the page is linked under where a title written for a search
32
+ * result is too long to read as a menu entry — the `noscript` index.
33
+ * @default the route's own title
34
+ */
35
+ short?: string;
36
+ /**
37
+ * What the page is for, written after an em dash next to its link in the
38
+ * `noscript` index.
39
+ * @default undefined — the link stands on its own
40
+ */
41
+ note?: string;
42
+ /**
43
+ * Whether the route also answers every address beneath it, so a section
44
+ * carrying more pages than a table can hold — an entry per structure, per
45
+ * ligand, per identifier — is indexed under the section rather than under the
46
+ * home page. Those addresses are canonical to the section itself.
47
+ * @default false
48
+ */
49
+ prefix?: boolean;
19
50
  }
20
51
 
21
52
  /**
22
53
  * The route an address names.
54
+ *
55
+ * An address a route claims exactly always wins over one that claims it as a
56
+ * subtree, and between two subtrees the longer claim wins, so `/molecules/HEM`
57
+ * is a molecule rather than whatever `/` answers.
23
58
  * @param routes - Every address the site answers.
24
59
  * @param path - Absolute path, without a query string.
25
60
  * @returns Its entry, or `undefined` when the site does not know the address.
@@ -28,11 +63,7 @@ export function routeFor(
28
63
  routes: readonly RouteMeta[],
29
64
  path: string,
30
65
  ): RouteMeta | undefined {
31
- const wanted = trimTrailingSlash(path) || '/';
32
- for (const route of routes) {
33
- if (trimTrailingSlash(route.path) === wanted) return route;
34
- }
35
- return undefined;
66
+ return exactRoute(routes, path) ?? prefixRoute(routes, path);
36
67
  }
37
68
 
38
69
  /**
@@ -41,20 +72,51 @@ export function routeFor(
41
72
  * An address the site does not know is described as the home page rather than
42
73
  * invented on the fly, which is what the router does with it too. The query
43
74
  * string never reaches the answer: the structure being drawn and the
44
- * configuration a shared link carries are not pages of their own.
75
+ * configuration a shared link carries are not pages of their own. An absolute
76
+ * address is read for its path, so an app handing over `location.href` after an
77
+ * in-app move is answered rather than silently described as the home page.
78
+ *
79
+ * The route table is written from the site's own root, and a server behind a
80
+ * mount is handed the address the browser asked for — `/surge/exercises` for a
81
+ * table that names `/exercises`. So the address is read at the site's own root
82
+ * first, and the four lookups run in this order:
83
+ *
84
+ * 1. the mount taken off, claimed exactly;
85
+ * 2. the address as written, claimed exactly;
86
+ * 3. the mount taken off, claimed as a subtree;
87
+ * 4. the address as written, claimed as a subtree.
88
+ *
89
+ * Exact before subtree, or a `prefix` route — a home page answering everything
90
+ * beneath it above all — would claim every mounted address and the mount would
91
+ * never come off. Stripped before as-written, or the mount itself would open
92
+ * whichever page happens to carry the mount's own name rather than the site's
93
+ * front page. Taking the address as written second is what leaves an unmounted
94
+ * caller answering exactly as before, and lets a table whose own paths start
95
+ * with the mount's name still be read.
45
96
  * @param routes - Every address the site answers.
46
- * @param url - The address, query string and fragment included.
97
+ * @param url - The address, query string and fragment included, either as a
98
+ * path or as an absolute `scheme://host/path` address.
99
+ * @param basePath - The path the site is mounted at, when the address carries
100
+ * it, written `surge`, `/surge` or `/surge/`.
101
+ * @default '' — the address is already written from the site's own root
47
102
  * @returns The route it is indexed as.
48
103
  * @throws {Error} When the table is empty, so there is no page to fall back to.
49
104
  */
50
105
  export function pageMetaFor(
51
106
  routes: readonly RouteMeta[],
52
107
  url: string,
108
+ basePath = '',
53
109
  ): RouteMeta {
54
110
  const home = homeRoute(routes);
55
- const cut = url.search(/[?#]/);
56
- const path = cut === -1 ? url : url.slice(0, cut);
57
- return routeFor(routes, path) ?? home;
111
+ const path = pathOf(url);
112
+ const own = stripBasePath(basePath, path);
113
+ return (
114
+ exactRoute(routes, own) ??
115
+ exactRoute(routes, path) ??
116
+ prefixRoute(routes, own) ??
117
+ prefixRoute(routes, path) ??
118
+ home
119
+ );
58
120
  }
59
121
 
60
122
  /**
@@ -66,14 +128,119 @@ export function pageMetaFor(
66
128
  export function homeRoute(routes: readonly RouteMeta[]): RouteMeta {
67
129
  const first = routes[0];
68
130
  if (first === undefined) throw new Error('a site answers at least one route');
69
- return routeFor(routes, '/') ?? first;
131
+ return exactRoute(routes, '/') ?? first;
70
132
  }
71
133
 
72
134
  /**
73
- * Drop a trailing slash, so `/about/` and `/about` are one page.
135
+ * Check a route table before a build reads it as a set of file names.
136
+ *
137
+ * An address written twice ships two sitemap entries and two links to a page
138
+ * only the first entry describes, and one carrying a `..` segment writes its
139
+ * file outside the build output — a real build asked for `/../escaped` and got
140
+ * a sibling of `dist`. Two addresses that differ only in an empty segment or in
141
+ * case are the same defect wearing a disguise: `//x` and `/x` both write
142
+ * `dist/x/index.html`, and so do `/About` and `/about` on the case-insensitive
143
+ * filesystem macOS and Windows ship by default — one file, two sitemap entries,
144
+ * and only one of the two descriptions survives. All of it is author
145
+ * configuration read at build time, so it is refused where it is written rather
146
+ * than repaired where it lands.
147
+ * @param routes - Every address the site answers.
148
+ * @throws {Error} When the table is empty, names one address twice — under any
149
+ * of those spellings — or carries a path that is not one.
150
+ */
151
+ export function assertRoutes(routes: readonly RouteMeta[]): void {
152
+ if (routes.length === 0) throw new Error('a site answers at least one route');
153
+
154
+ const claimed = new Set<string>();
155
+ const folded = new Map<string, string>();
156
+ for (const route of routes) {
157
+ const written = JSON.stringify(route.path);
158
+ assertPath(route.path, written);
159
+ const address = trimTrailingSlash(route.path) || '/';
160
+ if (address.includes('//')) {
161
+ throw new Error(`a route path names no empty segment: ${written}`);
162
+ }
163
+ if (claimed.has(address)) {
164
+ throw new Error(`a route path is written once: ${written}`);
165
+ }
166
+ const first = folded.get(address.toLowerCase());
167
+ if (first !== undefined) {
168
+ throw new Error(
169
+ `two route paths name one file on a case-insensitive disk: ${first} and ${written}`,
170
+ );
171
+ }
172
+ claimed.add(address);
173
+ folded.set(address.toLowerCase(), written);
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Drop the trailing slashes, so `/about/` and `/about` are one page and an
179
+ * origin written `https://host/surge//` composes one address rather than one
180
+ * with an empty segment in it.
74
181
  * @param value - A path or an origin.
75
- * @returns It, without the trailing slash `/` itself keeps.
182
+ * @returns It, without the trailing slashes `/` itself keeps.
76
183
  */
77
184
  export function trimTrailingSlash(value: string): string {
78
- return value.length > 1 && value.endsWith('/') ? value.slice(0, -1) : value;
185
+ const trimmed = value.replace(TRAILING_SLASHES, '');
186
+ return trimmed === '' && value !== '' ? '/' : trimmed;
187
+ }
188
+
189
+ function assertPath(path: string, written: string): void {
190
+ if (!path.startsWith('/')) {
191
+ throw new Error(`a route path starts at the site root: ${written}`);
192
+ }
193
+ if (QUERY_OR_FRAGMENT.test(path)) {
194
+ throw new Error(
195
+ `a route path carries no query string and no fragment: ${written}`,
196
+ );
197
+ }
198
+ if (path.split('/').includes('..')) {
199
+ throw new Error(`a route path stays inside the site: ${written}`);
200
+ }
201
+ }
202
+
203
+ // The path half of whatever the caller had at hand: an absolute address, or a
204
+ // path already, with the query string and the fragment cut off either way.
205
+ function pathOf(url: string): string {
206
+ if (ABSOLUTE_URL.test(url) && URL.canParse(url)) return new URL(url).pathname;
207
+ const cut = url.search(QUERY_OR_FRAGMENT);
208
+ return cut === -1 ? url : url.slice(0, cut);
209
+ }
210
+
211
+ function exactRoute(
212
+ routes: readonly RouteMeta[],
213
+ path: string,
214
+ ): RouteMeta | undefined {
215
+ const wanted = trimTrailingSlash(path) || '/';
216
+ for (const route of routes) {
217
+ if ((trimTrailingSlash(route.path) || '/') === wanted) return route;
218
+ }
219
+ return undefined;
220
+ }
221
+
222
+ function prefixRoute(
223
+ routes: readonly RouteMeta[],
224
+ path: string,
225
+ ): RouteMeta | undefined {
226
+ const wanted = trimTrailingSlash(path) || '/';
227
+ let claimed: RouteMeta | undefined;
228
+ let claimedLength = -1;
229
+
230
+ for (const route of routes) {
231
+ if (route.prefix !== true) continue;
232
+ const routePath = trimTrailingSlash(route.path) || '/';
233
+ if (!isUnder(routePath, wanted)) continue;
234
+ if (routePath.length > claimedLength) {
235
+ claimed = route;
236
+ claimedLength = routePath.length;
237
+ }
238
+ }
239
+ return claimed;
240
+ }
241
+
242
+ function isUnder(routePath: string, path: string): boolean {
243
+ // `/surgeon` is not a page of `/surge`, so a claim only holds when what
244
+ // follows it is a path of its own.
245
+ return routePath === '/' || path.startsWith(`${routePath}/`);
79
246
  }