@matra/site-generation 0.1.0-site.1 → 0.1.0-site.3
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 +2 -2
- package/src/index.mjs +1 -1
- package/src/site.mjs +132 -33
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matra/site-generation",
|
|
3
|
-
"version": "0.1.0-site.
|
|
3
|
+
"version": "0.1.0-site.3",
|
|
4
4
|
"description": "Declarative static site generation for Matra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"src"
|
|
11
11
|
],
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@matra/core": "^0.3.1-site.
|
|
13
|
+
"@matra/core": "^0.3.1-site.1",
|
|
14
14
|
"@matra/html": "^0.3.1-site.0",
|
|
15
15
|
"js-yaml": "^5.2.2",
|
|
16
16
|
"markdown-it": "^14.3.0"
|
package/src/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { buildSite, renderLiterateDocument } from "./site.mjs"
|
|
1
|
+
export { buildSite, getSiteProgramDocuments, renderLiterateDocument } from "./site.mjs"
|
package/src/site.mjs
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
readFile,
|
|
9
9
|
rename,
|
|
10
10
|
rm,
|
|
11
|
+
rmdir,
|
|
11
12
|
writeFile,
|
|
12
13
|
} from "node:fs/promises"
|
|
13
14
|
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path"
|
|
@@ -19,47 +20,29 @@ import { markdownToNodes } from "./markdown.mjs"
|
|
|
19
20
|
|
|
20
21
|
const CONFIG_NAMES = ["matra.site.yaml", "matra.site.json", "matra.site.matra"]
|
|
21
22
|
const SITE_KEYS = new Set(["route", "layout", "data"])
|
|
23
|
+
const OUTPUT_MANIFEST = ".matra-site-manifest.json"
|
|
22
24
|
|
|
23
25
|
export async function renderLiterateDocument(markdown, options = {}) {
|
|
24
|
-
const bundle = await buildLiterate(markdown, {
|
|
25
|
-
|
|
26
|
+
const bundle = await buildLiterate(markdown, {
|
|
27
|
+
document: options.document,
|
|
28
|
+
moduleRoot: options.moduleRoot,
|
|
29
|
+
})
|
|
30
|
+
const value = await runLiterate(bundle, {
|
|
31
|
+
globals: options.globals,
|
|
32
|
+
moduleRoot: options.moduleRoot,
|
|
33
|
+
})
|
|
26
34
|
return staticValueToAST(value)
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
export async function buildSite(directory) {
|
|
30
|
-
const root = resolve(directory)
|
|
31
|
-
const config = await readConfig(root)
|
|
32
|
-
const settings = normalizeSettings(config, root)
|
|
33
|
-
for (const input of [
|
|
34
|
-
settings.pagesDirectory,
|
|
35
|
-
...settings.documentDirectories,
|
|
36
|
-
settings.layoutsDirectory,
|
|
37
|
-
...(settings.assetsDirectory ? [settings.assetsDirectory] : []),
|
|
38
|
-
]) {
|
|
39
|
-
if (
|
|
40
|
-
input === settings.outputDirectory ||
|
|
41
|
-
input.startsWith(`${settings.outputDirectory}${sep}`) ||
|
|
42
|
-
settings.outputDirectory.startsWith(`${input}${sep}`)
|
|
43
|
-
) {
|
|
44
|
-
throw new Error("site.output must not overlap a site input directory")
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
await Promise.all([
|
|
48
|
-
assertDirectory(root, settings.pagesDirectory),
|
|
49
|
-
...settings.documentDirectories.map(path => assertDirectory(root, path)),
|
|
50
|
-
assertDirectory(root, settings.layoutsDirectory, { optional: true }),
|
|
51
|
-
...(settings.assetsDirectory ? [assertDirectory(root, settings.assetsDirectory)] : []),
|
|
52
|
-
])
|
|
53
|
-
const documents = await collectDocuments(root, settings)
|
|
54
|
-
const layouts = await collectLayouts(root, settings)
|
|
55
|
-
const pages = await collectPages(root, settings, documents, layouts)
|
|
38
|
+
const { root, settings, documents, layouts, pages } = await loadSite(resolve(directory))
|
|
56
39
|
const assets = settings.assetsDirectory ? await collectAssets(settings.assetsDirectory) : []
|
|
57
40
|
const outputPlan = planOutput(pages, assets, settings)
|
|
58
41
|
settings.routes = new Map(pages.map(page => [page.path, page.route]))
|
|
59
42
|
settings.routesByRoute = new Set(pages.map(page => page.route))
|
|
60
43
|
settings.assetFiles = new Set(assets.map(asset => resolve(asset.source)))
|
|
61
44
|
settings.assetOutputs = new Set(assets.map(asset => `/${asset.output.split(sep).join("/")}`))
|
|
62
|
-
await validateOutput(root, settings.outputDirectory,
|
|
45
|
+
await validateOutput(root, settings.outputDirectory, { files: [] })
|
|
63
46
|
settings.documents = documents.map(document => ({
|
|
64
47
|
id: document.id,
|
|
65
48
|
path: document.id,
|
|
@@ -74,11 +57,17 @@ export async function buildSite(directory) {
|
|
|
74
57
|
),
|
|
75
58
|
}))
|
|
76
59
|
for (const layout of layouts.values()) {
|
|
77
|
-
layout.bundle = await buildLiterate(layout.source, {
|
|
60
|
+
layout.bundle = await buildLiterate(layout.source, {
|
|
61
|
+
document: layout.document,
|
|
62
|
+
moduleRoot: root,
|
|
63
|
+
})
|
|
78
64
|
}
|
|
79
65
|
for (const page of pages) {
|
|
80
66
|
if (page.kind === "program") {
|
|
81
|
-
page.bundle = await buildLiterate(page.source, {
|
|
67
|
+
page.bundle = await buildLiterate(page.source, {
|
|
68
|
+
document: page.document,
|
|
69
|
+
moduleRoot: root,
|
|
70
|
+
})
|
|
82
71
|
}
|
|
83
72
|
}
|
|
84
73
|
|
|
@@ -99,6 +88,43 @@ export async function buildSite(directory) {
|
|
|
99
88
|
return rendered.map(file => join(settings.outputDirectory, file.path))
|
|
100
89
|
}
|
|
101
90
|
|
|
91
|
+
export async function getSiteProgramDocuments(directory) {
|
|
92
|
+
const { layouts, pages } = await loadSite(resolve(directory))
|
|
93
|
+
return [
|
|
94
|
+
...[...layouts.values()].map(layout => layout.document),
|
|
95
|
+
...pages.filter(page => page.kind === "program").map(page => page.document),
|
|
96
|
+
].sort((left, right) => left.localeCompare(right, "en"))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function loadSite(root) {
|
|
100
|
+
const config = await readConfig(root)
|
|
101
|
+
const settings = normalizeSettings(config, root)
|
|
102
|
+
for (const input of [
|
|
103
|
+
settings.pagesDirectory,
|
|
104
|
+
...settings.documentDirectories,
|
|
105
|
+
settings.layoutsDirectory,
|
|
106
|
+
...(settings.assetsDirectory ? [settings.assetsDirectory] : []),
|
|
107
|
+
]) {
|
|
108
|
+
if (
|
|
109
|
+
input === settings.outputDirectory ||
|
|
110
|
+
input.startsWith(`${settings.outputDirectory}${sep}`) ||
|
|
111
|
+
settings.outputDirectory.startsWith(`${input}${sep}`)
|
|
112
|
+
) {
|
|
113
|
+
throw new Error("site.output must not overlap a site input directory")
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
await Promise.all([
|
|
117
|
+
assertDirectory(root, settings.pagesDirectory),
|
|
118
|
+
...settings.documentDirectories.map(path => assertDirectory(root, path)),
|
|
119
|
+
assertDirectory(root, settings.layoutsDirectory, { optional: true }),
|
|
120
|
+
...(settings.assetsDirectory ? [assertDirectory(root, settings.assetsDirectory)] : []),
|
|
121
|
+
])
|
|
122
|
+
const documents = await collectDocuments(root, settings)
|
|
123
|
+
const layouts = await collectLayouts(root, settings)
|
|
124
|
+
const pages = await collectPages(root, settings, documents, layouts)
|
|
125
|
+
return { root, settings, documents, layouts, pages }
|
|
126
|
+
}
|
|
127
|
+
|
|
102
128
|
async function readConfig(root) {
|
|
103
129
|
const found = []
|
|
104
130
|
for (const name of CONFIG_NAMES) {
|
|
@@ -465,6 +491,7 @@ async function renderPage(page, settings) {
|
|
|
465
491
|
}
|
|
466
492
|
const value = await runLiterate(page.bundle, {
|
|
467
493
|
globals: globalsFor(page, settings, []),
|
|
494
|
+
moduleRoot: settings.root,
|
|
468
495
|
})
|
|
469
496
|
return staticValueToAST(value)
|
|
470
497
|
}
|
|
@@ -475,7 +502,7 @@ async function renderLayout(layout, content, page, settings) {
|
|
|
475
502
|
}
|
|
476
503
|
const pageContent = content.tag === "$root" ? content.children : [content]
|
|
477
504
|
const globals = globalsFor(page, settings, pageContent)
|
|
478
|
-
const value = await runLiterate(layout.bundle, { globals })
|
|
505
|
+
const value = await runLiterate(layout.bundle, { globals, moduleRoot: settings.root })
|
|
479
506
|
return staticValueToAST(value)
|
|
480
507
|
}
|
|
481
508
|
|
|
@@ -574,6 +601,9 @@ function planOutput(pages, assets, settings) {
|
|
|
574
601
|
const seen = new Map()
|
|
575
602
|
for (const output of outputs) {
|
|
576
603
|
const normalized = output.split(sep).join("/").toLocaleLowerCase("en-US")
|
|
604
|
+
if (normalized === OUTPUT_MANIFEST || normalized.startsWith(`${OUTPUT_MANIFEST}/`)) {
|
|
605
|
+
throw new Error(`Reserved site output path: ${output}`)
|
|
606
|
+
}
|
|
577
607
|
if (seen.has(normalized))
|
|
578
608
|
throw new Error(`Output collision: ${output} and ${seen.get(normalized)}`)
|
|
579
609
|
seen.set(normalized, output)
|
|
@@ -634,7 +664,7 @@ async function validateOutputFile(output, relativePath) {
|
|
|
634
664
|
async function publish(root, outputDirectory, rendered) {
|
|
635
665
|
const parent = dirname(outputDirectory)
|
|
636
666
|
await ensureDirectoryPath(root, parent)
|
|
637
|
-
await validateOutput(root, outputDirectory, { files:
|
|
667
|
+
await validateOutput(root, outputDirectory, { files: [] })
|
|
638
668
|
const stage = await mkdtemp(join(parent, `.${basename(outputDirectory)}-stage-`))
|
|
639
669
|
const backup = join(parent, `.${basename(outputDirectory)}-backup-${randomUUID()}`)
|
|
640
670
|
try {
|
|
@@ -650,11 +680,22 @@ async function publish(root, outputDirectory, rendered) {
|
|
|
650
680
|
await validateTree(outputDirectory)
|
|
651
681
|
await cp(outputDirectory, stage, { recursive: true, force: true })
|
|
652
682
|
}
|
|
683
|
+
const previousFiles = await readOutputManifest(stage)
|
|
684
|
+
const files = rendered.map(file => file.path.split(sep).join("/")).sort()
|
|
685
|
+
const currentFiles = new Set(files)
|
|
686
|
+
for (const file of previousFiles) {
|
|
687
|
+
if (!currentFiles.has(file)) await removeGeneratedFile(stage, file)
|
|
688
|
+
}
|
|
689
|
+
await validateOutput(root, stage, { files: rendered.map(file => file.path) })
|
|
653
690
|
for (const file of rendered) {
|
|
654
691
|
const stagedPath = join(stage, file.path)
|
|
655
692
|
await mkdir(dirname(stagedPath), { recursive: true })
|
|
656
693
|
await writeFile(stagedPath, file.content)
|
|
657
694
|
}
|
|
695
|
+
await writeFile(
|
|
696
|
+
join(stage, OUTPUT_MANIFEST),
|
|
697
|
+
`${JSON.stringify({ version: 1, files }, null, 2)}\n`,
|
|
698
|
+
)
|
|
658
699
|
let hadOutput = false
|
|
659
700
|
try {
|
|
660
701
|
await rename(outputDirectory, backup)
|
|
@@ -674,6 +715,64 @@ async function publish(root, outputDirectory, rendered) {
|
|
|
674
715
|
}
|
|
675
716
|
}
|
|
676
717
|
|
|
718
|
+
async function readOutputManifest(output) {
|
|
719
|
+
const path = join(output, OUTPUT_MANIFEST)
|
|
720
|
+
let source
|
|
721
|
+
try {
|
|
722
|
+
source = await readFile(path, "utf8")
|
|
723
|
+
} catch (error) {
|
|
724
|
+
if (error.code === "ENOENT") return []
|
|
725
|
+
throw error
|
|
726
|
+
}
|
|
727
|
+
let manifest
|
|
728
|
+
try {
|
|
729
|
+
manifest = JSON.parse(source)
|
|
730
|
+
} catch (error) {
|
|
731
|
+
throw new SyntaxError(`Invalid site output manifest: ${path}`, { cause: error })
|
|
732
|
+
}
|
|
733
|
+
if (
|
|
734
|
+
!isRecord(manifest) ||
|
|
735
|
+
manifest.version !== 1 ||
|
|
736
|
+
Object.keys(manifest).some(key => !["version", "files"].includes(key)) ||
|
|
737
|
+
!Array.isArray(manifest.files)
|
|
738
|
+
) {
|
|
739
|
+
throw new TypeError(`Invalid site output manifest: ${path}`)
|
|
740
|
+
}
|
|
741
|
+
const seen = new Set()
|
|
742
|
+
for (const file of manifest.files) {
|
|
743
|
+
if (
|
|
744
|
+
typeof file !== "string" ||
|
|
745
|
+
file.includes("\\") ||
|
|
746
|
+
file.split("/").some(part => !isSafeSegment(part)) ||
|
|
747
|
+
file.toLocaleLowerCase("en-US") === OUTPUT_MANIFEST ||
|
|
748
|
+
file.toLocaleLowerCase("en-US").startsWith(`${OUTPUT_MANIFEST}/`)
|
|
749
|
+
) {
|
|
750
|
+
throw new TypeError(`Invalid generated file path in site output manifest: ${path}`)
|
|
751
|
+
}
|
|
752
|
+
const key = file.toLocaleLowerCase("en-US")
|
|
753
|
+
if (seen.has(key))
|
|
754
|
+
throw new TypeError(`Duplicate generated file in site output manifest: ${path}`)
|
|
755
|
+
seen.add(key)
|
|
756
|
+
await validateOutputFile(output, file.split("/").join(sep))
|
|
757
|
+
}
|
|
758
|
+
return manifest.files
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
async function removeGeneratedFile(output, file) {
|
|
762
|
+
const path = join(output, ...file.split("/"))
|
|
763
|
+
await rm(path, { force: true })
|
|
764
|
+
let directory = dirname(path)
|
|
765
|
+
while (directory !== output) {
|
|
766
|
+
try {
|
|
767
|
+
await rmdir(directory)
|
|
768
|
+
} catch (error) {
|
|
769
|
+
if (error.code === "ENOTEMPTY" || error.code === "EEXIST") break
|
|
770
|
+
if (error.code !== "ENOENT") throw error
|
|
771
|
+
}
|
|
772
|
+
directory = dirname(directory)
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
677
776
|
async function ensureDirectoryPath(root, path) {
|
|
678
777
|
if (path !== root && !path.startsWith(`${root}${sep}`)) {
|
|
679
778
|
throw new Error(`Site output parent escapes project root: ${path}`)
|