@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
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { searchLoaders } from "../runtime/search-loaders.ts"
|
|
2
|
+
import { createSearchCollection, type SearchCollection } from "../runtime/search-store.ts"
|
|
3
|
+
|
|
4
|
+
export type { SearchHit } from "../runtime/search.ts"
|
|
5
|
+
export type { SearchOptions, SearchStatus } from "../runtime/search-store.ts"
|
|
6
|
+
|
|
7
|
+
export interface UseSearchCollectionOptions {
|
|
8
|
+
/** Loads the index when the component mounts instead of on the first `search()`; default `false`. */
|
|
9
|
+
immediate?: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const collections = new Map<string, SearchCollection>()
|
|
13
|
+
|
|
14
|
+
/** The collection's search state, one per page load; throws for a collection that `content.search.collections` does not list. */
|
|
15
|
+
export function searchCollectionOf(name: string): SearchCollection {
|
|
16
|
+
const searches = searchLoaders()
|
|
17
|
+
const load = Object.hasOwn(searches, name) ? searches[name] : undefined
|
|
18
|
+
if (!load)
|
|
19
|
+
throw new Error(
|
|
20
|
+
`useSearchCollection(${JSON.stringify(name)}): add ${JSON.stringify(name)} to content.search.collections`,
|
|
21
|
+
)
|
|
22
|
+
let collection = collections.get(name)
|
|
23
|
+
if (!collection) collections.set(name, (collection = createSearchCollection(load)))
|
|
24
|
+
return collection
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** `hamur dev`: the collection's index, when a page loaded it, is loaded again. */
|
|
28
|
+
export function forgetSearch(name: string): void {
|
|
29
|
+
collections.get(name)?.reset()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const ignore = (): void => {}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createComponent,
|
|
3
|
+
createMemo,
|
|
4
|
+
createSignal,
|
|
5
|
+
merge,
|
|
6
|
+
omit,
|
|
7
|
+
onCleanup,
|
|
8
|
+
onSettled,
|
|
9
|
+
type Accessor,
|
|
10
|
+
type Component,
|
|
11
|
+
} from "solid-js"
|
|
12
|
+
import { dynamic, type JSX } from "@solidjs/web"
|
|
13
|
+
import { Link, MarkdownRenderer, useAsyncData, useHead, useRoute, useSeoMeta } from "@hamur/adapter-solid"
|
|
14
|
+
import type { MdRoot } from "hamur/markdown"
|
|
15
|
+
import type { CollectionName } from "../index.ts"
|
|
16
|
+
import type { PageEntry, PageFields } from "../types.ts"
|
|
17
|
+
import { checkPageCollection, loadPage, pageHead, pageKey, pageSeo, settledPage, type ContentPage } from "./page.ts"
|
|
18
|
+
import { isInternal, prepare } from "./prose.ts"
|
|
19
|
+
import {
|
|
20
|
+
ignore,
|
|
21
|
+
searchCollectionOf,
|
|
22
|
+
type SearchHit,
|
|
23
|
+
type SearchOptions,
|
|
24
|
+
type SearchStatus,
|
|
25
|
+
type UseSearchCollectionOptions,
|
|
26
|
+
} from "./search.ts"
|
|
27
|
+
|
|
28
|
+
export type { ContentPage, SearchHit, SearchOptions, SearchStatus, UseSearchCollectionOptions }
|
|
29
|
+
|
|
30
|
+
export interface ContentRendererProps {
|
|
31
|
+
/** A page entry, which renders its `body`, or a Markdown AST. */
|
|
32
|
+
value: PageFields | MdRoot
|
|
33
|
+
/** Markdown components by kebab-case name and prose components by `Prose<Tag>` name; `false` registers none. */
|
|
34
|
+
components?: Record<string, Component<any>> | false
|
|
35
|
+
/** The wrapper element; `"div"` by default. */
|
|
36
|
+
tag?: string
|
|
37
|
+
/** Renders the entry's `excerpt` instead of its `body`. */
|
|
38
|
+
excerpt?: boolean
|
|
39
|
+
/** Space-separated tags whose element is dropped and whose children are kept. */
|
|
40
|
+
unwrap?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type ProseProps = Record<string, unknown> & { children?: JSX.Element }
|
|
44
|
+
|
|
45
|
+
const elements = new Map<string, Component<Record<string, unknown>>>()
|
|
46
|
+
|
|
47
|
+
function element(tag: string): Component<Record<string, unknown>> {
|
|
48
|
+
let made = elements.get(tag)
|
|
49
|
+
if (!made) elements.set(tag, (made = dynamic(() => tag, { static: true }) as Component<Record<string, unknown>>))
|
|
50
|
+
return made
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const plain =
|
|
54
|
+
(tag: string) =>
|
|
55
|
+
(props: ProseProps): JSX.Element =>
|
|
56
|
+
createComponent(element(tag), props)
|
|
57
|
+
|
|
58
|
+
export const ProseH1 = plain("h1")
|
|
59
|
+
export const ProseH2 = plain("h2")
|
|
60
|
+
export const ProseH3 = plain("h3")
|
|
61
|
+
export const ProseH4 = plain("h4")
|
|
62
|
+
export const ProseH5 = plain("h5")
|
|
63
|
+
export const ProseH6 = plain("h6")
|
|
64
|
+
export const ProseP = plain("p")
|
|
65
|
+
export const ProseUl = plain("ul")
|
|
66
|
+
export const ProseOl = plain("ol")
|
|
67
|
+
export const ProseLi = plain("li")
|
|
68
|
+
export const ProseBlockquote = plain("blockquote")
|
|
69
|
+
export const ProseCode = plain("code")
|
|
70
|
+
export const ProseImg = plain("img")
|
|
71
|
+
export const ProseTable = plain("table")
|
|
72
|
+
export const ProseHr = plain("hr")
|
|
73
|
+
export const ProseStrong = plain("strong")
|
|
74
|
+
export const ProseEm = plain("em")
|
|
75
|
+
|
|
76
|
+
/** An internal `href` is a `Link`, as `MarkdownRenderer` writes it. */
|
|
77
|
+
export function ProseA(props: ProseProps): JSX.Element {
|
|
78
|
+
const rest = omit(props, "href")
|
|
79
|
+
return createMemo(() =>
|
|
80
|
+
isInternal(props.href)
|
|
81
|
+
? createComponent(
|
|
82
|
+
Link as unknown as Component<Record<string, unknown>>,
|
|
83
|
+
merge(rest, {
|
|
84
|
+
get to() {
|
|
85
|
+
return props.href
|
|
86
|
+
},
|
|
87
|
+
}),
|
|
88
|
+
)
|
|
89
|
+
: createComponent(
|
|
90
|
+
element("a"),
|
|
91
|
+
merge(rest, {
|
|
92
|
+
get href() {
|
|
93
|
+
return props.href
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
),
|
|
97
|
+
) as unknown as JSX.Element
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function ProsePre(props: ProseProps): JSX.Element {
|
|
101
|
+
const rest = omit(props, "language", "highlights", "code", "filename")
|
|
102
|
+
return createComponent(
|
|
103
|
+
element("pre"),
|
|
104
|
+
merge(rest, {
|
|
105
|
+
get "data-filename"() {
|
|
106
|
+
return typeof props.filename === "string" ? props.filename : undefined
|
|
107
|
+
},
|
|
108
|
+
}),
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const DEFAULTS = {
|
|
113
|
+
a: ProseA,
|
|
114
|
+
h1: ProseH1,
|
|
115
|
+
h2: ProseH2,
|
|
116
|
+
h3: ProseH3,
|
|
117
|
+
h4: ProseH4,
|
|
118
|
+
h5: ProseH5,
|
|
119
|
+
h6: ProseH6,
|
|
120
|
+
p: ProseP,
|
|
121
|
+
ul: ProseUl,
|
|
122
|
+
ol: ProseOl,
|
|
123
|
+
li: ProseLi,
|
|
124
|
+
blockquote: ProseBlockquote,
|
|
125
|
+
pre: ProsePre,
|
|
126
|
+
code: ProseCode,
|
|
127
|
+
img: ProseImg,
|
|
128
|
+
table: ProseTable,
|
|
129
|
+
hr: ProseHr,
|
|
130
|
+
strong: ProseStrong,
|
|
131
|
+
em: ProseEm,
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** A page entry's body, or a Markdown AST, through `MarkdownRenderer` with the prose components of `components`. */
|
|
135
|
+
export function ContentRenderer(props: ContentRendererProps): JSX.Element {
|
|
136
|
+
return createMemo(() => {
|
|
137
|
+
const prepared = prepare({
|
|
138
|
+
value: props.value,
|
|
139
|
+
components: props.components,
|
|
140
|
+
defaults: DEFAULTS,
|
|
141
|
+
excerpt: props.excerpt,
|
|
142
|
+
unwrap: props.unwrap,
|
|
143
|
+
})
|
|
144
|
+
if (!prepared) return undefined
|
|
145
|
+
return createComponent(element(props.tag ?? "div"), {
|
|
146
|
+
get children() {
|
|
147
|
+
return createComponent(MarkdownRenderer, { value: prepared.root, components: prepared.components })
|
|
148
|
+
},
|
|
149
|
+
})
|
|
150
|
+
}) as unknown as JSX.Element
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface UseSearchCollectionResult {
|
|
154
|
+
status: Accessor<SearchStatus>
|
|
155
|
+
search(query: string, options?: SearchOptions): Promise<SearchHit[]>
|
|
156
|
+
init(): Promise<void>
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The collection's search index, loaded on the first `search()` or `init()`, or at mount with `immediate`; `status` is a signal. */
|
|
160
|
+
export function useSearchCollection(
|
|
161
|
+
name: string,
|
|
162
|
+
{ immediate = false }: UseSearchCollectionOptions = {},
|
|
163
|
+
): UseSearchCollectionResult {
|
|
164
|
+
const collection = searchCollectionOf(name)
|
|
165
|
+
const [status, setStatus] = createSignal<SearchStatus>(collection.status)
|
|
166
|
+
onCleanup(collection.subscribe(() => setStatus(() => collection.status)))
|
|
167
|
+
if (immediate) onSettled(() => void collection.init().catch(ignore))
|
|
168
|
+
return { status, search: collection.search, init: collection.init }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** An accessor of the entry of `collection` whose `path` is the route path, `undefined` while it loads; reading it throws the 404 when there is none. */
|
|
172
|
+
export function useContentPage<N extends CollectionName>(collection: N): Accessor<ContentPage<N> | undefined> {
|
|
173
|
+
checkPageCollection(collection)
|
|
174
|
+
const route = useRoute()
|
|
175
|
+
const { data, error, status } = useAsyncData<PageEntry | null>(
|
|
176
|
+
() => pageKey(collection, route().path),
|
|
177
|
+
() => loadPage(collection, route().path),
|
|
178
|
+
)
|
|
179
|
+
const page = () => settledPage(route().path, { data: data(), error: error(), status: status() })
|
|
180
|
+
useSeoMeta(() => pageSeo(page()))
|
|
181
|
+
useHead(() => pageHead(page()))
|
|
182
|
+
return page as unknown as Accessor<ContentPage<N> | undefined>
|
|
183
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import ProseA from "./ProseA.svelte"
|
|
2
|
+
import ProseH1 from "./ProseH1.svelte"
|
|
3
|
+
import ProseH2 from "./ProseH2.svelte"
|
|
4
|
+
import ProseH3 from "./ProseH3.svelte"
|
|
5
|
+
import ProseH4 from "./ProseH4.svelte"
|
|
6
|
+
import ProseH5 from "./ProseH5.svelte"
|
|
7
|
+
import ProseH6 from "./ProseH6.svelte"
|
|
8
|
+
import ProseP from "./ProseP.svelte"
|
|
9
|
+
import ProseUl from "./ProseUl.svelte"
|
|
10
|
+
import ProseOl from "./ProseOl.svelte"
|
|
11
|
+
import ProseLi from "./ProseLi.svelte"
|
|
12
|
+
import ProseBlockquote from "./ProseBlockquote.svelte"
|
|
13
|
+
import ProsePre from "./ProsePre.svelte"
|
|
14
|
+
import ProseCode from "./ProseCode.svelte"
|
|
15
|
+
import ProseImg from "./ProseImg.svelte"
|
|
16
|
+
import ProseTable from "./ProseTable.svelte"
|
|
17
|
+
import ProseHr from "./ProseHr.svelte"
|
|
18
|
+
import ProseStrong from "./ProseStrong.svelte"
|
|
19
|
+
import ProseEm from "./ProseEm.svelte"
|
|
20
|
+
|
|
21
|
+
export const DEFAULTS = {
|
|
22
|
+
a: ProseA,
|
|
23
|
+
h1: ProseH1,
|
|
24
|
+
h2: ProseH2,
|
|
25
|
+
h3: ProseH3,
|
|
26
|
+
h4: ProseH4,
|
|
27
|
+
h5: ProseH5,
|
|
28
|
+
h6: ProseH6,
|
|
29
|
+
p: ProseP,
|
|
30
|
+
ul: ProseUl,
|
|
31
|
+
ol: ProseOl,
|
|
32
|
+
li: ProseLi,
|
|
33
|
+
blockquote: ProseBlockquote,
|
|
34
|
+
pre: ProsePre,
|
|
35
|
+
code: ProseCode,
|
|
36
|
+
img: ProseImg,
|
|
37
|
+
table: ProseTable,
|
|
38
|
+
hr: ProseHr,
|
|
39
|
+
strong: ProseStrong,
|
|
40
|
+
em: ProseEm,
|
|
41
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useAsyncData, useHead, useRoute, useSeoMeta } from "@hamur/adapter-svelte"
|
|
2
|
+
import type { CollectionName } from "../index.ts"
|
|
3
|
+
import type { PageEntry } from "../types.ts"
|
|
4
|
+
import { checkPageCollection, loadPage, pageHead, pageKey, pageSeo, settledPage, type ContentPage } from "./page.ts"
|
|
5
|
+
|
|
6
|
+
export type { ContentPage }
|
|
7
|
+
|
|
8
|
+
export interface UseContentPageResult<N extends string> {
|
|
9
|
+
/** The entry whose `path` is the route path, `undefined` while it loads; reading it throws the 404 when there is none. */
|
|
10
|
+
readonly page: ContentPage<N> | undefined
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The entry of `collection` whose `path` is the route path; `page` is reactive where it is read. */
|
|
14
|
+
export function useContentPage<N extends CollectionName>(collection: N): UseContentPageResult<N> {
|
|
15
|
+
checkPageCollection(collection)
|
|
16
|
+
const route = useRoute()
|
|
17
|
+
const result = useAsyncData<PageEntry | null>(
|
|
18
|
+
() => pageKey(collection, route.path),
|
|
19
|
+
() => loadPage(collection, route.path),
|
|
20
|
+
)
|
|
21
|
+
const read = () => settledPage(route.path, { data: result.data, error: result.error, status: result.status })
|
|
22
|
+
useSeoMeta(() => pageSeo(result.status === "success" ? result.data : undefined))
|
|
23
|
+
useHead(() => pageHead(result.status === "success" ? result.data : undefined))
|
|
24
|
+
return {
|
|
25
|
+
get page() {
|
|
26
|
+
return read() as unknown as ContentPage<N> | undefined
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { onMount } from "svelte"
|
|
2
|
+
import { createSubscriber } from "svelte/reactivity"
|
|
3
|
+
import {
|
|
4
|
+
ignore,
|
|
5
|
+
searchCollectionOf,
|
|
6
|
+
type SearchHit,
|
|
7
|
+
type SearchOptions,
|
|
8
|
+
type SearchStatus,
|
|
9
|
+
type UseSearchCollectionOptions,
|
|
10
|
+
} from "./search.ts"
|
|
11
|
+
|
|
12
|
+
export type { SearchHit, SearchOptions, SearchStatus, UseSearchCollectionOptions }
|
|
13
|
+
|
|
14
|
+
export interface UseSearchCollectionResult {
|
|
15
|
+
readonly status: SearchStatus
|
|
16
|
+
search(query: string, options?: SearchOptions): Promise<SearchHit[]>
|
|
17
|
+
init(): Promise<void>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The collection's search index, loaded on the first `search()` or `init()`, or at mount with `immediate`; `status` is reactive where it is read. */
|
|
21
|
+
export function useSearchCollection(
|
|
22
|
+
name: string,
|
|
23
|
+
{ immediate = false }: UseSearchCollectionOptions = {},
|
|
24
|
+
): UseSearchCollectionResult {
|
|
25
|
+
const collection = searchCollectionOf(name)
|
|
26
|
+
const subscribe = createSubscriber((update) => collection.subscribe(update))
|
|
27
|
+
if (immediate) onMount(() => void collection.init().catch(ignore))
|
|
28
|
+
return {
|
|
29
|
+
get status() {
|
|
30
|
+
subscribe()
|
|
31
|
+
return collection.status
|
|
32
|
+
},
|
|
33
|
+
search: collection.search,
|
|
34
|
+
init: collection.init,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export { default as ContentRenderer } from "./ContentRenderer.svelte"
|
|
2
|
+
export { default as ProseA } from "./ProseA.svelte"
|
|
3
|
+
export { default as ProseH1 } from "./ProseH1.svelte"
|
|
4
|
+
export { default as ProseH2 } from "./ProseH2.svelte"
|
|
5
|
+
export { default as ProseH3 } from "./ProseH3.svelte"
|
|
6
|
+
export { default as ProseH4 } from "./ProseH4.svelte"
|
|
7
|
+
export { default as ProseH5 } from "./ProseH5.svelte"
|
|
8
|
+
export { default as ProseH6 } from "./ProseH6.svelte"
|
|
9
|
+
export { default as ProseP } from "./ProseP.svelte"
|
|
10
|
+
export { default as ProseUl } from "./ProseUl.svelte"
|
|
11
|
+
export { default as ProseOl } from "./ProseOl.svelte"
|
|
12
|
+
export { default as ProseLi } from "./ProseLi.svelte"
|
|
13
|
+
export { default as ProseBlockquote } from "./ProseBlockquote.svelte"
|
|
14
|
+
export { default as ProsePre } from "./ProsePre.svelte"
|
|
15
|
+
export { default as ProseCode } from "./ProseCode.svelte"
|
|
16
|
+
export { default as ProseImg } from "./ProseImg.svelte"
|
|
17
|
+
export { default as ProseTable } from "./ProseTable.svelte"
|
|
18
|
+
export { default as ProseHr } from "./ProseHr.svelte"
|
|
19
|
+
export { default as ProseStrong } from "./ProseStrong.svelte"
|
|
20
|
+
export { default as ProseEm } from "./ProseEm.svelte"
|
|
21
|
+
export { useSearchCollection } from "./svelte-search.ts"
|
|
22
|
+
export type {
|
|
23
|
+
SearchHit,
|
|
24
|
+
SearchOptions,
|
|
25
|
+
SearchStatus,
|
|
26
|
+
UseSearchCollectionOptions,
|
|
27
|
+
UseSearchCollectionResult,
|
|
28
|
+
} from "./svelte-search.ts"
|
|
29
|
+
export { useContentPage } from "./svelte-page.ts"
|
|
30
|
+
export type { ContentPage, UseContentPageResult } from "./svelte-page.ts"
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { expect, mock } from "bun:test"
|
|
2
|
+
import type { RenderContext } from "hamur/adapter"
|
|
3
|
+
import { withBase } from "hamur/app"
|
|
4
|
+
import type { MdRoot } from "hamur/markdown"
|
|
5
|
+
import { buildSearchIndex } from "../../runtime/search.ts"
|
|
6
|
+
import { setPageSource } from "../../runtime/page-source.ts"
|
|
7
|
+
import { setSearchLoaders } from "../../runtime/search-loaders.ts"
|
|
8
|
+
import type { PageEntry } from "../../types.ts"
|
|
9
|
+
import { MARKDOWN_AST, expectMarkdownHtml } from "../../../../../tests/utils/markdown-renderer.ts"
|
|
10
|
+
|
|
11
|
+
export { MARKDOWN_AST }
|
|
12
|
+
|
|
13
|
+
/** The build writes `#hamur/markdown-components` and the page source; a unit test has no app, so the app has no prose files and the two collections `docs` (page) and `authors` (data). It also installs the one search collection a unit test has. */
|
|
14
|
+
export const noProseFiles = (): void => {
|
|
15
|
+
mock.module("#hamur/markdown-components", () => ({ prose: {}, default: {} }))
|
|
16
|
+
setPageSource({
|
|
17
|
+
query: () => {
|
|
18
|
+
throw new Error("no query in a unit test")
|
|
19
|
+
},
|
|
20
|
+
types: { docs: "page", authors: "data" },
|
|
21
|
+
})
|
|
22
|
+
setSearchLoaders({
|
|
23
|
+
docs: async () => {
|
|
24
|
+
searchLoads.count++
|
|
25
|
+
return { default: SEARCH_INDEX }
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The one search collection a unit test has, `docs`, and how often its index was imported. */
|
|
31
|
+
export const searchLoads = { count: 0 }
|
|
32
|
+
|
|
33
|
+
export const SEARCH_INDEX = buildSearchIndex([
|
|
34
|
+
{ id: "/a#components", title: "Components", titles: [], content: "Pieces of a page.", level: 2 },
|
|
35
|
+
{ id: "/a#install", title: "Install", titles: [], content: "Run the installer.", level: 2 },
|
|
36
|
+
])
|
|
37
|
+
|
|
38
|
+
const EXCERPT: MdRoot = ["root", {}, ["p", {}, "Lead ", ["strong", {}, "in"]]]
|
|
39
|
+
|
|
40
|
+
export const ENTRY = {
|
|
41
|
+
collection: "blog",
|
|
42
|
+
id: "blog/a",
|
|
43
|
+
path: "/blog/a",
|
|
44
|
+
body: MARKDOWN_AST,
|
|
45
|
+
excerpt: EXCERPT,
|
|
46
|
+
} as unknown as PageEntry
|
|
47
|
+
|
|
48
|
+
export const WITHOUT_EXCERPT = { ...ENTRY, excerpt: undefined } as unknown as PageEntry
|
|
49
|
+
|
|
50
|
+
export function renderContext(component: unknown, props?: Record<string, unknown>): RenderContext {
|
|
51
|
+
return {
|
|
52
|
+
url: new URL("http://localhost/"),
|
|
53
|
+
route: {
|
|
54
|
+
name: "index",
|
|
55
|
+
path: "/",
|
|
56
|
+
fullPath: "/",
|
|
57
|
+
params: {},
|
|
58
|
+
query: {},
|
|
59
|
+
search: {},
|
|
60
|
+
hash: "",
|
|
61
|
+
meta: {},
|
|
62
|
+
matched: [],
|
|
63
|
+
},
|
|
64
|
+
tree: { components: [{ id: "page", component, ...(props ? { props } : {}) }] },
|
|
65
|
+
payload: {},
|
|
66
|
+
head: {} as unknown as RenderContext["head"],
|
|
67
|
+
app: {} as unknown as RenderContext["app"],
|
|
68
|
+
stream: false,
|
|
69
|
+
baseURL: "/",
|
|
70
|
+
href: (to: unknown) => withBase(String(to), "/"),
|
|
71
|
+
} as unknown as RenderContext
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Hydration comments and keys out; Solid's server render writes a class value with a trailing space. */
|
|
75
|
+
export const clean = (html: string): string =>
|
|
76
|
+
html
|
|
77
|
+
.replace(/<!--[\s\S]*?-->/g, "")
|
|
78
|
+
.replace(/ _hk=(?:"[^"]*"|[^\s>]*)/g, "")
|
|
79
|
+
.replace(/\s+>/g, ">")
|
|
80
|
+
.replace(/ class="([^"]*?) "/g, ' class="$1"')
|
|
81
|
+
|
|
82
|
+
/** `value` an entry: the body of `MARKDOWN_AST` in a `div`, written as `MarkdownRenderer` writes it. */
|
|
83
|
+
export function expectDefault(rendered: string): void {
|
|
84
|
+
expect(clean(rendered)).toMatch(/<div><h1\b[^>]*\bid="about"[^>]*>About<\/h1>/)
|
|
85
|
+
expectMarkdownHtml(rendered)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A `ProseA` that writes `<a class="own">` and a `ProsePre` that writes `language` and `code` as attributes. */
|
|
89
|
+
export function expectOverrides(rendered: string): void {
|
|
90
|
+
const html = clean(rendered)
|
|
91
|
+
expect(html.match(/<a\b[^>]*\bclass="own"/g)).toHaveLength(3)
|
|
92
|
+
expect(html).toMatch(/<a\b(?=[^>]*class="own")(?=[^>]*href="#")[^>]*>/)
|
|
93
|
+
expect(html).not.toContain("data-hamur-prefetch")
|
|
94
|
+
expect(html).toMatch(
|
|
95
|
+
/<pre\b[^>]*data-lang="ts"[^>]*data-code="a (?:<|<) b"[^>]*><code\b[^>]*class="language-ts"[^>]*>a < b<\/code><\/pre>/,
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** `excerpt` with `unwrap: "p"` and `tag: "section"`. */
|
|
100
|
+
export function expectExcerpt(rendered: string): void {
|
|
101
|
+
expect(clean(rendered)).toContain("<section>Lead <strong>in</strong></section>")
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function expectNothing(rendered: string): void {
|
|
105
|
+
expect(clean(rendered)).not.toMatch(/<(div|section|h1|p)\b/)
|
|
106
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computed,
|
|
3
|
+
defineComponent,
|
|
4
|
+
getCurrentScope,
|
|
5
|
+
h,
|
|
6
|
+
onMounted,
|
|
7
|
+
onScopeDispose,
|
|
8
|
+
shallowRef,
|
|
9
|
+
type Component,
|
|
10
|
+
type ComputedRef,
|
|
11
|
+
type PropType,
|
|
12
|
+
} from "vue"
|
|
13
|
+
import { Link, MarkdownRenderer, useApp, useAsyncData, useHead, useRoute, useSeoMeta } from "@hamur/adapter-vue"
|
|
14
|
+
import type { MdRoot } from "hamur/markdown"
|
|
15
|
+
import type { CollectionName } from "../index.ts"
|
|
16
|
+
import type { PageEntry, PageFields } from "../types.ts"
|
|
17
|
+
import { checkPageCollection, loadPage, pageHead, pageKey, pageSeo, settledPage, type ContentPage } from "./page.ts"
|
|
18
|
+
import { isInternal, prepare } from "./prose.ts"
|
|
19
|
+
import {
|
|
20
|
+
ignore,
|
|
21
|
+
searchCollectionOf,
|
|
22
|
+
type SearchHit,
|
|
23
|
+
type SearchOptions,
|
|
24
|
+
type SearchStatus,
|
|
25
|
+
type UseSearchCollectionOptions,
|
|
26
|
+
} from "./search.ts"
|
|
27
|
+
|
|
28
|
+
export type { ContentPage, SearchHit, SearchOptions, SearchStatus, UseSearchCollectionOptions }
|
|
29
|
+
|
|
30
|
+
export interface ContentRendererProps {
|
|
31
|
+
/** A page entry, which renders its `body`, or a Markdown AST. */
|
|
32
|
+
value: PageFields | MdRoot
|
|
33
|
+
/** Markdown components by kebab-case name and prose components by `Prose<Tag>` name; `false` registers none. */
|
|
34
|
+
components?: Record<string, Component> | false
|
|
35
|
+
/** The wrapper element; `"div"` by default. */
|
|
36
|
+
tag?: string
|
|
37
|
+
/** Renders the entry's `excerpt` instead of its `body`. */
|
|
38
|
+
excerpt?: boolean
|
|
39
|
+
/** Space-separated tags whose element is dropped and whose children are kept. */
|
|
40
|
+
unwrap?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const plain = (tag: string, name: string) =>
|
|
44
|
+
defineComponent({
|
|
45
|
+
name,
|
|
46
|
+
setup(_props, { slots }) {
|
|
47
|
+
return () => h(tag, null, slots.default?.())
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
export const ProseH1 = plain("h1", "ProseH1")
|
|
52
|
+
export const ProseH2 = plain("h2", "ProseH2")
|
|
53
|
+
export const ProseH3 = plain("h3", "ProseH3")
|
|
54
|
+
export const ProseH4 = plain("h4", "ProseH4")
|
|
55
|
+
export const ProseH5 = plain("h5", "ProseH5")
|
|
56
|
+
export const ProseH6 = plain("h6", "ProseH6")
|
|
57
|
+
export const ProseP = plain("p", "ProseP")
|
|
58
|
+
export const ProseUl = plain("ul", "ProseUl")
|
|
59
|
+
export const ProseOl = plain("ol", "ProseOl")
|
|
60
|
+
export const ProseLi = plain("li", "ProseLi")
|
|
61
|
+
export const ProseBlockquote = plain("blockquote", "ProseBlockquote")
|
|
62
|
+
export const ProseCode = plain("code", "ProseCode")
|
|
63
|
+
export const ProseImg = plain("img", "ProseImg")
|
|
64
|
+
export const ProseTable = plain("table", "ProseTable")
|
|
65
|
+
export const ProseHr = plain("hr", "ProseHr")
|
|
66
|
+
export const ProseStrong = plain("strong", "ProseStrong")
|
|
67
|
+
export const ProseEm = plain("em", "ProseEm")
|
|
68
|
+
|
|
69
|
+
/** An internal `href` is a `Link`, as `MarkdownRenderer` writes it. */
|
|
70
|
+
export const ProseA = defineComponent({
|
|
71
|
+
name: "ProseA",
|
|
72
|
+
inheritAttrs: false,
|
|
73
|
+
props: { href: { type: String, default: undefined } },
|
|
74
|
+
setup(props, { attrs, slots }) {
|
|
75
|
+
return () =>
|
|
76
|
+
isInternal(props.href)
|
|
77
|
+
? h(Link, { ...attrs, to: props.href }, { default: slots.default })
|
|
78
|
+
: h("a", { href: props.href, ...attrs }, slots.default?.())
|
|
79
|
+
},
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
export const ProsePre = defineComponent({
|
|
83
|
+
name: "ProsePre",
|
|
84
|
+
props: {
|
|
85
|
+
language: { type: String, default: undefined },
|
|
86
|
+
filename: { type: String, default: undefined },
|
|
87
|
+
highlights: { type: Array as PropType<number[]>, default: undefined },
|
|
88
|
+
code: { type: String, default: undefined },
|
|
89
|
+
},
|
|
90
|
+
setup(props, { slots }) {
|
|
91
|
+
return () => h("pre", props.filename === undefined ? null : { "data-filename": props.filename }, slots.default?.())
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
const DEFAULTS = {
|
|
96
|
+
a: ProseA,
|
|
97
|
+
h1: ProseH1,
|
|
98
|
+
h2: ProseH2,
|
|
99
|
+
h3: ProseH3,
|
|
100
|
+
h4: ProseH4,
|
|
101
|
+
h5: ProseH5,
|
|
102
|
+
h6: ProseH6,
|
|
103
|
+
p: ProseP,
|
|
104
|
+
ul: ProseUl,
|
|
105
|
+
ol: ProseOl,
|
|
106
|
+
li: ProseLi,
|
|
107
|
+
blockquote: ProseBlockquote,
|
|
108
|
+
pre: ProsePre,
|
|
109
|
+
code: ProseCode,
|
|
110
|
+
img: ProseImg,
|
|
111
|
+
table: ProseTable,
|
|
112
|
+
hr: ProseHr,
|
|
113
|
+
strong: ProseStrong,
|
|
114
|
+
em: ProseEm,
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A page entry's body, or a Markdown AST, through `MarkdownRenderer` with the prose components of `components`. */
|
|
118
|
+
export const ContentRenderer = defineComponent({
|
|
119
|
+
name: "ContentRenderer",
|
|
120
|
+
props: {
|
|
121
|
+
value: { type: [Array, Object] as PropType<PageFields | MdRoot>, default: undefined },
|
|
122
|
+
components: { type: [Object, Boolean] as PropType<Record<string, Component> | false>, default: undefined },
|
|
123
|
+
tag: { type: String, default: "div" },
|
|
124
|
+
excerpt: { type: Boolean, default: false },
|
|
125
|
+
unwrap: { type: String, default: undefined },
|
|
126
|
+
},
|
|
127
|
+
setup(props) {
|
|
128
|
+
return () => {
|
|
129
|
+
const prepared = prepare({
|
|
130
|
+
value: props.value,
|
|
131
|
+
components: props.components,
|
|
132
|
+
defaults: DEFAULTS,
|
|
133
|
+
excerpt: props.excerpt,
|
|
134
|
+
unwrap: props.unwrap,
|
|
135
|
+
})
|
|
136
|
+
return prepared
|
|
137
|
+
? h(props.tag, [h(MarkdownRenderer, { value: prepared.root, components: prepared.components })])
|
|
138
|
+
: null
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
export interface UseSearchCollectionResult {
|
|
144
|
+
status: ComputedRef<SearchStatus>
|
|
145
|
+
search(query: string, options?: SearchOptions): Promise<SearchHit[]>
|
|
146
|
+
init(): Promise<void>
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The collection's search index, loaded on the first `search()` or `init()`, or at mount with `immediate`; `status` is a read-only ref. */
|
|
150
|
+
export function useSearchCollection(
|
|
151
|
+
name: string,
|
|
152
|
+
{ immediate = false }: UseSearchCollectionOptions = {},
|
|
153
|
+
): UseSearchCollectionResult {
|
|
154
|
+
const collection = searchCollectionOf(name)
|
|
155
|
+
const status = shallowRef<SearchStatus>(collection.status)
|
|
156
|
+
const stop = collection.subscribe(() => {
|
|
157
|
+
status.value = collection.status
|
|
158
|
+
})
|
|
159
|
+
if (getCurrentScope()) onScopeDispose(stop)
|
|
160
|
+
if (immediate) onMounted(() => void collection.init().catch(ignore))
|
|
161
|
+
return { status: computed(() => status.value), search: collection.search, init: collection.init }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Awaited in `setup`, resolves to a ref of the entry of `collection` whose `path` is the route path; rejects with the 404 when there is none. */
|
|
165
|
+
export async function useContentPage<N extends CollectionName>(collection: N): Promise<ComputedRef<ContentPage<N>>> {
|
|
166
|
+
checkPageCollection(collection)
|
|
167
|
+
const route = useRoute()
|
|
168
|
+
const store = useApp().asyncData
|
|
169
|
+
const key = () => pageKey(collection, route.path)
|
|
170
|
+
const { data, error, status } = useAsyncData<PageEntry | null>(key, () => loadPage(collection, route.path))
|
|
171
|
+
// A head entry made after an await has no component to belong to, so it is made first and reads what has settled.
|
|
172
|
+
useSeoMeta(() => pageSeo(status.value === "success" ? data.value : undefined))
|
|
173
|
+
useHead(() => pageHead(status.value === "success" ? data.value : undefined))
|
|
174
|
+
await store.get(key())?.promise
|
|
175
|
+
let last: PageEntry | undefined
|
|
176
|
+
const page = computed(() => {
|
|
177
|
+
last = settledPage(route.path, { data: data.value, error: error.value, status: status.value }) ?? last
|
|
178
|
+
return last
|
|
179
|
+
})
|
|
180
|
+
void page.value
|
|
181
|
+
return page as unknown as ComputedRef<ContentPage<N>>
|
|
182
|
+
}
|