react-cheminfo 0.4.0 → 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.
- package/README.md +182 -34
- package/lib/ecosystem/core/index.d.ts +1 -1
- package/lib/ecosystem/core/index.d.ts.map +1 -1
- package/lib/ecosystem/core/index.js +1 -1
- package/lib/ecosystem/core/index.js.map +1 -1
- package/lib/ecosystem/core/lookup.d.ts +11 -0
- package/lib/ecosystem/core/lookup.d.ts.map +1 -1
- package/lib/ecosystem/core/lookup.js +15 -0
- package/lib/ecosystem/core/lookup.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 +14 -4
- 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 +4 -1
- package/lib/ecosystem/ui/glyphs.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/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 +15 -0
- package/lib/seo/core/index.d.ts.map +1 -1
- package/lib/seo/core/index.js +8 -0
- 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 +76 -0
- package/lib/seo/core/pageMeta.d.ts.map +1 -0
- package/lib/seo/core/pageMeta.js +88 -0
- package/lib/seo/core/pageMeta.js.map +1 -0
- 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 +120 -0
- package/lib/seo/core/routes.d.ts.map +1 -0
- package/lib/seo/core/routes.js +188 -0
- package/lib/seo/core/routes.js.map +1 -0
- package/lib/seo/core/siteFiles.d.ts +69 -0
- package/lib/seo/core/siteFiles.d.ts.map +1 -0
- package/lib/seo/core/siteFiles.js +84 -0
- package/lib/seo/core/siteFiles.js.map +1 -0
- 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/index.d.ts +5 -0
- package/lib/seo/vite/index.d.ts.map +1 -0
- package/lib/seo/vite/index.js +3 -0
- package/lib/seo/vite/index.js.map +1 -0
- package/lib/seo/vite/ogCard.d.ts +41 -0
- package/lib/seo/vite/ogCard.d.ts.map +1 -0
- package/lib/seo/vite/ogCard.js +82 -0
- package/lib/seo/vite/ogCard.js.map +1 -0
- package/lib/seo/vite/prerender.d.ts +89 -0
- package/lib/seo/vite/prerender.d.ts.map +1 -0
- package/lib/seo/vite/prerender.js +114 -0
- package/lib/seo/vite/prerender.js.map +1 -0
- package/lib/vite.d.ts +2 -0
- package/lib/vite.d.ts.map +1 -0
- package/lib/vite.js +2 -0
- package/lib/vite.js.map +1 -0
- package/package.json +7 -2
- package/src/ecosystem/core/index.ts +1 -1
- package/src/ecosystem/core/lookup.ts +16 -0
- package/src/ecosystem/core/sites.ts +16 -5
- package/src/ecosystem/ui/glyphs.tsx +20 -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/seo/core/documentMeta.ts +5 -5
- package/src/seo/core/index.ts +27 -0
- package/src/seo/core/noscript.ts +195 -0
- package/src/seo/core/pageMeta.ts +128 -0
- package/src/seo/core/robots.ts +114 -0
- package/src/seo/core/routes.ts +246 -0
- package/src/seo/core/siteFiles.ts +112 -0
- 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/index.ts +4 -0
- package/src/seo/vite/ogCard.ts +102 -0
- package/src/seo/vite/prerender.ts +203 -0
- package/src/vite.ts +1 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The 1200×630 card a link to a site unfurls into, as a page to screenshot.
|
|
3
|
+
*
|
|
4
|
+
* The card is the site's own mark, its two colours and its name, all read from
|
|
5
|
+
* its record — so it is generated rather than hand-drawn. A mark redrawn in the
|
|
6
|
+
* card is a mark that drifts from the one the site shows.
|
|
7
|
+
*
|
|
8
|
+
* React is reached for only when a card is actually drawn, and never at module
|
|
9
|
+
* load: this module sits in the same entry point as the prerender plugin, which
|
|
10
|
+
* a site must be able to import with none of the optional peers installed.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { siteById } from '../../ecosystem/core/lookup.ts';
|
|
14
|
+
import type { EcosystemSite, SiteId } from '../../ecosystem/core/sites.ts';
|
|
15
|
+
import { escapeText } from '../../share/core/escape.ts';
|
|
16
|
+
|
|
17
|
+
/** The width every card is drawn at. */
|
|
18
|
+
export const OG_WIDTH = 1200;
|
|
19
|
+
|
|
20
|
+
/** The height every card is drawn at. */
|
|
21
|
+
export const OG_HEIGHT = 630;
|
|
22
|
+
|
|
23
|
+
/** What the card says, beyond the site's own name and mark. */
|
|
24
|
+
export interface OgCardOptions {
|
|
25
|
+
/** The site, named or passed. */
|
|
26
|
+
site: EcosystemSite | SiteId;
|
|
27
|
+
/**
|
|
28
|
+
* The sentence under the name.
|
|
29
|
+
* @default the site's tagline
|
|
30
|
+
*/
|
|
31
|
+
description?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The card, as a standalone page.
|
|
36
|
+
*
|
|
37
|
+
* Screenshot it at {@link OG_WIDTH} × {@link OG_HEIGHT} — a headless browser is
|
|
38
|
+
* the only thing here that can rasterise it, and every site already has one for
|
|
39
|
+
* its end-to-end tests.
|
|
40
|
+
*
|
|
41
|
+
* Drawing the mark is the one thing here that needs React, so React is loaded
|
|
42
|
+
* on the call rather than by the module. The card is drawn by a build script
|
|
43
|
+
* that already awaits a browser, so the await costs it nothing.
|
|
44
|
+
* @param options - Which site, and what it says.
|
|
45
|
+
* @returns A complete HTML document.
|
|
46
|
+
*/
|
|
47
|
+
export async function ogCardHtml(options: OgCardOptions): Promise<string> {
|
|
48
|
+
const site =
|
|
49
|
+
typeof options.site === 'string' ? siteById(options.site) : options.site;
|
|
50
|
+
const description = options.description ?? site.tagline;
|
|
51
|
+
const [{ createElement }, { renderToStaticMarkup }, { SiteMark }] =
|
|
52
|
+
await Promise.all([
|
|
53
|
+
import('react'),
|
|
54
|
+
import('react-dom/server'),
|
|
55
|
+
import('../../ecosystem/ui/marks.tsx'),
|
|
56
|
+
]);
|
|
57
|
+
const mark = renderToStaticMarkup(
|
|
58
|
+
createElement(SiteMark, { site, size: 132, colors: 'literal' }),
|
|
59
|
+
);
|
|
60
|
+
const dot = site.name.dot === true ? '<span class="dot">.</span>' : '';
|
|
61
|
+
|
|
62
|
+
return `<!doctype html>
|
|
63
|
+
<html lang="en">
|
|
64
|
+
<head>
|
|
65
|
+
<meta charset="utf-8" />
|
|
66
|
+
<style>
|
|
67
|
+
* { box-sizing: border-box; margin: 0; }
|
|
68
|
+
body {
|
|
69
|
+
display: flex;
|
|
70
|
+
width: ${OG_WIDTH}px;
|
|
71
|
+
height: ${OG_HEIGHT}px;
|
|
72
|
+
flex-direction: column;
|
|
73
|
+
justify-content: center;
|
|
74
|
+
padding: 88px;
|
|
75
|
+
background: #ffffff;
|
|
76
|
+
color: #16202c;
|
|
77
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
|
|
78
|
+
Helvetica, Arial, sans-serif;
|
|
79
|
+
gap: 28px;
|
|
80
|
+
}
|
|
81
|
+
h1 { font-size: 76px; font-weight: 700; letter-spacing: -0.02em; }
|
|
82
|
+
.lead { color: ${site.brand}; }
|
|
83
|
+
.alt { color: ${site.brandAlt}; }
|
|
84
|
+
.dot { color: #8a96a3; }
|
|
85
|
+
p { max-width: 900px; color: #5b6875; font-size: 34px; line-height: 1.35; }
|
|
86
|
+
.rule {
|
|
87
|
+
width: 180px;
|
|
88
|
+
height: 10px;
|
|
89
|
+
border-radius: 5px;
|
|
90
|
+
background: ${site.brandAlt};
|
|
91
|
+
}
|
|
92
|
+
</style>
|
|
93
|
+
</head>
|
|
94
|
+
<body>
|
|
95
|
+
${mark}
|
|
96
|
+
<h1><span class="lead">${escapeText(site.name.lead)}</span>${dot}<span class="alt">${escapeText(site.name.alt)}</span></h1>
|
|
97
|
+
<div class="rule"></div>
|
|
98
|
+
<p>${escapeText(description)}</p>
|
|
99
|
+
</body>
|
|
100
|
+
</html>
|
|
101
|
+
`;
|
|
102
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write one real HTML file per routed address, and everything else a crawler
|
|
3
|
+
* fetches on its own.
|
|
4
|
+
*
|
|
5
|
+
* A site served by a static image has nothing to rewrite a head per request: a
|
|
6
|
+
* crawler gets whatever came off the wire. Without this every address carries
|
|
7
|
+
* the same title and a search engine folds the whole site into one result.
|
|
8
|
+
*
|
|
9
|
+
* The build's `index.html` is the template: it declares where its head and its
|
|
10
|
+
* crawl path go with `<!--cheminfo:head-->` and `<!--cheminfo:body-->`, and
|
|
11
|
+
* every file written here is filled from it. The dev server is filled the same
|
|
12
|
+
* way, from the home route, so what a developer opens is what the build ships.
|
|
13
|
+
*
|
|
14
|
+
* These files are also what makes the server's catch-all fallback unnecessary.
|
|
15
|
+
* Every address the tool answers is on disk, so an address that is *not* on
|
|
16
|
+
* disk is genuinely not a page and must 404 rather than serving the tool under
|
|
17
|
+
* a name it does not have.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
21
|
+
import { dirname, join, resolve } from 'node:path';
|
|
22
|
+
|
|
23
|
+
import type { Logger, Plugin } from 'vite';
|
|
24
|
+
|
|
25
|
+
import type { EcosystemSite, SiteId } from '../../ecosystem/core/sites.ts';
|
|
26
|
+
import type { NoscriptText } from '../core/noscript.ts';
|
|
27
|
+
import { noscriptIndex } from '../core/noscript.ts';
|
|
28
|
+
import { pageHeadTags } from '../core/pageMeta.ts';
|
|
29
|
+
import type { RobotsDisallow } from '../core/robots.ts';
|
|
30
|
+
import { robotsTxt } from '../core/robots.ts';
|
|
31
|
+
import type { RouteMeta } from '../core/routes.ts';
|
|
32
|
+
import { assertRoutes, homeRoute, trimTrailingSlash } from '../core/routes.ts';
|
|
33
|
+
import { sitemapXml } from '../core/siteFiles.ts';
|
|
34
|
+
import { structuredDataScript } from '../core/structuredData.ts';
|
|
35
|
+
import { PAGE_BODY_MARKER, PAGE_HEAD_MARKER, fill } from '../core/template.ts';
|
|
36
|
+
|
|
37
|
+
/** What the build needs to know to write the site's addresses. */
|
|
38
|
+
export interface PrerenderOptions {
|
|
39
|
+
/** The site, named or passed. */
|
|
40
|
+
site: EcosystemSite | SiteId;
|
|
41
|
+
/** Every address it answers, each with its title and description. */
|
|
42
|
+
routes: readonly RouteMeta[];
|
|
43
|
+
/**
|
|
44
|
+
* Where the site is served, mount path included. Every absolute address is
|
|
45
|
+
* built on it, and `robots.txt` and the `noscript` index write their paths
|
|
46
|
+
* under its mount. The files on disk are laid out from the build's own root
|
|
47
|
+
* either way: it is the server that puts them under the mount.
|
|
48
|
+
* @default `https://<the site's host>`
|
|
49
|
+
*/
|
|
50
|
+
origin?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Addresses `robots.txt` keeps out of the index, e.g. `/v1/`, each optionally
|
|
53
|
+
* with the sentence saying why. Set to `false` to write no `robots.txt` at
|
|
54
|
+
* all, for a site that ships its own.
|
|
55
|
+
* @default []
|
|
56
|
+
*/
|
|
57
|
+
robots?: false | ReadonlyArray<string | RobotsDisallow>;
|
|
58
|
+
/**
|
|
59
|
+
* The schema.org category of the structured-data block, or `false` to write
|
|
60
|
+
* none.
|
|
61
|
+
* @default 'EducationalApplication'
|
|
62
|
+
*/
|
|
63
|
+
category?: false | string;
|
|
64
|
+
/**
|
|
65
|
+
* What the tool needs to run, named in the structured-data block.
|
|
66
|
+
* @default 'Any modern browser'
|
|
67
|
+
*/
|
|
68
|
+
operatingSystem?: string;
|
|
69
|
+
/**
|
|
70
|
+
* What the tool does, in the structured-data block.
|
|
71
|
+
* @default the site's tagline
|
|
72
|
+
*/
|
|
73
|
+
description?: string;
|
|
74
|
+
/**
|
|
75
|
+
* What a browser has to offer for the tool to run.
|
|
76
|
+
* @default 'Requires JavaScript'
|
|
77
|
+
*/
|
|
78
|
+
browserRequirements?: string;
|
|
79
|
+
/**
|
|
80
|
+
* The currency the zero price is quoted in.
|
|
81
|
+
* @default 'EUR'
|
|
82
|
+
*/
|
|
83
|
+
currency?: string;
|
|
84
|
+
/**
|
|
85
|
+
* Whether the built page carries a `noscript` index of the addresses, what it
|
|
86
|
+
* says, which pages it lists and how it writes their addresses. It is the
|
|
87
|
+
* only crawl path through a site whose body is an empty root element, so
|
|
88
|
+
* `false` leaves one without any — and is also what lets a template ship no
|
|
89
|
+
* `<!--cheminfo:body-->` at all.
|
|
90
|
+
* @default true
|
|
91
|
+
*/
|
|
92
|
+
noscript?: boolean | NoscriptText;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Prerender every routed address of a cheminfo site.
|
|
97
|
+
* @param options - The site, its routes, and what a crawler is told.
|
|
98
|
+
* @returns The Vite plugin.
|
|
99
|
+
* @throws {Error} When the route table names an address twice or names one that
|
|
100
|
+
* is not a path, or when the origin is not an absolute address.
|
|
101
|
+
*/
|
|
102
|
+
export function cheminfoPrerender(options: PrerenderOptions): Plugin {
|
|
103
|
+
const { site, routes, origin, robots = [] } = options;
|
|
104
|
+
assertRoutes(routes);
|
|
105
|
+
const structuredData = structuredDataOf(options);
|
|
106
|
+
const crawlPath = crawlPathOf(options);
|
|
107
|
+
|
|
108
|
+
let out = 'dist';
|
|
109
|
+
let serve = false;
|
|
110
|
+
let logger: Logger | null = null;
|
|
111
|
+
|
|
112
|
+
const page = (template: string, url: string) => {
|
|
113
|
+
const head = fill(
|
|
114
|
+
template,
|
|
115
|
+
PAGE_HEAD_MARKER,
|
|
116
|
+
`${pageHeadTags({ site, routes, origin, url })}${structuredData}`,
|
|
117
|
+
);
|
|
118
|
+
return crawlPath === '' ? head : fill(head, PAGE_BODY_MARKER, crawlPath);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
name: 'cheminfo:prerender',
|
|
123
|
+
|
|
124
|
+
configResolved(config) {
|
|
125
|
+
serve = config.command === 'serve';
|
|
126
|
+
out = resolve(config.root, config.build.outDir);
|
|
127
|
+
logger = config.logger;
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
// A dev run has no build to prerender, so the page vite serves is filled
|
|
131
|
+
// from the home route rather than shipped with its markers showing.
|
|
132
|
+
transformIndexHtml: {
|
|
133
|
+
order: 'post',
|
|
134
|
+
handler: (html: string) =>
|
|
135
|
+
serve ? page(html, homeRoute(routes).path) : html,
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
closeBundle() {
|
|
139
|
+
if (serve) return;
|
|
140
|
+
const template = readFileSync(join(out, 'index.html'), 'utf8');
|
|
141
|
+
|
|
142
|
+
const write = (url: string, file: string) => {
|
|
143
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
144
|
+
writeFileSync(file, page(template, url));
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
let root = false;
|
|
148
|
+
for (const route of routes) {
|
|
149
|
+
const address = trimTrailingSlash(route.path);
|
|
150
|
+
if (address === '/') root = true;
|
|
151
|
+
write(
|
|
152
|
+
route.path,
|
|
153
|
+
address === '/'
|
|
154
|
+
? join(out, 'index.html')
|
|
155
|
+
: join(out, address.slice(1), 'index.html'),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
// The file a static server hands out for the mount itself. A table naming
|
|
159
|
+
// no root would otherwise leave the template vite built, and ship a site
|
|
160
|
+
// whose front page carries its markers instead of a head.
|
|
161
|
+
if (!root) write(homeRoute(routes).path, join(out, 'index.html'));
|
|
162
|
+
|
|
163
|
+
writeFileSync(
|
|
164
|
+
join(out, 'sitemap.xml'),
|
|
165
|
+
sitemapXml({ site, routes, origin }),
|
|
166
|
+
);
|
|
167
|
+
if (robots !== false) {
|
|
168
|
+
writeFileSync(
|
|
169
|
+
join(out, 'robots.txt'),
|
|
170
|
+
robotsTxt({ site, routes, origin }, robots),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
logger?.info(
|
|
175
|
+
`${routes.length} pages prerendered, and listed in sitemap.xml`,
|
|
176
|
+
);
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function structuredDataOf(options: PrerenderOptions): string {
|
|
182
|
+
const { category, ...rest } = options;
|
|
183
|
+
if (category === false) return '';
|
|
184
|
+
const { site, routes, origin } = rest;
|
|
185
|
+
const { operatingSystem, description, browserRequirements, currency } = rest;
|
|
186
|
+
return `\n${structuredDataScript({
|
|
187
|
+
site,
|
|
188
|
+
routes,
|
|
189
|
+
origin,
|
|
190
|
+
category,
|
|
191
|
+
operatingSystem,
|
|
192
|
+
description,
|
|
193
|
+
browserRequirements,
|
|
194
|
+
currency,
|
|
195
|
+
})}`;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function crawlPathOf(options: PrerenderOptions): string {
|
|
199
|
+
const { site, routes, origin, noscript = true } = options;
|
|
200
|
+
if (noscript === false) return '';
|
|
201
|
+
const { routes: listed, ...prose } = noscript === true ? {} : noscript;
|
|
202
|
+
return noscriptIndex({ site, origin, ...prose, routes: listed ?? routes });
|
|
203
|
+
}
|
package/src/vite.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './seo/vite/index.ts';
|