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.
Files changed (174) hide show
  1. package/README.md +198 -34
  2. package/lib/citation/core/index.d.ts +1 -0
  3. package/lib/citation/core/index.d.ts.map +1 -1
  4. package/lib/citation/core/index.js +1 -0
  5. package/lib/citation/core/index.js.map +1 -1
  6. package/lib/citation/core/platformPaper.d.ts +14 -0
  7. package/lib/citation/core/platformPaper.d.ts.map +1 -0
  8. package/lib/citation/core/platformPaper.js +28 -0
  9. package/lib/citation/core/platformPaper.js.map +1 -0
  10. package/lib/color/core/index.d.ts +1 -1
  11. package/lib/color/core/index.d.ts.map +1 -1
  12. package/lib/color/core/index.js +1 -1
  13. package/lib/color/core/index.js.map +1 -1
  14. package/lib/core.d.ts +1 -0
  15. package/lib/core.d.ts.map +1 -1
  16. package/lib/core.js +1 -0
  17. package/lib/core.js.map +1 -1
  18. package/lib/ecosystem/core/sites.d.ts +1 -1
  19. package/lib/ecosystem/core/sites.d.ts.map +1 -1
  20. package/lib/ecosystem/core/sites.js +15 -5
  21. package/lib/ecosystem/core/sites.js.map +1 -1
  22. package/lib/ecosystem/ui/glyphs.d.ts.map +1 -1
  23. package/lib/ecosystem/ui/glyphs.js +3 -0
  24. package/lib/ecosystem/ui/glyphs.js.map +1 -1
  25. package/lib/orbital/core/index.d.ts +1 -1
  26. package/lib/orbital/core/index.d.ts.map +1 -1
  27. package/lib/orbital/core/index.js +1 -1
  28. package/lib/orbital/core/index.js.map +1 -1
  29. package/lib/orbital/ui/AtomicOrbitalCanvas.d.ts +5 -0
  30. package/lib/orbital/ui/AtomicOrbitalCanvas.d.ts.map +1 -1
  31. package/lib/orbital/ui/AtomicOrbitalCanvas.js +4 -3
  32. package/lib/orbital/ui/AtomicOrbitalCanvas.js.map +1 -1
  33. package/lib/orbital/ui/AtomicOrbitalViewer.d.ts +5 -0
  34. package/lib/orbital/ui/AtomicOrbitalViewer.d.ts.map +1 -1
  35. package/lib/orbital/ui/AtomicOrbitalViewer.js.map +1 -1
  36. package/lib/orbital/ui/axesGeometry.d.ts +27 -0
  37. package/lib/orbital/ui/axesGeometry.d.ts.map +1 -0
  38. package/lib/orbital/ui/axesGeometry.js +74 -0
  39. package/lib/orbital/ui/axesGeometry.js.map +1 -0
  40. package/lib/orbital/ui/camera.d.ts +7 -0
  41. package/lib/orbital/ui/camera.d.ts.map +1 -1
  42. package/lib/orbital/ui/camera.js +8 -1
  43. package/lib/orbital/ui/camera.js.map +1 -1
  44. package/lib/orbital/ui/renderAxes.d.ts +53 -0
  45. package/lib/orbital/ui/renderAxes.d.ts.map +1 -0
  46. package/lib/orbital/ui/renderAxes.js +110 -0
  47. package/lib/orbital/ui/renderAxes.js.map +1 -0
  48. package/lib/orbital/ui/viewer.d.ts +16 -0
  49. package/lib/orbital/ui/viewer.d.ts.map +1 -1
  50. package/lib/orbital/ui/viewer.js +24 -2
  51. package/lib/orbital/ui/viewer.js.map +1 -1
  52. package/lib/periodic/core/categories.d.ts +25 -0
  53. package/lib/periodic/core/categories.d.ts.map +1 -0
  54. package/lib/periodic/core/categories.js +70 -0
  55. package/lib/periodic/core/categories.js.map +1 -0
  56. package/lib/periodic/core/elements.d.ts +49 -0
  57. package/lib/periodic/core/elements.d.ts.map +1 -0
  58. package/lib/periodic/core/elements.js +175 -0
  59. package/lib/periodic/core/elements.js.map +1 -0
  60. package/lib/periodic/core/index.d.ts +6 -0
  61. package/lib/periodic/core/index.d.ts.map +1 -0
  62. package/lib/periodic/core/index.js +4 -0
  63. package/lib/periodic/core/index.js.map +1 -0
  64. package/lib/periodic/core/layout.d.ts +73 -0
  65. package/lib/periodic/core/layout.d.ts.map +1 -0
  66. package/lib/periodic/core/layout.js +132 -0
  67. package/lib/periodic/core/layout.js.map +1 -0
  68. package/lib/periodic/ui/CategoryLegend.d.ts +26 -0
  69. package/lib/periodic/ui/CategoryLegend.d.ts.map +1 -0
  70. package/lib/periodic/ui/CategoryLegend.js +53 -0
  71. package/lib/periodic/ui/CategoryLegend.js.map +1 -0
  72. package/lib/periodic/ui/ElementCell.d.ts +53 -0
  73. package/lib/periodic/ui/ElementCell.d.ts.map +1 -0
  74. package/lib/periodic/ui/ElementCell.js +56 -0
  75. package/lib/periodic/ui/ElementCell.js.map +1 -0
  76. package/lib/periodic/ui/PeriodicTable.d.ts +90 -0
  77. package/lib/periodic/ui/PeriodicTable.d.ts.map +1 -0
  78. package/lib/periodic/ui/PeriodicTable.js +77 -0
  79. package/lib/periodic/ui/PeriodicTable.js.map +1 -0
  80. package/lib/periodic/ui/PeriodicTableChrome.d.ts +36 -0
  81. package/lib/periodic/ui/PeriodicTableChrome.d.ts.map +1 -0
  82. package/lib/periodic/ui/PeriodicTableChrome.js +72 -0
  83. package/lib/periodic/ui/PeriodicTableChrome.js.map +1 -0
  84. package/lib/periodic/ui/index.d.ts +7 -0
  85. package/lib/periodic/ui/index.d.ts.map +1 -0
  86. package/lib/periodic/ui/index.js +4 -0
  87. package/lib/periodic/ui/index.js.map +1 -0
  88. package/lib/seo/core/documentMeta.d.ts +2 -2
  89. package/lib/seo/core/documentMeta.js +4 -3
  90. package/lib/seo/core/documentMeta.js.map +1 -1
  91. package/lib/seo/core/index.d.ts +13 -4
  92. package/lib/seo/core/index.d.ts.map +1 -1
  93. package/lib/seo/core/index.js +8 -3
  94. package/lib/seo/core/index.js.map +1 -1
  95. package/lib/seo/core/noscript.d.ts +97 -0
  96. package/lib/seo/core/noscript.d.ts.map +1 -0
  97. package/lib/seo/core/noscript.js +93 -0
  98. package/lib/seo/core/noscript.js.map +1 -0
  99. package/lib/seo/core/pageMeta.d.ts +30 -14
  100. package/lib/seo/core/pageMeta.d.ts.map +1 -1
  101. package/lib/seo/core/pageMeta.js +40 -43
  102. package/lib/seo/core/pageMeta.js.map +1 -1
  103. package/lib/seo/core/robots.d.ts +55 -0
  104. package/lib/seo/core/robots.d.ts.map +1 -0
  105. package/lib/seo/core/robots.js +70 -0
  106. package/lib/seo/core/robots.js.map +1 -0
  107. package/lib/seo/core/routes.d.ts +73 -5
  108. package/lib/seo/core/routes.d.ts.map +1 -1
  109. package/lib/seo/core/routes.js +142 -16
  110. package/lib/seo/core/routes.js.map +1 -1
  111. package/lib/seo/core/siteFiles.d.ts +39 -43
  112. package/lib/seo/core/siteFiles.d.ts.map +1 -1
  113. package/lib/seo/core/siteFiles.js +53 -69
  114. package/lib/seo/core/siteFiles.js.map +1 -1
  115. package/lib/seo/core/startDocumentMeta.d.ts +44 -0
  116. package/lib/seo/core/startDocumentMeta.d.ts.map +1 -0
  117. package/lib/seo/core/startDocumentMeta.js +47 -0
  118. package/lib/seo/core/startDocumentMeta.js.map +1 -0
  119. package/lib/seo/core/structuredData.d.ts +48 -0
  120. package/lib/seo/core/structuredData.d.ts.map +1 -0
  121. package/lib/seo/core/structuredData.js +41 -0
  122. package/lib/seo/core/structuredData.js.map +1 -0
  123. package/lib/seo/core/template.d.ts +48 -0
  124. package/lib/seo/core/template.d.ts.map +1 -0
  125. package/lib/seo/core/template.js +53 -0
  126. package/lib/seo/core/template.js.map +1 -0
  127. package/lib/seo/vite/ogCard.d.ts +9 -1
  128. package/lib/seo/vite/ogCard.d.ts.map +1 -1
  129. package/lib/seo/vite/ogCard.js +14 -4
  130. package/lib/seo/vite/ogCard.js.map +1 -1
  131. package/lib/seo/vite/prerender.d.ts +38 -7
  132. package/lib/seo/vite/prerender.d.ts.map +1 -1
  133. package/lib/seo/vite/prerender.js +68 -30
  134. package/lib/seo/vite/prerender.js.map +1 -1
  135. package/lib/ui.d.ts +1 -0
  136. package/lib/ui.d.ts.map +1 -1
  137. package/lib/ui.js +1 -0
  138. package/lib/ui.js.map +1 -1
  139. package/package.json +2 -1
  140. package/src/citation/core/index.ts +1 -0
  141. package/src/citation/core/platformPaper.ts +32 -0
  142. package/src/color/core/index.ts +6 -1
  143. package/src/core.ts +1 -0
  144. package/src/ecosystem/core/sites.ts +16 -5
  145. package/src/ecosystem/ui/glyphs.tsx +19 -0
  146. package/src/orbital/core/index.ts +1 -1
  147. package/src/orbital/ui/AtomicOrbitalCanvas.tsx +9 -2
  148. package/src/orbital/ui/AtomicOrbitalViewer.tsx +5 -0
  149. package/src/orbital/ui/axesGeometry.ts +91 -0
  150. package/src/orbital/ui/camera.ts +9 -1
  151. package/src/orbital/ui/renderAxes.ts +190 -0
  152. package/src/orbital/ui/viewer.ts +32 -2
  153. package/src/periodic/core/categories.ts +83 -0
  154. package/src/periodic/core/elements.ts +217 -0
  155. package/src/periodic/core/index.ts +27 -0
  156. package/src/periodic/core/layout.ts +183 -0
  157. package/src/periodic/ui/CategoryLegend.tsx +105 -0
  158. package/src/periodic/ui/ElementCell.tsx +137 -0
  159. package/src/periodic/ui/PeriodicTable.tsx +226 -0
  160. package/src/periodic/ui/PeriodicTableChrome.tsx +170 -0
  161. package/src/periodic/ui/index.ts +6 -0
  162. package/src/seo/core/documentMeta.ts +5 -5
  163. package/src/seo/core/index.ts +19 -12
  164. package/src/seo/core/noscript.ts +195 -0
  165. package/src/seo/core/pageMeta.ts +54 -53
  166. package/src/seo/core/robots.ts +114 -0
  167. package/src/seo/core/routes.ts +181 -14
  168. package/src/seo/core/siteFiles.ts +58 -96
  169. package/src/seo/core/startDocumentMeta.ts +77 -0
  170. package/src/seo/core/structuredData.ts +80 -0
  171. package/src/seo/core/template.ts +54 -0
  172. package/src/seo/vite/ogCard.ts +15 -5
  173. package/src/seo/vite/prerender.ts +105 -58
  174. package/src/ui.ts +1 -0
