@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,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,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> = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }
|
|
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
|
+
}
|
|
@@ -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
|
+
}
|