@matra/site-generation 0.4.0 → 0.4.1-preview.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.ja.md +7 -0
- package/README.md +5 -0
- package/package.json +3 -3
- package/src/site-worker.mjs +24 -0
- package/src/site.mjs +324 -23
package/README.ja.md
CHANGED
|
@@ -44,6 +44,11 @@ console.log(`Generated ${outputs.length} files`)
|
|
|
44
44
|
`buildSitePreview(directory)`は同じビルドを実行し、ローカル配信用に`files`、
|
|
45
45
|
`outputDirectory`、`routes`(ルートから出力相対パスへのmap)、`basePath`、`notFound`を返します。
|
|
46
46
|
|
|
47
|
+
設定の`site.hooks.module`に信頼する`.mjs`を指定すると、`pages`・`markdown`・`assets`フックを
|
|
48
|
+
ファイル方式と組み合わせられます。入力、出力、監視は[フックの契約](../../spec/site-generation.ja.md#cliのページmarkdownフック)
|
|
49
|
+
を参照してください。フックはsandboxやtimeoutなしで実行されます。
|
|
50
|
+
`assets`と`pages`の戻り値の共通`data`は`0.4.1-preview.1`から利用できます。
|
|
51
|
+
|
|
47
52
|
## JavaScriptからの直接登録
|
|
48
53
|
|
|
49
54
|
ページdataがすでにJavaScriptにあり、中間fileを作らずMatraに直接渡す場合は
|
|
@@ -88,6 +93,8 @@ console.log(`Generated ${outputs.length} files`)
|
|
|
88
93
|
両APIはlayout、asset、base path、再利用可能なMatra部品に対応します。
|
|
89
94
|
Matra部品は`fn Name(props, children) { ... }`で定義し、単一の文書nodeを返します。
|
|
90
95
|
子要素なしの呼び出しは`Name(props)`または`Name(props, [])`と書きます。
|
|
96
|
+
markup属性では`<meta name="description" content=page.data.description />`のように
|
|
97
|
+
ページdataのmemberを直接参照できます。
|
|
91
98
|
設定schema、入力規則、部品の動作、出力保証の詳細は
|
|
92
99
|
[サイト生成の契約](../../spec/site-generation.ja.md)を参照してください。
|
|
93
100
|
|
package/README.md
CHANGED
|
@@ -45,6 +45,11 @@ same versioned schema.
|
|
|
45
45
|
`buildSitePreview(directory)` performs the same build and additionally returns its `files`,
|
|
46
46
|
`outputDirectory`, `routes` (route to relative output path), `basePath`, and `notFound` for local serving.
|
|
47
47
|
|
|
48
|
+
Set `site.hooks.module` to a trusted `.mjs` file to combine `pages`, `markdown`, and `assets` hooks with
|
|
49
|
+
file-based generation. See the [hook contract](../../spec/site-generation.md#cli-page-and-markdown-hooks)
|
|
50
|
+
for inputs, outputs, and watching. Hooks execute without an OS sandbox or timeout.
|
|
51
|
+
The `assets` hook and shared `data` returned by `pages` require `0.4.1-preview.1` or later.
|
|
52
|
+
|
|
48
53
|
## Direct JavaScript API
|
|
49
54
|
|
|
50
55
|
Use `generateSite()` when page data is already available in JavaScript and
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matra/site-generation",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1-preview.1",
|
|
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.4.
|
|
13
|
+
"@matra/core": "^0.4.1",
|
|
14
14
|
"@matra/html": "^0.4.1",
|
|
15
15
|
"js-yaml": "^5.2.2",
|
|
16
16
|
"markdown-it": "^14.3.0"
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"scripts": {
|
|
29
|
-
"build": "node --check src/index.mjs && node --check src/site.mjs && node --check src/markdown.mjs",
|
|
29
|
+
"build": "node --check src/index.mjs && node --check src/site.mjs && node --check src/site-worker.mjs && node --check src/markdown.mjs",
|
|
30
30
|
"check": "pnpm run build",
|
|
31
31
|
"test": "pnpm run build && node --test"
|
|
32
32
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { parentPort, workerData } from "node:worker_threads"
|
|
2
|
+
import { buildSiteInProcess } from "./site.mjs"
|
|
3
|
+
|
|
4
|
+
try {
|
|
5
|
+
const result = await buildSiteInProcess(
|
|
6
|
+
workerData.root,
|
|
7
|
+
workerData.observe
|
|
8
|
+
? (path, directory) => {
|
|
9
|
+
const status = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT))
|
|
10
|
+
parentPort.postMessage({ type: "input", path, directory, status: status.buffer })
|
|
11
|
+
Atomics.wait(status, 0, 0)
|
|
12
|
+
if (Atomics.load(status, 0) !== 1) throw new Error("Site input monitoring failed")
|
|
13
|
+
}
|
|
14
|
+
: undefined,
|
|
15
|
+
)
|
|
16
|
+
parentPort.postMessage({ type: "result", result })
|
|
17
|
+
} catch (error) {
|
|
18
|
+
parentPort.postMessage({
|
|
19
|
+
type: "error",
|
|
20
|
+
message: error?.message ?? String(error),
|
|
21
|
+
code: error?.code,
|
|
22
|
+
document: error?.document,
|
|
23
|
+
})
|
|
24
|
+
}
|
package/src/site.mjs
CHANGED
|
@@ -15,6 +15,7 @@ import { createRequire } from "node:module"
|
|
|
15
15
|
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path"
|
|
16
16
|
import { pathToFileURL } from "node:url"
|
|
17
17
|
import { types } from "node:util"
|
|
18
|
+
import { Worker } from "node:worker_threads"
|
|
18
19
|
import { evaluateStatic, isMatraAST, parse, staticValueToAST } from "@matra/core"
|
|
19
20
|
import {
|
|
20
21
|
buildLiterate,
|
|
@@ -48,8 +49,18 @@ export async function buildSite(directory) {
|
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
export async function buildSitePreview(directory, onInputs) {
|
|
51
|
-
const
|
|
52
|
-
const
|
|
52
|
+
const root = resolve(directory)
|
|
53
|
+
for (const name of CONFIG_NAMES) onInputs?.(join(root, name))
|
|
54
|
+
const config = await readConfig(root)
|
|
55
|
+
const settings = normalizeSettings(config, root)
|
|
56
|
+
if (settings.hooks) return buildSiteInWorker(root, onInputs)
|
|
57
|
+
return buildSiteInProcess(root, onInputs, config, settings)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function buildSiteInProcess(root, onInputs, config, settings) {
|
|
61
|
+
config ??= await readConfig(root)
|
|
62
|
+
settings ??= normalizeSettings(config, root)
|
|
63
|
+
const site = await loadSite(root, onInputs, config, settings)
|
|
53
64
|
const components = []
|
|
54
65
|
if (settings.componentsDirectory) {
|
|
55
66
|
for (const path of await walkFiles(settings.componentsDirectory, settings.observeDirectory)) {
|
|
@@ -60,7 +71,76 @@ export async function buildSitePreview(directory, onInputs) {
|
|
|
60
71
|
})
|
|
61
72
|
}
|
|
62
73
|
}
|
|
63
|
-
|
|
74
|
+
let templates = new Map()
|
|
75
|
+
let hookAssets = []
|
|
76
|
+
if (settings.hooks) {
|
|
77
|
+
const hooks = await loadHooks(root, settings, onInputs)
|
|
78
|
+
if (hooks.markdown) {
|
|
79
|
+
if (settings.markdown) {
|
|
80
|
+
throw new Error("site.markdown cannot be combined with the markdown hook")
|
|
81
|
+
}
|
|
82
|
+
settings.markdown = hooks.markdown
|
|
83
|
+
settings.markdownLabel = `markdown hook (${settings.hooks.module})`
|
|
84
|
+
}
|
|
85
|
+
if (hooks.pages) {
|
|
86
|
+
let registrations
|
|
87
|
+
try {
|
|
88
|
+
registrations = await hooks.pages({ root })
|
|
89
|
+
} catch (error) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`pages hook (${settings.hooks.module}): ${error?.message ?? String(error)}`,
|
|
92
|
+
{
|
|
93
|
+
cause: error,
|
|
94
|
+
},
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
let direct
|
|
98
|
+
try {
|
|
99
|
+
assertPlainData(registrations, "pages hook result")
|
|
100
|
+
const returned = Array.isArray(registrations) ? { pages: registrations } : registrations
|
|
101
|
+
if (!isRecord(returned)) throw new TypeError("pages hook must return an array or object")
|
|
102
|
+
rejectUnknownKeys(returned, new Set(["pages", "templates", "data"]), "pages hook result")
|
|
103
|
+
if (returned.data !== undefined) {
|
|
104
|
+
if (!isRecord(returned.data)) throw new TypeError("pages hook data must be an object")
|
|
105
|
+
settings.data = { ...structuredClone(returned.data), ...settings.data }
|
|
106
|
+
}
|
|
107
|
+
direct = normalizeDirectSite(
|
|
108
|
+
{
|
|
109
|
+
root,
|
|
110
|
+
output: relative(root, settings.outputDirectory),
|
|
111
|
+
format: settings.format,
|
|
112
|
+
defaultLayout: settings.defaultLayout,
|
|
113
|
+
pages: returned.pages,
|
|
114
|
+
...(returned.templates === undefined ? {} : { templates: returned.templates }),
|
|
115
|
+
},
|
|
116
|
+
site.layouts,
|
|
117
|
+
)
|
|
118
|
+
sortAndValidateRoutes([...site.pages, ...direct.pages])
|
|
119
|
+
const existing = [...site.documents, ...site.pages, ...site.layouts.values(), ...components]
|
|
120
|
+
const existingPaths = new Set(
|
|
121
|
+
existing.map(source => (source.path ?? resolve(root, source.document)).toLowerCase()),
|
|
122
|
+
)
|
|
123
|
+
for (const template of direct.templates.values()) {
|
|
124
|
+
if (existingPaths.has(template.path.toLowerCase())) {
|
|
125
|
+
throw new Error(`Template conflicts with a site input: ${template.document}`)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
await addDirectDocuments(direct.pages, site.documents, settings, [
|
|
129
|
+
...site.pages,
|
|
130
|
+
...site.layouts.values(),
|
|
131
|
+
...components,
|
|
132
|
+
...direct.templates.values(),
|
|
133
|
+
])
|
|
134
|
+
} catch (error) {
|
|
135
|
+
throw new Error(`pages hook (${settings.hooks.module}): ${error.message}`, { cause: error })
|
|
136
|
+
}
|
|
137
|
+
site.pages.push(...direct.pages)
|
|
138
|
+
sortAndValidateRoutes(site.pages)
|
|
139
|
+
templates = direct.templates
|
|
140
|
+
}
|
|
141
|
+
if (hooks.assets) hookAssets = await collectHookAssets(hooks.assets, root, settings, onInputs)
|
|
142
|
+
}
|
|
143
|
+
const result = await generateLoadedSite({ ...site, components, templates, hookAssets })
|
|
64
144
|
return {
|
|
65
145
|
...result,
|
|
66
146
|
outputDirectory: settings.outputDirectory,
|
|
@@ -69,6 +149,164 @@ export async function buildSitePreview(directory, onInputs) {
|
|
|
69
149
|
}
|
|
70
150
|
}
|
|
71
151
|
|
|
152
|
+
function buildSiteInWorker(root, onInputs) {
|
|
153
|
+
return new Promise((resolveBuild, rejectBuild) => {
|
|
154
|
+
const worker = new Worker(new URL("./site-worker.mjs", import.meta.url), {
|
|
155
|
+
workerData: { root, observe: Boolean(onInputs) },
|
|
156
|
+
})
|
|
157
|
+
let settled = false
|
|
158
|
+
worker.on("message", message => {
|
|
159
|
+
if (message.type === "input") {
|
|
160
|
+
const status = new Int32Array(message.status)
|
|
161
|
+
try {
|
|
162
|
+
onInputs?.(message.path, message.directory)
|
|
163
|
+
Atomics.store(status, 0, 1)
|
|
164
|
+
} catch (error) {
|
|
165
|
+
Atomics.store(status, 0, 2)
|
|
166
|
+
settled = true
|
|
167
|
+
void worker.terminate()
|
|
168
|
+
rejectBuild(error)
|
|
169
|
+
} finally {
|
|
170
|
+
Atomics.notify(status, 0)
|
|
171
|
+
}
|
|
172
|
+
} else if (message.type === "result") {
|
|
173
|
+
settled = true
|
|
174
|
+
worker.terminate().then(() => resolveBuild(message.result), rejectBuild)
|
|
175
|
+
} else if (message.type === "error") {
|
|
176
|
+
settled = true
|
|
177
|
+
const error = new Error(message.message)
|
|
178
|
+
if (message.code) error.code = message.code
|
|
179
|
+
if (message.document) error.document = message.document
|
|
180
|
+
worker.terminate().then(() => rejectBuild(error), rejectBuild)
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
worker.on("error", error => {
|
|
184
|
+
settled = true
|
|
185
|
+
rejectBuild(error)
|
|
186
|
+
})
|
|
187
|
+
worker.on("exit", code => {
|
|
188
|
+
if (!settled) rejectBuild(new Error(`Site build worker exited unexpectedly (code ${code})`))
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function loadHooks(root, settings, onInputs) {
|
|
194
|
+
const { module, watch } = settings.hooks
|
|
195
|
+
for (const path of [module, ...watch]) {
|
|
196
|
+
const absolute = resolve(root, path)
|
|
197
|
+
onInputs?.(absolute)
|
|
198
|
+
assertInputOutputSeparation(absolute, settings.outputDirectory)
|
|
199
|
+
await assertSafeHookPath(root, absolute)
|
|
200
|
+
const info = await lstat(absolute)
|
|
201
|
+
if (info.isDirectory()) {
|
|
202
|
+
if (path === module) throw new Error(`Hook module must be a regular file: ${path}`)
|
|
203
|
+
await walkFiles(absolute, settings.observeDirectory)
|
|
204
|
+
} else if (!info.isFile()) {
|
|
205
|
+
throw new Error(`Hook input must be a regular file or directory: ${path}`)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
let hooks
|
|
209
|
+
try {
|
|
210
|
+
hooks = await import(pathToFileURL(resolve(root, module)).href)
|
|
211
|
+
} catch (error) {
|
|
212
|
+
throw new Error(`hook module ${module}: ${error?.message ?? String(error)}`, { cause: error })
|
|
213
|
+
}
|
|
214
|
+
for (const name of ["pages", "markdown", "assets"]) {
|
|
215
|
+
if (hooks[name] !== undefined && typeof hooks[name] !== "function") {
|
|
216
|
+
throw new TypeError(`hook module ${module}: ${name} must be a function`)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return hooks
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function collectHookAssets(hook, root, settings, onInputs) {
|
|
223
|
+
try {
|
|
224
|
+
const result = await hook({ root })
|
|
225
|
+
if (types.isProxy(result) || !Array.isArray(result)) {
|
|
226
|
+
throw new TypeError("assets must return an array")
|
|
227
|
+
}
|
|
228
|
+
const arrayDescriptors = Object.getOwnPropertyDescriptors(result)
|
|
229
|
+
if (
|
|
230
|
+
Reflect.ownKeys(arrayDescriptors).length !== result.length + 1 ||
|
|
231
|
+
Object.keys(arrayDescriptors).some(key => key !== "length" && !/^(0|[1-9]\d*)$/.test(key))
|
|
232
|
+
) {
|
|
233
|
+
throw new TypeError("assets must return a dense array without extra properties")
|
|
234
|
+
}
|
|
235
|
+
const entries = Array.from({ length: result.length }, (_, index) => {
|
|
236
|
+
const descriptor = arrayDescriptors[index]
|
|
237
|
+
if (!descriptor?.enumerable || !Object.hasOwn(descriptor, "value")) {
|
|
238
|
+
throw new TypeError(`assets[${index}] must be a data property`)
|
|
239
|
+
}
|
|
240
|
+
const entry = descriptor.value
|
|
241
|
+
const label = `assets[${index}]`
|
|
242
|
+
if (types.isProxy(entry) || !isRecord(entry))
|
|
243
|
+
throw new TypeError(`${label} must be an object`)
|
|
244
|
+
const descriptors = Object.getOwnPropertyDescriptors(entry)
|
|
245
|
+
const keys = Reflect.ownKeys(descriptors)
|
|
246
|
+
for (const key of keys) {
|
|
247
|
+
const property = descriptors[key]
|
|
248
|
+
if (typeof key !== "string" || !property.enumerable || !Object.hasOwn(property, "value")) {
|
|
249
|
+
throw new TypeError(`${label} must contain enumerable string-keyed data properties`)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
const values = Object.fromEntries(keys.map(key => [key, descriptors[key].value]))
|
|
253
|
+
rejectUnknownKeys(values, new Set(["path", "source", "content"]), label)
|
|
254
|
+
const safePath = (value, name) => {
|
|
255
|
+
if (
|
|
256
|
+
typeof value !== "string" ||
|
|
257
|
+
value.includes("\\") ||
|
|
258
|
+
value.split("/").some(part => !isSafeSegment(part))
|
|
259
|
+
) {
|
|
260
|
+
throw new TypeError(`${label}.${name} must be a safe project-relative path`)
|
|
261
|
+
}
|
|
262
|
+
return value
|
|
263
|
+
}
|
|
264
|
+
const path = safePath(values.path, "path")
|
|
265
|
+
if (Object.hasOwn(values, "source") === Object.hasOwn(values, "content")) {
|
|
266
|
+
throw new TypeError(`${label} must specify exactly one of source or content`)
|
|
267
|
+
}
|
|
268
|
+
if (Object.hasOwn(values, "source")) {
|
|
269
|
+
const source = resolve(root, safePath(values.source, "source"))
|
|
270
|
+
assertInputOutputSeparation(source, settings.outputDirectory)
|
|
271
|
+
return { output: join(...path.split("/")), source }
|
|
272
|
+
}
|
|
273
|
+
if (
|
|
274
|
+
typeof values.content !== "string" &&
|
|
275
|
+
(!(values.content instanceof Uint8Array) || types.isProxy(values.content))
|
|
276
|
+
) {
|
|
277
|
+
throw new TypeError(`${label}.content must be a string or Uint8Array`)
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
output: join(...path.split("/")),
|
|
281
|
+
content: typeof values.content === "string" ? values.content : Buffer.from(values.content),
|
|
282
|
+
}
|
|
283
|
+
})
|
|
284
|
+
for (const entry of entries) {
|
|
285
|
+
if (!entry.source) continue
|
|
286
|
+
onInputs?.(entry.source)
|
|
287
|
+
await assertSafeHookPath(root, entry.source)
|
|
288
|
+
if (!(await lstat(entry.source)).isFile()) {
|
|
289
|
+
throw new Error(`asset source must be a regular file: ${entry.source}`)
|
|
290
|
+
}
|
|
291
|
+
entry.content = await readFile(entry.source)
|
|
292
|
+
}
|
|
293
|
+
return entries
|
|
294
|
+
} catch (error) {
|
|
295
|
+
throw new Error(`assets hook (${settings.hooks.module}): ${error?.message ?? String(error)}`, {
|
|
296
|
+
cause: error,
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async function assertSafeHookPath(root, path) {
|
|
302
|
+
let current = root
|
|
303
|
+
for (const part of relative(root, path).split(sep).filter(Boolean)) {
|
|
304
|
+
current = join(current, part)
|
|
305
|
+
const info = await lstat(current)
|
|
306
|
+
if (info.isSymbolicLink()) throw new Error(`Hook input must not contain a symlink: ${current}`)
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
72
310
|
async function generateLoadedSite({
|
|
73
311
|
root,
|
|
74
312
|
settings,
|
|
@@ -77,6 +315,7 @@ async function generateLoadedSite({
|
|
|
77
315
|
pages,
|
|
78
316
|
components,
|
|
79
317
|
templates = new Map(),
|
|
318
|
+
hookAssets = [],
|
|
80
319
|
}) {
|
|
81
320
|
settings.components = []
|
|
82
321
|
const names = new Set()
|
|
@@ -100,14 +339,20 @@ async function generateLoadedSite({
|
|
|
100
339
|
...(settings.publicDirectory
|
|
101
340
|
? await collectAssets(settings.publicDirectory, "", settings.observeDirectory)
|
|
102
341
|
: []),
|
|
342
|
+
...hookAssets,
|
|
103
343
|
]
|
|
104
344
|
const outputPlan = planOutput(pages, assets, settings)
|
|
105
345
|
settings.routes = new Map(pages.map(page => [page.path, page.route]))
|
|
106
346
|
settings.routesByRoute = new Set(pages.map(page => page.route))
|
|
107
|
-
settings.assetFiles = new Map(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
347
|
+
settings.assetFiles = new Map()
|
|
348
|
+
for (const asset of assets) {
|
|
349
|
+
if (!asset.source) continue
|
|
350
|
+
if (settings.assetFiles.has(asset.source)) {
|
|
351
|
+
throw new Error(`Asset source is registered more than once: ${asset.source}`)
|
|
352
|
+
}
|
|
353
|
+
settings.assetFiles.set(asset.source, `/${asset.output.split(sep).join("/")}`)
|
|
354
|
+
}
|
|
355
|
+
settings.assetOutputs = new Set(assets.map(asset => `/${asset.output.split(sep).join("/")}`))
|
|
111
356
|
await validateOutput(root, settings.outputDirectory, { files: [] })
|
|
112
357
|
const transform =
|
|
113
358
|
settings.markdown === "unified" ? await loadUnifiedPreset(root) : settings.markdown
|
|
@@ -224,7 +469,8 @@ async function loadUnifiedPreset(root) {
|
|
|
224
469
|
async function convertMarkdown(document, page, settings, transform, metadata) {
|
|
225
470
|
const path = relative(settings.root, document.path).split(sep).join("/")
|
|
226
471
|
const firstLine = document.bodyLine
|
|
227
|
-
const location = line =>
|
|
472
|
+
const location = line =>
|
|
473
|
+
`${path}:${firstLine + line - 1}${page ? ` (route ${page.route})` : ""}${settings.markdownLabel ? `: ${settings.markdownLabel}` : ""}`
|
|
228
474
|
let result
|
|
229
475
|
if (transform) {
|
|
230
476
|
try {
|
|
@@ -292,19 +538,45 @@ export async function generateSite(options) {
|
|
|
292
538
|
await assertDirectory(root, directory)
|
|
293
539
|
}
|
|
294
540
|
const documents = await collectDocuments(root, settings)
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
...site.
|
|
300
|
-
|
|
541
|
+
await addDirectDocuments(
|
|
542
|
+
site.pages,
|
|
543
|
+
documents,
|
|
544
|
+
settings,
|
|
545
|
+
[...site.layouts.values(), ...site.templates.values(), ...site.components],
|
|
546
|
+
true,
|
|
547
|
+
)
|
|
548
|
+
return (await generateLoadedSite({ ...site, documents })).files
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
async function addDirectDocuments(
|
|
552
|
+
pages,
|
|
553
|
+
documents,
|
|
554
|
+
settings,
|
|
555
|
+
existingSources,
|
|
556
|
+
checkAllSources = false,
|
|
557
|
+
) {
|
|
558
|
+
const existingPaths = new Set(
|
|
559
|
+
[...documents, ...existingSources]
|
|
560
|
+
.filter(source => source.path || source.document)
|
|
561
|
+
.map(source => (source.path ?? resolve(settings.root, source.document)).toLowerCase()),
|
|
562
|
+
)
|
|
563
|
+
for (const page of pages) {
|
|
564
|
+
if (existingPaths.has(page.path.toLowerCase())) {
|
|
565
|
+
throw new Error(`Collected document conflicts with a logical document: ${page.document}`)
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
const logicalPaths = new Set(
|
|
569
|
+
[...pages, ...(checkAllSources ? existingSources : [])].map(source =>
|
|
570
|
+
source.path.toLowerCase(),
|
|
571
|
+
),
|
|
572
|
+
)
|
|
301
573
|
for (const document of documents) {
|
|
302
574
|
if (logicalPaths.has(document.path.toLowerCase())) {
|
|
303
575
|
throw new Error(`Collected document conflicts with a logical document: ${document.path}`)
|
|
304
576
|
}
|
|
305
577
|
}
|
|
306
578
|
const ids = new Set(documents.map(document => document.id.toLowerCase()))
|
|
307
|
-
for (const page of
|
|
579
|
+
for (const page of pages.filter(page => page.kind === "markdown")) {
|
|
308
580
|
const id = page.document
|
|
309
581
|
if (ids.has(id.toLowerCase())) throw new Error(`Duplicate document ID ignoring case: ${id}`)
|
|
310
582
|
ids.add(id.toLowerCase())
|
|
@@ -322,10 +594,9 @@ export async function generateSite(options) {
|
|
|
322
594
|
})
|
|
323
595
|
}
|
|
324
596
|
documents.sort((left, right) => left.id.localeCompare(right.id, "en"))
|
|
325
|
-
return (await generateLoadedSite({ ...site, documents })).files
|
|
326
597
|
}
|
|
327
598
|
|
|
328
|
-
function normalizeDirectSite(input) {
|
|
599
|
+
function normalizeDirectSite(input, inheritedLayouts) {
|
|
329
600
|
if (types.isProxy(input) || !isRecord(input))
|
|
330
601
|
throw new TypeError("generateSite options must be an object")
|
|
331
602
|
const descriptors = Object.getOwnPropertyDescriptors(input)
|
|
@@ -433,7 +704,7 @@ function normalizeDirectSite(input) {
|
|
|
433
704
|
}),
|
|
434
705
|
)
|
|
435
706
|
}
|
|
436
|
-
const layouts = namedSources(options.layouts, "layouts")
|
|
707
|
+
const layouts = inheritedLayouts ?? namedSources(options.layouts, "layouts")
|
|
437
708
|
const templates = namedSources(options.templates, "templates")
|
|
438
709
|
selectedLayout(undefined, settings.defaultLayout, layouts, "defaultLayout")
|
|
439
710
|
const components = [...namedSources(options.components, "components").values()]
|
|
@@ -505,10 +776,10 @@ export async function getSiteProgramDocuments(directory) {
|
|
|
505
776
|
].sort((left, right) => left.localeCompare(right, "en"))
|
|
506
777
|
}
|
|
507
778
|
|
|
508
|
-
async function loadSite(root, onInputs) {
|
|
779
|
+
async function loadSite(root, onInputs, config, normalizedSettings) {
|
|
509
780
|
for (const name of CONFIG_NAMES) onInputs?.(join(root, name))
|
|
510
|
-
|
|
511
|
-
const settings = normalizeSettings(config, root)
|
|
781
|
+
config ??= await readConfig(root)
|
|
782
|
+
const settings = normalizedSettings ?? normalizeSettings(config, root)
|
|
512
783
|
const inputs = [
|
|
513
784
|
settings.pagesDirectory,
|
|
514
785
|
...settings.documentDirectories,
|
|
@@ -522,8 +793,15 @@ async function loadSite(root, onInputs) {
|
|
|
522
793
|
onInputs?.(input)
|
|
523
794
|
}
|
|
524
795
|
if (onInputs) settings.observeDirectory = path => onInputs(path, true)
|
|
796
|
+
const pagesExist = await assertDirectory(root, settings.pagesDirectory, {
|
|
797
|
+
optional: Boolean(settings.hooks),
|
|
798
|
+
})
|
|
799
|
+
if (!pagesExist) {
|
|
800
|
+
settings.documentDirectories = settings.documentDirectories.filter(
|
|
801
|
+
path => path !== settings.pagesDirectory,
|
|
802
|
+
)
|
|
803
|
+
}
|
|
525
804
|
await Promise.all([
|
|
526
|
-
assertDirectory(root, settings.pagesDirectory),
|
|
527
805
|
...settings.documentDirectories.map(path => assertDirectory(root, path)),
|
|
528
806
|
assertDirectory(root, settings.layoutsDirectory, { optional: true }),
|
|
529
807
|
...(settings.componentsDirectory ? [assertDirectory(root, settings.componentsDirectory)] : []),
|
|
@@ -532,7 +810,7 @@ async function loadSite(root, onInputs) {
|
|
|
532
810
|
])
|
|
533
811
|
const documents = await collectDocuments(root, settings)
|
|
534
812
|
const layouts = await collectLayouts(root, settings)
|
|
535
|
-
const pages = await collectPages(root, settings, documents, layouts)
|
|
813
|
+
const pages = pagesExist ? await collectPages(root, settings, documents, layouts) : []
|
|
536
814
|
return { root, settings, documents, layouts, pages }
|
|
537
815
|
}
|
|
538
816
|
|
|
@@ -589,6 +867,8 @@ function normalizeSettings(config, root) {
|
|
|
589
867
|
"markdown",
|
|
590
868
|
"basePath",
|
|
591
869
|
"data",
|
|
870
|
+
"hooks",
|
|
871
|
+
"includeDocumentsInTemplates",
|
|
592
872
|
]),
|
|
593
873
|
"site",
|
|
594
874
|
)
|
|
@@ -622,6 +902,19 @@ function normalizeSettings(config, root) {
|
|
|
622
902
|
? undefined
|
|
623
903
|
: resolveDirectory(site.components, "components", site.components)
|
|
624
904
|
const outputDirectory = resolveDirectory(site.output, "output", "dist")
|
|
905
|
+
let hooks
|
|
906
|
+
if (site.hooks !== undefined) {
|
|
907
|
+
if (!isRecord(site.hooks)) throw new TypeError("site.hooks must be an object")
|
|
908
|
+
rejectUnknownKeys(site.hooks, new Set(["module", "watch"]), "site.hooks")
|
|
909
|
+
if (typeof site.hooks.module !== "string" || !site.hooks.module.endsWith(".mjs")) {
|
|
910
|
+
throw new TypeError("site.hooks.module must be a project-relative .mjs path")
|
|
911
|
+
}
|
|
912
|
+
resolveDirectory(site.hooks.module, "hooks.module")
|
|
913
|
+
const watch = site.hooks.watch ?? []
|
|
914
|
+
if (!Array.isArray(watch)) throw new TypeError("site.hooks.watch must be an array of paths")
|
|
915
|
+
for (const path of watch) resolveDirectory(path, "hooks.watch")
|
|
916
|
+
hooks = { module: site.hooks.module, watch }
|
|
917
|
+
}
|
|
625
918
|
const assetsDirectory =
|
|
626
919
|
site.assets === undefined ? undefined : resolveDirectory(site.assets, "assets", site.assets)
|
|
627
920
|
const publicDirectory =
|
|
@@ -652,6 +945,12 @@ function normalizeSettings(config, root) {
|
|
|
652
945
|
if (site.data !== undefined && !isRecord(site.data)) {
|
|
653
946
|
throw new TypeError("site.data must be an object")
|
|
654
947
|
}
|
|
948
|
+
if (
|
|
949
|
+
site.includeDocumentsInTemplates !== undefined &&
|
|
950
|
+
typeof site.includeDocumentsInTemplates !== "boolean"
|
|
951
|
+
) {
|
|
952
|
+
throw new TypeError("site.includeDocumentsInTemplates must be a boolean")
|
|
953
|
+
}
|
|
655
954
|
if (
|
|
656
955
|
site.defaultLayout !== undefined &&
|
|
657
956
|
site.defaultLayout !== false &&
|
|
@@ -673,10 +972,12 @@ function normalizeSettings(config, root) {
|
|
|
673
972
|
? undefined
|
|
674
973
|
: normalizeRoute(site.notFound, format, "site.notFound"),
|
|
675
974
|
markdown: site.markdown,
|
|
975
|
+
hooks,
|
|
676
976
|
defaultLayout: site.defaultLayout === undefined ? false : site.defaultLayout,
|
|
677
977
|
format,
|
|
678
978
|
basePath,
|
|
679
979
|
data: site.data ?? {},
|
|
980
|
+
includeDocumentsInTemplates: site.includeDocumentsInTemplates ?? true,
|
|
680
981
|
}
|
|
681
982
|
}
|
|
682
983
|
|