react-cheminfo 0.4.1 → 0.7.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 +198 -34
- package/lib/citation/core/index.d.ts +1 -0
- package/lib/citation/core/index.d.ts.map +1 -1
- package/lib/citation/core/index.js +1 -0
- package/lib/citation/core/index.js.map +1 -1
- package/lib/citation/core/platformPaper.d.ts +14 -0
- package/lib/citation/core/platformPaper.d.ts.map +1 -0
- package/lib/citation/core/platformPaper.js +28 -0
- package/lib/citation/core/platformPaper.js.map +1 -0
- package/lib/color/core/index.d.ts +1 -1
- package/lib/color/core/index.d.ts.map +1 -1
- package/lib/color/core/index.js +1 -1
- package/lib/color/core/index.js.map +1 -1
- package/lib/core.d.ts +1 -0
- package/lib/core.d.ts.map +1 -1
- package/lib/core.js +1 -0
- package/lib/core.js.map +1 -1
- package/lib/ecosystem/core/sites.d.ts +1 -1
- package/lib/ecosystem/core/sites.d.ts.map +1 -1
- package/lib/ecosystem/core/sites.js +15 -5
- package/lib/ecosystem/core/sites.js.map +1 -1
- package/lib/ecosystem/ui/glyphs.d.ts.map +1 -1
- package/lib/ecosystem/ui/glyphs.js +3 -0
- package/lib/ecosystem/ui/glyphs.js.map +1 -1
- package/lib/orbital/core/index.d.ts +1 -1
- package/lib/orbital/core/index.d.ts.map +1 -1
- package/lib/orbital/core/index.js +1 -1
- package/lib/orbital/core/index.js.map +1 -1
- package/lib/orbital/ui/AtomicOrbitalCanvas.d.ts +5 -0
- package/lib/orbital/ui/AtomicOrbitalCanvas.d.ts.map +1 -1
- package/lib/orbital/ui/AtomicOrbitalCanvas.js +4 -3
- package/lib/orbital/ui/AtomicOrbitalCanvas.js.map +1 -1
- package/lib/orbital/ui/AtomicOrbitalViewer.d.ts +5 -0
- package/lib/orbital/ui/AtomicOrbitalViewer.d.ts.map +1 -1
- package/lib/orbital/ui/AtomicOrbitalViewer.js.map +1 -1
- package/lib/orbital/ui/axesGeometry.d.ts +27 -0
- package/lib/orbital/ui/axesGeometry.d.ts.map +1 -0
- package/lib/orbital/ui/axesGeometry.js +74 -0
- package/lib/orbital/ui/axesGeometry.js.map +1 -0
- package/lib/orbital/ui/camera.d.ts +7 -0
- package/lib/orbital/ui/camera.d.ts.map +1 -1
- package/lib/orbital/ui/camera.js +8 -1
- package/lib/orbital/ui/camera.js.map +1 -1
- package/lib/orbital/ui/renderAxes.d.ts +53 -0
- package/lib/orbital/ui/renderAxes.d.ts.map +1 -0
- package/lib/orbital/ui/renderAxes.js +110 -0
- package/lib/orbital/ui/renderAxes.js.map +1 -0
- package/lib/orbital/ui/viewer.d.ts +16 -0
- package/lib/orbital/ui/viewer.d.ts.map +1 -1
- package/lib/orbital/ui/viewer.js +24 -2
- package/lib/orbital/ui/viewer.js.map +1 -1
- package/lib/periodic/core/categories.d.ts +25 -0
- package/lib/periodic/core/categories.d.ts.map +1 -0
- package/lib/periodic/core/categories.js +70 -0
- package/lib/periodic/core/categories.js.map +1 -0
- package/lib/periodic/core/elements.d.ts +49 -0
- package/lib/periodic/core/elements.d.ts.map +1 -0
- package/lib/periodic/core/elements.js +175 -0
- package/lib/periodic/core/elements.js.map +1 -0
- package/lib/periodic/core/index.d.ts +6 -0
- package/lib/periodic/core/index.d.ts.map +1 -0
- package/lib/periodic/core/index.js +4 -0
- package/lib/periodic/core/index.js.map +1 -0
- package/lib/periodic/core/layout.d.ts +73 -0
- package/lib/periodic/core/layout.d.ts.map +1 -0
- package/lib/periodic/core/layout.js +132 -0
- package/lib/periodic/core/layout.js.map +1 -0
- package/lib/periodic/ui/CategoryLegend.d.ts +26 -0
- package/lib/periodic/ui/CategoryLegend.d.ts.map +1 -0
- package/lib/periodic/ui/CategoryLegend.js +53 -0
- package/lib/periodic/ui/CategoryLegend.js.map +1 -0
- package/lib/periodic/ui/ElementCell.d.ts +53 -0
- package/lib/periodic/ui/ElementCell.d.ts.map +1 -0
- package/lib/periodic/ui/ElementCell.js +56 -0
- package/lib/periodic/ui/ElementCell.js.map +1 -0
- package/lib/periodic/ui/PeriodicTable.d.ts +90 -0
- package/lib/periodic/ui/PeriodicTable.d.ts.map +1 -0
- package/lib/periodic/ui/PeriodicTable.js +77 -0
- package/lib/periodic/ui/PeriodicTable.js.map +1 -0
- package/lib/periodic/ui/PeriodicTableChrome.d.ts +36 -0
- package/lib/periodic/ui/PeriodicTableChrome.d.ts.map +1 -0
- package/lib/periodic/ui/PeriodicTableChrome.js +72 -0
- package/lib/periodic/ui/PeriodicTableChrome.js.map +1 -0
- package/lib/periodic/ui/index.d.ts +7 -0
- package/lib/periodic/ui/index.d.ts.map +1 -0
- package/lib/periodic/ui/index.js +4 -0
- package/lib/periodic/ui/index.js.map +1 -0
- package/lib/seo/core/documentMeta.d.ts +2 -2
- package/lib/seo/core/documentMeta.js +4 -3
- package/lib/seo/core/documentMeta.js.map +1 -1
- package/lib/seo/core/index.d.ts +13 -4
- package/lib/seo/core/index.d.ts.map +1 -1
- package/lib/seo/core/index.js +8 -3
- package/lib/seo/core/index.js.map +1 -1
- package/lib/seo/core/noscript.d.ts +97 -0
- package/lib/seo/core/noscript.d.ts.map +1 -0
- package/lib/seo/core/noscript.js +93 -0
- package/lib/seo/core/noscript.js.map +1 -0
- package/lib/seo/core/pageMeta.d.ts +30 -14
- package/lib/seo/core/pageMeta.d.ts.map +1 -1
- package/lib/seo/core/pageMeta.js +40 -43
- package/lib/seo/core/pageMeta.js.map +1 -1
- package/lib/seo/core/robots.d.ts +55 -0
- package/lib/seo/core/robots.d.ts.map +1 -0
- package/lib/seo/core/robots.js +70 -0
- package/lib/seo/core/robots.js.map +1 -0
- package/lib/seo/core/routes.d.ts +73 -5
- package/lib/seo/core/routes.d.ts.map +1 -1
- package/lib/seo/core/routes.js +142 -16
- package/lib/seo/core/routes.js.map +1 -1
- package/lib/seo/core/siteFiles.d.ts +39 -43
- package/lib/seo/core/siteFiles.d.ts.map +1 -1
- package/lib/seo/core/siteFiles.js +53 -69
- package/lib/seo/core/siteFiles.js.map +1 -1
- package/lib/seo/core/startDocumentMeta.d.ts +44 -0
- package/lib/seo/core/startDocumentMeta.d.ts.map +1 -0
- package/lib/seo/core/startDocumentMeta.js +47 -0
- package/lib/seo/core/startDocumentMeta.js.map +1 -0
- package/lib/seo/core/structuredData.d.ts +48 -0
- package/lib/seo/core/structuredData.d.ts.map +1 -0
- package/lib/seo/core/structuredData.js +41 -0
- package/lib/seo/core/structuredData.js.map +1 -0
- package/lib/seo/core/template.d.ts +48 -0
- package/lib/seo/core/template.d.ts.map +1 -0
- package/lib/seo/core/template.js +53 -0
- package/lib/seo/core/template.js.map +1 -0
- package/lib/seo/vite/ogCard.d.ts +9 -1
- package/lib/seo/vite/ogCard.d.ts.map +1 -1
- package/lib/seo/vite/ogCard.js +14 -4
- package/lib/seo/vite/ogCard.js.map +1 -1
- package/lib/seo/vite/prerender.d.ts +38 -7
- package/lib/seo/vite/prerender.d.ts.map +1 -1
- package/lib/seo/vite/prerender.js +68 -30
- package/lib/seo/vite/prerender.js.map +1 -1
- package/lib/ui.d.ts +1 -0
- package/lib/ui.d.ts.map +1 -1
- package/lib/ui.js +1 -0
- package/lib/ui.js.map +1 -1
- package/package.json +2 -1
- package/src/citation/core/index.ts +1 -0
- package/src/citation/core/platformPaper.ts +32 -0
- package/src/color/core/index.ts +6 -1
- package/src/core.ts +1 -0
- package/src/ecosystem/core/sites.ts +16 -5
- package/src/ecosystem/ui/glyphs.tsx +19 -0
- package/src/orbital/core/index.ts +1 -1
- package/src/orbital/ui/AtomicOrbitalCanvas.tsx +9 -2
- package/src/orbital/ui/AtomicOrbitalViewer.tsx +5 -0
- package/src/orbital/ui/axesGeometry.ts +91 -0
- package/src/orbital/ui/camera.ts +9 -1
- package/src/orbital/ui/renderAxes.ts +190 -0
- package/src/orbital/ui/viewer.ts +32 -2
- package/src/periodic/core/categories.ts +83 -0
- package/src/periodic/core/elements.ts +217 -0
- package/src/periodic/core/index.ts +27 -0
- package/src/periodic/core/layout.ts +183 -0
- package/src/periodic/ui/CategoryLegend.tsx +105 -0
- package/src/periodic/ui/ElementCell.tsx +137 -0
- package/src/periodic/ui/PeriodicTable.tsx +226 -0
- package/src/periodic/ui/PeriodicTableChrome.tsx +170 -0
- package/src/periodic/ui/index.ts +6 -0
- package/src/seo/core/documentMeta.ts +5 -5
- package/src/seo/core/index.ts +19 -12
- package/src/seo/core/noscript.ts +195 -0
- package/src/seo/core/pageMeta.ts +54 -53
- package/src/seo/core/robots.ts +114 -0
- package/src/seo/core/routes.ts +181 -14
- package/src/seo/core/siteFiles.ts +58 -96
- package/src/seo/core/startDocumentMeta.ts +77 -0
- package/src/seo/core/structuredData.ts +80 -0
- package/src/seo/core/template.ts +54 -0
- package/src/seo/vite/ogCard.ts +15 -5
- package/src/seo/vite/prerender.ts +105 -58
- package/src/ui.ts +1 -0
package/src/seo/core/routes.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
56
|
-
const
|
|
57
|
-
return
|
|
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
|
|
131
|
+
return exactRoute(routes, '/') ?? first;
|
|
70
132
|
}
|
|
71
133
|
|
|
72
134
|
/**
|
|
73
|
-
*
|
|
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
|
|
182
|
+
* @returns It, without the trailing slashes `/` itself keeps.
|
|
76
183
|
*/
|
|
77
184
|
export function trimTrailingSlash(value: string): string {
|
|
78
|
-
|
|
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
|
}
|
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* The sitemap, and what every other file a crawler fetches on its own is
|
|
3
|
+
* derived from: which site is being written, where it is served, and the path
|
|
4
|
+
* it is mounted at.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* A deployment names where it serves the site in full — origin and mount path
|
|
7
|
+
* in one value — because the origin is what a canonical link and a sitemap
|
|
8
|
+
* entry need. The mount is read back out of it here, so the addresses these
|
|
9
|
+
* files hand out start where the site actually answers.
|
|
8
10
|
*/
|
|
9
11
|
|
|
10
|
-
import { siteById
|
|
12
|
+
import { siteById } from '../../ecosystem/core/lookup.ts';
|
|
11
13
|
import type { EcosystemSite, SiteId } from '../../ecosystem/core/sites.ts';
|
|
12
|
-
import {
|
|
14
|
+
import { basePathOf } from '../../router/core/basePath.ts';
|
|
15
|
+
import { escapeText } from '../../share/core/escape.ts';
|
|
13
16
|
|
|
14
17
|
import type { RouteMeta } from './routes.ts';
|
|
15
18
|
import { trimTrailingSlash } from './routes.ts';
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
// A crawler fetches what it is given over HTTP, so an origin is written in one
|
|
21
|
+
// of the two schemes it speaks. Parsing alone does not say that: `localhost:3000`
|
|
22
|
+
// parses, with `localhost:` as its scheme and `3000` as its path.
|
|
23
|
+
const HTTP_ORIGIN = /^https?:\/\//i;
|
|
19
24
|
|
|
20
25
|
/** What a crawler is told about the site as a whole. */
|
|
21
26
|
export interface SiteFilesOptions {
|
|
@@ -24,7 +29,9 @@ export interface SiteFilesOptions {
|
|
|
24
29
|
/** Every address it answers. */
|
|
25
30
|
routes: readonly RouteMeta[];
|
|
26
31
|
/**
|
|
27
|
-
*
|
|
32
|
+
* Where the site is served, mount path included, e.g.
|
|
33
|
+
* `https://learn.cheminfo.org/surge`. Every absolute address is built on it,
|
|
34
|
+
* and every path one of these files writes starts at its mount.
|
|
28
35
|
* @default `https://<the site's host>`
|
|
29
36
|
*/
|
|
30
37
|
origin?: string;
|
|
@@ -32,11 +39,20 @@ export interface SiteFilesOptions {
|
|
|
32
39
|
|
|
33
40
|
/**
|
|
34
41
|
* Every routed address, as the sitemap lists them.
|
|
42
|
+
*
|
|
43
|
+
* A sitemap names at least one address: `<url>` is required by the sitemaps.org
|
|
44
|
+
* schema, and `robots.txt` advertises the file, so an empty one is reported as
|
|
45
|
+
* an error on every fetch rather than read as a site with nothing to index.
|
|
35
46
|
* @param options - The site and its routes.
|
|
36
47
|
* @returns The `sitemap.xml` document.
|
|
48
|
+
* @throws {Error} When the site answers no route, or names an origin that is
|
|
49
|
+
* not an absolute address.
|
|
37
50
|
*/
|
|
38
51
|
export function sitemapXml(options: SiteFilesOptions): string {
|
|
39
52
|
const origin = originOf(options);
|
|
53
|
+
if (options.routes.length === 0) {
|
|
54
|
+
throw new Error('a sitemap lists at least one address');
|
|
55
|
+
}
|
|
40
56
|
const entries = options.routes
|
|
41
57
|
.map(
|
|
42
58
|
(route) =>
|
|
@@ -51,100 +67,46 @@ ${entries}
|
|
|
51
67
|
}
|
|
52
68
|
|
|
53
69
|
/**
|
|
54
|
-
* The
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* API prefix and its documentation are not pages. The sitemap is named only
|
|
58
|
-
* because this module also writes it: a `Sitemap:` line pointing at a 404 is
|
|
59
|
-
* reported as an error on every fetch.
|
|
60
|
-
* @param options - The site and its routes.
|
|
61
|
-
* @param disallow - Address prefixes to keep out of the index.
|
|
62
|
-
* @returns The `robots.txt` document.
|
|
70
|
+
* The site these files are being written for.
|
|
71
|
+
* @param site - The site, named or passed.
|
|
72
|
+
* @returns Its record.
|
|
63
73
|
*/
|
|
64
|
-
export function
|
|
65
|
-
|
|
66
|
-
disallow: readonly string[] = [],
|
|
67
|
-
): string {
|
|
68
|
-
const lines = ['User-agent: *', 'Allow: /'];
|
|
69
|
-
for (const path of disallow) lines.push(`Disallow: ${path}`);
|
|
70
|
-
lines.push('', `Sitemap: ${originOf(options)}/sitemap.xml`, '');
|
|
71
|
-
return lines.join('\n');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** What the structured-data block says the tool is. */
|
|
75
|
-
export interface StructuredDataOptions extends SiteFilesOptions {
|
|
76
|
-
/**
|
|
77
|
-
* The schema.org application category.
|
|
78
|
-
* @default 'EducationalApplication'
|
|
79
|
-
*/
|
|
80
|
-
category?: string;
|
|
81
|
-
/**
|
|
82
|
-
* What the tool needs to run.
|
|
83
|
-
* @default 'Any modern browser'
|
|
84
|
-
*/
|
|
85
|
-
operatingSystem?: string;
|
|
74
|
+
export function resolveSite(site: EcosystemSite | SiteId): EcosystemSite {
|
|
75
|
+
return typeof site === 'string' ? siteById(site) : site;
|
|
86
76
|
}
|
|
87
77
|
|
|
88
78
|
/**
|
|
89
|
-
*
|
|
79
|
+
* Where the site is served, as an absolute address without a trailing slash.
|
|
90
80
|
*
|
|
91
|
-
* It is
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
81
|
+
* It is an absolute `http` or `https` address or it is refused: a canonical
|
|
82
|
+
* link, an `og:url` and a sitemap entry are addresses a crawler resolves on its
|
|
83
|
+
* own, and one written from an origin missing its scheme is resolved against
|
|
84
|
+
* whatever directory the page was fetched from — pointing every page of the
|
|
85
|
+
* site at a sibling of itself. A dev or staging origin written `localhost:3000`
|
|
86
|
+
* is refused for the same reason: it parses, but as a path under a `localhost:`
|
|
87
|
+
* scheme, so the mount read back off it would be `/3000`.
|
|
88
|
+
* @param options - The site and where it is served.
|
|
89
|
+
* @returns The origin, mount path included when the deployment named one.
|
|
90
|
+
* @throws {Error} When the deployment named something that is not an absolute
|
|
91
|
+
* `http` or `https` address.
|
|
95
92
|
*/
|
|
96
|
-
export function
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
applicationCategory: options.category ?? 'EducationalApplication',
|
|
105
|
-
operatingSystem: options.operatingSystem ?? 'Any modern browser',
|
|
106
|
-
offers: { '@type': 'Offer', price: '0', priceCurrency: 'EUR' },
|
|
107
|
-
publisher: { '@type': 'Organization', name: 'cheminfo' },
|
|
108
|
-
};
|
|
109
|
-
const json = JSON.stringify(data, null, 2).replaceAll(
|
|
110
|
-
'<',
|
|
111
|
-
SCRIPT_SAFE_LESS_THAN,
|
|
112
|
-
);
|
|
113
|
-
return `<script type="application/ld+json">\n${json}\n</script>`;
|
|
93
|
+
export function originOf(options: SiteFilesOptions): string {
|
|
94
|
+
const origin = options.origin ?? `https://${resolveSite(options.site).host}`;
|
|
95
|
+
if (!HTTP_ORIGIN.test(origin) || !URL.canParse(origin)) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`an origin is an absolute address, e.g. https://surge.cheminfo.org: ${JSON.stringify(origin)}`,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return trimTrailingSlash(origin);
|
|
114
101
|
}
|
|
115
102
|
|
|
116
103
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* @param options - The site and its routes.
|
|
123
|
-
* @returns The `noscript` block, ready to put in the body.
|
|
104
|
+
* The path the deployment is mounted at, read off the address it named.
|
|
105
|
+
* @param options - The site and where it is served.
|
|
106
|
+
* @returns `''` for a site owning its host, `/surge` for one mounted under it.
|
|
107
|
+
* @throws {Error} When the deployment named something that is not an absolute
|
|
108
|
+
* address, so there is no path to read off it.
|
|
124
109
|
*/
|
|
125
|
-
export function
|
|
126
|
-
|
|
127
|
-
const items = options.routes
|
|
128
|
-
.map(
|
|
129
|
-
(route) =>
|
|
130
|
-
` <li><a href="${escapeAttribute(route.path)}">${escapeText(route.title)}</a></li>`,
|
|
131
|
-
)
|
|
132
|
-
.join('\n');
|
|
133
|
-
return `<noscript>
|
|
134
|
-
<h1>${escapeText(siteDisplayName(site))}</h1>
|
|
135
|
-
<p>${escapeText(site.tagline)} This tool needs JavaScript; these are the pages it offers:</p>
|
|
136
|
-
<ul>
|
|
137
|
-
${items}
|
|
138
|
-
</ul>
|
|
139
|
-
</noscript>`;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function resolveSite(site: EcosystemSite | SiteId): EcosystemSite {
|
|
143
|
-
return typeof site === 'string' ? siteById(site) : site;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function originOf(options: SiteFilesOptions): string {
|
|
147
|
-
return trimTrailingSlash(
|
|
148
|
-
options.origin ?? `https://${resolveSite(options.site).host}`,
|
|
149
|
-
);
|
|
110
|
+
export function mountPathOf(options: SiteFilesOptions): string {
|
|
111
|
+
return basePathOf(originOf(options));
|
|
150
112
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keep the tab and the canonical link in step with the page on screen.
|
|
3
|
+
*
|
|
4
|
+
* The server, or the build that wrote one file per address, already titled the
|
|
5
|
+
* page it handed out; this is what a move inside the app changes, and what a
|
|
6
|
+
* crawler that renders the page reads afterwards. Every site did the same three
|
|
7
|
+
* things around it — read the address it is on, look it up in its route table,
|
|
8
|
+
* write the head — so all three live here, and a site says only where its
|
|
9
|
+
* address is read and how a change to it is noticed.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { writeDocumentMeta } from './documentMeta.ts';
|
|
13
|
+
import type { PageMetaOptions } from './pageMeta.ts';
|
|
14
|
+
import { pageDocumentMeta } from './pageMeta.ts';
|
|
15
|
+
|
|
16
|
+
/** Where a site's address is read, and how a change to it is noticed. */
|
|
17
|
+
export interface StartDocumentMetaOptions extends Omit<
|
|
18
|
+
PageMetaOptions,
|
|
19
|
+
'url' | 'image'
|
|
20
|
+
> {
|
|
21
|
+
/**
|
|
22
|
+
* The address on screen, query string included: a path, or the absolute
|
|
23
|
+
* address read off the page. It is read again on every write, so a `follow`
|
|
24
|
+
* that tracks what it reads — a signals `effect` — notices the next page.
|
|
25
|
+
*/
|
|
26
|
+
url: () => string;
|
|
27
|
+
/**
|
|
28
|
+
* How a change of page is noticed: `effect` from `@preact/signals-react`
|
|
29
|
+
* follows whichever signals `url` reads and hands back the function that
|
|
30
|
+
* stops it. Left out, the head is written once, which is what a site calling
|
|
31
|
+
* this from its own `popstate` handler wants.
|
|
32
|
+
* @default undefined — the head is written once
|
|
33
|
+
*/
|
|
34
|
+
follow?: (write: () => void) => () => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Write the head of the page on screen, and keep it in step as the page
|
|
39
|
+
* changes.
|
|
40
|
+
*
|
|
41
|
+
* Nothing happens where there is no document — a prerender script, a unit test
|
|
42
|
+
* of the route table — so this is safe to call from a module either of them
|
|
43
|
+
* imports.
|
|
44
|
+
* @param options - The site, its routes, where its address is read and how a
|
|
45
|
+
* change to it is noticed.
|
|
46
|
+
* @returns The function that stops following, which does nothing when nothing
|
|
47
|
+
* was followed.
|
|
48
|
+
* @throws {Error} When the site answers no route, or names an origin that is
|
|
49
|
+
* not an absolute address.
|
|
50
|
+
*/
|
|
51
|
+
export function startDocumentMeta(
|
|
52
|
+
options: StartDocumentMetaOptions,
|
|
53
|
+
): () => void {
|
|
54
|
+
if (typeof document === 'undefined') return stopNothing;
|
|
55
|
+
|
|
56
|
+
const write = (): void => {
|
|
57
|
+
writeDocumentMeta(
|
|
58
|
+
pageDocumentMeta({
|
|
59
|
+
site: options.site,
|
|
60
|
+
routes: options.routes,
|
|
61
|
+
url: options.url(),
|
|
62
|
+
origin: options.origin,
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return (options.follow ?? writeOnce)(write);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function writeOnce(write: () => void): () => void {
|
|
71
|
+
write();
|
|
72
|
+
return stopNothing;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function stopNothing(): void {
|
|
76
|
+
// Nothing was followed, so there is nothing to stop.
|
|
77
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One `application/ld+json` block describing the tool.
|
|
3
|
+
*
|
|
4
|
+
* It is the same on every page of a site — what varies per page is the head —
|
|
5
|
+
* so it is written into the built page once rather than per route.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { siteDisplayName } from '../../ecosystem/core/lookup.ts';
|
|
9
|
+
|
|
10
|
+
import type { SiteFilesOptions } from './siteFiles.ts';
|
|
11
|
+
import { originOf, resolveSite } from './siteFiles.ts';
|
|
12
|
+
|
|
13
|
+
/** The sequence that must not appear raw inside a script element. */
|
|
14
|
+
const SCRIPT_SAFE_LESS_THAN = String.raw`\u003c`;
|
|
15
|
+
|
|
16
|
+
/** What the structured-data block says the tool is. */
|
|
17
|
+
export interface StructuredDataOptions extends SiteFilesOptions {
|
|
18
|
+
/**
|
|
19
|
+
* The schema.org application category.
|
|
20
|
+
* @default 'EducationalApplication'
|
|
21
|
+
*/
|
|
22
|
+
category?: string;
|
|
23
|
+
/**
|
|
24
|
+
* What the tool needs to run.
|
|
25
|
+
* @default 'Any modern browser'
|
|
26
|
+
*/
|
|
27
|
+
operatingSystem?: string;
|
|
28
|
+
/**
|
|
29
|
+
* What the tool does, in the words a search result is read in. A site whose
|
|
30
|
+
* indexed sentence says more than the line its tile in the family menu
|
|
31
|
+
* carries writes it here.
|
|
32
|
+
* @default the site's tagline
|
|
33
|
+
*/
|
|
34
|
+
description?: string;
|
|
35
|
+
/**
|
|
36
|
+
* What a browser has to offer for the tool to run. Ours run in the page.
|
|
37
|
+
* @default 'Requires JavaScript'
|
|
38
|
+
*/
|
|
39
|
+
browserRequirements?: string;
|
|
40
|
+
/**
|
|
41
|
+
* The currency the price is quoted in. The price is zero either way, but the
|
|
42
|
+
* pair has to agree with the audience the site is read by.
|
|
43
|
+
* @default 'EUR'
|
|
44
|
+
*/
|
|
45
|
+
currency?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The structured-data block, ready to put in the head.
|
|
50
|
+
*
|
|
51
|
+
* It always says the tool is free: the price is zero, and a block that leaves
|
|
52
|
+
* that implicit is one a rich result declines to show.
|
|
53
|
+
* @param options - The site, and what kind of application it is.
|
|
54
|
+
* @returns The script tag.
|
|
55
|
+
*/
|
|
56
|
+
export function structuredDataScript(options: StructuredDataOptions): string {
|
|
57
|
+
const site = resolveSite(options.site);
|
|
58
|
+
const data = {
|
|
59
|
+
'@context': 'https://schema.org',
|
|
60
|
+
'@type': 'WebApplication',
|
|
61
|
+
name: siteDisplayName(site),
|
|
62
|
+
url: `${originOf(options)}/`,
|
|
63
|
+
description: options.description ?? site.tagline,
|
|
64
|
+
applicationCategory: options.category ?? 'EducationalApplication',
|
|
65
|
+
operatingSystem: options.operatingSystem ?? 'Any modern browser',
|
|
66
|
+
browserRequirements: options.browserRequirements ?? 'Requires JavaScript',
|
|
67
|
+
offers: {
|
|
68
|
+
'@type': 'Offer',
|
|
69
|
+
price: '0',
|
|
70
|
+
priceCurrency: options.currency ?? 'EUR',
|
|
71
|
+
},
|
|
72
|
+
isAccessibleForFree: true,
|
|
73
|
+
publisher: { '@type': 'Organization', name: 'cheminfo' },
|
|
74
|
+
};
|
|
75
|
+
const json = JSON.stringify(data, null, 2).replaceAll(
|
|
76
|
+
'<',
|
|
77
|
+
SCRIPT_SAFE_LESS_THAN,
|
|
78
|
+
);
|
|
79
|
+
return `<script type="application/ld+json">\n${json}\n</script>`;
|
|
80
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a built page lets its head and its body be written.
|
|
3
|
+
*
|
|
4
|
+
* Every address of a site needs its own title, description, canonical and card,
|
|
5
|
+
* and a static build ships one `index.html`. Rather than look for the tags a
|
|
6
|
+
* page already carries and operate on them, the template says where they go:
|
|
7
|
+
* two comments, replaced by what the build or the server writes for the address
|
|
8
|
+
* being answered.
|
|
9
|
+
*
|
|
10
|
+
* ```html
|
|
11
|
+
* <head>
|
|
12
|
+
* <meta charset="utf-8" />
|
|
13
|
+
* <link rel="icon" href="%BASE_URL%favicon.svg" />
|
|
14
|
+
* <!--cheminfo:head-->
|
|
15
|
+
* </head>
|
|
16
|
+
* <body>
|
|
17
|
+
* <div id="root"></div>
|
|
18
|
+
* <!--cheminfo:body-->
|
|
19
|
+
* </body>
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* The template carries no title and no description of its own, so nothing can
|
|
23
|
+
* be duplicated and nothing has to be taken back out. Nothing is parsed and
|
|
24
|
+
* nothing is searched for but the marker, so what the rest of the page holds — a
|
|
25
|
+
* byte order mark, an implicit head, a `</head>` its prose displays or a script
|
|
26
|
+
* quotes, an unterminated comment — cannot reach the result.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** Where the head a crawler reads is written. */
|
|
30
|
+
export const PAGE_HEAD_MARKER = '<!--cheminfo:head-->';
|
|
31
|
+
|
|
32
|
+
/** Where the crawl path a visitor with no JavaScript reads is written. */
|
|
33
|
+
export const PAGE_BODY_MARKER = '<!--cheminfo:body-->';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Write content in the place the template kept for it.
|
|
37
|
+
*
|
|
38
|
+
* The marker is consumed, so a page is always filled from the template and
|
|
39
|
+
* never from a filled page: applying this twice throws rather than writing a
|
|
40
|
+
* second head, which is why idempotence is not something the caller has to
|
|
41
|
+
* defend.
|
|
42
|
+
* @param html - The template.
|
|
43
|
+
* @param marker - {@link PAGE_HEAD_MARKER} or {@link PAGE_BODY_MARKER}.
|
|
44
|
+
* @param content - The markup to write, taken as written: no `$&`, `$1` or
|
|
45
|
+
* `$<name>` is expanded.
|
|
46
|
+
* @returns The page.
|
|
47
|
+
* @throws {Error} When the template carries no such marker, rather than
|
|
48
|
+
* silently shipping a page with no head.
|
|
49
|
+
*/
|
|
50
|
+
export function fill(html: string, marker: string, content: string): string {
|
|
51
|
+
const at = html.indexOf(marker);
|
|
52
|
+
if (at === -1) throw new Error(`the page carries no ${marker}`);
|
|
53
|
+
return html.slice(0, at) + content + html.slice(at + marker.length);
|
|
54
|
+
}
|