@charmland/gem-vite 0.1.0 → 0.1.1
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 +4 -0
- package/dist/html.d.ts +2 -0
- package/dist/index.js +179 -98
- package/dist/output.d.ts +16 -5
- package/package.json +5 -6
package/README.md
CHANGED
|
@@ -26,3 +26,7 @@ export default defineConfig({
|
|
|
26
26
|
Add `content/index.md`, then run `vite dev` or `vite build`. The package's public entrypoint is `@charmland/gem-vite`, which exports the `gem` plugin and `GemOptions`.
|
|
27
27
|
|
|
28
28
|
The default setup uses Gem's prebuilt theme and does not need the Elm compiler. Install and use Elm only when customizing and recompiling Gem's Elm sources. See the [Gem documentation](https://github.com/charmbracelet/gem#readme) for customization details.
|
|
29
|
+
|
|
30
|
+
## Build performance
|
|
31
|
+
|
|
32
|
+
`generateBundle` runs sequentially: it renders and emits the OG covers first, then prerenders every page in source order and writes its `.md` and HTML. Prerendering boots the real Elm theme in jsdom, which is CPU-bound, and the build prints one `[gem] build timings` line separating covers, prerendering, the 404 page and the total. Covers use Takumi's native renderer (`@takumi-rs/core`), loaded lazily and drawing each PNG in a single native pass with no headless browser or separate SVG rasterization step.
|
package/dist/html.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export interface DocumentHtmlOptions {
|
|
|
8
8
|
client?: boolean;
|
|
9
9
|
origin?: string | null;
|
|
10
10
|
ogImage?: boolean;
|
|
11
|
+
/** The deployment mount path (`"/"` or `"/sub/"`); prefixes head URLs. */
|
|
12
|
+
basePath?: string;
|
|
11
13
|
}
|
|
12
14
|
export declare function routeFile(route: string): string;
|
|
13
15
|
export declare function documentHtml(options: DocumentHtmlOptions): string;
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
import { existsSync as existsSync2, readFileSync as readFileSync3 } from "node:fs";
|
|
3
3
|
import { createRequire as createRequire2 } from "node:module";
|
|
4
4
|
import { extname, join as join2, relative, resolve, sep } from "node:path";
|
|
5
|
-
import {
|
|
5
|
+
import { performance } from "node:perf_hooks";
|
|
6
|
+
import { CONFIG_NAMES, compileSite, loadConfig, normalizeBase, parseFrontmatter } from "@charmland/gem-compiler";
|
|
6
7
|
|
|
7
8
|
// src/output.ts
|
|
8
9
|
function markdownFile(route) {
|
|
@@ -13,11 +14,28 @@ function markdownFile(route) {
|
|
|
13
14
|
function markdownRoute(route) {
|
|
14
15
|
return `/${markdownFile(route)}`;
|
|
15
16
|
}
|
|
16
|
-
function
|
|
17
|
+
function withBase(base, route) {
|
|
18
|
+
const prefix = base && base !== "/" ? base.replace(/\/+$/, "") : "";
|
|
19
|
+
const path = route === "/" || route === "" ? "/" : route.startsWith("/") ? route : `/${route}`;
|
|
20
|
+
if (prefix === "")
|
|
21
|
+
return path;
|
|
22
|
+
return path === "/" ? `${prefix}/` : `${prefix}${path}`;
|
|
23
|
+
}
|
|
24
|
+
function originOnly(raw) {
|
|
25
|
+
const value = raw.trim();
|
|
26
|
+
if (!value)
|
|
27
|
+
return null;
|
|
28
|
+
const withScheme = /^https?:\/\//i.test(value) ? value : `https://${value}`;
|
|
29
|
+
try {
|
|
30
|
+
return new URL(withScheme).origin;
|
|
31
|
+
} catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function absoluteUrl(origin, route, base = "/") {
|
|
17
36
|
if (!origin)
|
|
18
37
|
return null;
|
|
19
|
-
|
|
20
|
-
return `${origin.replace(/\/$/, "")}${path}`;
|
|
38
|
+
return `${origin.replace(/\/$/, "")}${withBase(base, route)}`;
|
|
21
39
|
}
|
|
22
40
|
function ogFile(route) {
|
|
23
41
|
if (route === "/" || route === "")
|
|
@@ -27,8 +45,8 @@ function ogFile(route) {
|
|
|
27
45
|
function ogRoute(route) {
|
|
28
46
|
return `/${ogFile(route)}`;
|
|
29
47
|
}
|
|
30
|
-
function sitemapXml(pages, origin) {
|
|
31
|
-
const entries = pages.map((page) => absoluteUrl(origin, page.route)).filter((url) => url !== null).map((url) => ` <url><loc>${escapeXml(url)}</loc></url>`).join(`
|
|
48
|
+
function sitemapXml(pages, origin, base = "/") {
|
|
49
|
+
const entries = pages.map((page) => absoluteUrl(origin, page.route, base)).filter((url) => url !== null).map((url) => ` <url><loc>${escapeXml(url)}</loc></url>`).join(`
|
|
32
50
|
`);
|
|
33
51
|
return [
|
|
34
52
|
`<?xml version="1.0" encoding="UTF-8"?>`,
|
|
@@ -39,15 +57,16 @@ function sitemapXml(pages, origin) {
|
|
|
39
57
|
].join(`
|
|
40
58
|
`);
|
|
41
59
|
}
|
|
42
|
-
function robotsTxt(origin) {
|
|
60
|
+
function robotsTxt(origin, base = "/") {
|
|
43
61
|
const lines = ["User-agent: *", "Allow: /"];
|
|
44
|
-
if (origin)
|
|
45
|
-
lines.push("", `Sitemap: ${origin.replace(/\/$/, "")}/sitemap.xml`);
|
|
62
|
+
if (origin) {
|
|
63
|
+
lines.push("", `Sitemap: ${origin.replace(/\/$/, "")}${withBase(base, "/sitemap.xml")}`);
|
|
64
|
+
}
|
|
46
65
|
return `${lines.join(`
|
|
47
66
|
`)}
|
|
48
67
|
`;
|
|
49
68
|
}
|
|
50
|
-
function llmsTxt(site, origin) {
|
|
69
|
+
function llmsTxt(site, origin, base = "/") {
|
|
51
70
|
const lines = [`# ${site.site.title}`, ""];
|
|
52
71
|
if (site.site.description)
|
|
53
72
|
lines.push(`> ${site.site.description}`, "");
|
|
@@ -55,7 +74,7 @@ function llmsTxt(site, origin) {
|
|
|
55
74
|
lines.push(`## ${doc.label}`, "");
|
|
56
75
|
for (const item of flatten(doc.nav)) {
|
|
57
76
|
const page = site.pages.find((candidate) => candidate.route === item.route);
|
|
58
|
-
const url = absoluteUrl(origin, item.route) ?? markdownRoute(item.route);
|
|
77
|
+
const url = absoluteUrl(origin, item.route, base) ?? withBase(base, markdownRoute(item.route));
|
|
59
78
|
const summary = page?.summary ? `: ${page.summary}` : "";
|
|
60
79
|
lines.push(`- [${item.title}](${url})${summary}`);
|
|
61
80
|
}
|
|
@@ -65,8 +84,8 @@ function llmsTxt(site, origin) {
|
|
|
65
84
|
`)}
|
|
66
85
|
`;
|
|
67
86
|
}
|
|
68
|
-
function jsonLd(site, page, origin) {
|
|
69
|
-
const url = page ? absoluteUrl(origin, page.route) : origin;
|
|
87
|
+
function jsonLd(site, page, origin, base = "/") {
|
|
88
|
+
const url = page ? absoluteUrl(origin, page.route, base) : base && base !== "/" ? absoluteUrl(origin, "/", base) : origin;
|
|
70
89
|
const data = page ? {
|
|
71
90
|
"@context": "https://schema.org",
|
|
72
91
|
"@type": "TechArticle",
|
|
@@ -129,14 +148,15 @@ function documentHtml(options) {
|
|
|
129
148
|
styles,
|
|
130
149
|
client = false,
|
|
131
150
|
origin = null,
|
|
132
|
-
ogImage = false
|
|
151
|
+
ogImage = false,
|
|
152
|
+
basePath = "/"
|
|
133
153
|
} = options;
|
|
134
154
|
const meta = site.site;
|
|
135
155
|
const landing = site.docs[0]?.root ?? "/";
|
|
136
156
|
const title = page && page.route !== landing ? `${page.title} - ${meta.title}` : meta.title;
|
|
137
157
|
const description = page && page.description || meta.description || "";
|
|
138
|
-
const canonical = page ? absoluteUrl(origin, page.route) : origin;
|
|
139
|
-
const image = ogImage && page ? absoluteUrl(origin, ogRoute(page.route)) : null;
|
|
158
|
+
const canonical = page ? absoluteUrl(origin, page.route, basePath) : basePath && basePath !== "/" ? absoluteUrl(origin, "/", basePath) : origin;
|
|
159
|
+
const image = ogImage && page ? absoluteUrl(origin, ogRoute(page.route), basePath) : null;
|
|
140
160
|
const keywords = page && page.tags.length ? page.tags.join(", ") : "";
|
|
141
161
|
const allowSystem = site.theme?.system ? "true" : "false";
|
|
142
162
|
const modeScript = `<script>(function(){try{` + `var p=localStorage.getItem("gem-theme"),s=${allowSystem};` + `if(p!=="light"&&p!=="dark"&&p!=="system")p="system";` + `var t=p==="system"?(!window.matchMedia||window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"):p;` + `var r=document.documentElement;r.setAttribute("data-theme",t);r.style.colorScheme=t` + `}catch(e){}})()</script>`;
|
|
@@ -160,11 +180,11 @@ function documentHtml(options) {
|
|
|
160
180
|
<meta name="twitter:title" content="${escapeAttribute(title)}" />
|
|
161
181
|
${description ? `<meta name="twitter:description" content="${escapeAttribute(description)}" />` : ""}
|
|
162
182
|
${image ? `<meta name="twitter:image" content="${escapeAttribute(image)}" />` : ""}
|
|
163
|
-
<script type="application/ld+json">${jsonLd(site, page, origin)}</script>
|
|
183
|
+
<script type="application/ld+json">${jsonLd(site, page, origin, basePath)}</script>
|
|
164
184
|
${modeScript}
|
|
165
185
|
${styles.map((href) => `<link rel="stylesheet" href="${escapeAttribute(href)}" />`).join(`
|
|
166
186
|
`)}
|
|
167
|
-
${client ? `<script type="module" src="
|
|
187
|
+
${client ? `<script type="module" src="${escapeAttribute(clientScript(basePath))}"></script>` : ""}
|
|
168
188
|
</head>
|
|
169
189
|
<body>${body}</body>
|
|
170
190
|
${scripts.map((src) => `<script type="module" src="${escapeAttribute(src)}"></script>`).join(`
|
|
@@ -178,15 +198,18 @@ function escapeHtml(value) {
|
|
|
178
198
|
function escapeAttribute(value) {
|
|
179
199
|
return escapeHtml(value).replace(/"/g, """);
|
|
180
200
|
}
|
|
201
|
+
function clientScript(basePath) {
|
|
202
|
+
const prefix = basePath === "/" || basePath === "" ? "" : basePath.replace(/\/$/, "");
|
|
203
|
+
return `${prefix}/@vite/client`;
|
|
204
|
+
}
|
|
181
205
|
|
|
182
206
|
// src/og.ts
|
|
183
207
|
import { readFileSync } from "node:fs";
|
|
184
208
|
import { createRequire } from "node:module";
|
|
185
209
|
import { join } from "node:path";
|
|
186
|
-
import { Resvg } from "@resvg/resvg-js";
|
|
187
|
-
import satori from "satori";
|
|
188
210
|
var WIDTH = 1200;
|
|
189
211
|
var HEIGHT = 630;
|
|
212
|
+
var FONT_FAMILY = "Fraktion";
|
|
190
213
|
function require_(root) {
|
|
191
214
|
return createRequire(join(root, "package.json"));
|
|
192
215
|
}
|
|
@@ -204,6 +227,19 @@ function loadFont(root) {
|
|
|
204
227
|
}
|
|
205
228
|
return fontData;
|
|
206
229
|
}
|
|
230
|
+
var renderer = null;
|
|
231
|
+
function loadRenderer(font) {
|
|
232
|
+
renderer ??= (async () => {
|
|
233
|
+
const { Renderer } = await import("@takumi-rs/core");
|
|
234
|
+
const instance = new Renderer;
|
|
235
|
+
await instance.registerFont({ name: FONT_FAMILY, data: font });
|
|
236
|
+
return instance;
|
|
237
|
+
})().catch((error) => {
|
|
238
|
+
renderer = null;
|
|
239
|
+
throw error;
|
|
240
|
+
});
|
|
241
|
+
return renderer;
|
|
242
|
+
}
|
|
207
243
|
async function renderOgImage(site, page, root) {
|
|
208
244
|
const font = loadFont(root);
|
|
209
245
|
if (!font)
|
|
@@ -212,71 +248,63 @@ async function renderOgImage(site, page, root) {
|
|
|
212
248
|
const title = page ? page.title : site.site.title;
|
|
213
249
|
const eyebrow = page ? site.site.title : site.site.description || site.site.title;
|
|
214
250
|
const node = {
|
|
215
|
-
type: "
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
color: "#a8a29e"
|
|
238
|
-
},
|
|
239
|
-
children: [
|
|
240
|
-
{
|
|
241
|
-
type: "div",
|
|
242
|
-
props: {
|
|
243
|
-
style: { width: "52px", height: "10px", background: accent, borderRadius: "5px" }
|
|
244
|
-
}
|
|
245
|
-
},
|
|
246
|
-
{ type: "div", props: { children: eyebrow } }
|
|
247
|
-
]
|
|
248
|
-
}
|
|
251
|
+
type: "container",
|
|
252
|
+
style: {
|
|
253
|
+
width: WIDTH,
|
|
254
|
+
height: HEIGHT,
|
|
255
|
+
display: "flex",
|
|
256
|
+
flexDirection: "column",
|
|
257
|
+
justifyContent: "space-between",
|
|
258
|
+
boxSizing: "border-box",
|
|
259
|
+
padding: "72px 80px",
|
|
260
|
+
backgroundColor: "#141414",
|
|
261
|
+
color: "#f5f0e8",
|
|
262
|
+
fontFamily: FONT_FAMILY
|
|
263
|
+
},
|
|
264
|
+
children: [
|
|
265
|
+
{
|
|
266
|
+
type: "container",
|
|
267
|
+
style: {
|
|
268
|
+
display: "flex",
|
|
269
|
+
alignItems: "center",
|
|
270
|
+
gap: "20px",
|
|
271
|
+
fontSize: 30,
|
|
272
|
+
color: "#a8a29e"
|
|
249
273
|
},
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
274
|
+
children: [
|
|
275
|
+
{
|
|
276
|
+
type: "container",
|
|
253
277
|
style: {
|
|
254
278
|
display: "flex",
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
279
|
+
width: "52px",
|
|
280
|
+
height: "10px",
|
|
281
|
+
backgroundColor: accent,
|
|
282
|
+
borderRadius: "5px"
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
{ type: "text", text: eyebrow }
|
|
286
|
+
]
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
type: "container",
|
|
290
|
+
style: {
|
|
291
|
+
display: "flex",
|
|
292
|
+
fontSize: title.length > 48 ? 62 : 76,
|
|
293
|
+
lineHeight: 1.1,
|
|
294
|
+
letterSpacing: "-0.02em"
|
|
261
295
|
},
|
|
262
|
-
{
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
296
|
+
children: [{ type: "text", text: title }]
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
type: "container",
|
|
300
|
+
style: { display: "flex", fontSize: 26, color: "#78716c" },
|
|
301
|
+
children: [{ type: "text", text: page ? page.route : "/" }]
|
|
302
|
+
}
|
|
303
|
+
]
|
|
271
304
|
};
|
|
272
305
|
try {
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
height: HEIGHT,
|
|
276
|
-
fonts: [{ name: "Fraktion", data: font, weight: 400, style: "normal" }]
|
|
277
|
-
});
|
|
278
|
-
const resvg = new Resvg(svg, { fitTo: { mode: "width", value: WIDTH } });
|
|
279
|
-
return resvg.render().asPng();
|
|
306
|
+
const takumi = await loadRenderer(font);
|
|
307
|
+
return await takumi.render(node, { width: WIDTH, height: HEIGHT, format: "png" });
|
|
280
308
|
} catch {
|
|
281
309
|
return null;
|
|
282
310
|
}
|
|
@@ -305,11 +333,12 @@ async function renderRoute(options) {
|
|
|
305
333
|
if (!/Not implemented/.test(error.message))
|
|
306
334
|
console.warn(`[gem] ${error.message}`);
|
|
307
335
|
});
|
|
336
|
+
const origin = originOnly(content.site.url) ?? "https://gem.local";
|
|
308
337
|
const dom = new JSDOM(html, {
|
|
309
338
|
runScripts: "dangerously",
|
|
310
339
|
pretendToBeVisual: true,
|
|
311
340
|
virtualConsole,
|
|
312
|
-
url: `${content.
|
|
341
|
+
url: `${origin}${withBase(content.base, route)}`
|
|
313
342
|
});
|
|
314
343
|
try {
|
|
315
344
|
dom.window.eval(source);
|
|
@@ -341,6 +370,7 @@ function gem(options = {}) {
|
|
|
341
370
|
let server = null;
|
|
342
371
|
let isBuild = false;
|
|
343
372
|
let base = "/";
|
|
373
|
+
let basePath = "/";
|
|
344
374
|
let outDir = "";
|
|
345
375
|
let publicDir = "";
|
|
346
376
|
let logger = console;
|
|
@@ -374,7 +404,7 @@ function gem(options = {}) {
|
|
|
374
404
|
return pending;
|
|
375
405
|
const at = ++revision;
|
|
376
406
|
pendingRevision = at;
|
|
377
|
-
pending = compileSite({ root, configFile: options.configFile, onWarn: warn }).then((document) => {
|
|
407
|
+
pending = compileSite({ root, configFile: options.configFile, base: basePath, onWarn: warn }).then((document) => {
|
|
378
408
|
if (at === revision)
|
|
379
409
|
site = document;
|
|
380
410
|
return document;
|
|
@@ -411,6 +441,7 @@ function gem(options = {}) {
|
|
|
411
441
|
root = resolved.root;
|
|
412
442
|
isBuild = resolved.command === "build";
|
|
413
443
|
base = resolved.base || "/";
|
|
444
|
+
basePath = normalizeBase(base);
|
|
414
445
|
outDir = resolve(root, resolved.build.outDir);
|
|
415
446
|
publicDir = resolved.publicDir;
|
|
416
447
|
logger = resolved.logger;
|
|
@@ -467,7 +498,13 @@ function gem(options = {}) {
|
|
|
467
498
|
server.middlewares.use((req, res, next) => {
|
|
468
499
|
if (req.method !== "GET" && req.method !== "HEAD")
|
|
469
500
|
return next();
|
|
470
|
-
const
|
|
501
|
+
const raw = req.url || "/";
|
|
502
|
+
const [rawPath, ...rawQuery] = raw.split("?");
|
|
503
|
+
const query = rawQuery.length ? `?${rawQuery.join("?")}` : "";
|
|
504
|
+
const stripped = stripBasePath(basePath, rawPath ?? "/");
|
|
505
|
+
if (stripped === null)
|
|
506
|
+
return next();
|
|
507
|
+
const path = stripped;
|
|
471
508
|
if (path.startsWith("/@") || path.startsWith("/node_modules"))
|
|
472
509
|
return next();
|
|
473
510
|
if (path.endsWith(".md")) {
|
|
@@ -491,10 +528,11 @@ function gem(options = {}) {
|
|
|
491
528
|
}
|
|
492
529
|
compile().then((content) => {
|
|
493
530
|
const route = normalizeRoute(path);
|
|
531
|
+
const documentBase = content.base;
|
|
494
532
|
const landing = landingPage(content);
|
|
495
533
|
if (route === "/" && landing && landing !== "/") {
|
|
496
534
|
res.statusCode = 302;
|
|
497
|
-
res.setHeader("Location", landing);
|
|
535
|
+
res.setHeader("Location", `${joinBase(documentBase, landing)}${query}`);
|
|
498
536
|
res.end();
|
|
499
537
|
return;
|
|
500
538
|
}
|
|
@@ -507,9 +545,10 @@ function gem(options = {}) {
|
|
|
507
545
|
site: content,
|
|
508
546
|
page,
|
|
509
547
|
body: "",
|
|
510
|
-
scripts: [
|
|
548
|
+
scripts: [joinBase(documentBase, `@id/${ENTRY}`)],
|
|
511
549
|
styles: [],
|
|
512
550
|
client: true,
|
|
551
|
+
basePath: documentBase,
|
|
513
552
|
origin
|
|
514
553
|
}));
|
|
515
554
|
}, next);
|
|
@@ -532,45 +571,62 @@ function gem(options = {}) {
|
|
|
532
571
|
}
|
|
533
572
|
const themePath = resolveTheme(root, options.theme);
|
|
534
573
|
const bundleSource = themePath ? loadThemeBundle(themePath) : null;
|
|
535
|
-
const
|
|
574
|
+
const documentBase = content.base;
|
|
575
|
+
const { origin, ignoredPath } = resolveOrigin(content.site.url);
|
|
576
|
+
if (ignoredPath) {
|
|
577
|
+
warn(`site.url path "${ignoredPath}" is ignored; deploy under a subpath with the Vite \`base\` option (currently "${documentBase}").`);
|
|
578
|
+
}
|
|
536
579
|
if (!origin) {
|
|
537
580
|
warn("seo: no site.url and no deploy env; canonical, sitemap and og:url are skipped.");
|
|
538
581
|
}
|
|
539
582
|
if (!bundleSource && options.prerender !== false) {
|
|
540
583
|
warn("prerender skipped: no theme bundle. Set `theme` or build @charmland/gem first.");
|
|
541
584
|
}
|
|
542
|
-
this.emitFile({ type: "asset", fileName: "robots.txt", source: robotsTxt(origin) });
|
|
543
|
-
this.emitFile({
|
|
585
|
+
this.emitFile({ type: "asset", fileName: "robots.txt", source: robotsTxt(origin, documentBase) });
|
|
586
|
+
this.emitFile({
|
|
587
|
+
type: "asset",
|
|
588
|
+
fileName: "llms.txt",
|
|
589
|
+
source: llmsTxt(content, origin, documentBase)
|
|
590
|
+
});
|
|
544
591
|
if (origin) {
|
|
545
592
|
this.emitFile({
|
|
546
593
|
type: "asset",
|
|
547
594
|
fileName: "sitemap.xml",
|
|
548
|
-
source: sitemapXml(content.pages, origin)
|
|
595
|
+
source: sitemapXml(content.pages, origin, documentBase)
|
|
549
596
|
});
|
|
550
597
|
}
|
|
598
|
+
const pages = content.pages;
|
|
599
|
+
const bundleText = typeof bundleSource === "string" ? bundleSource : "";
|
|
600
|
+
const canRender = bundleText !== "";
|
|
601
|
+
const startedAt = performance.now();
|
|
602
|
+
const timing = { covers: 0, renders: 0, notFound: 0 };
|
|
603
|
+
const coversStarted = performance.now();
|
|
551
604
|
const images = new Set;
|
|
552
|
-
for (const page of
|
|
605
|
+
for (const page of pages) {
|
|
553
606
|
const cover = await renderOgImage(content, page, root);
|
|
554
607
|
if (!cover)
|
|
555
608
|
continue;
|
|
556
609
|
images.add(page.route);
|
|
557
610
|
this.emitFile({ type: "asset", fileName: ogFile(page.route), source: cover });
|
|
558
611
|
}
|
|
559
|
-
|
|
612
|
+
timing.covers += performance.now() - coversStarted;
|
|
613
|
+
const rendersStarted = performance.now();
|
|
614
|
+
for (const page of pages) {
|
|
560
615
|
const markdownSource = readFileSync3(join2(root, page.source), "utf8");
|
|
561
616
|
this.emitFile({
|
|
562
617
|
type: "asset",
|
|
563
618
|
fileName: markdownFile(page.route),
|
|
564
619
|
source: parseFrontmatter(markdownSource).body
|
|
565
620
|
});
|
|
566
|
-
const rendered =
|
|
567
|
-
source:
|
|
621
|
+
const rendered = canRender ? await renderRoute({
|
|
622
|
+
source: bundleText,
|
|
568
623
|
html: documentHtml({
|
|
569
624
|
site: content,
|
|
570
625
|
page,
|
|
571
626
|
body: "",
|
|
572
627
|
scripts: [],
|
|
573
628
|
styles,
|
|
629
|
+
basePath: documentBase,
|
|
574
630
|
origin,
|
|
575
631
|
ogImage: images.has(page.route)
|
|
576
632
|
}),
|
|
@@ -590,25 +646,29 @@ function gem(options = {}) {
|
|
|
590
646
|
body: rendered,
|
|
591
647
|
scripts,
|
|
592
648
|
styles,
|
|
649
|
+
basePath: documentBase,
|
|
593
650
|
origin,
|
|
594
651
|
ogImage: images.has(page.route)
|
|
595
652
|
})
|
|
596
653
|
});
|
|
597
654
|
}
|
|
655
|
+
timing.renders += performance.now() - rendersStarted;
|
|
598
656
|
const landing = landingPage(content);
|
|
599
657
|
if (landing && landing !== "/") {
|
|
600
658
|
this.emitFile({ type: "asset", fileName: "index.html", source: landingRedirect(landing) });
|
|
601
659
|
}
|
|
660
|
+
const notFoundStarted = performance.now();
|
|
602
661
|
const notFoundHtml = documentHtml({
|
|
603
662
|
site: content,
|
|
604
663
|
page: null,
|
|
605
664
|
body: "",
|
|
606
665
|
scripts: [],
|
|
607
666
|
styles,
|
|
667
|
+
basePath: documentBase,
|
|
608
668
|
origin
|
|
609
669
|
});
|
|
610
|
-
const notFoundBody =
|
|
611
|
-
source:
|
|
670
|
+
const notFoundBody = canRender ? await renderRoute({
|
|
671
|
+
source: bundleText,
|
|
612
672
|
html: notFoundHtml,
|
|
613
673
|
content,
|
|
614
674
|
route: "/404",
|
|
@@ -623,9 +683,13 @@ function gem(options = {}) {
|
|
|
623
683
|
body: notFoundBody,
|
|
624
684
|
scripts,
|
|
625
685
|
styles,
|
|
686
|
+
basePath: documentBase,
|
|
626
687
|
origin
|
|
627
688
|
})
|
|
628
689
|
});
|
|
690
|
+
timing.notFound += performance.now() - notFoundStarted;
|
|
691
|
+
const total = performance.now() - startedAt;
|
|
692
|
+
logger.info(`[gem] build timings: ${pages.length} pages; covers ${(timing.covers / 1000).toFixed(1)}s; ` + `prerender ${(timing.renders / 1000).toFixed(1)}s; ` + `404 ${(timing.notFound / 1000).toFixed(1)}s; ` + `total ${(total / 1000).toFixed(1)}s`);
|
|
629
693
|
}
|
|
630
694
|
};
|
|
631
695
|
}
|
|
@@ -637,10 +701,17 @@ function landingPage(content) {
|
|
|
637
701
|
}
|
|
638
702
|
function resolveOrigin(configured) {
|
|
639
703
|
const raw = configured || process.env.GEM_URL || process.env.VERCEL_URL || process.env.CF_PAGES_URL || process.env.URL || "";
|
|
640
|
-
if (!raw)
|
|
641
|
-
return null;
|
|
642
|
-
const value = raw.
|
|
643
|
-
|
|
704
|
+
if (!raw.trim())
|
|
705
|
+
return { origin: null, ignoredPath: null };
|
|
706
|
+
const value = raw.trim();
|
|
707
|
+
const withScheme = /^https?:\/\//i.test(value) ? value : `https://${value}`;
|
|
708
|
+
try {
|
|
709
|
+
const url = new URL(withScheme);
|
|
710
|
+
const path = url.pathname;
|
|
711
|
+
return { origin: url.origin, ignoredPath: path && path !== "/" ? path : null };
|
|
712
|
+
} catch {
|
|
713
|
+
return { origin: originOnly(value), ignoredPath: null };
|
|
714
|
+
}
|
|
644
715
|
}
|
|
645
716
|
function entrySource(content, root, configuredEntry) {
|
|
646
717
|
const custom = configuredEntry || content.theme.entry;
|
|
@@ -681,6 +752,16 @@ function inDirectory(file, directory) {
|
|
|
681
752
|
function joinBase(base, file) {
|
|
682
753
|
return `${base.replace(/\/$/, "")}/${file.replace(/^\//, "")}`;
|
|
683
754
|
}
|
|
755
|
+
function stripBasePath(basePath, path) {
|
|
756
|
+
if (basePath === "/")
|
|
757
|
+
return path;
|
|
758
|
+
const prefix = basePath.replace(/\/$/, "");
|
|
759
|
+
if (path === prefix || path === basePath)
|
|
760
|
+
return "/";
|
|
761
|
+
if (path.startsWith(basePath))
|
|
762
|
+
return `/${path.slice(basePath.length)}`;
|
|
763
|
+
return null;
|
|
764
|
+
}
|
|
684
765
|
function errorMessage(error) {
|
|
685
766
|
return error instanceof Error ? error.message : String(error);
|
|
686
767
|
}
|
package/dist/output.d.ts
CHANGED
|
@@ -5,13 +5,24 @@ export interface OutputContext {
|
|
|
5
5
|
}
|
|
6
6
|
export declare function markdownFile(route: string): string;
|
|
7
7
|
export declare function markdownRoute(route: string): string;
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Mount a base-free route under the deployment base. `base` is `"/"` or
|
|
10
|
+
* `"/sub/"`; the route always starts with `"/"`. Filesystem helpers stay
|
|
11
|
+
* base-free and never call this.
|
|
12
|
+
*/
|
|
13
|
+
export declare function withBase(base: string, route: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* The origin of a configured URL, dropping any path. A deploy path belongs in
|
|
16
|
+
* Vite's `base`, not in `site.url`, so a path here is ignored by the caller.
|
|
17
|
+
*/
|
|
18
|
+
export declare function originOnly(raw: string): string | null;
|
|
19
|
+
export declare function absoluteUrl(origin: string | null, route: string, base?: string): string | null;
|
|
9
20
|
export declare function ogFile(route: string): string;
|
|
10
21
|
export declare function ogRoute(route: string): string;
|
|
11
|
-
export declare function sitemapXml(pages: Page[], origin: string | null): string;
|
|
12
|
-
export declare function robotsTxt(origin: string | null): string;
|
|
13
|
-
export declare function llmsTxt(site: SiteDocument, origin: string | null): string;
|
|
14
|
-
export declare function jsonLd(site: SiteDocument, page: Page | null, origin: string | null): string;
|
|
22
|
+
export declare function sitemapXml(pages: Page[], origin: string | null, base?: string): string;
|
|
23
|
+
export declare function robotsTxt(origin: string | null, base?: string): string;
|
|
24
|
+
export declare function llmsTxt(site: SiteDocument, origin: string | null, base?: string): string;
|
|
25
|
+
export declare function jsonLd(site: SiteDocument, page: Page | null, origin: string | null, base?: string): string;
|
|
15
26
|
/**
|
|
16
27
|
* A site whose landing page sits under its prefix still has to answer at the
|
|
17
28
|
* build root, both locally and under a mount. A relative redirect does that
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@charmland/gem-vite",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "The Vite plugin that turns a markdown directory into a prerendered docs site.",
|
|
5
5
|
"keywords": ["gem", "vite", "vite-plugin", "documentation", "prerender"],
|
|
6
6
|
"homepage": "https://github.com/charmbracelet/gem/tree/main/packages/vite",
|
|
@@ -39,13 +39,12 @@
|
|
|
39
39
|
"prepack": "bun run build"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@charmland/gem-compiler": "0.1.
|
|
43
|
-
"@
|
|
44
|
-
"jsdom": "^30.1.3"
|
|
45
|
-
"satori": "^0.63.2"
|
|
42
|
+
"@charmland/gem-compiler": "0.1.1",
|
|
43
|
+
"@takumi-rs/core": "^2.20.0",
|
|
44
|
+
"jsdom": "^30.1.3"
|
|
46
45
|
},
|
|
47
46
|
"peerDependencies": {
|
|
48
|
-
"@charmland/gem": "^0.1.
|
|
47
|
+
"@charmland/gem": "^0.1.1",
|
|
49
48
|
"vite": ">=6"
|
|
50
49
|
},
|
|
51
50
|
"peerDependenciesMeta": {
|