@hamur/content 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +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 +110 -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
package/src/define.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { CollectionDefinition, ContentConfig, LiveCollectionDefinition, LiveLoader, Transformer } from "./types.ts"
|
|
2
|
+
import type { StandardSchemaV1 } from "hamur/server"
|
|
3
|
+
|
|
4
|
+
export function defineContentConfig<C extends Record<string, CollectionDefinition | LiveCollectionDefinition>>(
|
|
5
|
+
config: ContentConfig<C>,
|
|
6
|
+
): ContentConfig<C> {
|
|
7
|
+
return config
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function defineTransformer<T extends Transformer>(transformer: T): T {
|
|
11
|
+
return transformer
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function defineCollection<T extends CollectionDefinition>(collection: T): T {
|
|
15
|
+
return collection
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A collection fetched at request time by `loader`, read with `getLiveCollection` and `getLiveEntry`. */
|
|
19
|
+
export function defineLiveCollection<L extends LiveLoader, S extends StandardSchemaV1>(collection: {
|
|
20
|
+
loader: L
|
|
21
|
+
schema: S
|
|
22
|
+
}): LiveCollectionDefinition<L, S>
|
|
23
|
+
// Without a schema the type is fixed to `undefined`: inside defineContentConfig the return type's context would infer `StandardSchemaV1`.
|
|
24
|
+
export function defineLiveCollection<L extends LiveLoader>(collection: {
|
|
25
|
+
loader: L
|
|
26
|
+
schema?: undefined
|
|
27
|
+
}): LiveCollectionDefinition<L, undefined>
|
|
28
|
+
export function defineLiveCollection(collection: {
|
|
29
|
+
loader: LiveLoader
|
|
30
|
+
schema?: StandardSchemaV1
|
|
31
|
+
}): LiveCollectionDefinition {
|
|
32
|
+
return { ...collection, live: true }
|
|
33
|
+
}
|
package/src/env.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
interface ImportMeta {
|
|
2
|
+
readonly server?: boolean
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
declare module "#hamur/content/store" {
|
|
6
|
+
/** The collections of `content.config.ts` and their chunks. */
|
|
7
|
+
const store: import("./runtime/store.ts").ContentStore
|
|
8
|
+
export default store
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare module "#hamur/content/live" {
|
|
12
|
+
/** The live collections of `content.config.ts`. */
|
|
13
|
+
const collections: Record<string, import("./types.ts").LiveCollectionDefinition>
|
|
14
|
+
/** Every collection name of `content.config.ts`. */
|
|
15
|
+
export const names: readonly string[]
|
|
16
|
+
export default collections
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare module "#hamur/markdown-components" {
|
|
20
|
+
/** The app's `app/components/content/Prose<Tag>.<ext>` files by `Prose<Tag>`. */
|
|
21
|
+
export const prose: Record<string, any>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare module "#hamur/content/client" {
|
|
25
|
+
/** `content.clientQuery` and every collection of `content.config.ts`, with a chunk loader for the exposed ones under `"local"`. */
|
|
26
|
+
const client: import("./runtime/client.ts").ClientQueryConfig
|
|
27
|
+
export default client
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
declare module "#hamur/content/search" {
|
|
31
|
+
/** The search index chunk of each collection of `content.search.collections`, imported on first use. */
|
|
32
|
+
const searches: Record<string, () => Promise<{ default: import("./runtime/search.ts").SearchIndex }>>
|
|
33
|
+
export default searches
|
|
34
|
+
}
|
package/src/error.ts
ADDED
package/src/image.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from "hamur/server"
|
|
2
|
+
|
|
3
|
+
/** What `image()` outputs: an `ImageMeta` of `hamur` without `animated`. */
|
|
4
|
+
export interface ContentImage {
|
|
5
|
+
src: string
|
|
6
|
+
width: number
|
|
7
|
+
height: number
|
|
8
|
+
format: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const IMAGE_SCOPE = Symbol.for("hamur.content.image")
|
|
12
|
+
|
|
13
|
+
/** What the content build gives `image()` while it validates one entry; `undefined` for a message means the file is read. */
|
|
14
|
+
export interface ImageScope {
|
|
15
|
+
resolve(path: string): Promise<ContentImage | string>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface ScopeStore {
|
|
19
|
+
getStore(): ImageScope | undefined
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const URLISH = /^(?:[a-z][a-z0-9+.-]*:|\/)/i
|
|
23
|
+
|
|
24
|
+
/** A relative path in frontmatter or data, resolved against the entry file, as `{ src, width, height, format }` of the emitted image. */
|
|
25
|
+
export function image(): StandardSchemaV1<unknown, ContentImage> {
|
|
26
|
+
return {
|
|
27
|
+
"~standard": {
|
|
28
|
+
version: 1,
|
|
29
|
+
vendor: "hamur",
|
|
30
|
+
async validate(value) {
|
|
31
|
+
if (typeof value !== "string" || value === "") return { issues: [{ message: "expected an image path" }] }
|
|
32
|
+
if (URLISH.test(value))
|
|
33
|
+
return { issues: [{ message: `${value} is not a relative path; image() reads files next to the entry` }] }
|
|
34
|
+
const scope = ((globalThis as Record<symbol, unknown>)[IMAGE_SCOPE] as ScopeStore | undefined)?.getStore()
|
|
35
|
+
if (!scope) return { issues: [{ message: "image() runs at build only" }] }
|
|
36
|
+
const result = await scope.resolve(value)
|
|
37
|
+
return typeof result === "string" ? { issues: [{ message: result }] } : { value: result }
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Loads hamur so the augmentation below resolves in a program where nothing else imports bare "hamur".
|
|
2
|
+
import type {} from "hamur"
|
|
3
|
+
import type { ContentEntry, LiveCollectionDefinition, LiveCollectionItemOf } from "./types.ts"
|
|
4
|
+
|
|
5
|
+
export { default } from "./module.ts"
|
|
6
|
+
export { defineCollection, defineContentConfig, defineLiveCollection, defineTransformer } from "./define.ts"
|
|
7
|
+
export { fromComponent } from "./component.ts"
|
|
8
|
+
export { image } from "./image.ts"
|
|
9
|
+
export type { ContentImage } from "./image.ts"
|
|
10
|
+
export type { PropsSchemaOf } from "./component.ts"
|
|
11
|
+
export { file } from "./loaders/file.ts"
|
|
12
|
+
export { glob } from "./loaders/glob.ts"
|
|
13
|
+
export type { GlobOptions } from "./loaders/glob.ts"
|
|
14
|
+
export type {
|
|
15
|
+
CacheHint,
|
|
16
|
+
CollectionDefinition,
|
|
17
|
+
CollectionItemOf,
|
|
18
|
+
CollectionSource,
|
|
19
|
+
ContentConfig,
|
|
20
|
+
ContentEntry,
|
|
21
|
+
ContentFile,
|
|
22
|
+
ContentOptions,
|
|
23
|
+
ContentLoader,
|
|
24
|
+
LiveCollectionDefinition,
|
|
25
|
+
LiveCollectionItemOf,
|
|
26
|
+
LiveLoader,
|
|
27
|
+
LiveLoaderEntry,
|
|
28
|
+
LlmsOptions,
|
|
29
|
+
DataEntry,
|
|
30
|
+
DataFields,
|
|
31
|
+
LoaderContext,
|
|
32
|
+
LoaderEntry,
|
|
33
|
+
PageEntry,
|
|
34
|
+
PageFields,
|
|
35
|
+
RemoteSourceOptions,
|
|
36
|
+
Serialized,
|
|
37
|
+
SourceOptions,
|
|
38
|
+
Transformer,
|
|
39
|
+
} from "./types.ts"
|
|
40
|
+
|
|
41
|
+
/** Typegen fills it, `HamurContentCollections[<collection>] = <item type>`; empty until the app has a `content.config.ts` typegen has read. */
|
|
42
|
+
export interface HamurContentCollections {}
|
|
43
|
+
|
|
44
|
+
/** The collection names of `content.config.ts`, `string` while typegen has written none. */
|
|
45
|
+
export type CollectionName = keyof HamurContentCollections extends never
|
|
46
|
+
? string
|
|
47
|
+
: keyof HamurContentCollections & string
|
|
48
|
+
|
|
49
|
+
/** The item type of collection `N`; `ContentEntry` while typegen has written none. */
|
|
50
|
+
export type CollectionItem<N extends string> = N extends keyof HamurContentCollections
|
|
51
|
+
? HamurContentCollections[N]
|
|
52
|
+
: ContentEntry
|
|
53
|
+
|
|
54
|
+
/** The field names of collection `N`; `string` while typegen has written none or the schema has an open output. */
|
|
55
|
+
export type CollectionField<N extends string> = keyof CollectionItem<N> & string
|
|
56
|
+
|
|
57
|
+
/** Typegen fills it, `HamurLiveCollections[<collection>] = <its defineLiveCollection(…)>`. */
|
|
58
|
+
export interface HamurLiveCollections {}
|
|
59
|
+
|
|
60
|
+
/** The live collection names of `content.config.ts`, `string` while typegen has written none. */
|
|
61
|
+
export type LiveCollectionName = keyof HamurLiveCollections extends never ? string : keyof HamurLiveCollections & string
|
|
62
|
+
|
|
63
|
+
type LiveDefinitionOf<N extends string> = N extends keyof HamurLiveCollections
|
|
64
|
+
? HamurLiveCollections[N]
|
|
65
|
+
: LiveCollectionDefinition
|
|
66
|
+
|
|
67
|
+
/** The `data` type of live collection `N`'s entries; `Record<string, unknown>` while typegen has written none. */
|
|
68
|
+
export type LiveCollectionItem<N extends string> = N extends keyof HamurLiveCollections
|
|
69
|
+
? LiveCollectionItemOf<HamurLiveCollections[N]>
|
|
70
|
+
: Record<string, unknown>
|
|
71
|
+
|
|
72
|
+
/** The filter `getLiveCollection(N, filter)` passes to `loadCollection`. */
|
|
73
|
+
export type LiveCollectionFilter<N extends string> =
|
|
74
|
+
LiveDefinitionOf<N> extends { loader: { loadCollection(filter: infer F): unknown } } ? F : unknown
|
|
75
|
+
|
|
76
|
+
/** The filter `getLiveEntry(N, filter)` passes to `loadEntry`. */
|
|
77
|
+
export type LiveEntryFilter<N extends string> =
|
|
78
|
+
LiveDefinitionOf<N> extends { loader: { loadEntry(filter: infer F): unknown } } ? F : unknown
|
|
79
|
+
|
|
80
|
+
declare module "hamur" {
|
|
81
|
+
interface HamurModuleOptions {
|
|
82
|
+
content?: import("./types.ts").ContentOptions
|
|
83
|
+
}
|
|
84
|
+
/** What `hamur dev` sends a page when a content file is saved. */
|
|
85
|
+
interface HamurDevEvents {
|
|
86
|
+
"content:update": import("./build/dev.ts").ContentUpdate
|
|
87
|
+
"content:error": import("./build/dev.ts").ContentFailure
|
|
88
|
+
"content:fixed": import("./build/dev.ts").ContentFixed
|
|
89
|
+
}
|
|
90
|
+
interface HamurHooks {
|
|
91
|
+
/** Before a file is parsed; assign `file.body` to change what the parser reads. */
|
|
92
|
+
"content:file:beforeParse": (context: {
|
|
93
|
+
file: import("./types.ts").ContentFile
|
|
94
|
+
collection: string
|
|
95
|
+
}) => void | Promise<void>
|
|
96
|
+
/** After the schema and the transformers; change `entry` in place. */
|
|
97
|
+
"content:file:afterParse": (context: {
|
|
98
|
+
file: import("./types.ts").ContentFile
|
|
99
|
+
entry: import("./types.ts").ContentEntry
|
|
100
|
+
collection: string
|
|
101
|
+
}) => void | Promise<void>
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { ContentError } from "../error.ts"
|
|
2
|
+
import { DiagnosticError } from "hamur/kit"
|
|
3
|
+
|
|
4
|
+
export interface CsvOptions {
|
|
5
|
+
delimiter: string
|
|
6
|
+
header: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_CSV: CsvOptions = { delimiter: ",", header: true }
|
|
10
|
+
|
|
11
|
+
export const DATA_FORMATS = ["yml", "yaml", "json", "json5", "toml", "csv"] as const
|
|
12
|
+
|
|
13
|
+
/** `content.csv`, merged over the defaults. */
|
|
14
|
+
export function resolveCsv(value: unknown): CsvOptions {
|
|
15
|
+
if (value === undefined) return DEFAULT_CSV
|
|
16
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
17
|
+
throw new DiagnosticError("HAMUR_E5342", "content.csv must be { delimiter?: string; header?: boolean }")
|
|
18
|
+
const { delimiter = DEFAULT_CSV.delimiter, header = DEFAULT_CSV.header } = value as {
|
|
19
|
+
delimiter?: unknown
|
|
20
|
+
header?: unknown
|
|
21
|
+
}
|
|
22
|
+
if (
|
|
23
|
+
typeof delimiter !== "string" ||
|
|
24
|
+
[...delimiter].length !== 1 ||
|
|
25
|
+
delimiter === '"' ||
|
|
26
|
+
delimiter === "\n" ||
|
|
27
|
+
delimiter === "\r"
|
|
28
|
+
) {
|
|
29
|
+
throw new DiagnosticError(
|
|
30
|
+
"HAMUR_E5342",
|
|
31
|
+
`content.csv.delimiter must be one character other than a quote or a line break (got ${JSON.stringify(delimiter)})`,
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
if (typeof header !== "boolean")
|
|
35
|
+
throw new DiagnosticError("HAMUR_E5342", `content.csv.header must be a boolean (got ${JSON.stringify(header)})`)
|
|
36
|
+
return { delimiter, header }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface Row {
|
|
40
|
+
line: number
|
|
41
|
+
fields: string[]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** RFC 4180 records with the line each starts on; a blank line is no record, CRLF, CR and LF end a record. */
|
|
45
|
+
function csvRows(text: string, delimiter: string, shown: string): Row[] {
|
|
46
|
+
const rows: Row[] = []
|
|
47
|
+
const fail = (message: string, line: number, column: number): never => {
|
|
48
|
+
throw new ContentError("HAMUR_E5359", message, { at: `${shown}:${line}:${column}` })
|
|
49
|
+
}
|
|
50
|
+
let index = text.charCodeAt(0) === 0xfeff ? 1 : 0
|
|
51
|
+
let line = 1
|
|
52
|
+
let column = 1
|
|
53
|
+
let rowLine = 1
|
|
54
|
+
let fields: string[] = []
|
|
55
|
+
let field = ""
|
|
56
|
+
let fresh = true
|
|
57
|
+
let quoted = false
|
|
58
|
+
const endRow = () => {
|
|
59
|
+
if (fields.length === 0 && field === "" && !quoted) return
|
|
60
|
+
fields.push(field)
|
|
61
|
+
rows.push({ line: rowLine, fields })
|
|
62
|
+
fields = []
|
|
63
|
+
}
|
|
64
|
+
while (index < text.length) {
|
|
65
|
+
const char = text[index]!
|
|
66
|
+
if (char === '"' && fresh) {
|
|
67
|
+
const startLine = line
|
|
68
|
+
const startColumn = column
|
|
69
|
+
index++
|
|
70
|
+
column++
|
|
71
|
+
for (;;) {
|
|
72
|
+
if (index >= text.length) fail("unterminated quoted field", startLine, startColumn)
|
|
73
|
+
const next = text[index]!
|
|
74
|
+
if (next === '"') {
|
|
75
|
+
if (text[index + 1] === '"') {
|
|
76
|
+
field += '"'
|
|
77
|
+
index += 2
|
|
78
|
+
column += 2
|
|
79
|
+
continue
|
|
80
|
+
}
|
|
81
|
+
index++
|
|
82
|
+
column++
|
|
83
|
+
break
|
|
84
|
+
}
|
|
85
|
+
field += next
|
|
86
|
+
index++
|
|
87
|
+
if (next === "\n") {
|
|
88
|
+
line++
|
|
89
|
+
column = 1
|
|
90
|
+
} else column++
|
|
91
|
+
}
|
|
92
|
+
const after = text[index]
|
|
93
|
+
if (after !== undefined && after !== delimiter && after !== "\r" && after !== "\n")
|
|
94
|
+
fail("unexpected character after a closing quote", line, column)
|
|
95
|
+
fresh = false
|
|
96
|
+
quoted = true
|
|
97
|
+
continue
|
|
98
|
+
}
|
|
99
|
+
if (char === delimiter) {
|
|
100
|
+
fields.push(field)
|
|
101
|
+
field = ""
|
|
102
|
+
fresh = true
|
|
103
|
+
quoted = false
|
|
104
|
+
index++
|
|
105
|
+
column++
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
if (char === "\r" || char === "\n") {
|
|
109
|
+
endRow()
|
|
110
|
+
field = ""
|
|
111
|
+
fresh = true
|
|
112
|
+
quoted = false
|
|
113
|
+
index += char === "\r" && text[index + 1] === "\n" ? 2 : 1
|
|
114
|
+
line++
|
|
115
|
+
column = 1
|
|
116
|
+
rowLine = line
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
if (char === '"') fail("unexpected quote in an unquoted field", line, column)
|
|
120
|
+
field += char
|
|
121
|
+
fresh = false
|
|
122
|
+
index++
|
|
123
|
+
column++
|
|
124
|
+
}
|
|
125
|
+
endRow()
|
|
126
|
+
return rows
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** One object per record, keyed by the header row, or by `"0"`, `"1"`, … without one. */
|
|
130
|
+
export function parseCsv(text: string, options: CsvOptions, shown: string): Array<Record<string, string>> {
|
|
131
|
+
const rows = csvRows(text, options.delimiter, shown)
|
|
132
|
+
let names: string[] | undefined
|
|
133
|
+
if (options.header) names = rows.shift()?.fields
|
|
134
|
+
return rows.map(({ line, fields }) => {
|
|
135
|
+
if (names && fields.length !== names.length)
|
|
136
|
+
throw new ContentError(
|
|
137
|
+
"HAMUR_E5360",
|
|
138
|
+
`row has ${fields.length} ${fields.length === 1 ? "field" : "fields"}, the header has ${names.length}`,
|
|
139
|
+
{ at: `${shown}:${line}:1` },
|
|
140
|
+
)
|
|
141
|
+
return Object.fromEntries(fields.map((value, index) => [names ? names[index]! : String(index), value]))
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Bun's parsers report no position; the error is placed on the first line whose prefix of the file already fails the same way. */
|
|
146
|
+
function locate(parse: (text: string) => unknown, text: string, message: string): { line: number; column: number } {
|
|
147
|
+
const lines = text.split("\n")
|
|
148
|
+
const fails = (count: number): boolean => {
|
|
149
|
+
try {
|
|
150
|
+
parse(lines.slice(0, count).join("\n") + (count < lines.length ? "\n" : ""))
|
|
151
|
+
return false
|
|
152
|
+
} catch (error) {
|
|
153
|
+
return (error as Error).message === message
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
let low = 1
|
|
157
|
+
let high = lines.length
|
|
158
|
+
while (low < high) {
|
|
159
|
+
const middle = (low + high) >> 1
|
|
160
|
+
if (fails(middle)) high = middle
|
|
161
|
+
else low = middle + 1
|
|
162
|
+
}
|
|
163
|
+
const found = lines[low - 1]!.search(/\S/)
|
|
164
|
+
return { line: low, column: found === -1 ? 1 : found + 1 }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const PARSERS: Record<string, (text: string) => unknown> = {
|
|
168
|
+
yml: (text) => Bun.YAML.parse(text),
|
|
169
|
+
yaml: (text) => Bun.YAML.parse(text),
|
|
170
|
+
json: (text) => JSON.parse(text),
|
|
171
|
+
json5: (text) => Bun.JSON5.parse(text),
|
|
172
|
+
toml: (text) => Bun.TOML.parse(text),
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The value of one data file; an error is `<file>:<line>:<column>: <message>`. */
|
|
176
|
+
export function parseDataText(text: string, extension: string, shown: string, csv: CsvOptions = DEFAULT_CSV): unknown {
|
|
177
|
+
if (extension === "csv") return parseCsv(text, csv, shown)
|
|
178
|
+
const parse = PARSERS[extension]!
|
|
179
|
+
try {
|
|
180
|
+
return parse(text)
|
|
181
|
+
} catch (error) {
|
|
182
|
+
const message = (error as Error).message
|
|
183
|
+
const { line, column } = locate(parse, text, message)
|
|
184
|
+
throw new ContentError("HAMUR_E5359", message, { at: `${shown}:${line}:${column}` })
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { relative, resolve, sep } from "node:path"
|
|
2
|
+
import { ContentError } from "../error.ts"
|
|
3
|
+
import type { ContentLoader } from "../types.ts"
|
|
4
|
+
import { DATA_FORMATS, parseDataText } from "./data.ts"
|
|
5
|
+
|
|
6
|
+
const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
7
|
+
value !== null && typeof value === "object" && !Array.isArray(value)
|
|
8
|
+
|
|
9
|
+
/** One data file (yml, yaml, json, json5, toml or csv) below `<rootDir>/content`: an array (an item's `id` field, else its index, is its id) or an object (one entry per key). */
|
|
10
|
+
export function file(path: string): ContentLoader {
|
|
11
|
+
return {
|
|
12
|
+
name: "file",
|
|
13
|
+
async load(ctx) {
|
|
14
|
+
const absolute = resolve(ctx.rootDir, "content", path)
|
|
15
|
+
const shown = relative(ctx.rootDir, absolute).split(sep).join("/")
|
|
16
|
+
const dot = absolute.lastIndexOf(".")
|
|
17
|
+
const extension = absolute.slice(dot + 1)
|
|
18
|
+
if (!(DATA_FORMATS as readonly string[]).includes(extension))
|
|
19
|
+
throw new ContentError("HAMUR_E5355", `${shown}: .${extension} is not a data file (${DATA_FORMATS.join(", ")})`)
|
|
20
|
+
const source = Bun.file(absolute)
|
|
21
|
+
if (!(await source.exists())) throw new ContentError("HAMUR_E5358", `${shown}: file not found`)
|
|
22
|
+
const value = parseDataText(
|
|
23
|
+
await ctx.beforeParse({
|
|
24
|
+
id: path.slice(0, -extension.length - 1),
|
|
25
|
+
path: shown,
|
|
26
|
+
extension,
|
|
27
|
+
body: await source.text(),
|
|
28
|
+
}),
|
|
29
|
+
extension,
|
|
30
|
+
shown,
|
|
31
|
+
ctx.csv,
|
|
32
|
+
)
|
|
33
|
+
if (value === null || typeof value !== "object")
|
|
34
|
+
throw new ContentError("HAMUR_E5357", `${shown}: expected an array or an object of entries`)
|
|
35
|
+
const items: Array<[string, unknown]> = Array.isArray(value)
|
|
36
|
+
? value.map((item, index) => [isObject(item) && typeof item.id === "string" ? item.id : String(index), item])
|
|
37
|
+
: Object.entries(value)
|
|
38
|
+
for (const [id, item] of items) {
|
|
39
|
+
if (!isObject(item)) throw new ContentError("HAMUR_E5350", `${shown}: ${id}: a data entry is an object`)
|
|
40
|
+
const data = Array.isArray(value)
|
|
41
|
+
? Object.fromEntries(Object.entries(item).filter(([key]) => key !== "id"))
|
|
42
|
+
: item
|
|
43
|
+
ctx.store.set({
|
|
44
|
+
id,
|
|
45
|
+
stem: id,
|
|
46
|
+
file: absolute,
|
|
47
|
+
data,
|
|
48
|
+
digest: ctx.digest(`${shown}\0${id}\0${JSON.stringify(item)}`),
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
}
|