@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.
Files changed (183) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +14 -2
  3. package/dist/chunks/bun-render-65mwzeq2.js +58 -0
  4. package/dist/chunks/bun-render-7jxnxg3v.js +714 -0
  5. package/dist/chunks/bun-render-mxhn5twt.js +130 -0
  6. package/dist/chunks/bun-render-w7fh3fg7.js +36 -0
  7. package/dist/src/adapters/html.js +134 -0
  8. package/dist/src/adapters/preact.js +126 -0
  9. package/dist/src/adapters/react.js +116 -0
  10. package/dist/src/adapters/solid.js +153 -0
  11. package/dist/src/adapters/svelte.js +90 -0
  12. package/dist/src/adapters/vue.js +166 -0
  13. package/dist/src/index.js +2732 -0
  14. package/dist/src/query.js +97 -0
  15. package/dist/src/render.js +12 -0
  16. package/dist/src/server/index.js +349 -0
  17. package/dist/types/src/adapters/html.d.ts +54 -0
  18. package/dist/types/src/adapters/page.d.ts +21 -0
  19. package/dist/types/src/adapters/preact.d.ts +53 -0
  20. package/dist/types/src/adapters/prose.d.ts +21 -0
  21. package/dist/types/src/adapters/react.d.ts +53 -0
  22. package/dist/types/src/adapters/search.d.ts +12 -0
  23. package/dist/types/src/adapters/solid.d.ts +54 -0
  24. package/dist/types/src/adapters/svelte-page.d.ts +9 -0
  25. package/dist/types/src/adapters/svelte-search.d.ts +9 -0
  26. package/dist/types/src/adapters/svelte.d.ts +24 -0
  27. package/dist/types/src/adapters/vue.d.ts +189 -0
  28. package/dist/types/src/build/api.d.ts +24 -0
  29. package/dist/types/src/build/dev-route.d.ts +4 -0
  30. package/dist/types/src/build/dev.d.ts +48 -0
  31. package/dist/types/src/build/graphql.d.ts +10 -0
  32. package/dist/types/src/build/image.d.ts +16 -0
  33. package/dist/types/src/build/index.d.ts +76 -0
  34. package/dist/types/src/build/live.d.ts +6 -0
  35. package/dist/types/src/build/prerender.d.ts +8 -0
  36. package/dist/types/src/build/query.d.ts +10 -0
  37. package/dist/types/src/build/raw.d.ts +23 -0
  38. package/dist/types/src/build/search.d.ts +9 -0
  39. package/dist/types/src/build/types.d.ts +16 -0
  40. package/dist/types/src/component.d.ts +10 -0
  41. package/dist/types/src/config.d.ts +6 -0
  42. package/dist/types/src/define.d.ts +14 -0
  43. package/dist/types/src/error.d.ts +4 -0
  44. package/dist/types/src/image.d.ts +15 -0
  45. package/dist/types/src/index.d.ts +64 -0
  46. package/dist/types/src/loaders/data.d.ts +12 -0
  47. package/dist/types/src/loaders/file.d.ts +3 -0
  48. package/dist/types/src/loaders/git.d.ts +27 -0
  49. package/dist/types/src/loaders/glob.d.ts +14 -0
  50. package/dist/types/src/module.d.ts +3 -0
  51. package/dist/types/src/query.d.ts +17 -0
  52. package/dist/types/src/render.d.ts +9 -0
  53. package/dist/types/src/runtime/client.d.ts +25 -0
  54. package/dist/types/src/runtime/dev-client.d.ts +4 -0
  55. package/dist/types/src/runtime/dev-plugin.d.ts +12 -0
  56. package/dist/types/src/runtime/dev-store.d.ts +5 -0
  57. package/dist/types/src/runtime/live.d.ts +36 -0
  58. package/dist/types/src/runtime/navigation.d.ts +35 -0
  59. package/dist/types/src/runtime/page-plugin.d.ts +2 -0
  60. package/dist/types/src/runtime/page-source.d.ts +12 -0
  61. package/dist/types/src/runtime/query.d.ts +100 -0
  62. package/dist/types/src/runtime/raw-path.d.ts +2 -0
  63. package/dist/types/src/runtime/refresh-body.d.ts +4 -0
  64. package/dist/types/src/runtime/search-loaders.d.ts +7 -0
  65. package/dist/types/src/runtime/search-plugin.d.ts +2 -0
  66. package/dist/types/src/runtime/search-store.d.ts +19 -0
  67. package/dist/types/src/runtime/search.d.ts +28 -0
  68. package/dist/types/src/runtime/sections.d.ts +36 -0
  69. package/dist/types/src/runtime/server-store.d.ts +4 -0
  70. package/dist/types/src/runtime/store.d.ts +31 -0
  71. package/dist/types/src/server/door-dev-static.d.ts +3 -0
  72. package/dist/types/src/server/door-dev.d.ts +4 -0
  73. package/dist/types/src/server/door-static.d.ts +2 -0
  74. package/dist/types/src/server/door.d.ts +3 -0
  75. package/dist/types/src/server/endpoint.d.ts +1 -0
  76. package/dist/types/src/server/graphql.d.ts +24 -0
  77. package/dist/types/src/server/index.d.ts +10 -0
  78. package/dist/types/src/server/install.d.ts +1 -0
  79. package/dist/types/src/server/live.d.ts +10 -0
  80. package/dist/types/src/server/query.d.ts +15 -0
  81. package/dist/types/src/server/refresh.d.ts +2 -0
  82. package/dist/types/src/server/rest.d.ts +62 -0
  83. package/dist/types/src/server/search.d.ts +5 -0
  84. package/dist/types/src/server-config.d.ts +12 -0
  85. package/dist/types/src/source.d.ts +24 -0
  86. package/dist/types/src/types.d.ts +293 -0
  87. package/package.json +140 -4
  88. package/src/adapters/ContentRenderer.svelte +27 -0
  89. package/src/adapters/ProseA.svelte +11 -0
  90. package/src/adapters/ProseBlockquote.svelte +7 -0
  91. package/src/adapters/ProseCode.svelte +7 -0
  92. package/src/adapters/ProseEm.svelte +7 -0
  93. package/src/adapters/ProseH1.svelte +7 -0
  94. package/src/adapters/ProseH2.svelte +7 -0
  95. package/src/adapters/ProseH3.svelte +7 -0
  96. package/src/adapters/ProseH4.svelte +7 -0
  97. package/src/adapters/ProseH5.svelte +7 -0
  98. package/src/adapters/ProseH6.svelte +7 -0
  99. package/src/adapters/ProseHr.svelte +5 -0
  100. package/src/adapters/ProseImg.svelte +5 -0
  101. package/src/adapters/ProseLi.svelte +7 -0
  102. package/src/adapters/ProseOl.svelte +7 -0
  103. package/src/adapters/ProseP.svelte +7 -0
  104. package/src/adapters/ProsePre.svelte +21 -0
  105. package/src/adapters/ProseStrong.svelte +7 -0
  106. package/src/adapters/ProseTable.svelte +7 -0
  107. package/src/adapters/ProseUl.svelte +7 -0
  108. package/src/adapters/html.ts +152 -0
  109. package/src/adapters/page.ts +52 -0
  110. package/src/adapters/preact.ts +154 -0
  111. package/src/adapters/prose.ts +55 -0
  112. package/src/adapters/react.ts +142 -0
  113. package/src/adapters/search.ts +32 -0
  114. package/src/adapters/solid.ts +183 -0
  115. package/src/adapters/svelte-defaults.ts +41 -0
  116. package/src/adapters/svelte-page.ts +29 -0
  117. package/src/adapters/svelte-search.ts +36 -0
  118. package/src/adapters/svelte.ts +30 -0
  119. package/src/adapters/test/OwnA.svelte +7 -0
  120. package/src/adapters/test/OwnH1.svelte +7 -0
  121. package/src/adapters/test/OwnPre.svelte +7 -0
  122. package/src/adapters/test/support.ts +106 -0
  123. package/src/adapters/vue.ts +182 -0
  124. package/src/build/api.ts +201 -0
  125. package/src/build/dev-route.ts +5 -0
  126. package/src/build/dev.ts +299 -0
  127. package/src/build/graphql.ts +197 -0
  128. package/src/build/image.ts +60 -0
  129. package/src/build/index.ts +597 -0
  130. package/src/build/live.ts +34 -0
  131. package/src/build/prerender.ts +34 -0
  132. package/src/build/query.ts +70 -0
  133. package/src/build/raw.ts +157 -0
  134. package/src/build/search.ts +64 -0
  135. package/src/build/types.ts +108 -0
  136. package/src/component.ts +86 -0
  137. package/src/config.ts +156 -0
  138. package/src/define.ts +33 -0
  139. package/src/env.d.ts +34 -0
  140. package/src/error.ts +4 -0
  141. package/src/image.ts +41 -0
  142. package/src/index.ts +103 -0
  143. package/src/loaders/data.ts +186 -0
  144. package/src/loaders/file.ts +53 -0
  145. package/src/loaders/git.ts +266 -0
  146. package/src/loaders/glob.ts +179 -0
  147. package/src/module.ts +26 -0
  148. package/src/query.ts +82 -0
  149. package/src/render.ts +16 -0
  150. package/src/runtime/client.ts +75 -0
  151. package/src/runtime/dev-client.ts +6 -0
  152. package/src/runtime/dev-plugin.ts +64 -0
  153. package/src/runtime/dev-store.ts +19 -0
  154. package/src/runtime/live.ts +331 -0
  155. package/src/runtime/navigation.ts +225 -0
  156. package/src/runtime/page-plugin.ts +13 -0
  157. package/src/runtime/page-source.ts +22 -0
  158. package/src/runtime/query.ts +605 -0
  159. package/src/runtime/raw-path.ts +2 -0
  160. package/src/runtime/refresh-body.ts +22 -0
  161. package/src/runtime/search-loaders.ts +14 -0
  162. package/src/runtime/search-plugin.ts +6 -0
  163. package/src/runtime/search-store.ts +73 -0
  164. package/src/runtime/search.ts +126 -0
  165. package/src/runtime/sections.ts +155 -0
  166. package/src/runtime/server-store.ts +12 -0
  167. package/src/runtime/store.ts +59 -0
  168. package/src/server/door-dev-static.ts +6 -0
  169. package/src/server/door-dev.ts +7 -0
  170. package/src/server/door-static.ts +4 -0
  171. package/src/server/door.ts +6 -0
  172. package/src/server/endpoint.ts +17 -0
  173. package/src/server/graphql.ts +59 -0
  174. package/src/server/index.ts +17 -0
  175. package/src/server/install.ts +6 -0
  176. package/src/server/live.ts +51 -0
  177. package/src/server/query.ts +46 -0
  178. package/src/server/refresh.ts +19 -0
  179. package/src/server/rest.ts +157 -0
  180. package/src/server/search.ts +22 -0
  181. package/src/server-config.ts +24 -0
  182. package/src/source.ts +99 -0
  183. package/src/types.ts +282 -0
