@hamur/content 0.0.0-stage → 0.0.2
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 +22 -0
- package/README.md +14 -2
- package/dist/chunks/bun-render-65mwzeq2.js +58 -0
- package/dist/chunks/bun-render-7jxnxg3v.js +714 -0
- package/dist/chunks/bun-render-mxhn5twt.js +130 -0
- package/dist/chunks/bun-render-w7fh3fg7.js +36 -0
- package/dist/src/adapters/html.js +134 -0
- package/dist/src/adapters/preact.js +126 -0
- package/dist/src/adapters/react.js +116 -0
- package/dist/src/adapters/solid.js +153 -0
- package/dist/src/adapters/svelte.js +90 -0
- package/dist/src/adapters/vue.js +166 -0
- package/dist/src/index.js +2732 -0
- package/dist/src/query.js +97 -0
- package/dist/src/render.js +12 -0
- package/dist/src/server/index.js +349 -0
- package/dist/types/src/adapters/html.d.ts +54 -0
- package/dist/types/src/adapters/page.d.ts +21 -0
- package/dist/types/src/adapters/preact.d.ts +53 -0
- package/dist/types/src/adapters/prose.d.ts +21 -0
- package/dist/types/src/adapters/react.d.ts +53 -0
- package/dist/types/src/adapters/search.d.ts +12 -0
- package/dist/types/src/adapters/solid.d.ts +54 -0
- package/dist/types/src/adapters/svelte-page.d.ts +9 -0
- package/dist/types/src/adapters/svelte-search.d.ts +9 -0
- package/dist/types/src/adapters/svelte.d.ts +24 -0
- package/dist/types/src/adapters/vue.d.ts +189 -0
- package/dist/types/src/build/api.d.ts +24 -0
- package/dist/types/src/build/dev-route.d.ts +4 -0
- package/dist/types/src/build/dev.d.ts +48 -0
- package/dist/types/src/build/graphql.d.ts +10 -0
- package/dist/types/src/build/image.d.ts +16 -0
- package/dist/types/src/build/index.d.ts +76 -0
- package/dist/types/src/build/live.d.ts +6 -0
- package/dist/types/src/build/prerender.d.ts +8 -0
- package/dist/types/src/build/query.d.ts +10 -0
- package/dist/types/src/build/raw.d.ts +23 -0
- package/dist/types/src/build/search.d.ts +9 -0
- package/dist/types/src/build/types.d.ts +16 -0
- package/dist/types/src/component.d.ts +10 -0
- package/dist/types/src/config.d.ts +6 -0
- package/dist/types/src/define.d.ts +14 -0
- package/dist/types/src/error.d.ts +4 -0
- package/dist/types/src/image.d.ts +15 -0
- package/dist/types/src/index.d.ts +64 -0
- package/dist/types/src/loaders/data.d.ts +12 -0
- package/dist/types/src/loaders/file.d.ts +3 -0
- package/dist/types/src/loaders/git.d.ts +27 -0
- package/dist/types/src/loaders/glob.d.ts +14 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/query.d.ts +17 -0
- package/dist/types/src/render.d.ts +9 -0
- package/dist/types/src/runtime/client.d.ts +25 -0
- package/dist/types/src/runtime/dev-client.d.ts +4 -0
- package/dist/types/src/runtime/dev-plugin.d.ts +12 -0
- package/dist/types/src/runtime/dev-store.d.ts +5 -0
- package/dist/types/src/runtime/live.d.ts +36 -0
- package/dist/types/src/runtime/navigation.d.ts +35 -0
- package/dist/types/src/runtime/page-plugin.d.ts +2 -0
- package/dist/types/src/runtime/page-source.d.ts +12 -0
- package/dist/types/src/runtime/query.d.ts +100 -0
- package/dist/types/src/runtime/raw-path.d.ts +2 -0
- package/dist/types/src/runtime/refresh-body.d.ts +4 -0
- package/dist/types/src/runtime/search-loaders.d.ts +7 -0
- package/dist/types/src/runtime/search-plugin.d.ts +2 -0
- package/dist/types/src/runtime/search-store.d.ts +19 -0
- package/dist/types/src/runtime/search.d.ts +28 -0
- package/dist/types/src/runtime/sections.d.ts +36 -0
- package/dist/types/src/runtime/server-store.d.ts +4 -0
- package/dist/types/src/runtime/store.d.ts +31 -0
- package/dist/types/src/server/door-dev-static.d.ts +3 -0
- package/dist/types/src/server/door-dev.d.ts +4 -0
- package/dist/types/src/server/door-static.d.ts +2 -0
- package/dist/types/src/server/door.d.ts +3 -0
- package/dist/types/src/server/endpoint.d.ts +1 -0
- package/dist/types/src/server/graphql.d.ts +24 -0
- package/dist/types/src/server/index.d.ts +10 -0
- package/dist/types/src/server/install.d.ts +1 -0
- package/dist/types/src/server/live.d.ts +10 -0
- package/dist/types/src/server/query.d.ts +15 -0
- package/dist/types/src/server/refresh.d.ts +2 -0
- package/dist/types/src/server/rest.d.ts +62 -0
- package/dist/types/src/server/search.d.ts +5 -0
- package/dist/types/src/server-config.d.ts +12 -0
- package/dist/types/src/source.d.ts +24 -0
- package/dist/types/src/types.d.ts +293 -0
- package/package.json +140 -4
- package/src/adapters/ContentRenderer.svelte +27 -0
- package/src/adapters/ProseA.svelte +11 -0
- package/src/adapters/ProseBlockquote.svelte +7 -0
- package/src/adapters/ProseCode.svelte +7 -0
- package/src/adapters/ProseEm.svelte +7 -0
- package/src/adapters/ProseH1.svelte +7 -0
- package/src/adapters/ProseH2.svelte +7 -0
- package/src/adapters/ProseH3.svelte +7 -0
- package/src/adapters/ProseH4.svelte +7 -0
- package/src/adapters/ProseH5.svelte +7 -0
- package/src/adapters/ProseH6.svelte +7 -0
- package/src/adapters/ProseHr.svelte +5 -0
- package/src/adapters/ProseImg.svelte +5 -0
- package/src/adapters/ProseLi.svelte +7 -0
- package/src/adapters/ProseOl.svelte +7 -0
- package/src/adapters/ProseP.svelte +7 -0
- package/src/adapters/ProsePre.svelte +21 -0
- package/src/adapters/ProseStrong.svelte +7 -0
- package/src/adapters/ProseTable.svelte +7 -0
- package/src/adapters/ProseUl.svelte +7 -0
- package/src/adapters/html.ts +152 -0
- package/src/adapters/page.ts +52 -0
- package/src/adapters/preact.ts +154 -0
- package/src/adapters/prose.ts +55 -0
- package/src/adapters/react.ts +142 -0
- package/src/adapters/search.ts +32 -0
- package/src/adapters/solid.ts +183 -0
- package/src/adapters/svelte-defaults.ts +41 -0
- package/src/adapters/svelte-page.ts +29 -0
- package/src/adapters/svelte-search.ts +36 -0
- package/src/adapters/svelte.ts +30 -0
- package/src/adapters/test/OwnA.svelte +7 -0
- package/src/adapters/test/OwnH1.svelte +7 -0
- package/src/adapters/test/OwnPre.svelte +7 -0
- package/src/adapters/test/support.ts +106 -0
- package/src/adapters/vue.ts +182 -0
- package/src/build/api.ts +201 -0
- package/src/build/dev-route.ts +5 -0
- package/src/build/dev.ts +299 -0
- package/src/build/graphql.ts +197 -0
- package/src/build/image.ts +60 -0
- package/src/build/index.ts +597 -0
- package/src/build/live.ts +34 -0
- package/src/build/prerender.ts +34 -0
- package/src/build/query.ts +70 -0
- package/src/build/raw.ts +157 -0
- package/src/build/search.ts +64 -0
- package/src/build/types.ts +108 -0
- package/src/component.ts +86 -0
- package/src/config.ts +156 -0
- package/src/define.ts +33 -0
- package/src/env.d.ts +34 -0
- package/src/error.ts +4 -0
- package/src/image.ts +41 -0
- package/src/index.ts +103 -0
- package/src/loaders/data.ts +186 -0
- package/src/loaders/file.ts +53 -0
- package/src/loaders/git.ts +266 -0
- package/src/loaders/glob.ts +179 -0
- package/src/module.ts +26 -0
- package/src/query.ts +82 -0
- package/src/render.ts +16 -0
- package/src/runtime/client.ts +75 -0
- package/src/runtime/dev-client.ts +6 -0
- package/src/runtime/dev-plugin.ts +64 -0
- package/src/runtime/dev-store.ts +19 -0
- package/src/runtime/live.ts +331 -0
- package/src/runtime/navigation.ts +225 -0
- package/src/runtime/page-plugin.ts +13 -0
- package/src/runtime/page-source.ts +22 -0
- package/src/runtime/query.ts +605 -0
- package/src/runtime/raw-path.ts +2 -0
- package/src/runtime/refresh-body.ts +22 -0
- package/src/runtime/search-loaders.ts +14 -0
- package/src/runtime/search-plugin.ts +6 -0
- package/src/runtime/search-store.ts +73 -0
- package/src/runtime/search.ts +126 -0
- package/src/runtime/sections.ts +155 -0
- package/src/runtime/server-store.ts +12 -0
- package/src/runtime/store.ts +59 -0
- package/src/server/door-dev-static.ts +6 -0
- package/src/server/door-dev.ts +7 -0
- package/src/server/door-static.ts +4 -0
- package/src/server/door.ts +6 -0
- package/src/server/endpoint.ts +17 -0
- package/src/server/graphql.ts +59 -0
- package/src/server/index.ts +17 -0
- package/src/server/install.ts +6 -0
- package/src/server/live.ts +51 -0
- package/src/server/query.ts +46 -0
- package/src/server/refresh.ts +19 -0
- package/src/server/rest.ts +157 -0
- package/src/server/search.ts +22 -0
- package/src/server-config.ts +24 -0
- package/src/source.ts +99 -0
- package/src/types.ts +282 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { DiagnosticError, addAppPlugin, addServer, addTemplate, createResolver, type HamurContext } from "hamur/kit"
|
|
2
|
+
import type { ContentConfig, ContentOptions } from "../types.ts"
|
|
3
|
+
import { resolveApi, setupRest } from "./api.ts"
|
|
4
|
+
import { DEV_ROUTE } from "./dev-route.ts"
|
|
5
|
+
import { setupGraphQL } from "./graphql.ts"
|
|
6
|
+
import { resolveRaw } from "./raw.ts"
|
|
7
|
+
|
|
8
|
+
const SERVER = "@hamur/content/server"
|
|
9
|
+
|
|
10
|
+
/** `content.clientQuery`, `"local"` by default on the `static` preset, which serves no endpoint. */
|
|
11
|
+
export function resolveClientQuery(value: unknown, preset: string): "api" | "local" {
|
|
12
|
+
if (value === undefined) return preset === "static" ? "local" : "api"
|
|
13
|
+
if (value !== "api" && value !== "local")
|
|
14
|
+
throw new DiagnosticError(
|
|
15
|
+
"HAMUR_E5332",
|
|
16
|
+
`content.clientQuery must be "api" or "local" (got ${JSON.stringify(value)})`,
|
|
17
|
+
)
|
|
18
|
+
if (value === "api" && preset === "static")
|
|
19
|
+
throw new DiagnosticError("HAMUR_E5333", 'content.clientQuery "api" needs a server; the static preset uses "local"')
|
|
20
|
+
return value
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** `#hamur/content/client`: every collection's name, type, exposure and order; under `"local"` an exposed one imports its chunk, or in `hamur dev` fetches it from `devLoad`'s module. */
|
|
24
|
+
export function clientModule(config: ContentConfig, mode: "api" | "local", devLoad?: string, raw?: string): string {
|
|
25
|
+
const collections = Object.entries(config.collections).map(([name, { type, expose = true, order }]) => {
|
|
26
|
+
const meta = JSON.stringify({ type, expose, ...(order ? { order } : {}) })
|
|
27
|
+
const loader = devLoad
|
|
28
|
+
? `devLoad(${JSON.stringify(`${DEV_ROUTE}/chunks/${name}.json`)})`
|
|
29
|
+
: `() => import(${JSON.stringify(`#hamur/content/chunks/${name}.json`)})`
|
|
30
|
+
const load = mode === "local" && expose ? `, load: ${loader} }` : ""
|
|
31
|
+
return ` ${JSON.stringify(name)}: ${load ? `${meta.slice(0, -1)}${load}` : meta},`
|
|
32
|
+
})
|
|
33
|
+
const imports = devLoad && mode === "local" ? [`import { devLoad } from ${JSON.stringify(devLoad)};`, ""] : []
|
|
34
|
+
return [
|
|
35
|
+
...imports,
|
|
36
|
+
`export default {`,
|
|
37
|
+
` mode: ${JSON.stringify(mode)},`,
|
|
38
|
+
...(raw === undefined ? [] : [` raw: ${JSON.stringify(raw)},`]),
|
|
39
|
+
" collections: {",
|
|
40
|
+
...collections,
|
|
41
|
+
" },",
|
|
42
|
+
"};",
|
|
43
|
+
"",
|
|
44
|
+
].join("\n")
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The module's one door: `POST /_hamur/content/query` except on `static`, and in `hamur dev` the refresh route. */
|
|
48
|
+
export function doorOf(preset: string, dev: boolean): string {
|
|
49
|
+
if (dev) return preset === "static" ? "../server/door-dev-static.ts" : "../server/door-dev.ts"
|
|
50
|
+
return preset === "static" ? "../server/door-static.ts" : "../server/door.ts"
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The door with `POST /_hamur/content/query` (none on `static`), the store handed to `queryCollection`, and `#hamur/content/client`; with `content.api.rest` the door is generated, and `content.api.graphql` adds the collections' schema. */
|
|
54
|
+
export function setupQuery(options: ContentOptions, config: ContentConfig, hamur: HamurContext): void {
|
|
55
|
+
const { preset } = hamur.options
|
|
56
|
+
const mode = resolveClientQuery(options.clientQuery, preset)
|
|
57
|
+
const client = (hamur.options.build.importProtection.client ??= {})
|
|
58
|
+
;(client.deny ??= []).push(SERVER)
|
|
59
|
+
const api = resolveApi(options.api, preset, hamur.options.server.graphql !== false)
|
|
60
|
+
if (api.graphql) setupGraphQL(config, hamur)
|
|
61
|
+
if (api.rest) setupRest(config, hamur)
|
|
62
|
+
else addServer(createResolver(import.meta.url).resolve(doorOf(preset, hamur.options.dev)))
|
|
63
|
+
const devLoad = hamur.options.dev ? createResolver(import.meta.url).resolve("../runtime/dev-client.ts") : undefined
|
|
64
|
+
addAppPlugin({ src: createResolver(import.meta.url).resolve("../runtime/page-plugin.ts"), mode: "all" })
|
|
65
|
+
addTemplate({
|
|
66
|
+
filename: "content/client",
|
|
67
|
+
getContents: () =>
|
|
68
|
+
clientModule(config, mode, devLoad, resolveRaw(options.raw) ? hamur.options.app.baseURL : undefined),
|
|
69
|
+
})
|
|
70
|
+
}
|
package/src/build/raw.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { DiagnosticError, emitPublicAsset, type HamurContext } from "hamur/kit"
|
|
2
|
+
import { rawPath } from "../runtime/raw-path.ts"
|
|
3
|
+
import { runQuery, type QueryClause, type QueryCondition } from "../runtime/query.ts"
|
|
4
|
+
import type { ContentConfig, ContentEntry, ContentOptions, LlmsOptions, PageEntry } from "../types.ts"
|
|
5
|
+
import type { ContentIndex } from "./index.ts"
|
|
6
|
+
|
|
7
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
8
|
+
value !== null && typeof value === "object" && !Array.isArray(value)
|
|
9
|
+
|
|
10
|
+
const shown = (value: unknown): string => JSON.stringify(value) ?? String(value)
|
|
11
|
+
|
|
12
|
+
export { rawPath }
|
|
13
|
+
|
|
14
|
+
/** `content.raw`, `false` by default; `"body"` serves the loader's body, `true` the file on disk. */
|
|
15
|
+
export function resolveRaw(value: unknown): boolean | "body" {
|
|
16
|
+
if (value === undefined) return false
|
|
17
|
+
if (typeof value !== "boolean" && value !== "body")
|
|
18
|
+
throw new DiagnosticError("HAMUR_E5327", `content.raw must be a boolean or "body" (got ${shown(value)})`)
|
|
19
|
+
return value
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Checks `content.llms` against the collections and `site.url`. */
|
|
23
|
+
export function resolveLlms(
|
|
24
|
+
value: unknown,
|
|
25
|
+
config: ContentConfig,
|
|
26
|
+
siteUrl: string | undefined,
|
|
27
|
+
): LlmsOptions | undefined {
|
|
28
|
+
if (value === undefined || value === false) return undefined
|
|
29
|
+
const shape =
|
|
30
|
+
"content.llms must be { title: string, description?: string, sections: Array<{ title, collection, where? }> }"
|
|
31
|
+
if (!isRecord(value) || typeof value.title !== "string" || value.title === "")
|
|
32
|
+
throw new DiagnosticError("HAMUR_E5328", shape)
|
|
33
|
+
if (value.description !== undefined && typeof value.description !== "string")
|
|
34
|
+
throw new DiagnosticError("HAMUR_E5328", shape)
|
|
35
|
+
if (!Array.isArray(value.sections)) throw new DiagnosticError("HAMUR_E5328", shape)
|
|
36
|
+
if (siteUrl === undefined)
|
|
37
|
+
throw new DiagnosticError("HAMUR_E5329", "content.llms needs site.url for absolute page URLs")
|
|
38
|
+
value.sections.forEach((section: unknown, index) => {
|
|
39
|
+
const at = `content.llms.sections[${index}]`
|
|
40
|
+
if (!isRecord(section) || typeof section.title !== "string" || typeof section.collection !== "string")
|
|
41
|
+
throw new DiagnosticError("HAMUR_E5328", `${at} must be { title: string, collection: string, where? }`)
|
|
42
|
+
const collection = Object.hasOwn(config.collections, section.collection)
|
|
43
|
+
? config.collections[section.collection]
|
|
44
|
+
: undefined
|
|
45
|
+
if (!collection)
|
|
46
|
+
throw new DiagnosticError(
|
|
47
|
+
"HAMUR_E5330",
|
|
48
|
+
`${at}.collection ${shown(section.collection)} is not a collection of content.config.ts`,
|
|
49
|
+
)
|
|
50
|
+
if (collection.type !== "page")
|
|
51
|
+
throw new DiagnosticError("HAMUR_E5331", `${at}.collection ${shown(section.collection)} is not a page collection`)
|
|
52
|
+
const where = section.where
|
|
53
|
+
if (where !== undefined && !(isRecord(where) || (Array.isArray(where) && where.every(isRecord))))
|
|
54
|
+
throw new DiagnosticError(
|
|
55
|
+
"HAMUR_E5328",
|
|
56
|
+
`${at}.where must be a condition { field, operator, value? } or an array of them`,
|
|
57
|
+
)
|
|
58
|
+
})
|
|
59
|
+
return value as unknown as LlmsOptions
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const clausesOf = (where: QueryCondition | QueryCondition[] | undefined): QueryClause[] =>
|
|
63
|
+
where === undefined
|
|
64
|
+
? []
|
|
65
|
+
: (Array.isArray(where) ? where : [where]).map((condition) => ({ join: "AND", ...condition }))
|
|
66
|
+
|
|
67
|
+
export interface LlmsSection {
|
|
68
|
+
title: string
|
|
69
|
+
entries: PageEntry[]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Each section's page entries, in the collection's order, which `where` narrows. */
|
|
73
|
+
export function llmsSections(
|
|
74
|
+
llms: LlmsOptions,
|
|
75
|
+
config: ContentConfig,
|
|
76
|
+
index: Record<string, ContentEntry[]>,
|
|
77
|
+
): LlmsSection[] {
|
|
78
|
+
return llms.sections.map((section) => {
|
|
79
|
+
const where = clausesOf(section.where)
|
|
80
|
+
const found = runQuery(
|
|
81
|
+
section.collection,
|
|
82
|
+
config.collections[section.collection]!,
|
|
83
|
+
index[section.collection] ?? [],
|
|
84
|
+
where.length ? { where } : {},
|
|
85
|
+
)
|
|
86
|
+
return { title: section.title, entries: found as PageEntry[] }
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const oneLine = (text: string): string => text.replace(/\s+/g, " ").trim()
|
|
91
|
+
|
|
92
|
+
const linkText = (text: string): string => oneLine(text).replace(/[[\]]/g, "\\$&")
|
|
93
|
+
|
|
94
|
+
/** An absolute URL of a page: `site.url`, `app.baseURL` and the entry's path. */
|
|
95
|
+
export const pageUrl = (origin: string, baseURL: string, path: string): string =>
|
|
96
|
+
`${origin}${baseURL.replace(/\/$/, "")}${path}`
|
|
97
|
+
|
|
98
|
+
/** The llmstxt.org file. */
|
|
99
|
+
export function llmsText(llms: LlmsOptions, sections: readonly LlmsSection[], origin: string, baseURL: string): string {
|
|
100
|
+
const lines = [`# ${oneLine(llms.title)}`, ""]
|
|
101
|
+
if (llms.description) lines.push(`> ${oneLine(llms.description)}`, "")
|
|
102
|
+
for (const section of sections) {
|
|
103
|
+
lines.push(`## ${oneLine(section.title)}`, "")
|
|
104
|
+
for (const entry of section.entries) {
|
|
105
|
+
const description = entry.description ? `: ${oneLine(entry.description)}` : ""
|
|
106
|
+
lines.push(`- [${linkText(entry.title ?? entry.path)}](${pageUrl(origin, baseURL, entry.path)})${description}`)
|
|
107
|
+
}
|
|
108
|
+
lines.push("")
|
|
109
|
+
}
|
|
110
|
+
return `${lines.join("\n").trimEnd()}\n`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Every listed page's source joined by a rule. */
|
|
114
|
+
export const llmsFullText = (sources: readonly string[]): string => `${sources.join("\n\n---\n\n")}\n`
|
|
115
|
+
|
|
116
|
+
/** Writes `<path>.md`, `llms.txt` and `llms-full.txt` as public files, so every preset serves them and the static preset's output holds them. */
|
|
117
|
+
export function setupRaw(
|
|
118
|
+
options: ContentOptions,
|
|
119
|
+
config: ContentConfig,
|
|
120
|
+
index: ContentIndex,
|
|
121
|
+
hamur: HamurContext,
|
|
122
|
+
): void {
|
|
123
|
+
const raw = resolveRaw(options.raw)
|
|
124
|
+
const llms = resolveLlms(options.llms, config, hamur.options.site.url)
|
|
125
|
+
if (!raw && !llms) return
|
|
126
|
+
hamur.hooks.hook("build:before", async () => {
|
|
127
|
+
const entries = await index.read()
|
|
128
|
+
const text = async (entry: PageEntry): Promise<string | undefined> => {
|
|
129
|
+
if (raw === "body") return index.body(entry.collection, entry.id)
|
|
130
|
+
const file = index.file(entry.collection, entry.id)
|
|
131
|
+
return file === undefined ? undefined : Bun.file(file).text()
|
|
132
|
+
}
|
|
133
|
+
if (raw) {
|
|
134
|
+
for (const [name, collection] of Object.entries(config.collections)) {
|
|
135
|
+
if (collection.type !== "page") continue
|
|
136
|
+
for (const entry of (entries[name] ?? []) as PageEntry[]) {
|
|
137
|
+
const source = await text(entry)
|
|
138
|
+
if (source !== undefined) emitPublicAsset(rawPath(entry.path), source)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (llms) {
|
|
143
|
+
const sections = llmsSections(llms, config, entries)
|
|
144
|
+
emitPublicAsset("/llms.txt", llmsText(llms, sections, hamur.options.site.url!, hamur.options.app.baseURL))
|
|
145
|
+
const seen = new Set<string>()
|
|
146
|
+
const sources: string[] = []
|
|
147
|
+
for (const entry of sections.flatMap((section) => section.entries)) {
|
|
148
|
+
const key = `${entry.collection}\0${entry.id}`
|
|
149
|
+
if (seen.has(key)) continue
|
|
150
|
+
seen.add(key)
|
|
151
|
+
const source = await text(entry)
|
|
152
|
+
if (source !== undefined) sources.push(source.trimEnd())
|
|
153
|
+
}
|
|
154
|
+
emitPublicAsset("/llms-full.txt", llmsFullText(sources))
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { DiagnosticError, addAppPlugin, addTemplate, createResolver } from "hamur/kit"
|
|
2
|
+
import { buildSearchIndex, type SearchIndex } from "../runtime/search.ts"
|
|
3
|
+
import { resolveSectionOptions, sectionsOf, type SearchSection } from "../runtime/sections.ts"
|
|
4
|
+
import type { ContentConfig, ContentEntry, ContentOptions, PageEntry } from "../types.ts"
|
|
5
|
+
import { DEV_ROUTE } from "./dev-route.ts"
|
|
6
|
+
|
|
7
|
+
/** Code blocks are not indexed: they are a large share of a docs site and mostly noise as search text. */
|
|
8
|
+
const INDEXED = resolveSectionOptions({ ignoredTags: ["pre"] })
|
|
9
|
+
|
|
10
|
+
/** `content.search.collections`, each an exposed page collection of `content.config.ts`. */
|
|
11
|
+
export function resolveSearch(value: ContentOptions["search"] | undefined, config: ContentConfig): string[] {
|
|
12
|
+
if (value === undefined || value === false) return []
|
|
13
|
+
const { collections } = (value ?? {}) as { collections?: unknown }
|
|
14
|
+
if (!Array.isArray(collections) || !collections.every((name) => typeof name === "string"))
|
|
15
|
+
throw new DiagnosticError("HAMUR_E5323", "content.search must be false or { collections: string[] }")
|
|
16
|
+
collections.forEach((name: string, index) => {
|
|
17
|
+
const at = `content.search.collections[${index}] ${JSON.stringify(name)}`
|
|
18
|
+
const collection = Object.hasOwn(config.collections, name) ? config.collections[name] : undefined
|
|
19
|
+
if (!collection) throw new DiagnosticError("HAMUR_E5324", `${at} is not a collection of content.config.ts`)
|
|
20
|
+
if (collection.expose === false)
|
|
21
|
+
throw new DiagnosticError(
|
|
22
|
+
"HAMUR_E5325",
|
|
23
|
+
`${at} is not exposed to the client (expose: false); the search index is served to the browser`,
|
|
24
|
+
)
|
|
25
|
+
if (collection.type !== "page")
|
|
26
|
+
throw new DiagnosticError("HAMUR_E5326", `${at} is not a page collection; only pages have sections`)
|
|
27
|
+
})
|
|
28
|
+
return [...new Set(collections as string[])]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function searchIndexOf(entries: readonly ContentEntry[]): SearchIndex {
|
|
32
|
+
const sections: SearchSection[] = (entries as PageEntry[]).flatMap((entry) => sectionsOf(entry, INDEXED))
|
|
33
|
+
return buildSearchIndex(sections)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** `#hamur/content/search`: each listed collection's index as a lazily imported chunk, which the client bundler hashes; in `hamur dev` fetched from `devLoad`'s module. */
|
|
37
|
+
export function searchModule(names: readonly string[], devLoad?: string): string {
|
|
38
|
+
const loaders = names.map(
|
|
39
|
+
(name) =>
|
|
40
|
+
` ${JSON.stringify(name)}: ${devLoad ? `devLoad(${JSON.stringify(`${DEV_ROUTE}/search/${name}.json`)})` : `() => import(${JSON.stringify(`#hamur/content/search-index/${name}.json`)})`},`,
|
|
41
|
+
)
|
|
42
|
+
const imports = devLoad && names.length ? [`import { devLoad } from ${JSON.stringify(devLoad)};`, ""] : []
|
|
43
|
+
return [...imports, "export default {", ...loaders, "};", ""].join("\n")
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Returns the collections that get a search index. */
|
|
47
|
+
export function setupSearch(
|
|
48
|
+
options: ContentOptions,
|
|
49
|
+
config: ContentConfig,
|
|
50
|
+
entries: () => Promise<Record<string, ContentEntry[]>>,
|
|
51
|
+
dev = false,
|
|
52
|
+
): string[] {
|
|
53
|
+
const names = resolveSearch(options.search, config)
|
|
54
|
+
const resolver = createResolver(import.meta.url)
|
|
55
|
+
const devLoad = dev ? resolver.resolve("../runtime/dev-client.ts") : undefined
|
|
56
|
+
for (const name of names)
|
|
57
|
+
addTemplate({
|
|
58
|
+
filename: `content/search-index/${name}.json`,
|
|
59
|
+
getContents: async () => JSON.stringify(searchIndexOf((await entries())[name]!)),
|
|
60
|
+
})
|
|
61
|
+
addTemplate({ filename: "content/search", getContents: () => searchModule(names, devLoad) })
|
|
62
|
+
if (names.length) addAppPlugin({ src: resolver.resolve("../runtime/search-plugin.ts"), mode: "client" })
|
|
63
|
+
return names
|
|
64
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { posix, relative, sep } from "node:path"
|
|
2
|
+
import { addTemplate, addTypeTemplate, diagnostic, useLogger, type HamurContext } from "hamur/kit"
|
|
3
|
+
import type { StandardJSONSchemaV1 } from "hamur/server"
|
|
4
|
+
import type {
|
|
5
|
+
CollectionDefinition,
|
|
6
|
+
CollectionSource,
|
|
7
|
+
ContentConfig,
|
|
8
|
+
LoadedContentConfig,
|
|
9
|
+
SourceOptions,
|
|
10
|
+
} from "../types.ts"
|
|
11
|
+
|
|
12
|
+
const SCHEMAS = "content/schemas"
|
|
13
|
+
|
|
14
|
+
const posixOf = (path: string): string => path.split(sep).join("/")
|
|
15
|
+
|
|
16
|
+
/** `.hamur/types/content.d.ts`, one `HamurContentCollections` entry per collection and one `HamurLiveCollections` entry per live collection, typed from the config file's own types. */
|
|
17
|
+
export function contentTypes(config: ContentConfig, configImport: string, live: readonly string[] = []): string {
|
|
18
|
+
const entries = Object.keys(config.collections).map(
|
|
19
|
+
(name) => ` ${name}: CollectionItemOf<(typeof config)["collections"][${JSON.stringify(name)}]>;`,
|
|
20
|
+
)
|
|
21
|
+
const liveEntries = live.length
|
|
22
|
+
? [
|
|
23
|
+
" interface HamurLiveCollections {",
|
|
24
|
+
...live.map((name) => ` ${name}: (typeof config)["collections"][${JSON.stringify(name)}];`),
|
|
25
|
+
" }",
|
|
26
|
+
]
|
|
27
|
+
: []
|
|
28
|
+
return [
|
|
29
|
+
`import type { CollectionItemOf } from "@hamur/content";`,
|
|
30
|
+
`import type config from ${JSON.stringify(configImport)};`,
|
|
31
|
+
"",
|
|
32
|
+
`declare module "@hamur/content" {`,
|
|
33
|
+
" interface HamurContentCollections {",
|
|
34
|
+
...entries,
|
|
35
|
+
" }",
|
|
36
|
+
...liveEntries,
|
|
37
|
+
"}",
|
|
38
|
+
"",
|
|
39
|
+
].join("\n")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The globs of a collection's sources from the root, as an editor matches a file: `content/<cwd>/<include>`. */
|
|
43
|
+
export function sourceGlobs(source: CollectionSource | undefined): string[] {
|
|
44
|
+
if (source === undefined) return []
|
|
45
|
+
return (Array.isArray(source) ? source : [source]).flatMap((entry) => {
|
|
46
|
+
if (typeof entry !== "string" && entry.repository !== undefined) return []
|
|
47
|
+
const { include, cwd = "" } = (typeof entry === "string" ? { include: entry } : entry) as SourceOptions
|
|
48
|
+
return [posix.join("content", cwd, include)]
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface SchemaFiles {
|
|
53
|
+
/** The schema files below the build directory, by collection. */
|
|
54
|
+
files: Record<string, string>
|
|
55
|
+
/** Each schema file from the root, with the globs it validates (the shape of `yaml.schemas`). */
|
|
56
|
+
map: Record<string, string[]>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The JSON Schema of each collection whose schema implements Standard JSON Schema: what an author writes, so the schema's input. */
|
|
60
|
+
export function schemaFiles(config: ContentConfig, buildFolder: string, warn: (message: string) => void): SchemaFiles {
|
|
61
|
+
const files: Record<string, string> = {}
|
|
62
|
+
const map: Record<string, string[]> = {}
|
|
63
|
+
for (const [name, collection] of Object.entries(config.collections as Record<string, CollectionDefinition>)) {
|
|
64
|
+
if (collection.schema === undefined) continue
|
|
65
|
+
const converter = (collection.schema as unknown as Partial<StandardJSONSchemaV1>)["~standard"]?.jsonSchema
|
|
66
|
+
if (typeof converter?.input !== "function") {
|
|
67
|
+
warn(diagnostic("HAMUR_W5369", `${name}: schema has no JSON Schema; no editor schema written`))
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
let schema: Record<string, unknown>
|
|
71
|
+
try {
|
|
72
|
+
schema = converter.input({ target: "draft-2020-12" })
|
|
73
|
+
} catch (error) {
|
|
74
|
+
warn(
|
|
75
|
+
diagnostic(
|
|
76
|
+
"HAMUR_W5369",
|
|
77
|
+
`${name}: schema has no JSON Schema (${error instanceof Error ? error.message : String(error)}); no editor schema written`,
|
|
78
|
+
),
|
|
79
|
+
)
|
|
80
|
+
continue
|
|
81
|
+
}
|
|
82
|
+
files[name] = `${JSON.stringify(schema, null, 2)}\n`
|
|
83
|
+
const globs = sourceGlobs(collection.source)
|
|
84
|
+
if (globs.length) map[posix.join(buildFolder, SCHEMAS, `${name}.schema.json`)] = globs
|
|
85
|
+
}
|
|
86
|
+
return { files, map }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The item types and the editor schemas. */
|
|
90
|
+
export function setupTypes(config: LoadedContentConfig, hamur: HamurContext): void {
|
|
91
|
+
const { rootDir, buildDir } = hamur.options
|
|
92
|
+
const configImport = posixOf(relative(`${buildDir}${sep}types`, `${rootDir}${sep}content.config.ts`))
|
|
93
|
+
addTypeTemplate({
|
|
94
|
+
filename: "content.d.ts",
|
|
95
|
+
getContents: () =>
|
|
96
|
+
contentTypes(config, configImport.startsWith(".") ? configImport : `./${configImport}`, Object.keys(config.live)),
|
|
97
|
+
})
|
|
98
|
+
const logger = useLogger("content")
|
|
99
|
+
const { files, map } = schemaFiles(config, posixOf(relative(rootDir, buildDir)), (message) => logger.warn(message))
|
|
100
|
+
for (const [name, text] of Object.entries(files))
|
|
101
|
+
addTemplate({ filename: `${SCHEMAS}/${name}.schema.json`, write: true, getContents: () => text })
|
|
102
|
+
if (Object.keys(files).length)
|
|
103
|
+
addTemplate({
|
|
104
|
+
filename: "content/schemas.json",
|
|
105
|
+
write: true,
|
|
106
|
+
getContents: () => `${JSON.stringify(map, null, 2)}\n`,
|
|
107
|
+
})
|
|
108
|
+
}
|
package/src/component.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { existsSync, statSync } from "node:fs"
|
|
2
|
+
import { isAbsolute, resolve } from "node:path"
|
|
3
|
+
import { pathToFileURL } from "node:url"
|
|
4
|
+
import { DiagnosticError } from "hamur/kit"
|
|
5
|
+
import type { StandardSchemaV1 } from "hamur/server"
|
|
6
|
+
|
|
7
|
+
const COMPONENT = Symbol.for("hamur.content.fromComponent")
|
|
8
|
+
const SCRIPTS = [".ts", ".tsx", ".js", ".jsx", ".mjs"]
|
|
9
|
+
const SFC = [".vue", ".svelte"]
|
|
10
|
+
|
|
11
|
+
/** The `propsSchema` of a component module `M`, as its Standard Schema. */
|
|
12
|
+
export type PropsSchemaOf<M> = M extends { propsSchema: infer S extends StandardSchemaV1 } ? S : StandardSchemaV1
|
|
13
|
+
|
|
14
|
+
/** The `propsSchema` a component file exports, as a collection's schema; imported when `content.config.ts` loads. */
|
|
15
|
+
export function fromComponent<M = unknown>(path: string): PropsSchemaOf<M> {
|
|
16
|
+
if (typeof path !== "string" || path === "")
|
|
17
|
+
throw new DiagnosticError("HAMUR_E5318", "fromComponent: the component path must be a non-empty string")
|
|
18
|
+
return {
|
|
19
|
+
[COMPONENT]: path,
|
|
20
|
+
"~standard": {
|
|
21
|
+
version: 1,
|
|
22
|
+
vendor: "hamur",
|
|
23
|
+
validate() {
|
|
24
|
+
throw new DiagnosticError("HAMUR_E5319", `fromComponent: ${path} is read only through content.config.ts`)
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
} as unknown as PropsSchemaOf<M>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const componentPathOf = (schema: unknown): string | undefined =>
|
|
31
|
+
schema !== null && typeof schema === "object"
|
|
32
|
+
? ((schema as Record<symbol, unknown>)[COMPONENT] as string | undefined)
|
|
33
|
+
: undefined
|
|
34
|
+
|
|
35
|
+
/** A file, or a `#…` specifier through the app's `package.json` `imports`. */
|
|
36
|
+
function fileAt(candidate: string, rootDir: string): string | undefined {
|
|
37
|
+
if (candidate.startsWith("#")) {
|
|
38
|
+
try {
|
|
39
|
+
return Bun.resolveSync(candidate, rootDir)
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return existsSync(candidate) && statSync(candidate).isFile() ? candidate : undefined
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function fileOf(path: string, rootDir: string): string {
|
|
48
|
+
const base = path.startsWith("#") || isAbsolute(path) ? path : resolve(rootDir, path)
|
|
49
|
+
const sfc = SFC.find((extension) => base.endsWith(extension))
|
|
50
|
+
if (sfc) {
|
|
51
|
+
const props = fileAt(`${base.slice(0, -sfc.length)}.props.ts`, rootDir)
|
|
52
|
+
if (!props)
|
|
53
|
+
throw new DiagnosticError(
|
|
54
|
+
"HAMUR_E5320",
|
|
55
|
+
`fromComponent: ${path} is not importable at build; export propsSchema from ${path.slice(0, -sfc.length)}.props.ts`,
|
|
56
|
+
)
|
|
57
|
+
return props
|
|
58
|
+
}
|
|
59
|
+
const candidates = [
|
|
60
|
+
`${base}.props.ts`,
|
|
61
|
+
`${base}.props.js`,
|
|
62
|
+
base,
|
|
63
|
+
...SCRIPTS.map((extension) => `${base}${extension}`),
|
|
64
|
+
]
|
|
65
|
+
for (const candidate of candidates) {
|
|
66
|
+
const found = fileAt(candidate, rootDir)
|
|
67
|
+
if (found) return found
|
|
68
|
+
}
|
|
69
|
+
const sibling = SFC.find((extension) => fileAt(`${base}${extension}`, rootDir))
|
|
70
|
+
if (sibling)
|
|
71
|
+
throw new DiagnosticError(
|
|
72
|
+
"HAMUR_E5320",
|
|
73
|
+
`fromComponent: ${path}${sibling} is not importable at build; export propsSchema from ${path}.props.ts`,
|
|
74
|
+
)
|
|
75
|
+
throw new DiagnosticError("HAMUR_E5321", `fromComponent: ${path} not found`)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Imports the component (or its `.props.ts` next to a `.vue` or `.svelte` file) and returns its `propsSchema`. */
|
|
79
|
+
export async function loadPropsSchema(path: string, rootDir: string): Promise<StandardSchemaV1> {
|
|
80
|
+
const file = fileOf(path, rootDir)
|
|
81
|
+
const loaded = (await import(`${pathToFileURL(file).href}?t=${statSync(file).mtimeMs}`)) as { propsSchema?: unknown }
|
|
82
|
+
const schema = loaded.propsSchema as Partial<StandardSchemaV1> | undefined
|
|
83
|
+
if (typeof schema?.["~standard"]?.validate !== "function")
|
|
84
|
+
throw new DiagnosticError("HAMUR_E5322", `fromComponent: ${path} exports no propsSchema`)
|
|
85
|
+
return schema as StandardSchemaV1
|
|
86
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { existsSync, statSync } from "node:fs"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { pathToFileURL } from "node:url"
|
|
4
|
+
import { componentPathOf, loadPropsSchema } from "./component.ts"
|
|
5
|
+
import type {
|
|
6
|
+
CollectionDefinition,
|
|
7
|
+
ContentLoader,
|
|
8
|
+
LiveCollectionDefinition,
|
|
9
|
+
LiveLoader,
|
|
10
|
+
LoadedContentConfig,
|
|
11
|
+
Transformer,
|
|
12
|
+
} from "./types.ts"
|
|
13
|
+
import { DiagnosticError } from "hamur/kit"
|
|
14
|
+
|
|
15
|
+
export { defineCollection, defineContentConfig, defineLiveCollection, defineTransformer } from "./define.ts"
|
|
16
|
+
|
|
17
|
+
const NAME = /^[a-z][A-Za-z0-9]*$/
|
|
18
|
+
|
|
19
|
+
/** Imports `<rootDir>/content.config.ts` and checks its collections. */
|
|
20
|
+
export async function setupConfig(rootDir: string): Promise<LoadedContentConfig> {
|
|
21
|
+
const file = join(rootDir, "content.config.ts")
|
|
22
|
+
if (!existsSync(file))
|
|
23
|
+
throw new DiagnosticError("HAMUR_E5301", `@hamur/content needs content.config.ts in ${rootDir}`)
|
|
24
|
+
const loaded = (await import(`${pathToFileURL(file).href}?t=${statSync(file).mtimeMs}`)) as { default?: unknown }
|
|
25
|
+
const config = checkConfig(loaded.default)
|
|
26
|
+
for (const collection of Object.values(config.collections as Record<string, CollectionDefinition>)) {
|
|
27
|
+
const component = componentPathOf(collection.schema)
|
|
28
|
+
if (component !== undefined) collection.schema = await loadPropsSchema(component, rootDir)
|
|
29
|
+
}
|
|
30
|
+
return config
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Checks the config and returns it with its live collections moved from `collections` to `live`. */
|
|
34
|
+
export function checkConfig(value: unknown): LoadedContentConfig {
|
|
35
|
+
const collections = (value as { collections?: unknown } | null | undefined)?.collections
|
|
36
|
+
if (collections === null || typeof collections !== "object" || Array.isArray(collections)) {
|
|
37
|
+
throw new DiagnosticError(
|
|
38
|
+
"HAMUR_E5302",
|
|
39
|
+
"content.config.ts must default-export defineContentConfig({ collections: { … } })",
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
const built: Record<string, CollectionDefinition> = {}
|
|
43
|
+
const live: Record<string, LiveCollectionDefinition> = {}
|
|
44
|
+
for (const [name, raw] of Object.entries(collections as Record<string, unknown>)) {
|
|
45
|
+
if (!NAME.test(name))
|
|
46
|
+
throw new DiagnosticError(
|
|
47
|
+
"HAMUR_E5303",
|
|
48
|
+
`content.config.ts: collection name "${name}" must match [a-z][A-Za-z0-9]*`,
|
|
49
|
+
)
|
|
50
|
+
if ((raw as Partial<LiveCollectionDefinition> | null)?.live === true) {
|
|
51
|
+
live[name] = checkLive(name, raw as LiveCollectionDefinition)
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
const collection = (raw ?? {}) as Partial<CollectionDefinition>
|
|
55
|
+
const source = collection.source !== undefined
|
|
56
|
+
const loader = collection.loader !== undefined
|
|
57
|
+
if (source && loader)
|
|
58
|
+
throw new DiagnosticError(
|
|
59
|
+
"HAMUR_E5304",
|
|
60
|
+
`content.config.ts: collection "${name}" sets source and loader; set one`,
|
|
61
|
+
)
|
|
62
|
+
if (!source && !loader)
|
|
63
|
+
throw new DiagnosticError("HAMUR_E5305", `content.config.ts: collection "${name}" sets neither source nor loader`)
|
|
64
|
+
if (loader) {
|
|
65
|
+
const { name: loaderName, load } = collection.loader as Partial<ContentLoader>
|
|
66
|
+
if (typeof loaderName !== "string" || loaderName === "" || typeof load !== "function") {
|
|
67
|
+
throw new DiagnosticError(
|
|
68
|
+
"HAMUR_E5306",
|
|
69
|
+
`content.config.ts: collection "${name}": loader must be { name: string, load(ctx) }`,
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (collection.type !== "page" && collection.type !== "data") {
|
|
74
|
+
throw new DiagnosticError(
|
|
75
|
+
"HAMUR_E5307",
|
|
76
|
+
`content.config.ts: collection "${name}": type must be "page" or "data" (got ${JSON.stringify(collection.type)})`,
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
built[name] = collection as CollectionDefinition
|
|
80
|
+
}
|
|
81
|
+
checkTransformers((value as { transformers?: unknown }).transformers)
|
|
82
|
+
return { ...(value as object), collections: built, live }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function checkLive(name: string, collection: LiveCollectionDefinition): LiveCollectionDefinition {
|
|
86
|
+
const loader = (collection.loader ?? {}) as Partial<LiveLoader>
|
|
87
|
+
if (
|
|
88
|
+
typeof loader.loadCollection !== "function" ||
|
|
89
|
+
typeof loader.loadEntry !== "function" ||
|
|
90
|
+
(loader.name !== undefined && typeof loader.name !== "string")
|
|
91
|
+
) {
|
|
92
|
+
throw new DiagnosticError(
|
|
93
|
+
"HAMUR_E5308",
|
|
94
|
+
`content.config.ts: live collection "${name}": loader must be { name?: string, loadCollection(filter), loadEntry(filter) }`,
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
if (componentPathOf(collection.schema) !== undefined)
|
|
98
|
+
throw new DiagnosticError(
|
|
99
|
+
"HAMUR_E5309",
|
|
100
|
+
`content.config.ts: live collection "${name}": fromComponent is build-time only; pass the component's propsSchema itself`,
|
|
101
|
+
)
|
|
102
|
+
const schema = collection.schema as { "~standard"?: { validate?: unknown } } | undefined
|
|
103
|
+
if (schema !== undefined && typeof schema?.["~standard"]?.validate !== "function")
|
|
104
|
+
throw new DiagnosticError(
|
|
105
|
+
"HAMUR_E5310",
|
|
106
|
+
`content.config.ts: live collection "${name}": schema must be a Standard Schema`,
|
|
107
|
+
)
|
|
108
|
+
return collection
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const BUILT_IN = new Set(["md", "yml", "yaml", "json", "json5", "toml", "csv"])
|
|
112
|
+
|
|
113
|
+
function checkTransformers(value: unknown): void {
|
|
114
|
+
if (value === undefined) return
|
|
115
|
+
if (!Array.isArray(value))
|
|
116
|
+
throw new DiagnosticError(
|
|
117
|
+
"HAMUR_E5311",
|
|
118
|
+
"content.config.ts: transformers must be an array of defineTransformer({ … })",
|
|
119
|
+
)
|
|
120
|
+
const names = new Set<string>()
|
|
121
|
+
const extensions = new Map<string, string>()
|
|
122
|
+
for (const [index, raw] of value.entries()) {
|
|
123
|
+
const item = (raw ?? {}) as Partial<Transformer>
|
|
124
|
+
const at = `content.config.ts: transformers[${index}]`
|
|
125
|
+
if (typeof item.name !== "string" || item.name === "")
|
|
126
|
+
throw new DiagnosticError("HAMUR_E5312", `${at}: name must be a non-empty string`)
|
|
127
|
+
if (names.has(item.name)) throw new DiagnosticError("HAMUR_E5313", `${at}: name "${item.name}" is listed twice`)
|
|
128
|
+
names.add(item.name)
|
|
129
|
+
if (
|
|
130
|
+
!Array.isArray(item.extensions) ||
|
|
131
|
+
item.extensions.some((extension) => typeof extension !== "string" || !/^\.[A-Za-z0-9]+$/.test(extension))
|
|
132
|
+
) {
|
|
133
|
+
throw new DiagnosticError(
|
|
134
|
+
"HAMUR_E5314",
|
|
135
|
+
`${at} "${item.name}": extensions must be an array of extensions with a dot, like [".adoc"]`,
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
for (const key of ["parse", "transform"] as const) {
|
|
139
|
+
if (item[key] !== undefined && typeof item[key] !== "function")
|
|
140
|
+
throw new DiagnosticError("HAMUR_E5315", `${at} "${item.name}": ${key} must be a function`)
|
|
141
|
+
}
|
|
142
|
+
if (item.parse === undefined) continue
|
|
143
|
+
for (const extension of item.extensions) {
|
|
144
|
+
const bare = extension.slice(1)
|
|
145
|
+
if (BUILT_IN.has(bare))
|
|
146
|
+
throw new DiagnosticError("HAMUR_E5316", `${at} "${item.name}": ${extension} is a built-in content format`)
|
|
147
|
+
const other = extensions.get(bare)
|
|
148
|
+
if (other !== undefined)
|
|
149
|
+
throw new DiagnosticError(
|
|
150
|
+
"HAMUR_E5317",
|
|
151
|
+
`${at} "${item.name}": ${extension} is already parsed by transformer "${other}"`,
|
|
152
|
+
)
|
|
153
|
+
extensions.set(bare, item.name)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|