@contentbit/astro 0.3.0 → 0.4.0
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/README.md +3 -2
- package/components/ContentBlocks.astro +6 -8
- package/components/RenderNodes.astro +8 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/render-block.d.ts +13 -4
- package/dist/render-block.d.ts.map +1 -1
- package/dist/render-block.js +71 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -46,7 +46,8 @@ props as component props, a reserved `node` prop with the full block node
|
|
|
46
46
|
`<slot />`.
|
|
47
47
|
|
|
48
48
|
The default prose pipeline is marked (GFM) with raw HTML escaped — the same
|
|
49
|
-
safe-by-default stance as every other render target.
|
|
50
|
-
|
|
49
|
+
safe-by-default stance as every other render target. Contentbit is only the
|
|
50
|
+
block/validation layer: pass your own `renderMarkdown` when your site already
|
|
51
|
+
has a Markdown pipeline, including Astro's async Sätteri renderer in Astro 7.
|
|
51
52
|
|
|
52
53
|
Docs: https://contentbit.dev/docs
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
import type { DocumentNode } from '@contentbit/core'
|
|
3
|
-
import type {
|
|
3
|
+
import type { AstroBlockRenderer, AstroMarkdownRenderer } from '@contentbit/astro'
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import { defaultRenderMarkdown } from '../dist/markdown.js'
|
|
5
|
+
import { defaultRenderMarkdown, genericAstroRenderers } from '@contentbit/astro'
|
|
8
6
|
import RenderNodes from './RenderNodes.astro'
|
|
9
7
|
|
|
10
8
|
interface Props {
|
|
@@ -16,14 +14,14 @@ interface Props {
|
|
|
16
14
|
* reserved `node` prop (the full ValidatedBlockNode); nested content
|
|
17
15
|
* arrives through the default <slot />. A block prop literally named
|
|
18
16
|
* "node" is shadowed by the reserved prop.
|
|
19
|
-
|
|
17
|
+
*/
|
|
20
18
|
components?: Record<string, unknown>
|
|
21
19
|
/** Per-block string renderers, merged over the generic html defaults. */
|
|
22
|
-
renderers?: Record<string,
|
|
20
|
+
renderers?: Record<string, AstroBlockRenderer>
|
|
23
21
|
/** CSS class prefix for default renderers. Default "cb-". */
|
|
24
22
|
classPrefix?: string
|
|
25
23
|
/** Prose pipeline. Default: marked (GFM) with raw HTML escaped. */
|
|
26
|
-
renderMarkdown?:
|
|
24
|
+
renderMarkdown?: AstroMarkdownRenderer
|
|
27
25
|
/** Invalid-block behavior: throw, visible dev box, or escaped body. Default "annotated". */
|
|
28
26
|
onInvalid?: 'strict' | 'annotated' | 'fallback'
|
|
29
27
|
}
|
|
@@ -41,7 +39,7 @@ const {
|
|
|
41
39
|
<RenderNodes
|
|
42
40
|
nodes={document.children}
|
|
43
41
|
components={components}
|
|
44
|
-
renderers={{ ...
|
|
42
|
+
renderers={{ ...genericAstroRenderers, ...renderers }}
|
|
45
43
|
classPrefix={classPrefix}
|
|
46
44
|
renderMarkdown={renderMarkdown}
|
|
47
45
|
onInvalid={onInvalid}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
---
|
|
2
2
|
import type { ContentNode, ValidatedBlockNode } from '@contentbit/core'
|
|
3
|
-
import type {
|
|
3
|
+
import type { AstroBlockRenderer, AstroMarkdownRenderer } from '@contentbit/astro'
|
|
4
4
|
|
|
5
5
|
import { isValidatedBlock } from '@contentbit/core'
|
|
6
6
|
import { fallbackMarkdown, invalidBlockHtml, unrenderableBlockError } from '@contentbit/html'
|
|
7
7
|
|
|
8
|
-
import { renderBlockShell } from '
|
|
8
|
+
import { renderBlockShell } from '@contentbit/astro'
|
|
9
9
|
|
|
10
10
|
interface Props {
|
|
11
11
|
nodes: ContentNode[]
|
|
12
12
|
components: Record<string, unknown>
|
|
13
|
-
renderers: Record<string,
|
|
13
|
+
renderers: Record<string, AstroBlockRenderer>
|
|
14
14
|
classPrefix: string
|
|
15
|
-
renderMarkdown:
|
|
15
|
+
renderMarkdown: AstroMarkdownRenderer
|
|
16
16
|
onInvalid: 'strict' | 'annotated' | 'fallback'
|
|
17
17
|
}
|
|
18
18
|
|
|
@@ -26,12 +26,12 @@ type Plan =
|
|
|
26
26
|
| { kind: 'override'; node: ValidatedBlockNode<unknown>; Component: OverrideComponent }
|
|
27
27
|
| { kind: 'shell'; parts: string[]; childSlots: ContentNode[][] }
|
|
28
28
|
|
|
29
|
-
const plans: Plan[] = nodes.map((node) => {
|
|
30
|
-
if (node.type === 'markdown') return { kind: 'html', html: renderMarkdown(node.value) }
|
|
29
|
+
const plans: Plan[] = await Promise.all(nodes.map(async (node) => {
|
|
30
|
+
if (node.type === 'markdown') return { kind: 'html', html: await renderMarkdown(node.value) }
|
|
31
31
|
if (isValidatedBlock(node)) {
|
|
32
32
|
const override = components[node.name]
|
|
33
33
|
if (override) return { kind: 'override', node, Component: override as OverrideComponent }
|
|
34
|
-
const shell = renderBlockShell(node, { classPrefix, renderMarkdown, renderers })
|
|
34
|
+
const shell = await renderBlockShell(node, { classPrefix, renderMarkdown, renderers })
|
|
35
35
|
if (shell) return { kind: 'shell', ...shell }
|
|
36
36
|
}
|
|
37
37
|
// Not validated, or validated with no renderer and no override. The
|
|
@@ -42,7 +42,7 @@ const plans: Plan[] = nodes.map((node) => {
|
|
|
42
42
|
return { kind: 'html', html: invalidBlockHtml(node, classPrefix) }
|
|
43
43
|
}
|
|
44
44
|
return { kind: 'html', html: fallbackMarkdown(node.body) }
|
|
45
|
-
})
|
|
45
|
+
}))
|
|
46
46
|
---
|
|
47
47
|
|
|
48
48
|
{
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { defaultRenderMarkdown } from './markdown.js';
|
|
2
|
-
export { renderBlockShell, type BlockShell, type RenderBlockOptions } from './render-block.js';
|
|
2
|
+
export { genericAstroRenderers, renderBlockShell, type AstroBlockRenderContext, type AstroBlockRenderer, type AstroMarkdownRenderer, type BlockShell, type MaybePromise, type RenderBlockOptions, } from './render-block.js';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,KAAK,uBAAuB,EAC5B,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,EAC1B,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,kBAAkB,GACxB,MAAM,mBAAmB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { defaultRenderMarkdown } from './markdown.js';
|
|
2
|
-
export { renderBlockShell } from './render-block.js';
|
|
2
|
+
export { genericAstroRenderers, renderBlockShell, } from './render-block.js';
|
package/dist/render-block.d.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import type { ContentNode, ValidatedBlockNode } from '@contentbit/core';
|
|
2
|
-
|
|
2
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
3
|
+
export type AstroMarkdownRenderer = (md: string) => MaybePromise<string>;
|
|
4
|
+
export interface AstroBlockRenderContext {
|
|
5
|
+
cls(name: string): string;
|
|
6
|
+
escape(value: string): string;
|
|
7
|
+
renderMarkdown: AstroMarkdownRenderer;
|
|
8
|
+
renderNodes(nodes: ContentNode[]): string;
|
|
9
|
+
}
|
|
10
|
+
export type AstroBlockRenderer = (node: ValidatedBlockNode<unknown>, ctx: AstroBlockRenderContext) => MaybePromise<string>;
|
|
3
11
|
export interface RenderBlockOptions {
|
|
4
12
|
classPrefix: string;
|
|
5
|
-
renderMarkdown:
|
|
6
|
-
renderers: Record<string,
|
|
13
|
+
renderMarkdown: AstroMarkdownRenderer;
|
|
14
|
+
renderers: Record<string, AstroBlockRenderer>;
|
|
7
15
|
}
|
|
8
16
|
export interface BlockShell {
|
|
9
17
|
/** HTML fragments; childSlots[i] renders between parts[i] and parts[i + 1]. */
|
|
@@ -17,5 +25,6 @@ export interface BlockShell {
|
|
|
17
25
|
* through Astro (where component overrides apply). Returns null when no
|
|
18
26
|
* renderer is registered.
|
|
19
27
|
*/
|
|
20
|
-
export declare function renderBlockShell(node: ValidatedBlockNode<unknown>, opts: RenderBlockOptions): BlockShell | null
|
|
28
|
+
export declare function renderBlockShell(node: ValidatedBlockNode<unknown>, opts: RenderBlockOptions): Promise<BlockShell | null>;
|
|
29
|
+
export declare const genericAstroRenderers: Record<string, AstroBlockRenderer>;
|
|
21
30
|
//# sourceMappingURL=render-block.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render-block.d.ts","sourceRoot":"","sources":["../src/render-block.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"render-block.d.ts","sourceRoot":"","sources":["../src/render-block.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AAMvE,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;AAC5C,MAAM,MAAM,qBAAqB,GAAG,CAAC,EAAE,EAAE,MAAM,KAAK,YAAY,CAAC,MAAM,CAAC,CAAA;AAExE,MAAM,WAAW,uBAAuB;IACtC,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;IACzB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;IAC7B,cAAc,EAAE,qBAAqB,CAAA;IACrC,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,GAAG,MAAM,CAAA;CAC1C;AAED,MAAM,MAAM,kBAAkB,GAAG,CAC/B,IAAI,EAAE,kBAAkB,CAAC,OAAO,CAAC,EACjC,GAAG,EAAE,uBAAuB,KACzB,YAAY,CAAC,MAAM,CAAC,CAAA;AAEzB,MAAM,WAAW,kBAAkB;IACjC,WAAW,EAAE,MAAM,CAAA;IACnB,cAAc,EAAE,qBAAqB,CAAA;IACrC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAA;CAC9C;AAED,MAAM,WAAW,UAAU;IACzB,+EAA+E;IAC/E,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,iFAAiF;IACjF,UAAU,EAAE,WAAW,EAAE,EAAE,CAAA;CAC5B;AAOD;;;;;GAKG;AACH,wBAAsB,gBAAgB,CACpC,IAAI,EAAE,kBAAkB,CAAC,OAAO,CAAC,EACjC,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CA2B5B;AAiFD,eAAO,MAAM,qBAAqB,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CASpE,CAAA"}
|
package/dist/render-block.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { splitProsCons } from '@contentbit/blocks';
|
|
2
|
+
import { isValidatedBlock } from '@contentbit/core';
|
|
1
3
|
import { escapeHtml } from '@contentbit/html';
|
|
2
4
|
// NUL is impossible in renderer output, so it cannot collide with real HTML.
|
|
3
5
|
const slotToken = (i) => `\u0000cb:${i}\u0000`;
|
|
@@ -9,12 +11,12 @@ const SLOT_RE = /\u0000cb:(\d+)\u0000/g;
|
|
|
9
11
|
* through Astro (where component overrides apply). Returns null when no
|
|
10
12
|
* renderer is registered.
|
|
11
13
|
*/
|
|
12
|
-
export function renderBlockShell(node, opts) {
|
|
14
|
+
export async function renderBlockShell(node, opts) {
|
|
13
15
|
const renderer = opts.renderers[node.name];
|
|
14
16
|
if (!renderer)
|
|
15
17
|
return null;
|
|
16
18
|
const slots = [];
|
|
17
|
-
const html = renderer(node, {
|
|
19
|
+
const html = await renderer(node, {
|
|
18
20
|
cls: (name) => `${opts.classPrefix}${name}`,
|
|
19
21
|
escape: escapeHtml,
|
|
20
22
|
renderMarkdown: opts.renderMarkdown,
|
|
@@ -37,3 +39,70 @@ export function renderBlockShell(node, opts) {
|
|
|
37
39
|
parts.push(html.slice(last));
|
|
38
40
|
return { parts, childSlots };
|
|
39
41
|
}
|
|
42
|
+
const callout = async (node, ctx) => {
|
|
43
|
+
const data = node.data;
|
|
44
|
+
const type = ctx.escape(String(node.props.type ?? 'note'));
|
|
45
|
+
const title = node.props.title;
|
|
46
|
+
const titleHtml = title
|
|
47
|
+
? `<div class="${ctx.cls('callout-title')}">${ctx.escape(title)}</div>`
|
|
48
|
+
: '';
|
|
49
|
+
return `<aside class="${ctx.cls('callout')} ${ctx.cls(`callout-${type}`)}">${titleHtml}${await ctx.renderMarkdown(data.markdown)}</aside>`;
|
|
50
|
+
};
|
|
51
|
+
const steps = (node, ctx) => {
|
|
52
|
+
const data = node.data;
|
|
53
|
+
const items = data.items.map((i) => `<li>${ctx.escape(i.text)}</li>`).join('');
|
|
54
|
+
return `<ol class="${ctx.cls('steps')}">${items}</ol>`;
|
|
55
|
+
};
|
|
56
|
+
const keyMetrics = (node, ctx) => {
|
|
57
|
+
const data = node.data;
|
|
58
|
+
const items = data.rows
|
|
59
|
+
.map((r) => `<div class="${ctx.cls('key-metrics-item')}"><span class="${ctx.cls('key-metrics-value')}">${ctx.escape(r.value)}</span><span class="${ctx.cls('key-metrics-label')}">${ctx.escape(r.label)}</span></div>`)
|
|
60
|
+
.join('');
|
|
61
|
+
return `<div class="${ctx.cls('key-metrics')}">${items}</div>`;
|
|
62
|
+
};
|
|
63
|
+
const quickRef = (node, ctx) => {
|
|
64
|
+
const data = node.data;
|
|
65
|
+
const rows = data.rows
|
|
66
|
+
.map((r) => `<div class="${ctx.cls('quick-ref-row')}"><dt>${ctx.escape(r.key)}</dt><dd>${ctx.escape(r.value)}</dd></div>`)
|
|
67
|
+
.join('');
|
|
68
|
+
return `<dl class="${ctx.cls('quick-ref')}">${rows}</dl>`;
|
|
69
|
+
};
|
|
70
|
+
const comparison = (node, ctx) => {
|
|
71
|
+
const data = node.data;
|
|
72
|
+
const head = `<thead><tr><th scope="col"></th><th scope="col">${ctx.escape(String(node.props.left))}</th><th scope="col">${ctx.escape(String(node.props.right))}</th></tr></thead>`;
|
|
73
|
+
const rows = data.rows
|
|
74
|
+
.map((r) => `<tr><th scope="row">${ctx.escape(r.label)}</th><td>${ctx.escape(r.left)}</td><td>${ctx.escape(r.right)}</td></tr>`)
|
|
75
|
+
.join('');
|
|
76
|
+
return `<table class="${ctx.cls('comparison')}">${head}<tbody>${rows}</tbody></table>`;
|
|
77
|
+
};
|
|
78
|
+
const prosCons = (node, ctx) => {
|
|
79
|
+
const { pros, cons } = splitProsCons(node.data);
|
|
80
|
+
const list = (cls, title, items) => `<div class="${ctx.cls(cls)}"><div class="${ctx.cls('pros-cons-heading')}">${title}</div><ul>${items.map((i) => `<li>${ctx.escape(i)}</li>`).join('')}</ul></div>`;
|
|
81
|
+
return `<div class="${ctx.cls('pros-cons')}">${list('pros-cons-pros', 'Pros', pros)}${list('pros-cons-cons', 'Cons', cons)}</div>`;
|
|
82
|
+
};
|
|
83
|
+
const tabs = async (node, ctx) => {
|
|
84
|
+
const data = node.data;
|
|
85
|
+
const sections = await Promise.all(data.blocks.map(async (tab) => {
|
|
86
|
+
const body = isValidatedBlock(tab) ? tab.data.markdown : tab.body;
|
|
87
|
+
return `<section class="${ctx.cls('tab')}"><h3 class="${ctx.cls('tab-title')}">${ctx.escape(String(tab.props.title))}</h3>${await ctx.renderMarkdown(body)}</section>`;
|
|
88
|
+
}));
|
|
89
|
+
return `<div class="${ctx.cls('tabs')}">${sections.join('')}</div>`;
|
|
90
|
+
};
|
|
91
|
+
const faq = async (node, ctx) => {
|
|
92
|
+
const data = node.data;
|
|
93
|
+
const items = await Promise.all(data.blocks.map(async (item) => {
|
|
94
|
+
const body = isValidatedBlock(item) ? item.data.markdown : item.body;
|
|
95
|
+
return `<details class="${ctx.cls('faq-item')}"><summary>${ctx.escape(String(item.props.question))}</summary>${await ctx.renderMarkdown(body)}</details>`;
|
|
96
|
+
}));
|
|
97
|
+
return `<div class="${ctx.cls('faq')}">${items.join('')}</div>`;
|
|
98
|
+
};
|
|
99
|
+
export const genericAstroRenderers = {
|
|
100
|
+
callout,
|
|
101
|
+
steps,
|
|
102
|
+
'key-metrics': keyMetrics,
|
|
103
|
+
'quick-ref': quickRef,
|
|
104
|
+
comparison,
|
|
105
|
+
'pros-cons': prosCons,
|
|
106
|
+
tabs,
|
|
107
|
+
faq,
|
|
108
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentbit/astro",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Astro renderer for Content Blocks: .astro components for rendering validated documents, with per-block component overrides.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
@@ -36,9 +36,9 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"marked": "^18.0.5",
|
|
39
|
-
"@contentbit/blocks": "0.
|
|
40
|
-
"@contentbit/core": "0.
|
|
41
|
-
"@contentbit/html": "0.
|
|
39
|
+
"@contentbit/blocks": "0.4.0",
|
|
40
|
+
"@contentbit/core": "0.4.0",
|
|
41
|
+
"@contentbit/html": "0.4.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@types/node": "^25.9.2",
|