@matra/site-generation 0.1.0-site.4 → 0.1.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/package.json +3 -3
- package/src/index.mjs +6 -1
- package/src/site.mjs +241 -33
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matra/site-generation",
|
|
3
|
-
"version": "0.1.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Declarative static site generation for Matra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
"src"
|
|
11
11
|
],
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@matra/core": "^0.3.1
|
|
14
|
-
"@matra/html": "^0.3.1
|
|
13
|
+
"@matra/core": "^0.3.1",
|
|
14
|
+
"@matra/html": "^0.3.1",
|
|
15
15
|
"js-yaml": "^5.2.2",
|
|
16
16
|
"markdown-it": "^14.3.0"
|
|
17
17
|
},
|
package/src/index.mjs
CHANGED
package/src/site.mjs
CHANGED
|
@@ -12,8 +12,14 @@ import {
|
|
|
12
12
|
writeFile,
|
|
13
13
|
} from "node:fs/promises"
|
|
14
14
|
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path"
|
|
15
|
+
import { types } from "node:util"
|
|
15
16
|
import { evaluateStatic, parse, staticValueToAST } from "@matra/core"
|
|
16
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
buildLiterate,
|
|
19
|
+
buildMatraComponent,
|
|
20
|
+
buildMatraProgram,
|
|
21
|
+
runLiterate,
|
|
22
|
+
} from "@matra/core/experimental/literate"
|
|
17
23
|
import { toHTML } from "@matra/html"
|
|
18
24
|
import { load as loadYaml } from "js-yaml"
|
|
19
25
|
import { markdownToNodes } from "./markdown.mjs"
|
|
@@ -36,21 +42,36 @@ export async function renderLiterateDocument(markdown, options = {}) {
|
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
export async function buildSite(directory) {
|
|
39
|
-
const
|
|
40
|
-
settings
|
|
45
|
+
const site = await loadSite(resolve(directory))
|
|
46
|
+
const { root, settings } = site
|
|
47
|
+
const components = []
|
|
41
48
|
if (settings.componentsDirectory) {
|
|
42
|
-
const names = new Set()
|
|
43
49
|
for (const path of await walkFiles(settings.componentsDirectory)) {
|
|
44
50
|
if (!path.endsWith(".matra")) continue
|
|
45
|
-
|
|
51
|
+
components.push({
|
|
52
|
+
source: await readFile(path, "utf8"),
|
|
46
53
|
document: relative(root, path).split(sep).join("/"),
|
|
47
54
|
})
|
|
48
|
-
const key = component.name.toLowerCase()
|
|
49
|
-
if (names.has(key)) throw new Error(`${path}: Duplicate component name: ${component.name}`)
|
|
50
|
-
names.add(key)
|
|
51
|
-
settings.components.push(component)
|
|
52
55
|
}
|
|
53
56
|
}
|
|
57
|
+
return generateLoadedSite({ ...site, components })
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function generateLoadedSite({ root, settings, documents, layouts, pages, components }) {
|
|
61
|
+
settings.components = []
|
|
62
|
+
const names = new Set()
|
|
63
|
+
for (const source of components) {
|
|
64
|
+
const component = await buildMatraComponent(source.source, { document: source.document })
|
|
65
|
+
if (source.name !== undefined && source.name !== component.name) {
|
|
66
|
+
throw new Error(`${source.document}: component name must match '${source.name}'`)
|
|
67
|
+
}
|
|
68
|
+
const key = component.name.toLowerCase()
|
|
69
|
+
if (names.has(key)) {
|
|
70
|
+
throw new Error(`${source.document}: Duplicate component name: ${component.name}`)
|
|
71
|
+
}
|
|
72
|
+
names.add(key)
|
|
73
|
+
settings.components.push(component)
|
|
74
|
+
}
|
|
54
75
|
const assets = settings.assetsDirectory ? await collectAssets(settings.assetsDirectory) : []
|
|
55
76
|
const outputPlan = planOutput(pages, assets, settings)
|
|
56
77
|
settings.routes = new Map(pages.map(page => [page.path, page.route]))
|
|
@@ -61,7 +82,7 @@ export async function buildSite(directory) {
|
|
|
61
82
|
settings.documents = documents.map(document => ({
|
|
62
83
|
id: document.id,
|
|
63
84
|
path: document.id,
|
|
64
|
-
metadata: metadataValues(document.metadata),
|
|
85
|
+
metadata: document.metadataIsData ? document.metadata : metadataValues(document.metadata),
|
|
65
86
|
content: markdownToNodes(
|
|
66
87
|
document.body,
|
|
67
88
|
settings.routes.has(document.path)
|
|
@@ -72,17 +93,11 @@ export async function buildSite(directory) {
|
|
|
72
93
|
),
|
|
73
94
|
}))
|
|
74
95
|
for (const layout of layouts.values()) {
|
|
75
|
-
layout.bundle = await
|
|
76
|
-
document: layout.document,
|
|
77
|
-
moduleRoot: root,
|
|
78
|
-
})
|
|
96
|
+
layout.bundle = await buildTemplate(layout, root)
|
|
79
97
|
}
|
|
80
98
|
for (const page of pages) {
|
|
81
99
|
if (page.kind === "program") {
|
|
82
|
-
page.bundle = await
|
|
83
|
-
document: page.document,
|
|
84
|
-
moduleRoot: root,
|
|
85
|
-
})
|
|
100
|
+
page.bundle = await buildTemplate(page, root)
|
|
86
101
|
}
|
|
87
102
|
}
|
|
88
103
|
|
|
@@ -92,7 +107,7 @@ export async function buildSite(directory) {
|
|
|
92
107
|
const document = page.layout
|
|
93
108
|
? await renderLayout(layouts.get(page.layout), content, page, settings)
|
|
94
109
|
: content
|
|
95
|
-
assertCompleteDocument(document, page.
|
|
110
|
+
assertCompleteDocument(document, page.path)
|
|
96
111
|
rendered.push({
|
|
97
112
|
path: outputPlan.routes.get(page.route),
|
|
98
113
|
content: `<!doctype html>\n${toHTML(document, { basePath: settings.basePath })}\n`,
|
|
@@ -103,6 +118,182 @@ export async function buildSite(directory) {
|
|
|
103
118
|
return rendered.map(file => join(settings.outputDirectory, file.path))
|
|
104
119
|
}
|
|
105
120
|
|
|
121
|
+
function buildTemplate(template, root) {
|
|
122
|
+
return template.sourceKind === "matra"
|
|
123
|
+
? buildMatraProgram(template.source, { document: template.document })
|
|
124
|
+
: buildLiterate(template.source, { document: template.document, moduleRoot: root })
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function generateSite(options) {
|
|
128
|
+
const site = normalizeDirectSite(options)
|
|
129
|
+
const { root, settings } = site
|
|
130
|
+
const info = await lstat(root)
|
|
131
|
+
if (!info.isDirectory()) throw new Error(`Expected a project directory: ${root}`)
|
|
132
|
+
for (const directory of [
|
|
133
|
+
...settings.documentDirectories,
|
|
134
|
+
...(settings.assetsDirectory ? [settings.assetsDirectory] : []),
|
|
135
|
+
]) {
|
|
136
|
+
assertInputOutputSeparation(directory, settings.outputDirectory)
|
|
137
|
+
await assertDirectory(root, directory)
|
|
138
|
+
}
|
|
139
|
+
const documents = await collectDocuments(root, settings)
|
|
140
|
+
const logicalPaths = new Set([
|
|
141
|
+
...site.pages.map(page => page.path.toLowerCase()),
|
|
142
|
+
...[...site.layouts.values()].map(layout => layout.path.toLowerCase()),
|
|
143
|
+
...site.components.map(component => component.path.toLowerCase()),
|
|
144
|
+
])
|
|
145
|
+
for (const document of documents) {
|
|
146
|
+
if (logicalPaths.has(document.path.toLowerCase())) {
|
|
147
|
+
throw new Error(`Collected document conflicts with a logical document: ${document.path}`)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const ids = new Set(documents.map(document => document.id.toLowerCase()))
|
|
151
|
+
for (const page of site.pages.filter(page => page.kind === "markdown")) {
|
|
152
|
+
const id = page.document
|
|
153
|
+
if (ids.has(id.toLowerCase())) throw new Error(`Duplicate document ID ignoring case: ${id}`)
|
|
154
|
+
ids.add(id.toLowerCase())
|
|
155
|
+
documents.push({
|
|
156
|
+
id,
|
|
157
|
+
path: page.path,
|
|
158
|
+
body: page.body,
|
|
159
|
+
metadata: page.data,
|
|
160
|
+
metadataIsData: true,
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
documents.sort((left, right) => left.id.localeCompare(right.id, "en"))
|
|
164
|
+
return generateLoadedSite({ ...site, documents })
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function normalizeDirectSite(input) {
|
|
168
|
+
if (!isRecord(input)) throw new TypeError("generateSite options must be an object")
|
|
169
|
+
assertPlainData(input, "generateSite options")
|
|
170
|
+
const options = structuredClone(input)
|
|
171
|
+
rejectUnknownKeys(
|
|
172
|
+
options,
|
|
173
|
+
new Set([
|
|
174
|
+
"root",
|
|
175
|
+
"output",
|
|
176
|
+
"format",
|
|
177
|
+
"assets",
|
|
178
|
+
"basePath",
|
|
179
|
+
"data",
|
|
180
|
+
"defaultLayout",
|
|
181
|
+
"pages",
|
|
182
|
+
"layouts",
|
|
183
|
+
"components",
|
|
184
|
+
"documents",
|
|
185
|
+
]),
|
|
186
|
+
"generateSite",
|
|
187
|
+
)
|
|
188
|
+
if (options.root !== undefined && (typeof options.root !== "string" || !options.root)) {
|
|
189
|
+
throw new TypeError("root must be a non-empty path")
|
|
190
|
+
}
|
|
191
|
+
const root = resolve(options.root ?? process.cwd())
|
|
192
|
+
const siteOptions = Object.fromEntries(
|
|
193
|
+
["output", "format", "assets", "basePath", "data", "defaultLayout", "documents"]
|
|
194
|
+
.filter(key => Object.hasOwn(options, key))
|
|
195
|
+
.map(key => [key, options[key]]),
|
|
196
|
+
)
|
|
197
|
+
const settings = normalizeSettings({ version: 1, site: siteOptions }, root)
|
|
198
|
+
if (settings.outputDirectory === root) throw new Error("site.output must not be the project root")
|
|
199
|
+
const documentDirectories =
|
|
200
|
+
typeof options.documents === "string" ? [options.documents] : (options.documents ?? [])
|
|
201
|
+
settings.documentDirectories = [...new Set(documentDirectories.map(path => resolve(root, path)))]
|
|
202
|
+
const documents = new Set()
|
|
203
|
+
const sourceEntry = (entry, fallback, label) => {
|
|
204
|
+
if (!isRecord(entry)) throw new TypeError(`${label} must be an object`)
|
|
205
|
+
if (typeof entry.source !== "string") throw new TypeError(`${label}.source must be a string`)
|
|
206
|
+
const document = entry.document === undefined ? fallback : entry.document
|
|
207
|
+
if (
|
|
208
|
+
typeof document !== "string" ||
|
|
209
|
+
!document ||
|
|
210
|
+
isAbsolute(document) ||
|
|
211
|
+
document.includes("\\") ||
|
|
212
|
+
document.includes("%") ||
|
|
213
|
+
document.split("/").some(part => !isSafeSegment(part))
|
|
214
|
+
) {
|
|
215
|
+
throw new TypeError(`${label}.document must be a safe project-relative logical path`)
|
|
216
|
+
}
|
|
217
|
+
const key = document.toLowerCase()
|
|
218
|
+
if (documents.has(key))
|
|
219
|
+
throw new Error(`Duplicate logical document path ignoring case: ${document}`)
|
|
220
|
+
documents.add(key)
|
|
221
|
+
const path = resolve(root, document)
|
|
222
|
+
assertInputOutputSeparation(path, settings.outputDirectory)
|
|
223
|
+
return { source: entry.source, document, path, sourceKind: "matra" }
|
|
224
|
+
}
|
|
225
|
+
const namedSources = (entries, label) => {
|
|
226
|
+
if (entries === undefined) return new Map()
|
|
227
|
+
if (!isRecord(entries)) throw new TypeError(`${label} must be an object`)
|
|
228
|
+
const names = new Set()
|
|
229
|
+
return new Map(
|
|
230
|
+
Object.entries(entries).map(([name, entry]) => {
|
|
231
|
+
if (!/^[A-Za-z0-9_-]+(?:\/[A-Za-z0-9_-]+)*$/.test(name)) {
|
|
232
|
+
throw new TypeError(`Invalid ${label} name: ${name}`)
|
|
233
|
+
}
|
|
234
|
+
if (names.has(name.toLowerCase())) throw new Error(`Duplicate ${label} name: ${name}`)
|
|
235
|
+
names.add(name.toLowerCase())
|
|
236
|
+
if (!isRecord(entry)) throw new TypeError(`${label}.${name} must be an object`)
|
|
237
|
+
rejectUnknownKeys(entry, new Set(["source", "document"]), `${label}.${name}`)
|
|
238
|
+
return [name, { ...sourceEntry(entry, `${label}/${name}.matra`, `${label}.${name}`), name }]
|
|
239
|
+
}),
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
const layouts = namedSources(options.layouts, "layouts")
|
|
243
|
+
selectedLayout(undefined, settings.defaultLayout, layouts, "defaultLayout")
|
|
244
|
+
const components = [...namedSources(options.components, "components").values()]
|
|
245
|
+
if (!Array.isArray(options.pages)) throw new TypeError("pages must be an array")
|
|
246
|
+
const pages = options.pages.map((entry, index) => {
|
|
247
|
+
const label = `pages[${index}]`
|
|
248
|
+
if (!isRecord(entry)) throw new TypeError(`${label} must be an object`)
|
|
249
|
+
rejectUnknownKeys(
|
|
250
|
+
entry,
|
|
251
|
+
new Set(["kind", "route", "source", "layout", "data", "document"]),
|
|
252
|
+
label,
|
|
253
|
+
)
|
|
254
|
+
if (!["matra", "markdown"].includes(entry.kind))
|
|
255
|
+
throw new TypeError(`${label}.kind must be 'matra' or 'markdown'`)
|
|
256
|
+
if (typeof entry.route !== "string") throw new TypeError(`${label}.route must be a string`)
|
|
257
|
+
const controls = siteControls(
|
|
258
|
+
{
|
|
259
|
+
site: {
|
|
260
|
+
...(Object.hasOwn(entry, "layout") ? { layout: entry.layout } : {}),
|
|
261
|
+
...(Object.hasOwn(entry, "data") ? { data: entry.data } : {}),
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
label,
|
|
265
|
+
)
|
|
266
|
+
const route = normalizeRoute(entry.route, settings.format, label)
|
|
267
|
+
const stem = route === "/" ? "index" : route.slice(1).replace(/\/$/, "")
|
|
268
|
+
const source = sourceEntry(
|
|
269
|
+
entry,
|
|
270
|
+
`pages/${stem}.${entry.kind === "matra" ? "matra" : "md"}`,
|
|
271
|
+
label,
|
|
272
|
+
)
|
|
273
|
+
return {
|
|
274
|
+
...source,
|
|
275
|
+
kind: entry.kind === "matra" ? "program" : "markdown",
|
|
276
|
+
id: source.document,
|
|
277
|
+
body: entry.source,
|
|
278
|
+
route,
|
|
279
|
+
data: controls.data ?? {},
|
|
280
|
+
layout: selectedLayout(controls.layout, settings.defaultLayout, layouts, source.document),
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
sortAndValidateRoutes(pages)
|
|
284
|
+
return { root, settings, layouts, pages, components }
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function assertInputOutputSeparation(input, output) {
|
|
288
|
+
if (
|
|
289
|
+
input === output ||
|
|
290
|
+
input.startsWith(`${output}${sep}`) ||
|
|
291
|
+
output.startsWith(`${input}${sep}`)
|
|
292
|
+
) {
|
|
293
|
+
throw new Error("site.output must not overlap a site input directory or logical document")
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
106
297
|
export async function getSiteProgramDocuments(directory) {
|
|
107
298
|
const { layouts, pages } = await loadSite(resolve(directory))
|
|
108
299
|
return [
|
|
@@ -121,13 +312,7 @@ async function loadSite(root) {
|
|
|
121
312
|
...(settings.componentsDirectory ? [settings.componentsDirectory] : []),
|
|
122
313
|
...(settings.assetsDirectory ? [settings.assetsDirectory] : []),
|
|
123
314
|
]) {
|
|
124
|
-
|
|
125
|
-
input === settings.outputDirectory ||
|
|
126
|
-
input.startsWith(`${settings.outputDirectory}${sep}`) ||
|
|
127
|
-
settings.outputDirectory.startsWith(`${input}${sep}`)
|
|
128
|
-
) {
|
|
129
|
-
throw new Error("site.output must not overlap a site input directory")
|
|
130
|
-
}
|
|
315
|
+
assertInputOutputSeparation(input, settings.outputDirectory)
|
|
131
316
|
}
|
|
132
317
|
await Promise.all([
|
|
133
318
|
assertDirectory(root, settings.pagesDirectory),
|
|
@@ -394,6 +579,11 @@ async function collectPages(root, settings, documents, layouts) {
|
|
|
394
579
|
layout: selectedLayout(controls.layout, settings.defaultLayout, layouts, path),
|
|
395
580
|
})
|
|
396
581
|
}
|
|
582
|
+
sortAndValidateRoutes(pages)
|
|
583
|
+
return pages
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function sortAndValidateRoutes(pages) {
|
|
397
587
|
pages.sort((left, right) => left.route.localeCompare(right.route, "en"))
|
|
398
588
|
const routes = new Set()
|
|
399
589
|
for (const page of pages) {
|
|
@@ -401,7 +591,6 @@ async function collectPages(root, settings, documents, layouts) {
|
|
|
401
591
|
if (routes.has(key)) throw new Error(`Duplicate route: ${page.route}`)
|
|
402
592
|
routes.add(key)
|
|
403
593
|
}
|
|
404
|
-
return pages
|
|
405
594
|
}
|
|
406
595
|
|
|
407
596
|
function parseFrontMatter(source, path) {
|
|
@@ -522,7 +711,7 @@ async function renderPage(page, settings) {
|
|
|
522
711
|
|
|
523
712
|
async function renderLayout(layout, content, page, settings) {
|
|
524
713
|
if (content.tag === "html") {
|
|
525
|
-
throw new TypeError(`${page.
|
|
714
|
+
throw new TypeError(`${page.path}: a complete HTML document cannot be placed inside a layout`)
|
|
526
715
|
}
|
|
527
716
|
const pageContent = content.tag === "$root" ? content.children : [content]
|
|
528
717
|
const globals = globalsFor(page, settings, pageContent)
|
|
@@ -550,7 +739,9 @@ function globalsFor(page, settings, content) {
|
|
|
550
739
|
|
|
551
740
|
function resolveMarkdownLink(href, kind, page, settings) {
|
|
552
741
|
if (/^(?:[A-Za-z][A-Za-z0-9+.-]*:|\/\/)/.test(href) || href.startsWith("#")) return href
|
|
553
|
-
const
|
|
742
|
+
const suffixStart = href.search(/[?#]/)
|
|
743
|
+
const pathname = suffixStart === -1 ? href : href.slice(0, suffixStart)
|
|
744
|
+
const suffix = suffixStart === -1 ? "" : href.slice(suffixStart)
|
|
554
745
|
if (!pathname) return href
|
|
555
746
|
if (pathname.startsWith("/")) {
|
|
556
747
|
const route = normalizeRoute(pathname, settings.format, page.path)
|
|
@@ -573,7 +764,7 @@ function resolveMarkdownLink(href, kind, page, settings) {
|
|
|
573
764
|
const assetPath = relative(assetRoot, target).split(sep).join("/")
|
|
574
765
|
return `/assets/${assetPath}${suffix}`
|
|
575
766
|
}
|
|
576
|
-
if (extname(target) === ".md" || pathname.endsWith(".matra.md")) {
|
|
767
|
+
if (settings.routes.has(target) || extname(target) === ".md" || pathname.endsWith(".matra.md")) {
|
|
577
768
|
const route = settings.routes.get(target)
|
|
578
769
|
if (!route) throw new Error(`${page.path}: local Markdown link does not resolve: ${href}`)
|
|
579
770
|
return `${route}${suffix}`
|
|
@@ -835,15 +1026,32 @@ function rejectUnknownKeys(value, allowed, label) {
|
|
|
835
1026
|
function assertPlainData(value, label, active = new Set()) {
|
|
836
1027
|
if (value === null || typeof value === "string" || typeof value === "boolean") return
|
|
837
1028
|
if (typeof value === "number" && Number.isFinite(value)) return
|
|
838
|
-
if (typeof value !== "object" || active.has(value)) {
|
|
1029
|
+
if (typeof value !== "object" || active.has(value) || types.isProxy(value)) {
|
|
839
1030
|
throw new TypeError(`${label} must contain only finite plain data`)
|
|
840
1031
|
}
|
|
841
1032
|
if (!Array.isArray(value) && !isRecord(value)) {
|
|
842
1033
|
throw new TypeError(`${label} must contain only plain objects and arrays`)
|
|
843
1034
|
}
|
|
1035
|
+
const descriptors = Object.getOwnPropertyDescriptors(value)
|
|
1036
|
+
const keys = Reflect.ownKeys(descriptors)
|
|
1037
|
+
const array = Array.isArray(value)
|
|
1038
|
+
if (
|
|
1039
|
+
array &&
|
|
1040
|
+
(keys.length !== value.length + 1 ||
|
|
1041
|
+
keys.some(
|
|
1042
|
+
key => key !== "length" && (typeof key !== "string" || !/^(0|[1-9]\d*)$/.test(key)),
|
|
1043
|
+
))
|
|
1044
|
+
) {
|
|
1045
|
+
throw new TypeError(`${label} must contain dense arrays without extra properties`)
|
|
1046
|
+
}
|
|
844
1047
|
active.add(value)
|
|
845
|
-
for (const
|
|
846
|
-
|
|
1048
|
+
for (const key of keys) {
|
|
1049
|
+
if (array && key === "length") continue
|
|
1050
|
+
const descriptor = descriptors[key]
|
|
1051
|
+
if (typeof key !== "string" || !descriptor.enumerable || !Object.hasOwn(descriptor, "value")) {
|
|
1052
|
+
throw new TypeError(`${label} must contain enumerable string-keyed data properties`)
|
|
1053
|
+
}
|
|
1054
|
+
assertPlainData(descriptor.value, label, active)
|
|
847
1055
|
}
|
|
848
1056
|
active.delete(value)
|
|
849
1057
|
}
|