@@ -0,0 +1,195 @@
1
+ /**
2
+ * A readable page for a visitor, or a crawler, with no JavaScript.
3
+ *
4
+ * The body of our sites is an empty root element, so this is the only crawl
5
+ * path through them that costs nothing to render — and it is honest: it says
6
+ * the tool needs JavaScript, and links the addresses it answers.
7
+ */
8
+
9
+ import { siteById, siteDisplayName } from '../../ecosystem/core/lookup.ts';
10
+ import type { EcosystemSite, SiteId } from '../../ecosystem/core/sites.ts';
11
+ import { ECOSYSTEM_SITES, siteUrl } from '../../ecosystem/core/sites.ts';
12
+ import { joinBasePath } from '../../router/core/basePath.ts';
13
+ import { escapeAttribute, escapeText } from '../../share/core/escape.ts';
14
+
15
+ import type { RouteMeta } from './routes.ts';
16
+ import type { SiteFilesOptions } from './siteFiles.ts';
17
+ import { mountPathOf, resolveSite } from './siteFiles.ts';
18
+
19
+ /** How the addresses of the site's own pages are written. */
20
+ export type NoscriptHrefs = 'absolute' | 'relative';
21
+
22
+ /** A page the block links, and the pages listed under it. */
23
+ export interface NoscriptRoute extends RouteMeta {
24
+ /**
25
+ * Pages listed under this one, as a list nested in its item — the sections of
26
+ * an exercise set under the set itself.
27
+ * @default undefined — the item carries no list
28
+ */
29
+ children?: readonly NoscriptRoute[];
30
+ }
31
+
32
+ /** Which of the family's other sites are listed, and how. */
33
+ export interface NoscriptEcosystem {
34
+ /**
35
+ * The sites listed, in the order they are named. The site writing the block
36
+ * is never one of them, whether or not it is named: the list is headed *Our
37
+ * other tools*.
38
+ * @default every other site in the family, in the family's own order
39
+ */
40
+ sites?: readonly SiteId[];
41
+ /**
42
+ * Whether each host is followed by ` — ` and the site's one-line tagline.
43
+ * @default true
44
+ */
45
+ taglines?: boolean;
46
+ }
47
+
48
+ /** The prose of the block, where the site says more than its record does. */
49
+ export interface NoscriptText {
50
+ /**
51
+ * The heading the block opens with.
52
+ * @default the site's display name
53
+ */
54
+ heading?: string;
55
+ /**
56
+ * The paragraph under it, taken as written. It is the one place a reader with
57
+ * no JavaScript is told what the tool is, so it may say more than the tagline
58
+ * — but it still has to say that the tool needs JavaScript.
59
+ * @default the tagline, followed by the sentence naming the requirement
60
+ */
61
+ intro?: string;
62
+ /**
63
+ * Whether the family's other sites are listed under the site's own pages, and
64
+ * which of them. A crawler that runs no script has no other path from one of
65
+ * our tools to the next, so a site that lists none leaves it with none.
66
+ * `true` lists every other site with its tagline; an object names the sites,
67
+ * or drops the taglines, or both.
68
+ * @default false
69
+ */
70
+ ecosystem?: boolean | NoscriptEcosystem;
71
+ /**
72
+ * How the site's own addresses are written. `'absolute'` writes them from the
73
+ * root of the host, under the mount the origin names. `'relative'` writes
74
+ * `./exercises` and `./`, which the page resolves against its own `<base>` —
75
+ * the only shape that works for an image whose mount is chosen at container
76
+ * startup, because it bakes no mount into the build at all. The `<base>` such
77
+ * a deployment stamps in ends with a slash, or a relative address resolves
78
+ * one directory too high.
79
+ * @default 'absolute'
80
+ */
81
+ hrefs?: NoscriptHrefs;
82
+ /**
83
+ * The pages the block links, when they are not the site's whole route table.
84
+ * A crawl path is a menu: a site whose table carries an entry per tutorial
85
+ * step lists the tutorial, not its hundred and thirty-seven steps.
86
+ * @default every route the site answers
87
+ */
88
+ routes?: readonly NoscriptRoute[];
89
+ }
90
+
91
+ /** What the block says, and which addresses it links. */
92
+ export interface NoscriptOptions
93
+ extends Omit<SiteFilesOptions, 'routes'>, Omit<NoscriptText, 'routes'> {
94
+ /** The addresses it links, each with the label it is linked under. */
95
+ routes: readonly NoscriptRoute[];
96
+ }
97
+
98
+ /**
99
+ * The `noscript` block, ready to put in the body.
100
+ *
101
+ * An absolute address is written under the mount the deployment named, so a
102
+ * build published as one tool among several on a shared host links its own
103
+ * pages rather than the root of the host it shares.
104
+ * @param options - The site, the pages it links, and the prose that opens the
105
+ * block.
106
+ * @returns The block.
107
+ * @throws {Error} When the deployment named an origin that is not an absolute
108
+ * address.
109
+ */
110
+ export function noscriptIndex(options: NoscriptOptions): string {
111
+ const site = resolveSite(options.site);
112
+ const hrefs = options.hrefs ?? 'absolute';
113
+ const mount = hrefs === 'absolute' ? mountPathOf(options) : '';
114
+ const heading = escapeText(options.heading ?? siteDisplayName(site));
115
+ const intro = escapeText(
116
+ options.intro ??
117
+ `${site.tagline} This tool needs JavaScript; these are the pages it offers:`,
118
+ );
119
+
120
+ return `<noscript>
121
+ <h1>${heading}</h1>
122
+ <p>${intro}</p>${pageList(options.routes, mount, hrefs, ' ')}${familyList(site.id, options.ecosystem)}
123
+ </noscript>`;
124
+ }
125
+
126
+ function pageList(
127
+ routes: readonly NoscriptRoute[],
128
+ mount: string,
129
+ hrefs: NoscriptHrefs,
130
+ indent: string,
131
+ ): string {
132
+ // A list with no item is not a list: `<ul>` holds at least one `<li>`.
133
+ if (routes.length === 0) return '';
134
+ const items = routes
135
+ .map((route) => pageItem(route, mount, hrefs, `${indent} `))
136
+ .join('\n');
137
+ return `\n${indent}<ul>\n${items}\n${indent}</ul>`;
138
+ }
139
+
140
+ function pageItem(
141
+ route: NoscriptRoute,
142
+ mount: string,
143
+ hrefs: NoscriptHrefs,
144
+ indent: string,
145
+ ): string {
146
+ const href = escapeAttribute(pageHref(route.path, mount, hrefs));
147
+ const label = escapeText(labelOf(route));
148
+ const note =
149
+ route.note === undefined || route.note.trim() === ''
150
+ ? ''
151
+ : ` — ${escapeText(route.note)}`;
152
+ const children = route.children ?? [];
153
+ const link = `<a href="${href}">${label}</a>${note}`;
154
+ if (children.length === 0) return `${indent}<li>${link}</li>`;
155
+ return `${indent}<li>${link}${pageList(children, mount, hrefs, `${indent} `)}
156
+ ${indent}</li>`;
157
+ }
158
+
159
+ function pageHref(path: string, mount: string, hrefs: NoscriptHrefs): string {
160
+ if (hrefs === 'absolute') return joinBasePath(mount, path);
161
+ return `./${path.startsWith('/') ? path.slice(1) : path}`;
162
+ }
163
+
164
+ function labelOf(route: NoscriptRoute): string {
165
+ const short = route.short;
166
+ return short !== undefined && short.trim() !== '' ? short : route.title;
167
+ }
168
+
169
+ function familyList(
170
+ current: SiteId,
171
+ ecosystem: boolean | NoscriptEcosystem | undefined,
172
+ ): string {
173
+ if (ecosystem === undefined || ecosystem === false) return '';
174
+ const listed = ecosystem === true ? {} : ecosystem;
175
+ const taglines = listed.taglines ?? true;
176
+ const items: string[] = [];
177
+ for (const site of familySites(listed.sites)) {
178
+ if (site.id === current) continue;
179
+ const tagline = taglines ? ` — ${escapeText(site.tagline)}` : '';
180
+ items.push(
181
+ ` <li><a href="${escapeAttribute(siteUrl(site))}">${escapeText(site.host)}</a>${tagline}</li>`,
182
+ );
183
+ }
184
+ if (items.length === 0) return '';
185
+ return `
186
+ <h2>Our other tools</h2>
187
+ <ul>
188
+ ${items.join('\n')}
189
+ </ul>`;
190
+ }
191
+
192
+ function familySites(sites: readonly SiteId[] | undefined): EcosystemSite[] {
193
+ if (sites === undefined) return [...ECOSYSTEM_SITES];
194
+ return sites.map((id) => siteById(id));
195
+ }
@@ -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
+ }