@contentbit/astro 0.4.2 → 0.6.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 CHANGED
@@ -24,6 +24,7 @@ import { genericBlocks } from '@contentbit/blocks'
24
24
  import { createBlockRegistry, parseDocument, validateDocument } from '@contentbit/core'
25
25
  import { ContentBlocks } from '@contentbit/astro/components'
26
26
  import { getEntry } from 'astro:content'
27
+ import MyCallout from '../components/MyCallout.astro'
27
28
 
28
29
  const entry = await getEntry('articles', 'example')
29
30
  if (!entry?.body) throw new Error('Entry "example" not found.')
@@ -32,22 +33,27 @@ const registry = createBlockRegistry().use(genericBlocks())
32
33
  const { document } = validateDocument(parseDocument(entry.body), registry)
33
34
  ---
34
35
 
35
- <ContentBlocks document={document} />
36
+ <ContentBlocks document={document} components={{ callout: MyCallout }} />
36
37
  ```
37
38
 
38
39
  For static pages this runs at build time, so throwing on diagnostics fails the
39
40
  build. To validate every content file in CI with `file:line:col` diagnostics,
40
41
  run the CLI: `contentbit validate "content/**/*.md"`.
41
42
 
42
- Override any block with your own Astro component via
43
- `components={{ callout: MyCallout }}`. Overrides receive the block's validated
44
- props as component props, a reserved `node` prop with the full block node
45
- (`node.data` holds the parsed content), and the block's nested content via
46
- `<slot />`.
47
-
48
- The default prose pipeline is marked (GFM) with raw HTML escaped — the same
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.
43
+ Render blocks with your own Astro components via
44
+ `components={{ callout: MyCallout }}`. Components receive the block's validated
45
+ props as component props, reserved `node` and `ctx` props, and the block's
46
+ nested content via `<slot />`. `node.data` holds the parsed content; use
47
+ `ctx.renderMarkdown(...)` for any Markdown strings inside that data so your
48
+ component stays on the host app's prose pipeline.
49
+
50
+ The default prose pipeline is only a tiny escaped-paragraph fallback. Contentbit
51
+ does not bring a Markdown engine into your Astro app: pass your own
52
+ `renderMarkdown` when your site already has a Markdown pipeline, including
53
+ Astro's async Sätteri renderer in Astro 7.
54
+
55
+ Pre-made block UI is distributed through the shadcn registry, where files are
56
+ copied into your app as editable `.astro` source:
57
+ `pnpm dlx shadcn@latest add @contentbit/astro-pack`.
52
58
 
53
59
  Docs: https://contentbit.dev/docs
@@ -1,26 +1,21 @@
1
1
  ---
2
- import type { DocumentNode } from '@contentbit/core'
3
- import type { AstroBlockRenderer, AstroMarkdownRenderer } from '@contentbit/astro'
2
+ import type { ValidatedDocumentNode } from '@contentbit/core'
3
+ import type { AstroBlockContext, AstroMarkdownRenderer } from '@contentbit/astro'
4
4
 
5
- import { defaultRenderMarkdown, genericAstroRenderers } from '@contentbit/astro'
5
+ import { defaultRenderMarkdown } from '@contentbit/astro'
6
6
  import RenderNodes from './RenderNodes.astro'
7
7
 
8
8
  interface Props {
9
9
  /** A validated document: validateDocument(parseDocument(entry.body), registry).document. */
10
- document: DocumentNode
10
+ document: ValidatedDocumentNode
11
11
  /**
12
12
  * Per-block Astro component overrides, keyed by block name. An override
13
- * receives the block's validated props spread as component props, plus a
14
- * reserved `node` prop (the full ValidatedBlockNode); nested content
15
- * arrives through the default <slot />. A block prop literally named
16
- * "node" is shadowed by the reserved prop.
13
+ * receives the block's validated props spread as component props, plus
14
+ * reserved `node` and `ctx` props; nested content arrives through the
15
+ * default <slot />. Block props literally named "node" or "ctx" are shadowed.
17
16
  */
18
17
  components?: Record<string, unknown>
19
- /** Per-block string renderers, merged over the generic html defaults. */
20
- renderers?: Record<string, AstroBlockRenderer>
21
- /** CSS class prefix for default renderers. Default "cb-". */
22
- classPrefix?: string
23
- /** Prose pipeline. Default: marked (GFM) with raw HTML escaped. */
18
+ /** Prose pipeline. Default: escaped paragraphs; pass your host Markdown renderer for full Markdown. */
24
19
  renderMarkdown?: AstroMarkdownRenderer
25
20
  /** Invalid-block behavior: throw, visible dev box, or escaped body. Default "annotated". */
26
21
  onInvalid?: 'strict' | 'annotated' | 'fallback'
@@ -29,18 +24,15 @@ interface Props {
29
24
  const {
30
25
  document,
31
26
  components = {},
32
- renderers,
33
- classPrefix = 'cb-',
34
27
  renderMarkdown = defaultRenderMarkdown,
35
28
  onInvalid = 'annotated',
36
29
  } = Astro.props
30
+ const ctx: AstroBlockContext = { renderMarkdown }
37
31
  ---
38
32
 
39
33
  <RenderNodes
40
34
  nodes={document.children}
41
35
  components={components}
42
- renderers={{ ...genericAstroRenderers, ...renderers }}
43
- classPrefix={classPrefix}
44
- renderMarkdown={renderMarkdown}
36
+ ctx={ctx}
45
37
  onInvalid={onInvalid}
46
38
  />
@@ -1,45 +1,37 @@
1
1
  ---
2
2
  import type { ContentNode, ValidatedBlockNode } from '@contentbit/core'
3
- import type { AstroBlockRenderer, AstroMarkdownRenderer } from '@contentbit/astro'
3
+ import type { AstroBlockContext } from '@contentbit/astro'
4
4
 
5
5
  import { isValidatedBlock } from '@contentbit/core'
6
- import { fallbackMarkdown, invalidBlockHtml, unrenderableBlockError } from '@contentbit/html'
7
6
 
8
- import { renderBlockShell } from '@contentbit/astro'
7
+ import { fallbackMarkdown, invalidBlockHtml, unrenderableBlockError } from '@contentbit/astro'
9
8
 
10
9
  interface Props {
11
10
  nodes: ContentNode[]
12
11
  components: Record<string, unknown>
13
- renderers: Record<string, AstroBlockRenderer>
14
- classPrefix: string
15
- renderMarkdown: AstroMarkdownRenderer
12
+ ctx: AstroBlockContext
16
13
  onInvalid: 'strict' | 'annotated' | 'fallback'
17
14
  }
18
15
 
19
- const { nodes, components, renderers, classPrefix, renderMarkdown, onInvalid } = Astro.props
20
- const childProps = { components, renderers, classPrefix, renderMarkdown, onInvalid }
16
+ const { nodes, components, ctx, onInvalid } = Astro.props
17
+ const childProps = { components, ctx, onInvalid }
21
18
 
22
19
  type OverrideComponent = (props: Record<string, unknown>) => unknown
23
20
 
24
21
  type Plan =
25
22
  | { kind: 'html'; html: string }
26
23
  | { kind: 'override'; node: ValidatedBlockNode<unknown>; Component: OverrideComponent }
27
- | { kind: 'shell'; parts: string[]; childSlots: ContentNode[][] }
28
24
 
29
25
  const plans: Plan[] = await Promise.all(nodes.map(async (node) => {
30
- if (node.type === 'markdown') return { kind: 'html', html: await renderMarkdown(node.value) }
26
+ if (node.type === 'markdown') return { kind: 'html', html: await ctx.renderMarkdown(node.value) }
31
27
  if (isValidatedBlock(node)) {
32
28
  const override = components[node.name]
33
29
  if (override) return { kind: 'override', node, Component: override as OverrideComponent }
34
- const shell = await renderBlockShell(node, { classPrefix, renderMarkdown, renderers })
35
- if (shell) return { kind: 'shell', ...shell }
36
30
  }
37
- // Not validated, or validated with no renderer and no override. The
38
- // invalid-block policy (markup, message, fallback escaping) is shared with
39
- // renderToHtml so the two targets cannot drift.
31
+ // Not validated, or validated with no component.
40
32
  if (onInvalid === 'strict') throw unrenderableBlockError(node.name)
41
33
  if (onInvalid === 'annotated') {
42
- return { kind: 'html', html: invalidBlockHtml(node, classPrefix) }
34
+ return { kind: 'html', html: invalidBlockHtml(node, 'cb-') }
43
35
  }
44
36
  return { kind: 'html', html: fallbackMarkdown(node.body) }
45
37
  }))
@@ -51,16 +43,10 @@ const plans: Plan[] = await Promise.all(nodes.map(async (node) => {
51
43
  if (plan.kind === 'override') {
52
44
  const Override = plan.Component
53
45
  return (
54
- <Override {...plan.node.props} node={plan.node}>
46
+ <Override {...plan.node.props} node={plan.node} ctx={ctx}>
55
47
  <Astro.self nodes={plan.node.children} {...childProps} />
56
48
  </Override>
57
49
  )
58
50
  }
59
- return plan.parts.map((part, i) => (
60
- <>
61
- <Fragment set:html={part} />
62
- {i < plan.childSlots.length && <Astro.self nodes={plan.childSlots[i]} {...childProps} />}
63
- </>
64
- ))
65
51
  })
66
52
  }
package/dist/html.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { BlockNode } from '@contentbit/core';
2
+ export declare function escapeHtml(value: string): string;
3
+ export declare function fallbackMarkdown(md: string): string;
4
+ export declare function invalidBlockHtml(node: Pick<BlockNode, 'name' | 'body'>, prefix: string): string;
5
+ export declare function unrenderableBlockError(name: string): Error;
6
+ //# sourceMappingURL=html.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../src/html.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAEjD,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAMhD;AAED,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAMnD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAE/F;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAE1D"}
package/dist/html.js ADDED
@@ -0,0 +1,20 @@
1
+ export function escapeHtml(value) {
2
+ return value
3
+ .replace(/&/g, '&amp;')
4
+ .replace(/</g, '&lt;')
5
+ .replace(/>/g, '&gt;')
6
+ .replace(/"/g, '&quot;');
7
+ }
8
+ export function fallbackMarkdown(md) {
9
+ return md
10
+ .trim()
11
+ .split(/\n{2,}/)
12
+ .map((p) => `<p>${escapeHtml(p)}</p>`)
13
+ .join('\n');
14
+ }
15
+ export function invalidBlockHtml(node, prefix) {
16
+ return `<div class="${prefix}invalid" data-cb-invalid="${escapeHtml(node.name)}"><pre>${escapeHtml(node.body)}</pre></div>`;
17
+ }
18
+ export function unrenderableBlockError(name) {
19
+ return new Error(`Cannot render block "${name}": not validated or no component registered.`);
20
+ }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { defaultRenderMarkdown } from './markdown.js';
2
- export { genericAstroRenderers, renderBlockShell, type AstroBlockRenderContext, type AstroBlockRenderer, type AstroMarkdownRenderer, type BlockShell, type MaybePromise, type RenderBlockOptions, } from './render-block.js';
2
+ export { fallbackMarkdown, invalidBlockHtml, unrenderableBlockError } from './html.js';
3
+ export type { AstroBlockContext, AstroMarkdownRenderer, MaybePromise } from './types.js';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -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,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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAA;AACtF,YAAY,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA"}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  export { defaultRenderMarkdown } from './markdown.js';
2
- export { genericAstroRenderers, renderBlockShell, } from './render-block.js';
2
+ export { fallbackMarkdown, invalidBlockHtml, unrenderableBlockError } from './html.js';
@@ -1,3 +1,4 @@
1
- /** Default prose pipeline: marked with GFM (its default), raw HTML escaped, synchronous. */
2
- export declare function defaultRenderMarkdown(source: string): string;
1
+ import type { AstroMarkdownRenderer } from './types.js';
2
+ /** Minimal safe prose fallback. Pass renderMarkdown to use the host app's Markdown pipeline. */
3
+ export declare const defaultRenderMarkdown: AstroMarkdownRenderer;
3
4
  //# sourceMappingURL=markdown.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../src/markdown.ts"],"names":[],"mappings":"AAgBA,4FAA4F;AAC5F,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAE5D"}
1
+ {"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../src/markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AAIvD,gGAAgG;AAChG,eAAO,MAAM,qBAAqB,EAAE,qBAEnC,CAAA"}
package/dist/markdown.js CHANGED
@@ -1,18 +1,5 @@
1
- import { escapeHtml } from '@contentbit/html';
2
- import { Marked } from 'marked';
3
- // marked passes raw HTML through verbatim; every other contentbit target
4
- // escapes by default, and content often comes from CMS users rather than
5
- // trusted committers. Escaping the html tokens (block and inline) keeps the
6
- // default pipeline XSS-safe; pass your own renderMarkdown to opt into raw
7
- // HTML for trusted content.
8
- const md = new Marked({
9
- renderer: {
10
- html({ text }) {
11
- return escapeHtml(text);
12
- },
13
- },
14
- });
15
- /** Default prose pipeline: marked with GFM (its default), raw HTML escaped, synchronous. */
16
- export function defaultRenderMarkdown(source) {
17
- return md.parse(source, { async: false });
18
- }
1
+ import { fallbackMarkdown } from './html.js';
2
+ /** Minimal safe prose fallback. Pass renderMarkdown to use the host app's Markdown pipeline. */
3
+ export const defaultRenderMarkdown = (source) => {
4
+ return fallbackMarkdown(source);
5
+ };
@@ -0,0 +1,6 @@
1
+ export type MaybePromise<T> = T | PromiseLike<T>;
2
+ export type AstroMarkdownRenderer = (md: string) => MaybePromise<string>;
3
+ export interface AstroBlockContext {
4
+ renderMarkdown: AstroMarkdownRenderer;
5
+ }
6
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;AAChD,MAAM,MAAM,qBAAqB,GAAG,CAAC,EAAE,EAAE,MAAM,KAAK,YAAY,CAAC,MAAM,CAAC,CAAA;AAExE,MAAM,WAAW,iBAAiB;IAChC,cAAc,EAAE,qBAAqB,CAAA;CACtC"}
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentbit/astro",
3
- "version": "0.4.2",
3
+ "version": "0.6.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",
@@ -35,16 +35,14 @@
35
35
  "access": "public"
36
36
  },
37
37
  "dependencies": {
38
- "marked": "^18.0.5",
39
- "@contentbit/blocks": "0.4.2",
40
- "@contentbit/core": "0.4.2",
41
- "@contentbit/html": "0.4.2"
38
+ "@contentbit/core": "0.6.0"
42
39
  },
43
40
  "devDependencies": {
44
41
  "@types/node": "^25.9.2",
45
42
  "astro": "^7.0.0",
46
43
  "typescript": "^5.9.3",
47
- "vitest": "^4.0.17"
44
+ "vitest": "^4.0.17",
45
+ "@contentbit/blocks": "0.6.0"
48
46
  },
49
47
  "peerDependencies": {
50
48
  "astro": "^5.0.0 || ^6.0.0 || ^7.0.0"
@@ -1,30 +0,0 @@
1
- import type { ContentNode, ValidatedBlockNode } from '@contentbit/core';
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>;
11
- export interface RenderBlockOptions {
12
- classPrefix: string;
13
- renderMarkdown: AstroMarkdownRenderer;
14
- renderers: Record<string, AstroBlockRenderer>;
15
- }
16
- export interface BlockShell {
17
- /** HTML fragments; childSlots[i] renders between parts[i] and parts[i + 1]. */
18
- parts: string[];
19
- /** Node groups handed back to Astro for recursive rendering, in output order. */
20
- childSlots: ContentNode[][];
21
- }
22
- /**
23
- * Render one validated block through its string renderer, capturing every
24
- * ctx.renderNodes() call as a placeholder so nested content can recurse
25
- * through Astro (where component overrides apply). Returns null when no
26
- * renderer is registered.
27
- */
28
- export declare function renderBlockShell(node: ValidatedBlockNode<unknown>, opts: RenderBlockOptions): Promise<BlockShell | null>;
29
- export declare const genericAstroRenderers: Record<string, AstroBlockRenderer>;
30
- //# sourceMappingURL=render-block.d.ts.map
@@ -1 +0,0 @@
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"}
@@ -1,108 +0,0 @@
1
- import { splitProsCons } from '@contentbit/blocks';
2
- import { isValidatedBlock } from '@contentbit/core';
3
- import { escapeHtml } from '@contentbit/html';
4
- // NUL is impossible in renderer output, so it cannot collide with real HTML.
5
- const slotToken = (i) => `\u0000cb:${i}\u0000`;
6
- // oxlint-disable-next-line no-control-regex -- NUL is the one byte that cannot appear in renderer output
7
- const SLOT_RE = /\u0000cb:(\d+)\u0000/g;
8
- /**
9
- * Render one validated block through its string renderer, capturing every
10
- * ctx.renderNodes() call as a placeholder so nested content can recurse
11
- * through Astro (where component overrides apply). Returns null when no
12
- * renderer is registered.
13
- */
14
- export async function renderBlockShell(node, opts) {
15
- const renderer = opts.renderers[node.name];
16
- if (!renderer)
17
- return null;
18
- const slots = [];
19
- const html = await renderer(node, {
20
- cls: (name) => `${opts.classPrefix}${name}`,
21
- escape: escapeHtml,
22
- renderMarkdown: opts.renderMarkdown,
23
- renderNodes(nodes) {
24
- slots.push(nodes);
25
- return slotToken(slots.length - 1);
26
- },
27
- });
28
- const parts = [];
29
- const childSlots = [];
30
- let last = 0;
31
- for (const m of html.matchAll(SLOT_RE)) {
32
- const slot = slots[Number(m[1])];
33
- if (slot === undefined)
34
- continue; // token-looking text in real content: leave it literal
35
- parts.push(html.slice(last, m.index));
36
- childSlots.push(slot);
37
- last = m.index + m[0].length;
38
- }
39
- parts.push(html.slice(last));
40
- return { parts, childSlots };
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
- };