@@ -0,0 +1,142 @@
1
+ import { createElement, useEffect, useSyncExternalStore, type ComponentType, type ReactNode } from "react"
2
+ import { Link, MarkdownRenderer, useAsyncData, useHead, useRoute, useSeoMeta } from "@hamur/adapter-react"
3
+ import type { MdRoot } from "hamur/markdown"
4
+ import type { CollectionName } from "../index.ts"
5
+ import type { PageEntry, PageFields } from "../types.ts"
6
+ import { checkPageCollection, loadPage, pageHead, pageKey, pageSeo, settledPage, type ContentPage } from "./page.ts"
7
+ import { isInternal, prepare } from "./prose.ts"
8
+ import {
9
+ ignore,
10
+ searchCollectionOf,
11
+ type SearchHit,
12
+ type SearchOptions,
13
+ type SearchStatus,
14
+ type UseSearchCollectionOptions,
15
+ } from "./search.ts"
16
+
17
+ export type { ContentPage, SearchHit, SearchOptions, SearchStatus, UseSearchCollectionOptions }
18
+
19
+ export interface ContentRendererProps {
20
+ /** A page entry, which renders its `body`, or a Markdown AST. */
21
+ value: PageFields | MdRoot
22
+ /** Markdown components by kebab-case name and prose components by `Prose<Tag>` name; `false` registers none. */
23
+ components?: Record<string, ComponentType<any>> | false
24
+ /** The wrapper element; `"div"` by default. */
25
+ tag?: string
26
+ /** Renders the entry's `excerpt` instead of its `body`. */
27
+ excerpt?: boolean
28
+ /** Space-separated tags whose element is dropped and whose children are kept. */
29
+ unwrap?: string
30
+ }
31
+
32
+ export type ProseProps = Record<string, unknown> & { children?: ReactNode }
33
+
34
+ const plain =
35
+ (tag: string) =>
36
+ (props: ProseProps): ReactNode =>
37
+ createElement(tag, props)
38
+
39
+ export const ProseH1 = plain("h1")
40
+ export const ProseH2 = plain("h2")
41
+ export const ProseH3 = plain("h3")
42
+ export const ProseH4 = plain("h4")
43
+ export const ProseH5 = plain("h5")
44
+ export const ProseH6 = plain("h6")
45
+ export const ProseP = plain("p")
46
+ export const ProseUl = plain("ul")
47
+ export const ProseOl = plain("ol")
48
+ export const ProseLi = plain("li")
49
+ export const ProseBlockquote = plain("blockquote")
50
+ export const ProseCode = plain("code")
51
+ export const ProseImg = plain("img")
52
+ export const ProseTable = plain("table")
53
+ export const ProseHr = plain("hr")
54
+ export const ProseStrong = plain("strong")
55
+ export const ProseEm = plain("em")
56
+
57
+ /** An internal `href` is a `Link`, as `MarkdownRenderer` writes it. */
58
+ export function ProseA({ href, ...rest }: ProseProps): ReactNode {
59
+ return isInternal(href)
60
+ ? createElement(Link as (props: Record<string, unknown>) => ReactNode, { ...rest, to: href })
61
+ : createElement("a", { href, ...rest })
62
+ }
63
+
64
+ export function ProsePre({
65
+ language: _language,
66
+ highlights: _highlights,
67
+ code: _code,
68
+ filename,
69
+ ...rest
70
+ }: ProseProps): ReactNode {
71
+ return createElement("pre", typeof filename === "string" ? { ...rest, "data-filename": filename } : rest)
72
+ }
73
+
74
+ const DEFAULTS = {
75
+ a: ProseA,
76
+ h1: ProseH1,
77
+ h2: ProseH2,
78
+ h3: ProseH3,
79
+ h4: ProseH4,
80
+ h5: ProseH5,
81
+ h6: ProseH6,
82
+ p: ProseP,
83
+ ul: ProseUl,
84
+ ol: ProseOl,
85
+ li: ProseLi,
86
+ blockquote: ProseBlockquote,
87
+ pre: ProsePre,
88
+ code: ProseCode,
89
+ img: ProseImg,
90
+ table: ProseTable,
91
+ hr: ProseHr,
92
+ strong: ProseStrong,
93
+ em: ProseEm,
94
+ }
95
+
96
+ /** A page entry's body, or a Markdown AST, through `MarkdownRenderer` with the prose components of `components`. */
97
+ export function ContentRenderer({ value, components, tag = "div", excerpt, unwrap }: ContentRendererProps): ReactNode {
98
+ const prepared = prepare({ value, components, defaults: DEFAULTS, excerpt, unwrap, rename: { class: "className" } })
99
+ return prepared
100
+ ? createElement(
101
+ tag,
102
+ null,
103
+ createElement(MarkdownRenderer, { value: prepared.root, components: prepared.components }),
104
+ )
105
+ : null
106
+ }
107
+
108
+ export interface UseSearchCollectionResult {
109
+ status: SearchStatus
110
+ search(query: string, options?: SearchOptions): Promise<SearchHit[]>
111
+ init(): Promise<void>
112
+ }
113
+
114
+ /** The collection's search index, loaded on the first `search()` or `init()`, or at mount with `immediate`; `status` is state. */
115
+ export function useSearchCollection(
116
+ name: string,
117
+ { immediate = false }: UseSearchCollectionOptions = {},
118
+ ): UseSearchCollectionResult {
119
+ const collection = searchCollectionOf(name)
120
+ const status = useSyncExternalStore(
121
+ collection.subscribe,
122
+ () => collection.status,
123
+ () => "idle" as SearchStatus,
124
+ )
125
+ useEffect(() => {
126
+ if (immediate) collection.init().catch(ignore)
127
+ }, [collection, immediate])
128
+ return { status, search: collection.search, init: collection.init }
129
+ }
130
+
131
+ /** The entry of `collection` whose `path` is the route path, suspending until it loads; throws the 404 when there is none. */
132
+ export function useContentPage<N extends CollectionName>(collection: N): ContentPage<N> {
133
+ checkPageCollection(collection)
134
+ const { path } = useRoute()
135
+ const { data, error, status } = useAsyncData<PageEntry | null>(pageKey(collection, path), () =>
136
+ loadPage(collection, path),
137
+ )
138
+ const page = settledPage(path, { data, error, status })
139
+ useSeoMeta(pageSeo(page))
140
+ useHead(pageHead(page))
141
+ return page as unknown as ContentPage<N>
142
+ }
@@ -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,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { href, children }: { href?: string; children?: Snippet } = $props()
5
+ </script>
6
+
7
+ <a class="own" {href}>{@render children?.()}</a>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { id, children }: { id?: string; children?: Snippet } = $props()
5
+ </script>
6
+
7
+ <h1 data-own {id}>{@render children?.()}</h1>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { language, code, children }: { language?: string; code?: string; children?: Snippet } = $props()
5
+ </script>
6
+
7
+ <pre data-lang={language} data-code={code}>{@render children?.()}</pre>
@@ -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 (?:&lt;|<) b"[^>]*><code\b[^>]*class="language-ts"[^>]*>a &lt; 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
+ }