@charmland/gem-vite 0.0.0-stage → 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/LICENSE +177 -0
- package/NOTICE +13 -0
- package/README.md +31 -2
- package/dist/html.d.ts +15 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +770 -0
- package/dist/og.d.ts +2 -0
- package/dist/output.d.ts +31 -0
- package/dist/prerender.d.ts +14 -0
- package/package.json +53 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
package/NOTICE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2026 Charmbracelet, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @charmland/gem-vite
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Build prerendered documentation sites from Markdown with Vite and Elm-backed rendering.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install --save-dev @charmland/gem-vite @charmland/gem vite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The Vite integration requires Node `^22.22.2 || ^24.15.0 || >=26.0.0`, matching the prerenderer's jsdom engine floor.
|
|
12
|
+
|
|
13
|
+
The default integration uses `@charmland/gem`. It is an optional peer only for projects supplying both a fully custom entry and prerender theme.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { defineConfig } from "vite";
|
|
19
|
+
import { gem } from "@charmland/gem-vite";
|
|
20
|
+
|
|
21
|
+
export default defineConfig({
|
|
22
|
+
plugins: [gem()],
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
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
|
+
|
|
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
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Page, SiteDocument } from "@charmland/gem-compiler/types";
|
|
2
|
+
export interface DocumentHtmlOptions {
|
|
3
|
+
site: SiteDocument;
|
|
4
|
+
page: Page | null;
|
|
5
|
+
body: string;
|
|
6
|
+
scripts: string[];
|
|
7
|
+
styles: string[];
|
|
8
|
+
client?: boolean;
|
|
9
|
+
origin?: string | null;
|
|
10
|
+
ogImage?: boolean;
|
|
11
|
+
/** The deployment mount path (`"/"` or `"/sub/"`); prefixes head URLs. */
|
|
12
|
+
basePath?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function routeFile(route: string): string;
|
|
15
|
+
export declare function documentHtml(options: DocumentHtmlOptions): string;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Vite plugin: serves the compiled document and the generated client
|
|
3
|
+
* entry in dev, and writes one prerendered HTML file per route at build time.
|
|
4
|
+
*/
|
|
5
|
+
import type { Plugin } from "vite";
|
|
6
|
+
export interface GemOptions {
|
|
7
|
+
root?: string;
|
|
8
|
+
configFile?: string;
|
|
9
|
+
/** Replaces the generated default entry. */
|
|
10
|
+
entry?: string;
|
|
11
|
+
/** Bundle used to prerender; without it routes are written unrendered. */
|
|
12
|
+
theme?: string;
|
|
13
|
+
/** Elm module to boot while prerendering, default `Gem.Main`. */
|
|
14
|
+
themeModule?: string;
|
|
15
|
+
prerender?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare function gem(options?: GemOptions): Plugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { existsSync as existsSync2, readFileSync as readFileSync3 } from "node:fs";
|
|
3
|
+
import { createRequire as createRequire2 } from "node:module";
|
|
4
|
+
import { extname, join as join2, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { performance } from "node:perf_hooks";
|
|
6
|
+
import { CONFIG_NAMES, compileSite, loadConfig, normalizeBase, parseFrontmatter } from "@charmland/gem-compiler";
|
|
7
|
+
|
|
8
|
+
// src/output.ts
|
|
9
|
+
function markdownFile(route) {
|
|
10
|
+
if (route === "/" || route === "")
|
|
11
|
+
return "index.md";
|
|
12
|
+
return `${route.replace(/^\/+|\/+$/g, "")}.md`;
|
|
13
|
+
}
|
|
14
|
+
function markdownRoute(route) {
|
|
15
|
+
return `/${markdownFile(route)}`;
|
|
16
|
+
}
|
|
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 = "/") {
|
|
36
|
+
if (!origin)
|
|
37
|
+
return null;
|
|
38
|
+
return `${origin.replace(/\/$/, "")}${withBase(base, route)}`;
|
|
39
|
+
}
|
|
40
|
+
function ogFile(route) {
|
|
41
|
+
if (route === "/" || route === "")
|
|
42
|
+
return "og/index.png";
|
|
43
|
+
return `og/${route.replace(/^\/+|\/+$/g, "")}.png`;
|
|
44
|
+
}
|
|
45
|
+
function ogRoute(route) {
|
|
46
|
+
return `/${ogFile(route)}`;
|
|
47
|
+
}
|
|
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(`
|
|
50
|
+
`);
|
|
51
|
+
return [
|
|
52
|
+
`<?xml version="1.0" encoding="UTF-8"?>`,
|
|
53
|
+
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`,
|
|
54
|
+
entries,
|
|
55
|
+
`</urlset>`,
|
|
56
|
+
``
|
|
57
|
+
].join(`
|
|
58
|
+
`);
|
|
59
|
+
}
|
|
60
|
+
function robotsTxt(origin, base = "/") {
|
|
61
|
+
const lines = ["User-agent: *", "Allow: /"];
|
|
62
|
+
if (origin) {
|
|
63
|
+
lines.push("", `Sitemap: ${origin.replace(/\/$/, "")}${withBase(base, "/sitemap.xml")}`);
|
|
64
|
+
}
|
|
65
|
+
return `${lines.join(`
|
|
66
|
+
`)}
|
|
67
|
+
`;
|
|
68
|
+
}
|
|
69
|
+
function llmsTxt(site, origin, base = "/") {
|
|
70
|
+
const lines = [`# ${site.site.title}`, ""];
|
|
71
|
+
if (site.site.description)
|
|
72
|
+
lines.push(`> ${site.site.description}`, "");
|
|
73
|
+
for (const doc of site.docs) {
|
|
74
|
+
lines.push(`## ${doc.label}`, "");
|
|
75
|
+
for (const item of flatten(doc.nav)) {
|
|
76
|
+
const page = site.pages.find((candidate) => candidate.route === item.route);
|
|
77
|
+
const url = absoluteUrl(origin, item.route, base) ?? withBase(base, markdownRoute(item.route));
|
|
78
|
+
const summary = page?.summary ? `: ${page.summary}` : "";
|
|
79
|
+
lines.push(`- [${item.title}](${url})${summary}`);
|
|
80
|
+
}
|
|
81
|
+
lines.push("");
|
|
82
|
+
}
|
|
83
|
+
return `${lines.join(`
|
|
84
|
+
`)}
|
|
85
|
+
`;
|
|
86
|
+
}
|
|
87
|
+
function jsonLd(site, page, origin, base = "/") {
|
|
88
|
+
const url = page ? absoluteUrl(origin, page.route, base) : base && base !== "/" ? absoluteUrl(origin, "/", base) : origin;
|
|
89
|
+
const data = page ? {
|
|
90
|
+
"@context": "https://schema.org",
|
|
91
|
+
"@type": "TechArticle",
|
|
92
|
+
headline: page.title,
|
|
93
|
+
description: page.description || page.summary || site.site.description,
|
|
94
|
+
url: url ?? undefined,
|
|
95
|
+
isPartOf: { "@type": "WebSite", name: site.site.title }
|
|
96
|
+
} : {
|
|
97
|
+
"@context": "https://schema.org",
|
|
98
|
+
"@type": "WebSite",
|
|
99
|
+
name: site.site.title,
|
|
100
|
+
description: site.site.description,
|
|
101
|
+
url: url ?? undefined
|
|
102
|
+
};
|
|
103
|
+
return JSON.stringify(data).replace(/</g, "\\u003c");
|
|
104
|
+
}
|
|
105
|
+
function flatten(nav) {
|
|
106
|
+
const out = [];
|
|
107
|
+
for (const item of nav) {
|
|
108
|
+
if (item.kind === "page")
|
|
109
|
+
out.push({ route: item.route, title: item.title });
|
|
110
|
+
else
|
|
111
|
+
out.push(...flatten(item.items));
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
function escapeXml(value) {
|
|
116
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
117
|
+
}
|
|
118
|
+
function landingRedirect(route) {
|
|
119
|
+
const href = `.${route.replace(/\/$/, "")}/`;
|
|
120
|
+
return [
|
|
121
|
+
`<!doctype html>`,
|
|
122
|
+
`<html lang="en">`,
|
|
123
|
+
`<head>`,
|
|
124
|
+
`<meta charset="utf-8">`,
|
|
125
|
+
`<meta http-equiv="refresh" content="0; url=${href}">`,
|
|
126
|
+
`<link rel="canonical" href="${href}">`,
|
|
127
|
+
`<title>Redirecting…</title>`,
|
|
128
|
+
`</head>`,
|
|
129
|
+
`<body><a href="${href}">Continue</a></body>`,
|
|
130
|
+
`</html>`,
|
|
131
|
+
``
|
|
132
|
+
].join(`
|
|
133
|
+
`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/html.ts
|
|
137
|
+
function routeFile(route) {
|
|
138
|
+
if (route === "/" || route === "")
|
|
139
|
+
return "index.html";
|
|
140
|
+
return `${route.replace(/^\/+|\/+$/g, "")}/index.html`;
|
|
141
|
+
}
|
|
142
|
+
function documentHtml(options) {
|
|
143
|
+
const {
|
|
144
|
+
site,
|
|
145
|
+
page,
|
|
146
|
+
body,
|
|
147
|
+
scripts,
|
|
148
|
+
styles,
|
|
149
|
+
client = false,
|
|
150
|
+
origin = null,
|
|
151
|
+
ogImage = false,
|
|
152
|
+
basePath = "/"
|
|
153
|
+
} = options;
|
|
154
|
+
const meta = site.site;
|
|
155
|
+
const landing = site.docs[0]?.root ?? "/";
|
|
156
|
+
const title = page && page.route !== landing ? `${page.title} - ${meta.title}` : meta.title;
|
|
157
|
+
const description = page && page.description || meta.description || "";
|
|
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;
|
|
160
|
+
const keywords = page && page.tags.length ? page.tags.join(", ") : "";
|
|
161
|
+
const allowSystem = site.theme?.system ? "true" : "false";
|
|
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>`;
|
|
163
|
+
return `<!doctype html>
|
|
164
|
+
<html lang="${escapeAttribute(meta.lang || "en")}">
|
|
165
|
+
<head>
|
|
166
|
+
<meta charset="utf-8" />
|
|
167
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
168
|
+
<title>${escapeHtml(title)}</title>
|
|
169
|
+
${description ? `<meta name="description" content="${escapeAttribute(description)}" />` : ""}
|
|
170
|
+
${keywords ? `<meta name="keywords" content="${escapeAttribute(keywords)}" />` : ""}
|
|
171
|
+
<meta name="robots" content="index, follow" />
|
|
172
|
+
${canonical ? `<link rel="canonical" href="${escapeAttribute(canonical)}" />` : ""}
|
|
173
|
+
<meta property="og:type" content="article" />
|
|
174
|
+
<meta property="og:site_name" content="${escapeAttribute(meta.title)}" />
|
|
175
|
+
<meta property="og:title" content="${escapeAttribute(title)}" />
|
|
176
|
+
${description ? `<meta property="og:description" content="${escapeAttribute(description)}" />` : ""}
|
|
177
|
+
${canonical ? `<meta property="og:url" content="${escapeAttribute(canonical)}" />` : ""}
|
|
178
|
+
${image ? `<meta property="og:image" content="${escapeAttribute(image)}" />` : ""}
|
|
179
|
+
<meta name="twitter:card" content="${image ? "summary_large_image" : "summary"}" />
|
|
180
|
+
<meta name="twitter:title" content="${escapeAttribute(title)}" />
|
|
181
|
+
${description ? `<meta name="twitter:description" content="${escapeAttribute(description)}" />` : ""}
|
|
182
|
+
${image ? `<meta name="twitter:image" content="${escapeAttribute(image)}" />` : ""}
|
|
183
|
+
<script type="application/ld+json">${jsonLd(site, page, origin, basePath)}</script>
|
|
184
|
+
${modeScript}
|
|
185
|
+
${styles.map((href) => `<link rel="stylesheet" href="${escapeAttribute(href)}" />`).join(`
|
|
186
|
+
`)}
|
|
187
|
+
${client ? `<script type="module" src="${escapeAttribute(clientScript(basePath))}"></script>` : ""}
|
|
188
|
+
</head>
|
|
189
|
+
<body>${body}</body>
|
|
190
|
+
${scripts.map((src) => `<script type="module" src="${escapeAttribute(src)}"></script>`).join(`
|
|
191
|
+
`)}
|
|
192
|
+
</html>
|
|
193
|
+
`;
|
|
194
|
+
}
|
|
195
|
+
function escapeHtml(value) {
|
|
196
|
+
return String(value).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
197
|
+
}
|
|
198
|
+
function escapeAttribute(value) {
|
|
199
|
+
return escapeHtml(value).replace(/"/g, """);
|
|
200
|
+
}
|
|
201
|
+
function clientScript(basePath) {
|
|
202
|
+
const prefix = basePath === "/" || basePath === "" ? "" : basePath.replace(/\/$/, "");
|
|
203
|
+
return `${prefix}/@vite/client`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/og.ts
|
|
207
|
+
import { readFileSync } from "node:fs";
|
|
208
|
+
import { createRequire } from "node:module";
|
|
209
|
+
import { join } from "node:path";
|
|
210
|
+
var WIDTH = 1200;
|
|
211
|
+
var HEIGHT = 630;
|
|
212
|
+
var FONT_FAMILY = "Fraktion";
|
|
213
|
+
function require_(root) {
|
|
214
|
+
return createRequire(join(root, "package.json"));
|
|
215
|
+
}
|
|
216
|
+
var fontData = null;
|
|
217
|
+
var fontLoaded = false;
|
|
218
|
+
function loadFont(root) {
|
|
219
|
+
if (fontLoaded)
|
|
220
|
+
return fontData;
|
|
221
|
+
fontLoaded = true;
|
|
222
|
+
try {
|
|
223
|
+
const path = require_(root).resolve("@charmland/gem/fonts/PPFraktionSans-Variable.ttf");
|
|
224
|
+
fontData = readFileSync(path);
|
|
225
|
+
} catch {
|
|
226
|
+
fontData = null;
|
|
227
|
+
}
|
|
228
|
+
return fontData;
|
|
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
|
+
}
|
|
243
|
+
async function renderOgImage(site, page, root) {
|
|
244
|
+
const font = loadFont(root);
|
|
245
|
+
if (!font)
|
|
246
|
+
return null;
|
|
247
|
+
const accent = site.theme.accent || "#b8461f";
|
|
248
|
+
const title = page ? page.title : site.site.title;
|
|
249
|
+
const eyebrow = page ? site.site.title : site.site.description || site.site.title;
|
|
250
|
+
const node = {
|
|
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"
|
|
273
|
+
},
|
|
274
|
+
children: [
|
|
275
|
+
{
|
|
276
|
+
type: "container",
|
|
277
|
+
style: {
|
|
278
|
+
display: "flex",
|
|
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"
|
|
295
|
+
},
|
|
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
|
+
]
|
|
304
|
+
};
|
|
305
|
+
try {
|
|
306
|
+
const takumi = await loadRenderer(font);
|
|
307
|
+
return await takumi.render(node, { width: WIDTH, height: HEIGHT, format: "png" });
|
|
308
|
+
} catch {
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// src/prerender.ts
|
|
314
|
+
import { existsSync, readFileSync as readFileSync2 } from "node:fs";
|
|
315
|
+
var cached = null;
|
|
316
|
+
function resolveModule(elm, path) {
|
|
317
|
+
return path.split(".").reduce((scope, part) => scope && typeof scope === "object" ? scope[part] : null, elm);
|
|
318
|
+
}
|
|
319
|
+
function loadThemeBundle(path) {
|
|
320
|
+
if (cached && cached.path === path)
|
|
321
|
+
return cached.source;
|
|
322
|
+
if (!existsSync(path))
|
|
323
|
+
return null;
|
|
324
|
+
const source = readFileSync2(path, "utf8");
|
|
325
|
+
cached = { path, source };
|
|
326
|
+
return source;
|
|
327
|
+
}
|
|
328
|
+
async function renderRoute(options) {
|
|
329
|
+
const { JSDOM, VirtualConsole } = await import("jsdom");
|
|
330
|
+
const { source, html, content, route, module: moduleName = "Gem.Main" } = options;
|
|
331
|
+
const virtualConsole = new VirtualConsole;
|
|
332
|
+
virtualConsole.on("jsdomError", (error) => {
|
|
333
|
+
if (!/Not implemented/.test(error.message))
|
|
334
|
+
console.warn(`[gem] ${error.message}`);
|
|
335
|
+
});
|
|
336
|
+
const origin = originOnly(content.site.url) ?? "https://gem.local";
|
|
337
|
+
const dom = new JSDOM(html, {
|
|
338
|
+
runScripts: "dangerously",
|
|
339
|
+
pretendToBeVisual: true,
|
|
340
|
+
virtualConsole,
|
|
341
|
+
url: `${origin}${withBase(content.base, route)}`
|
|
342
|
+
});
|
|
343
|
+
try {
|
|
344
|
+
dom.window.eval(source);
|
|
345
|
+
const elm = dom.window.Elm;
|
|
346
|
+
const theme = resolveModule(elm, moduleName);
|
|
347
|
+
if (!theme) {
|
|
348
|
+
throw new Error(`theme bundle did not expose Elm.${moduleName}`);
|
|
349
|
+
}
|
|
350
|
+
theme.init({ flags: content, node: dom.window.document.body });
|
|
351
|
+
await new Promise((resolve) => dom.window.requestAnimationFrame(() => resolve()));
|
|
352
|
+
return dom.window.document.body.innerHTML;
|
|
353
|
+
} finally {
|
|
354
|
+
dom.window.close();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// src/index.ts
|
|
359
|
+
var CONTENT = "virtual:gem/content";
|
|
360
|
+
var ENTRY = "virtual:gem/entry";
|
|
361
|
+
var PROJECT_ASSET = /\.(elm|ts|tsx|js|jsx|mjs|mts|css|scss|sass|html|svg|png|jpe?g|gif|webp|avif|ico|woff2?|ttf|otf|txt|xml|webmanifest|pdf)$/i;
|
|
362
|
+
var GENERATED = /(^|[\\/])(node_modules|\.git|elm-stuff|\.cache)([\\/]|$)/;
|
|
363
|
+
function gem(options = {}) {
|
|
364
|
+
let root = process.cwd();
|
|
365
|
+
let site = null;
|
|
366
|
+
let scope = { content: [], config: [], theme: [] };
|
|
367
|
+
let revision = 0;
|
|
368
|
+
let pending = null;
|
|
369
|
+
let pendingRevision = -1;
|
|
370
|
+
let server = null;
|
|
371
|
+
let isBuild = false;
|
|
372
|
+
let base = "/";
|
|
373
|
+
let basePath = "/";
|
|
374
|
+
let outDir = "";
|
|
375
|
+
let publicDir = "";
|
|
376
|
+
let logger = console;
|
|
377
|
+
const warnings = new Set;
|
|
378
|
+
const warn = (message) => {
|
|
379
|
+
if (warnings.has(message))
|
|
380
|
+
return;
|
|
381
|
+
warnings.add(message);
|
|
382
|
+
logger.warn(`[gem] ${message}`);
|
|
383
|
+
};
|
|
384
|
+
const readScope = () => {
|
|
385
|
+
const loaded = loadConfig(root, { file: options.configFile });
|
|
386
|
+
const config = loaded.config;
|
|
387
|
+
scope = {
|
|
388
|
+
content: config.docs.map((doc) => resolve(root, doc.dir)),
|
|
389
|
+
config: [...new Set([...CONFIG_NAMES.map((name) => join2(root, name)), loaded.file ?? ""])].filter((path) => path !== ""),
|
|
390
|
+
theme: [options.entry, config.theme.entry, config.theme.css].filter((path) => Boolean(path)).map((path) => resolve(root, path))
|
|
391
|
+
};
|
|
392
|
+
};
|
|
393
|
+
const watchScope = () => {
|
|
394
|
+
if (!server)
|
|
395
|
+
return;
|
|
396
|
+
for (const path of [...scope.content, ...scope.config, ...scope.theme])
|
|
397
|
+
server.watcher.add(path);
|
|
398
|
+
};
|
|
399
|
+
const generated = (file) => outDir !== "" && inDirectory(file, outDir) || GENERATED.test(file);
|
|
400
|
+
const compile = (force = false) => {
|
|
401
|
+
if (site && !force)
|
|
402
|
+
return Promise.resolve(site);
|
|
403
|
+
if (pending && pendingRevision === revision)
|
|
404
|
+
return pending;
|
|
405
|
+
const at = ++revision;
|
|
406
|
+
pendingRevision = at;
|
|
407
|
+
pending = compileSite({ root, configFile: options.configFile, base: basePath, onWarn: warn }).then((document) => {
|
|
408
|
+
if (at === revision)
|
|
409
|
+
site = document;
|
|
410
|
+
return document;
|
|
411
|
+
}).finally(() => {
|
|
412
|
+
if (pendingRevision === at)
|
|
413
|
+
pending = null;
|
|
414
|
+
});
|
|
415
|
+
return pending;
|
|
416
|
+
};
|
|
417
|
+
const invalidate = () => {
|
|
418
|
+
site = null;
|
|
419
|
+
revision++;
|
|
420
|
+
};
|
|
421
|
+
const reload = (environment, file) => {
|
|
422
|
+
logger.info(`[gem] page reload ${relative(root, file)}`, { clear: true, timestamp: true });
|
|
423
|
+
environment.hot.send({ type: "full-reload" });
|
|
424
|
+
};
|
|
425
|
+
return {
|
|
426
|
+
name: "gem",
|
|
427
|
+
enforce: "pre",
|
|
428
|
+
config(userConfig) {
|
|
429
|
+
if (options.root)
|
|
430
|
+
root = resolve(options.root);
|
|
431
|
+
if (userConfig.root)
|
|
432
|
+
root = resolve(userConfig.root);
|
|
433
|
+
if (existsSync2(join2(root, "index.html")))
|
|
434
|
+
return;
|
|
435
|
+
return {
|
|
436
|
+
appType: "custom",
|
|
437
|
+
build: { rollupOptions: { input: ENTRY } }
|
|
438
|
+
};
|
|
439
|
+
},
|
|
440
|
+
configResolved(resolved) {
|
|
441
|
+
root = resolved.root;
|
|
442
|
+
isBuild = resolved.command === "build";
|
|
443
|
+
base = resolved.base || "/";
|
|
444
|
+
basePath = normalizeBase(base);
|
|
445
|
+
outDir = resolve(root, resolved.build.outDir);
|
|
446
|
+
publicDir = resolved.publicDir;
|
|
447
|
+
logger = resolved.logger;
|
|
448
|
+
},
|
|
449
|
+
hotUpdate({ file, modules, timestamp }) {
|
|
450
|
+
if (this.environment.name !== "client")
|
|
451
|
+
return;
|
|
452
|
+
if (generated(file))
|
|
453
|
+
return [];
|
|
454
|
+
const changed = this.environment;
|
|
455
|
+
const isConfig = scope.config.includes(file);
|
|
456
|
+
const isContent = scope.content.some((dir) => inDirectory(file, dir));
|
|
457
|
+
if (isConfig || isContent) {
|
|
458
|
+
if (isConfig) {
|
|
459
|
+
readScope();
|
|
460
|
+
watchScope();
|
|
461
|
+
}
|
|
462
|
+
invalidate();
|
|
463
|
+
warnings.clear();
|
|
464
|
+
for (const id of [CONTENT, ENTRY]) {
|
|
465
|
+
const module = changed.moduleGraph.getModuleById(`\x00${id}`);
|
|
466
|
+
if (module)
|
|
467
|
+
changed.moduleGraph.invalidateModule(module, undefined, timestamp, true);
|
|
468
|
+
}
|
|
469
|
+
reload(changed, file);
|
|
470
|
+
return [];
|
|
471
|
+
}
|
|
472
|
+
if (modules.length > 0)
|
|
473
|
+
return;
|
|
474
|
+
const isPublic = publicDir !== "" && inDirectory(file, publicDir);
|
|
475
|
+
if (!isPublic && !PROJECT_ASSET.test(file))
|
|
476
|
+
return;
|
|
477
|
+
reload(changed, file);
|
|
478
|
+
return [];
|
|
479
|
+
},
|
|
480
|
+
resolveId(id) {
|
|
481
|
+
if (id === CONTENT || id === ENTRY)
|
|
482
|
+
return `\x00${id}`;
|
|
483
|
+
return null;
|
|
484
|
+
},
|
|
485
|
+
async load(id) {
|
|
486
|
+
if (id === `\x00${CONTENT}`) {
|
|
487
|
+
return `export default ${JSON.stringify(await compile())};`;
|
|
488
|
+
}
|
|
489
|
+
if (id === `\x00${ENTRY}`) {
|
|
490
|
+
return entrySource(await compile(), root, options.entry);
|
|
491
|
+
}
|
|
492
|
+
return null;
|
|
493
|
+
},
|
|
494
|
+
configureServer(devServer) {
|
|
495
|
+
server = devServer;
|
|
496
|
+
readScope();
|
|
497
|
+
watchScope();
|
|
498
|
+
server.middlewares.use((req, res, next) => {
|
|
499
|
+
if (req.method !== "GET" && req.method !== "HEAD")
|
|
500
|
+
return next();
|
|
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;
|
|
508
|
+
if (path.startsWith("/@") || path.startsWith("/node_modules"))
|
|
509
|
+
return next();
|
|
510
|
+
if (path.endsWith(".md")) {
|
|
511
|
+
compile().then((content) => {
|
|
512
|
+
const route = normalizeRoute(path.replace(/\.md$/, "").replace(/\/index$/, "") || "/");
|
|
513
|
+
const page = content.pages.find((item) => item.route === route);
|
|
514
|
+
if (!page) {
|
|
515
|
+
res.statusCode = 404;
|
|
516
|
+
res.end("not found");
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
res.setHeader("Content-Type", "text/markdown; charset=utf-8");
|
|
520
|
+
res.end(parseFrontmatter(readFileSync3(join2(root, page.source), "utf8")).body);
|
|
521
|
+
});
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (extname(path))
|
|
525
|
+
return next();
|
|
526
|
+
if (path.startsWith("/src") || path.startsWith("/public") || path.startsWith("/assets")) {
|
|
527
|
+
return next();
|
|
528
|
+
}
|
|
529
|
+
compile().then((content) => {
|
|
530
|
+
const route = normalizeRoute(path);
|
|
531
|
+
const documentBase = content.base;
|
|
532
|
+
const landing = landingPage(content);
|
|
533
|
+
if (route === "/" && landing && landing !== "/") {
|
|
534
|
+
res.statusCode = 302;
|
|
535
|
+
res.setHeader("Location", `${joinBase(documentBase, landing)}${query}`);
|
|
536
|
+
res.end();
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
const page = content.pages.find((item) => item.route === route) || null;
|
|
540
|
+
const host = req.headers.host;
|
|
541
|
+
const origin = host ? `${host.startsWith("localhost") || host.startsWith("127.") ? "http" : "https"}://${host}` : null;
|
|
542
|
+
res.statusCode = page ? 200 : 404;
|
|
543
|
+
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
544
|
+
res.end(documentHtml({
|
|
545
|
+
site: content,
|
|
546
|
+
page,
|
|
547
|
+
body: "",
|
|
548
|
+
scripts: [joinBase(documentBase, `@id/${ENTRY}`)],
|
|
549
|
+
styles: [],
|
|
550
|
+
client: true,
|
|
551
|
+
basePath: documentBase,
|
|
552
|
+
origin
|
|
553
|
+
}));
|
|
554
|
+
}, next);
|
|
555
|
+
});
|
|
556
|
+
},
|
|
557
|
+
async buildStart() {
|
|
558
|
+
await compile(true);
|
|
559
|
+
},
|
|
560
|
+
async generateBundle(_outputOptions, bundle) {
|
|
561
|
+
if (!isBuild)
|
|
562
|
+
return;
|
|
563
|
+
const content = await compile();
|
|
564
|
+
const scripts = [];
|
|
565
|
+
const styles = [];
|
|
566
|
+
for (const [file, asset] of Object.entries(bundle)) {
|
|
567
|
+
if (asset.type === "chunk" && asset.isEntry)
|
|
568
|
+
scripts.push(joinBase(base, file));
|
|
569
|
+
if (asset.type === "asset" && file.endsWith(".css"))
|
|
570
|
+
styles.push(joinBase(base, file));
|
|
571
|
+
}
|
|
572
|
+
const themePath = resolveTheme(root, options.theme);
|
|
573
|
+
const bundleSource = themePath ? loadThemeBundle(themePath) : null;
|
|
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
|
+
}
|
|
579
|
+
if (!origin) {
|
|
580
|
+
warn("seo: no site.url and no deploy env; canonical, sitemap and og:url are skipped.");
|
|
581
|
+
}
|
|
582
|
+
if (!bundleSource && options.prerender !== false) {
|
|
583
|
+
warn("prerender skipped: no theme bundle. Set `theme` or build @charmland/gem first.");
|
|
584
|
+
}
|
|
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
|
+
});
|
|
591
|
+
if (origin) {
|
|
592
|
+
this.emitFile({
|
|
593
|
+
type: "asset",
|
|
594
|
+
fileName: "sitemap.xml",
|
|
595
|
+
source: sitemapXml(content.pages, origin, documentBase)
|
|
596
|
+
});
|
|
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();
|
|
604
|
+
const images = new Set;
|
|
605
|
+
for (const page of pages) {
|
|
606
|
+
const cover = await renderOgImage(content, page, root);
|
|
607
|
+
if (!cover)
|
|
608
|
+
continue;
|
|
609
|
+
images.add(page.route);
|
|
610
|
+
this.emitFile({ type: "asset", fileName: ogFile(page.route), source: cover });
|
|
611
|
+
}
|
|
612
|
+
timing.covers += performance.now() - coversStarted;
|
|
613
|
+
const rendersStarted = performance.now();
|
|
614
|
+
for (const page of pages) {
|
|
615
|
+
const markdownSource = readFileSync3(join2(root, page.source), "utf8");
|
|
616
|
+
this.emitFile({
|
|
617
|
+
type: "asset",
|
|
618
|
+
fileName: markdownFile(page.route),
|
|
619
|
+
source: parseFrontmatter(markdownSource).body
|
|
620
|
+
});
|
|
621
|
+
const rendered = canRender ? await renderRoute({
|
|
622
|
+
source: bundleText,
|
|
623
|
+
html: documentHtml({
|
|
624
|
+
site: content,
|
|
625
|
+
page,
|
|
626
|
+
body: "",
|
|
627
|
+
scripts: [],
|
|
628
|
+
styles,
|
|
629
|
+
basePath: documentBase,
|
|
630
|
+
origin,
|
|
631
|
+
ogImage: images.has(page.route)
|
|
632
|
+
}),
|
|
633
|
+
content,
|
|
634
|
+
route: page.route,
|
|
635
|
+
module: options.themeModule || "Gem.Main"
|
|
636
|
+
}).catch((error) => {
|
|
637
|
+
warn(`prerender ${page.route}: ${errorMessage(error)}`);
|
|
638
|
+
return "";
|
|
639
|
+
}) : "";
|
|
640
|
+
this.emitFile({
|
|
641
|
+
type: "asset",
|
|
642
|
+
fileName: routeFile(page.route),
|
|
643
|
+
source: documentHtml({
|
|
644
|
+
site: content,
|
|
645
|
+
page,
|
|
646
|
+
body: rendered,
|
|
647
|
+
scripts,
|
|
648
|
+
styles,
|
|
649
|
+
basePath: documentBase,
|
|
650
|
+
origin,
|
|
651
|
+
ogImage: images.has(page.route)
|
|
652
|
+
})
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
timing.renders += performance.now() - rendersStarted;
|
|
656
|
+
const landing = landingPage(content);
|
|
657
|
+
if (landing && landing !== "/") {
|
|
658
|
+
this.emitFile({ type: "asset", fileName: "index.html", source: landingRedirect(landing) });
|
|
659
|
+
}
|
|
660
|
+
const notFoundStarted = performance.now();
|
|
661
|
+
const notFoundHtml = documentHtml({
|
|
662
|
+
site: content,
|
|
663
|
+
page: null,
|
|
664
|
+
body: "",
|
|
665
|
+
scripts: [],
|
|
666
|
+
styles,
|
|
667
|
+
basePath: documentBase,
|
|
668
|
+
origin
|
|
669
|
+
});
|
|
670
|
+
const notFoundBody = canRender ? await renderRoute({
|
|
671
|
+
source: bundleText,
|
|
672
|
+
html: notFoundHtml,
|
|
673
|
+
content,
|
|
674
|
+
route: "/404",
|
|
675
|
+
module: options.themeModule || "Gem.Main"
|
|
676
|
+
}).catch(() => "") : "";
|
|
677
|
+
this.emitFile({
|
|
678
|
+
type: "asset",
|
|
679
|
+
fileName: "404.html",
|
|
680
|
+
source: documentHtml({
|
|
681
|
+
site: content,
|
|
682
|
+
page: null,
|
|
683
|
+
body: notFoundBody,
|
|
684
|
+
scripts,
|
|
685
|
+
styles,
|
|
686
|
+
basePath: documentBase,
|
|
687
|
+
origin
|
|
688
|
+
})
|
|
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`);
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
function landingPage(content) {
|
|
697
|
+
const root = content.docs[0]?.root;
|
|
698
|
+
if (!root)
|
|
699
|
+
return null;
|
|
700
|
+
return content.pages.some((page) => page.route === root) ? root : null;
|
|
701
|
+
}
|
|
702
|
+
function resolveOrigin(configured) {
|
|
703
|
+
const raw = configured || process.env.GEM_URL || process.env.VERCEL_URL || process.env.CF_PAGES_URL || process.env.URL || "";
|
|
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
|
+
}
|
|
715
|
+
}
|
|
716
|
+
function entrySource(content, root, configuredEntry) {
|
|
717
|
+
const custom = configuredEntry || content.theme.entry;
|
|
718
|
+
const extra = content.theme.css ? `import ${JSON.stringify(resolve(root, content.theme.css))};` : "";
|
|
719
|
+
if (custom) {
|
|
720
|
+
return [`import ${JSON.stringify(resolve(root, custom))};`, extra].filter(Boolean).join(`
|
|
721
|
+
`);
|
|
722
|
+
}
|
|
723
|
+
return [
|
|
724
|
+
`import "@charmland/gem/theme.css";`,
|
|
725
|
+
extra,
|
|
726
|
+
`import { boot } from "@charmland/gem/boot";`,
|
|
727
|
+
`import content from ${JSON.stringify(CONTENT)};`,
|
|
728
|
+
`boot(content);`
|
|
729
|
+
].filter(Boolean).join(`
|
|
730
|
+
`);
|
|
731
|
+
}
|
|
732
|
+
function resolveTheme(root, explicit) {
|
|
733
|
+
if (explicit)
|
|
734
|
+
return resolve(root, explicit);
|
|
735
|
+
try {
|
|
736
|
+
return createRequire2(join2(root, "package.json")).resolve("@charmland/gem/theme.js");
|
|
737
|
+
} catch {
|
|
738
|
+
return null;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
function normalizeRoute(path) {
|
|
742
|
+
if (!path || path === "/")
|
|
743
|
+
return "/";
|
|
744
|
+
return path.replace(/\/+$/, "") || "/";
|
|
745
|
+
}
|
|
746
|
+
function inDirectory(file, directory) {
|
|
747
|
+
if (file === directory)
|
|
748
|
+
return true;
|
|
749
|
+
const base = directory.endsWith(sep) ? directory : directory + sep;
|
|
750
|
+
return file.startsWith(base);
|
|
751
|
+
}
|
|
752
|
+
function joinBase(base, file) {
|
|
753
|
+
return `${base.replace(/\/$/, "")}/${file.replace(/^\//, "")}`;
|
|
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
|
+
}
|
|
765
|
+
function errorMessage(error) {
|
|
766
|
+
return error instanceof Error ? error.message : String(error);
|
|
767
|
+
}
|
|
768
|
+
export {
|
|
769
|
+
gem
|
|
770
|
+
};
|
package/dist/og.d.ts
ADDED
package/dist/output.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Page, SiteDocument } from "@charmland/gem-compiler/types";
|
|
2
|
+
export interface OutputContext {
|
|
3
|
+
site: SiteDocument;
|
|
4
|
+
origin: string | null;
|
|
5
|
+
}
|
|
6
|
+
export declare function markdownFile(route: string): string;
|
|
7
|
+
export declare function markdownRoute(route: string): string;
|
|
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;
|
|
20
|
+
export declare function ogFile(route: string): string;
|
|
21
|
+
export declare function ogRoute(route: string): 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;
|
|
26
|
+
/**
|
|
27
|
+
* A site whose landing page sits under its prefix still has to answer at the
|
|
28
|
+
* build root, both locally and under a mount. A relative redirect does that
|
|
29
|
+
* without duplicating the page or its canonical URL.
|
|
30
|
+
*/
|
|
31
|
+
export declare function landingRedirect(route: string): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a route by booting the compiled theme inside jsdom. Elm has no
|
|
3
|
+
* server renderer; this is what keeps one renderer for crawlers and browser.
|
|
4
|
+
*/
|
|
5
|
+
import type { SiteDocument } from "@charmland/gem-compiler/types";
|
|
6
|
+
export interface RenderRouteOptions {
|
|
7
|
+
source: string;
|
|
8
|
+
html: string;
|
|
9
|
+
content: SiteDocument;
|
|
10
|
+
route: string;
|
|
11
|
+
module?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function loadThemeBundle(path: string): string | null;
|
|
14
|
+
export declare function renderRoute(options: RenderRouteOptions): Promise<string>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@charmland/gem-vite",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "The Vite plugin that turns a markdown directory into a prerendered docs site.",
|
|
5
|
+
"keywords": ["gem", "vite", "vite-plugin", "documentation", "prerender"],
|
|
6
|
+
"homepage": "https://github.com/charmbracelet/gem/tree/main/packages/vite",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/charmbracelet/gem/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/charmbracelet/gem.git",
|
|
13
|
+
"directory": "packages/vite"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"license": "Apache-2.0",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": "^22.22.2 || ^24.15.0 || >=26.0.0"
|
|
23
|
+
},
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"NOTICE"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
37
|
+
"build": "bun run clean && bun build src/index.ts --outdir dist --target node --format esm --packages external && bun run build:types",
|
|
38
|
+
"build:types": "tsc -p tsconfig.build.json",
|
|
39
|
+
"prepack": "bun run build"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@charmland/gem-compiler": "0.1.1",
|
|
43
|
+
"@takumi-rs/core": "^2.20.0",
|
|
44
|
+
"jsdom": "^30.1.3"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@charmland/gem": "^0.1.1",
|
|
48
|
+
"vite": ">=6"
|
|
49
|
+
},
|
|
50
|
+
"peerDependenciesMeta": {
|
|
51
|
+
"@charmland/gem": {
|
|
52
|
+
"optional": true
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|