@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,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <h1 {...rest}>{@render children?.()}</h1>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <h2 {...rest}>{@render children?.()}</h2>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <h3 {...rest}>{@render children?.()}</h3>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <h4 {...rest}>{@render children?.()}</h4>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <h5 {...rest}>{@render children?.()}</h5>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <h6 {...rest}>{@render children?.()}</h6>
@@ -0,0 +1,5 @@
1
+ <script lang="ts">
2
+ let { ...rest }: { [attribute: string]: unknown } = $props()
3
+ </script>
4
+
5
+ <hr {...rest} />
@@ -0,0 +1,5 @@
1
+ <script lang="ts">
2
+ let { ...rest }: { [attribute: string]: unknown } = $props()
3
+ </script>
4
+
5
+ <svelte:element this="img" {...rest} />
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <li {...rest}>{@render children?.()}</li>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <ol {...rest}>{@render children?.()}</ol>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <p {...rest}>{@render children?.()}</p>
@@ -0,0 +1,21 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let {
5
+ language: _language,
6
+ highlights: _highlights,
7
+ code: _code,
8
+ filename,
9
+ children,
10
+ ...rest
11
+ }: {
12
+ language?: string
13
+ highlights?: number[]
14
+ code?: string
15
+ filename?: string
16
+ children?: Snippet
17
+ [attribute: string]: unknown
18
+ } = $props()
19
+ </script>
20
+
21
+ <pre {...rest} data-filename={filename}>{@render children?.()}</pre>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <strong {...rest}>{@render children?.()}</strong>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <table {...rest}>{@render children?.()}</table>
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+
4
+ let { children, ...rest }: { children?: Snippet; [attribute: string]: unknown } = $props()
5
+ </script>
6
+
7
+ <ul {...rest}>{@render children?.()}</ul>
@@ -0,0 +1,152 @@
1
+ import { Link, MarkdownRenderer, useAsyncData, useHead, useRoute, useSeoMeta } from "@hamur/adapter-html"
2
+ import type { MdRoot } from "hamur/markdown"
3
+ import type { CollectionName } from "../index.ts"
4
+ import type { PageEntry, PageFields } from "../types.ts"
5
+ import { checkPageCollection, loadPage, pageHead, pageKey, pageSeo, settledPage, type ContentPage } from "./page.ts"
6
+ import { isInternal, prepare } from "./prose.ts"
7
+ import {
8
+ ignore,
9
+ searchCollectionOf,
10
+ type SearchHit,
11
+ type SearchOptions,
12
+ type SearchStatus,
13
+ type UseSearchCollectionOptions,
14
+ } from "./search.ts"
15
+
16
+ export type { ContentPage, SearchHit, SearchOptions, SearchStatus, UseSearchCollectionOptions }
17
+
18
+ export type ProseProps = Record<string, unknown> & { children?: string }
19
+
20
+ export interface ContentRendererProps {
21
+ /** A page entry, which renders its `body`, or a Markdown AST. */
22
+ value: PageFields | MdRoot
23
+ /** Markdown components by kebab-case name and prose components by `Prose<Tag>` name, each called with the node's props and `children` as a string; `false` registers none. */
24
+ components?: Record<string, (props: ProseProps) => string> | false
25
+ /** The wrapper element; `"div"` by default. */
26
+ tag?: string
27
+ /** Renders the entry's `excerpt` instead of its `body`. */
28
+ excerpt?: boolean
29
+ /** Space-separated tags whose element is dropped and whose children are kept. */
30
+ unwrap?: string
31
+ }
32
+
33
+ const VOID = new Set(["hr", "img"])
34
+ const ESCAPES: Record<string, string> = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }
35
+
36
+ function element(tag: string, props: Record<string, unknown>): string {
37
+ const { children = "", ...attrs } = props
38
+ let open = `<${tag}`
39
+ for (const [name, value] of Object.entries(attrs)) {
40
+ if (value === true) open += ` ${name}`
41
+ else if (typeof value === "string") open += ` ${name}="${value.replace(/[&<>"']/g, (char) => ESCAPES[char]!)}"`
42
+ }
43
+ return VOID.has(tag) ? `${open} />` : `${open}>${String(children)}</${tag}>`
44
+ }
45
+
46
+ const plain =
47
+ (tag: string) =>
48
+ (props: ProseProps): string =>
49
+ element(tag, props)
50
+
51
+ export const ProseH1 = plain("h1")
52
+ export const ProseH2 = plain("h2")
53
+ export const ProseH3 = plain("h3")
54
+ export const ProseH4 = plain("h4")
55
+ export const ProseH5 = plain("h5")
56
+ export const ProseH6 = plain("h6")
57
+ export const ProseP = plain("p")
58
+ export const ProseUl = plain("ul")
59
+ export const ProseOl = plain("ol")
60
+ export const ProseLi = plain("li")
61
+ export const ProseBlockquote = plain("blockquote")
62
+ export const ProseCode = plain("code")
63
+ export const ProseImg = plain("img")
64
+ export const ProseTable = plain("table")
65
+ export const ProseHr = plain("hr")
66
+ export const ProseStrong = plain("strong")
67
+ export const ProseEm = plain("em")
68
+
69
+ /** An internal `href` is a `Link`, as `MarkdownRenderer` writes it. */
70
+ export function ProseA({ href, ...rest }: ProseProps): string {
71
+ return isInternal(href) ? Link({ ...rest, to: href } as Parameters<typeof Link>[0]) : element("a", { href, ...rest })
72
+ }
73
+
74
+ export function ProsePre({
75
+ language: _language,
76
+ highlights: _highlights,
77
+ code: _code,
78
+ filename,
79
+ ...rest
80
+ }: ProseProps): string {
81
+ return element("pre", typeof filename === "string" ? { ...rest, "data-filename": filename } : rest)
82
+ }
83
+
84
+ const DEFAULTS = {
85
+ a: ProseA,
86
+ h1: ProseH1,
87
+ h2: ProseH2,
88
+ h3: ProseH3,
89
+ h4: ProseH4,
90
+ h5: ProseH5,
91
+ h6: ProseH6,
92
+ p: ProseP,
93
+ ul: ProseUl,
94
+ ol: ProseOl,
95
+ li: ProseLi,
96
+ blockquote: ProseBlockquote,
97
+ pre: ProsePre,
98
+ code: ProseCode,
99
+ img: ProseImg,
100
+ table: ProseTable,
101
+ hr: ProseHr,
102
+ strong: ProseStrong,
103
+ em: ProseEm,
104
+ }
105
+
106
+ /** A page entry's body, or a Markdown AST, through `MarkdownRenderer` with the prose components of `components`. */
107
+ export function ContentRenderer({ value, components, tag = "div", excerpt, unwrap }: ContentRendererProps): string {
108
+ const prepared = prepare({ value, components, defaults: DEFAULTS, excerpt, unwrap })
109
+ return prepared
110
+ ? element(tag, { children: MarkdownRenderer({ value: prepared.root, components: prepared.components }) })
111
+ : ""
112
+ }
113
+
114
+ export interface UseSearchCollectionResult {
115
+ readonly status: SearchStatus
116
+ /** Calls `listener` with the new status on each change; returns the function that stops it. */
117
+ onStatus(listener: (status: SearchStatus) => void): () => void
118
+ search(query: string, options?: SearchOptions): Promise<SearchHit[]>
119
+ init(): Promise<void>
120
+ }
121
+
122
+ /** The collection's search index, loaded on the first `search()` or `init()`, or at once in the browser with `immediate`; `status` is a getter. */
123
+ export function useSearchCollection(
124
+ name: string,
125
+ { immediate = false }: UseSearchCollectionOptions = {},
126
+ ): UseSearchCollectionResult {
127
+ const collection = searchCollectionOf(name)
128
+ if (immediate && !import.meta.server) collection.init().catch(ignore)
129
+ return {
130
+ get status() {
131
+ return collection.status
132
+ },
133
+ onStatus: (listener) => collection.subscribe(() => listener(collection.status)),
134
+ search: collection.search,
135
+ init: collection.init,
136
+ }
137
+ }
138
+
139
+ /** The entry of `collection` whose `path` is the route path, `undefined` while it loads; throws the 404 when there is none. */
140
+ export function useContentPage<N extends CollectionName>(collection: N): ContentPage<N> | undefined {
141
+ checkPageCollection(collection)
142
+ const { path } = useRoute()
143
+ const { data, error, status } = useAsyncData<PageEntry | null>(pageKey(collection, path), () =>
144
+ loadPage(collection, path),
145
+ )
146
+ const page = settledPage(path, { data, error, status })
147
+ if (page) {
148
+ useSeoMeta(pageSeo(page))
149
+ useHead(pageHead(page))
150
+ }
151
+ return page as unknown as ContentPage<N> | undefined
152
+ }
@@ -0,0 +1,52 @@
1
+ import { createError, type HamurError, type HeadInput, type SeoMetaInput } from "hamur/app"
2
+ import type { CollectionItem } from "../index.ts"
3
+ import { rawPath } from "../runtime/raw-path.ts"
4
+ import { pageSource } from "../runtime/page-source.ts"
5
+ import type { PageEntry, PageFields } from "../types.ts"
6
+
7
+ /** What `useContentPage(N)` finds: the page fields of the collection's item type, `PageEntry` while typegen has written none. */
8
+ export type ContentPage<N extends string> = Extract<CollectionItem<N>, PageFields>
9
+
10
+ export const pageKey = (collection: string, path: string): string => `content:${collection}:${path}`
11
+
12
+ /** A data collection has no paths. */
13
+ export function checkPageCollection(collection: string): void {
14
+ const { types } = pageSource()
15
+ if (Object.hasOwn(types, collection) && types[collection] === "data")
16
+ throw new Error(`useContentPage(${JSON.stringify(collection)}): a data collection has no paths`)
17
+ }
18
+
19
+ /** The entry of `collection` whose `path` is `path`; `null` (a value the payload keeps) when there is none. */
20
+ export async function loadPage(collection: string, path: string): Promise<PageEntry | null> {
21
+ return (await pageSource().query(collection).path(path).first()) ?? null
22
+ }
23
+
24
+ /** The entry's `seo` with `title` and `description` from the entry when `seo` has none. */
25
+ export function pageSeo(entry: PageFields | null | undefined): SeoMetaInput {
26
+ if (!entry) return {}
27
+ const { seo } = entry
28
+ return { ...seo, title: seo.title ?? entry.title, description: seo.description ?? entry.description } as SeoMetaInput
29
+ }
30
+
31
+ /** The head `link` to the page's Markdown copy; none unless `content.raw` is on. */
32
+ export function pageHead(entry: PageFields | null | undefined): HeadInput {
33
+ const { rawBase } = pageSource()
34
+ if (!entry || rawBase === undefined) return {}
35
+ return {
36
+ link: [{ rel: "alternate", type: "text/markdown", href: `${rawBase.replace(/\/$/, "")}${rawPath(entry.path)}` }],
37
+ }
38
+ }
39
+
40
+ export interface PageRead {
41
+ data: PageEntry | null | undefined
42
+ error: HamurError | undefined
43
+ status: string
44
+ }
45
+
46
+ /** The entry of a settled read; a failed read throws its error and a settled read without an entry the 404. `undefined` while the entry loads. */
47
+ export function settledPage(path: string, { data, error, status }: PageRead): PageEntry | undefined {
48
+ if (error) throw error
49
+ if (status !== "success") return undefined
50
+ if (!data) throw createError({ status: 404, message: `Page not found: ${path}` })
51
+ return data
52
+ }
@@ -0,0 +1,154 @@
1
+ import { createElement, type ComponentChildren, type ComponentType, type VNode } from "preact"
2
+ import { useEffect, useState } from "preact/hooks"
3
+ import { Link, MarkdownRenderer, useAsyncData, useHead, useRoute, useSeoMeta } from "@hamur/adapter-preact"
4
+ import type { MdRoot } from "hamur/markdown"
5
+ import type { CollectionName } from "../index.ts"
6
+ import type { PageEntry, PageFields } from "../types.ts"
7
+ import { checkPageCollection, loadPage, pageHead, pageKey, pageSeo, settledPage, type ContentPage } from "./page.ts"
8
+ import { isInternal, prepare } from "./prose.ts"
9
+ import {
10
+ ignore,
11
+ searchCollectionOf,
12
+ type SearchHit,
13
+ type SearchOptions,
14
+ type SearchStatus,
15
+ type UseSearchCollectionOptions,
16
+ } from "./search.ts"
17
+
18
+ export type { ContentPage, SearchHit, SearchOptions, SearchStatus, UseSearchCollectionOptions }
19
+
20
+ export interface ContentRendererProps {
21
+ /** A page entry, which renders its `body`, or a Markdown AST. */
22
+ value: PageFields | MdRoot
23
+ /** Markdown components by kebab-case name and prose components by `Prose<Tag>` name; `false` registers none. */
24
+ components?: Record<string, ComponentType<any>> | false
25
+ /** The wrapper element; `"div"` by default. */
26
+ tag?: string
27
+ /** Renders the entry's `excerpt` instead of its `body`. */
28
+ excerpt?: boolean
29
+ /** Space-separated tags whose element is dropped and whose children are kept. */
30
+ unwrap?: string
31
+ }
32
+
33
+ export type ProseProps = Record<string, unknown> & { children?: ComponentChildren }
34
+
35
+ const plain =
36
+ (tag: string) =>
37
+ (props: ProseProps): VNode =>
38
+ createElement(tag, props as unknown as Record<string, string>)
39
+
40
+ export const ProseH1 = plain("h1")
41
+ export const ProseH2 = plain("h2")
42
+ export const ProseH3 = plain("h3")
43
+ export const ProseH4 = plain("h4")
44
+ export const ProseH5 = plain("h5")
45
+ export const ProseH6 = plain("h6")
46
+ export const ProseP = plain("p")
47
+ export const ProseUl = plain("ul")
48
+ export const ProseOl = plain("ol")
49
+ export const ProseLi = plain("li")
50
+ export const ProseBlockquote = plain("blockquote")
51
+ export const ProseCode = plain("code")
52
+ export const ProseImg = plain("img")
53
+ export const ProseTable = plain("table")
54
+ export const ProseHr = plain("hr")
55
+ export const ProseStrong = plain("strong")
56
+ export const ProseEm = plain("em")
57
+
58
+ /** An internal `href` is a `Link`, as `MarkdownRenderer` writes it. */
59
+ export function ProseA({ href, ...rest }: ProseProps): VNode {
60
+ return isInternal(href)
61
+ ? createElement(
62
+ Link as unknown as (props: Record<string, unknown>) => VNode,
63
+ { ...rest, to: href } as unknown as Record<string, unknown>,
64
+ )
65
+ : createElement("a", { href, ...rest } as unknown as Record<string, string>)
66
+ }
67
+
68
+ export function ProsePre({
69
+ language: _language,
70
+ highlights: _highlights,
71
+ code: _code,
72
+ filename,
73
+ ...rest
74
+ }: ProseProps): VNode {
75
+ return createElement(
76
+ "pre",
77
+ (typeof filename === "string" ? { ...rest, "data-filename": filename } : rest) as unknown as Record<string, string>,
78
+ )
79
+ }
80
+
81
+ const DEFAULTS = {
82
+ a: ProseA,
83
+ h1: ProseH1,
84
+ h2: ProseH2,
85
+ h3: ProseH3,
86
+ h4: ProseH4,
87
+ h5: ProseH5,
88
+ h6: ProseH6,
89
+ p: ProseP,
90
+ ul: ProseUl,
91
+ ol: ProseOl,
92
+ li: ProseLi,
93
+ blockquote: ProseBlockquote,
94
+ pre: ProsePre,
95
+ code: ProseCode,
96
+ img: ProseImg,
97
+ table: ProseTable,
98
+ hr: ProseHr,
99
+ strong: ProseStrong,
100
+ em: ProseEm,
101
+ }
102
+
103
+ /** A page entry's body, or a Markdown AST, through `MarkdownRenderer` with the prose components of `components`. */
104
+ export function ContentRenderer({
105
+ value,
106
+ components,
107
+ tag = "div",
108
+ excerpt,
109
+ unwrap,
110
+ }: ContentRendererProps): VNode | null {
111
+ const prepared = prepare({ value, components, defaults: DEFAULTS, excerpt, unwrap })
112
+ return prepared
113
+ ? createElement(
114
+ tag,
115
+ null,
116
+ createElement(MarkdownRenderer, { value: prepared.root, components: prepared.components }),
117
+ )
118
+ : null
119
+ }
120
+
121
+ export interface UseSearchCollectionResult {
122
+ status: SearchStatus
123
+ search(query: string, options?: SearchOptions): Promise<SearchHit[]>
124
+ init(): Promise<void>
125
+ }
126
+
127
+ /** The collection's search index, loaded on the first `search()` or `init()`, or at mount with `immediate`; `status` is state. */
128
+ export function useSearchCollection(
129
+ name: string,
130
+ { immediate = false }: UseSearchCollectionOptions = {},
131
+ ): UseSearchCollectionResult {
132
+ const collection = searchCollectionOf(name)
133
+ const [status, setStatus] = useState<SearchStatus>(collection.status)
134
+ useEffect(() => {
135
+ setStatus(collection.status)
136
+ const stop = collection.subscribe(() => setStatus(collection.status))
137
+ if (immediate) collection.init().catch(ignore)
138
+ return stop
139
+ }, [collection, immediate])
140
+ return { status, search: collection.search, init: collection.init }
141
+ }
142
+
143
+ /** The entry of `collection` whose `path` is the route path, `undefined` while it loads; throws the 404 when there is none. */
144
+ export function useContentPage<N extends CollectionName>(collection: N): ContentPage<N> | undefined {
145
+ checkPageCollection(collection)
146
+ const { path } = useRoute()
147
+ const { data, error, status } = useAsyncData<PageEntry | null>(pageKey(collection, path), () =>
148
+ loadPage(collection, path),
149
+ )
150
+ const page = settledPage(path, { data, error, status })
151
+ useSeoMeta(pageSeo(page))
152
+ useHead(pageHead(page))
153
+ return page as unknown as ContentPage<N> | undefined
154
+ }
@@ -0,0 +1,55 @@
1
+ /// <reference path="../env.d.ts" />
2
+ import { markdownProse } from "hamur/adapter"
3
+ import type { MdRoot } from "hamur/markdown"
4
+ import { prose as files } from "#hamur/markdown-components"
5
+ import type { PageFields } from "../types.ts"
6
+
7
+ const pascal = (tag: string): string => `Prose${tag[0]!.toUpperCase()}${tag.slice(1)}`
8
+ const INTERNAL = /^\/(?!\/)/
9
+
10
+ /** An `href` that is an app path (`/…`, not `//…`), which `MarkdownRenderer` writes as `Link`. */
11
+ export const isInternal = (href: unknown): href is string => typeof href === "string" && INTERNAL.test(href)
12
+
13
+ export interface PrepareOptions<Component> {
14
+ value: PageFields | MdRoot | undefined
15
+ components: Record<string, Component> | false | undefined
16
+ /** The default prose component of each tag (`a`, `h1`, …); the app's `Prose<Tag>` file and a `Prose<Tag>` in `components` win, in that order. */
17
+ defaults: Readonly<Record<string, Component>>
18
+ excerpt: boolean | undefined
19
+ unwrap: string | undefined
20
+ /** Prop names a library spells differently from the attribute (`class` → `className`). */
21
+ rename?: Record<string, string>
22
+ }
23
+
24
+ export interface Prepared<Component> {
25
+ root: MdRoot
26
+ /** `components` for `MarkdownRenderer`: the Markdown components, and each prose component under its tag's key. */
27
+ components: Record<string, Component>
28
+ }
29
+
30
+ /** What `ContentRenderer` hands to `MarkdownRenderer`; undefined when there is nothing to render. */
31
+ export function prepare<Component>({
32
+ value,
33
+ components,
34
+ defaults,
35
+ excerpt,
36
+ unwrap,
37
+ rename,
38
+ }: PrepareOptions<Component>): Prepared<Component> | undefined {
39
+ if (value === undefined || value === null) {
40
+ if ((import.meta as { dev?: boolean }).dev === true) throw new Error("ContentRenderer: value is required")
41
+ return undefined
42
+ }
43
+ const source = Array.isArray(value) ? (excerpt ? undefined : value) : excerpt ? value.excerpt : value.body
44
+ if (!source) return undefined
45
+ const named = {
46
+ ...Object.fromEntries(Object.entries(defaults).map(([tag, component]) => [pascal(tag), component])),
47
+ ...(files as Record<string, Component>),
48
+ ...components,
49
+ }
50
+ const prose = markdownProse(source, named, {
51
+ drop: new Set((unwrap ?? "").split(/\s+/).filter(Boolean)),
52
+ ...(rename ? { rename } : {}),
53
+ })
54
+ return { root: prose.value as MdRoot, components: prose.components }
55
+ }