contentbit 0.4.2 → 0.5.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.
Files changed (38) hide show
  1. package/README.md +7 -2
  2. package/dist/bin.js +1609 -1616
  3. package/dist/cli-format.d.ts +17 -0
  4. package/dist/cli-format.d.ts.map +1 -0
  5. package/dist/cli-format.js +63 -0
  6. package/dist/commands/agents.d.ts.map +1 -1
  7. package/dist/commands/agents.js +9 -188
  8. package/dist/commands/docs.d.ts.map +1 -1
  9. package/dist/commands/docs.js +4 -1
  10. package/dist/commands/doctor.d.ts.map +1 -1
  11. package/dist/commands/doctor.js +80 -146
  12. package/dist/commands/init-targets.d.ts +27 -0
  13. package/dist/commands/init-targets.d.ts.map +1 -0
  14. package/dist/commands/init-targets.js +280 -0
  15. package/dist/commands/init.d.ts.map +1 -1
  16. package/dist/commands/init.js +26 -313
  17. package/dist/commands/instructions.d.ts.map +1 -1
  18. package/dist/commands/instructions.js +4 -1
  19. package/dist/commands/links.d.ts.map +1 -1
  20. package/dist/commands/links.js +18 -14
  21. package/dist/commands/render.d.ts.map +1 -1
  22. package/dist/commands/render.js +17 -10
  23. package/dist/commands/stats.d.ts.map +1 -1
  24. package/dist/commands/stats.js +33 -34
  25. package/dist/commands/studio.d.ts.map +1 -1
  26. package/dist/commands/studio.js +14 -12
  27. package/dist/commands/validate.d.ts.map +1 -1
  28. package/dist/commands/validate.js +39 -41
  29. package/dist/content-project.d.ts +32 -0
  30. package/dist/content-project.d.ts.map +1 -0
  31. package/dist/content-project.js +34 -0
  32. package/dist/load-registry.d.ts +4 -1
  33. package/dist/load-registry.d.ts.map +1 -1
  34. package/dist/load-registry.js +4 -2
  35. package/dist/run.d.ts +10 -1
  36. package/dist/run.d.ts.map +1 -1
  37. package/dist/run.js +1613 -1618
  38. package/package.json +4 -5
@@ -0,0 +1,280 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ export const TARGETS = ['react', 'markdown', 'astro'];
4
+ const RUNTIME_BASE = ['@contentbit/core', '@contentbit/blocks', 'zod'];
5
+ /** blocks/components.tsx — React components for custom blocks, next to their definitions. */
6
+ function blockComponentsTemplate(styled) {
7
+ const body = styled
8
+ ? ` return (
9
+ <figure className="my-6 border-s-2 ps-4">
10
+ <blockquote className="text-lg italic">{ctx.renderMarkdown(data.markdown)}</blockquote>
11
+ <figcaption className="text-muted-foreground mt-2 text-sm">
12
+ — {String(node.props.author)}
13
+ {node.props.role ? \`, \${String(node.props.role)}\` : null}
14
+ </figcaption>
15
+ </figure>
16
+ )`
17
+ : ` return (
18
+ <figure style={{ margin: '1.5rem 0', borderLeft: '2px solid #d4d4d4', paddingLeft: '1rem' }}>
19
+ <blockquote style={{ fontStyle: 'italic' }}>{ctx.renderMarkdown(data.markdown)}</blockquote>
20
+ <figcaption style={{ marginTop: '0.5rem', fontSize: '0.875rem', opacity: 0.7 }}>
21
+ — {String(node.props.author)}
22
+ {node.props.role ? \`, \${String(node.props.role)}\` : null}
23
+ </figcaption>
24
+ </figure>
25
+ )`;
26
+ return `import type { BlockComponent, BlockComponentProps } from '@contentbit/react'
27
+
28
+ // One React component per custom block, keyed by block name. Definitions
29
+ // live in ./registry.ts — add a block there, add its component here, and
30
+ // the rest of the app never changes.
31
+ function QuoteBlock({ node, ctx }: BlockComponentProps) {
32
+ const data = node.data as { markdown: string }
33
+ ${body}
34
+ }
35
+
36
+ export const blockComponents: Record<string, BlockComponent> = {
37
+ quote: QuoteBlock,
38
+ }
39
+ `;
40
+ }
41
+ /** The wrapper component: styled pack or headless, with or without a Markdown lib. */
42
+ function reactComponent(styled, mdWired, blocksImport) {
43
+ const mdImport = mdWired ? "import ReactMarkdown from 'react-markdown'\n" : '';
44
+ const mdProp = mdWired
45
+ ? '\n renderMarkdown={(md) => <ReactMarkdown>{md}</ReactMarkdown>}'
46
+ : `\n // TODO: plug your Markdown library in here, e.g. react-markdown.
47
+ // One function renders all prose: https://contentbit.dev/docs/guides/markdown
48
+ // renderMarkdown={(md) => <Markdown source={md} />}`;
49
+ const rendererImport = styled
50
+ ? `\n// The styled pack installed by shadcn. Yours to edit.
51
+ import { ContentRenderer } from '@/components/content-blocks/content-renderer'`
52
+ : '';
53
+ const renderer = styled ? 'ContentRenderer' : 'ContentBlocks';
54
+ const reactImport = styled ? '' : "import { ContentBlocks } from '@contentbit/react'\n";
55
+ return `'use client'
56
+
57
+ import { genericBlocks } from '@contentbit/blocks'
58
+ import { createBlockRegistry, parseDocument, stripFrontmatter, validateDocument } from '@contentbit/core'
59
+ ${reactImport}${mdImport}${rendererImport}
60
+ // Everything block-related lives in the blocks/ folder: definitions in
61
+ // registry.ts (shared with the validate CLI), components in components.tsx.
62
+ import customBlocks from '${blocksImport}/registry'
63
+ import { blockComponents } from '${blocksImport}/components'
64
+
65
+ const registry = createBlockRegistry().use(genericBlocks()).use(customBlocks)
66
+
67
+ export function Content({ source }: { source: string }) {
68
+ const result = validateDocument(parseDocument(stripFrontmatter(source)), registry)
69
+ return (
70
+ <${renderer}
71
+ document={result.document}
72
+ components={blockComponents}${mdProp}
73
+ />
74
+ )
75
+ }
76
+ `;
77
+ }
78
+ /** Where the component and example page belong for the detected framework. */
79
+ function detectFramework(cwd, deps) {
80
+ if ((deps['@tanstack/react-start'] || deps['@tanstack/react-router']) &&
81
+ existsSync(join(cwd, 'src/routes'))) {
82
+ return {
83
+ framework: 'tanstack',
84
+ componentPath: 'src/components/content-blocks.tsx',
85
+ pagePath: 'src/routes/example.tsx',
86
+ };
87
+ }
88
+ if (deps.next) {
89
+ const appDir = existsSync(join(cwd, 'src/app')) ? 'src/app' : 'app';
90
+ if (existsSync(join(cwd, appDir))) {
91
+ return {
92
+ framework: 'next',
93
+ componentPath: 'components/content-blocks.tsx',
94
+ pagePath: `${appDir}/example/page.tsx`,
95
+ };
96
+ }
97
+ }
98
+ return { framework: null, componentPath: 'components/content-blocks.tsx', pagePath: null };
99
+ }
100
+ const TANSTACK_PAGE = `import { createFileRoute } from '@tanstack/react-router'
101
+
102
+ import { Content } from '../components/content-blocks'
103
+ // Vite's ?raw import inlines the Markdown as a string at build time.
104
+ import source from '../../content/example.md?raw'
105
+
106
+ export const Route = createFileRoute('/example')({ component: ExamplePage })
107
+
108
+ function ExamplePage() {
109
+ return (
110
+ <main style={{ maxWidth: '42rem', margin: '0 auto', padding: '3rem 1.5rem' }}>
111
+ <Content source={source} />
112
+ </main>
113
+ )
114
+ }
115
+ `;
116
+ const NEXT_PAGE = `import { readFile } from 'node:fs/promises'
117
+
118
+ // If your project has no "@/" path alias, switch to a relative import.
119
+ import { Content } from '@/components/content-blocks'
120
+
121
+ export default async function ExamplePage() {
122
+ const source = await readFile('content/example.md', 'utf8')
123
+ return (
124
+ <main style={{ maxWidth: '42rem', margin: '0 auto', padding: '3rem 1.5rem' }}>
125
+ <Content source={source} />
126
+ </main>
127
+ )
128
+ }
129
+ `;
130
+ const ASTRO_CONTENT_CONFIG = `import { defineCollection } from 'astro:content'
131
+ import { glob } from 'astro/loaders'
132
+
133
+ export const collections = {
134
+ articles: defineCollection({
135
+ // Astro's builtin Markdown loader. Entry bodies are parsed and validated
136
+ // where they render (see src/pages/example.astro); \`contentbit validate\`
137
+ // covers the same files in CI.
138
+ loader: glob({ pattern: '**/*.md', base: './content' }),
139
+ }),
140
+ }
141
+ `;
142
+ const ASTRO_QUOTE_BLOCK = `---
143
+ // The Astro component for the custom \`quote\` block defined in blocks/registry.ts.
144
+ // Block props arrive as component props; nested content arrives via <slot />.
145
+ interface Props {
146
+ author: string
147
+ role?: string
148
+ }
149
+
150
+ const { author, role } = Astro.props
151
+ ---
152
+
153
+ <figure style="margin: 1.5rem 0; border-left: 2px solid #d4d4d4; padding-left: 1rem;">
154
+ <blockquote style="font-style: italic;"><slot /></blockquote>
155
+ <figcaption style="margin-top: 0.5rem; font-size: 0.875rem; opacity: 0.7;">
156
+ — {author}{role ? \`, \${role}\` : null}
157
+ </figcaption>
158
+ </figure>
159
+ `;
160
+ /** The example page: styled pack renderer or the headless ContentBlocks. */
161
+ function astroPage(styled) {
162
+ const importLine = styled
163
+ ? "import ContentRenderer from '../components/content-blocks/content-renderer.astro'"
164
+ : "import { ContentBlocks } from '@contentbit/astro/components'";
165
+ const renderer = styled ? 'ContentRenderer' : 'ContentBlocks';
166
+ return `---
167
+ import { genericBlocks } from '@contentbit/blocks'
168
+ import { createBlockRegistry, parseDocument, validateDocument } from '@contentbit/core'
169
+ import { getEntry } from 'astro:content'
170
+
171
+ ${importLine}
172
+
173
+ // Definitions in blocks/registry.ts are shared with the validate CLI.
174
+ import customBlocks from '../../blocks/registry'
175
+ import QuoteBlock from '../../blocks/QuoteBlock.astro'
176
+
177
+ // Entry ids are the file path relative to the collection base, minus ".md".
178
+ const entry = await getEntry('articles', 'example')
179
+ if (!entry?.body) throw new Error('Entry "example" not found in the articles collection.')
180
+
181
+ const registry = createBlockRegistry().use(genericBlocks()).use(customBlocks)
182
+ // Static pages render at build time, so invalid blocks fail the build here.
183
+ const result = validateDocument(parseDocument(entry.body), registry)
184
+ ---
185
+
186
+ <main style="max-width: 42rem; margin: 0 auto; padding: 3rem 1.5rem;">
187
+ <${renderer} document={result.document} components={{ quote: QuoteBlock }} />
188
+ </main>
189
+ `;
190
+ }
191
+ function runtimeWith(...extra) {
192
+ return [...RUNTIME_BASE, ...extra];
193
+ }
194
+ function reactRuntime(md) {
195
+ return runtimeWith('@contentbit/react', ...(md === 'none' ? [] : [md]));
196
+ }
197
+ async function createReactPlan(ctx) {
198
+ const files = [];
199
+ const layout = detectFramework(ctx.cwd, ctx.deps);
200
+ const componentsJsonPath = join(ctx.cwd, 'components.json');
201
+ const styled = !ctx.noStyled &&
202
+ existsSync(componentsJsonPath) &&
203
+ (await ctx.installStyledPack('@contentbit/generic-pack'));
204
+ const depth = layout.componentPath.split('/').length - 1;
205
+ const blocksImport = `${'../'.repeat(depth)}blocks`;
206
+ files.push(['blocks/components.tsx', blockComponentsTemplate(styled)]);
207
+ files.push([
208
+ layout.componentPath,
209
+ reactComponent(styled, ctx.md === 'react-markdown', blocksImport),
210
+ ]);
211
+ if (!ctx.noPage && layout.pagePath) {
212
+ files.push([layout.pagePath, layout.framework === 'tanstack' ? TANSTACK_PAGE : NEXT_PAGE]);
213
+ }
214
+ const nextSteps = !ctx.noPage && layout.pagePath
215
+ ? [' 2. Start the dev server and open /example to see the article rendered.']
216
+ : [
217
+ ' 2. Render it: import { Content } from "./components/content-blocks"',
218
+ ' <Content source={...content/example.md as a string} />',
219
+ ];
220
+ nextSteps.push(' 3. Styled components: pnpm dlx shadcn@latest add @contentbit/generic-pack');
221
+ return { files, nextSteps };
222
+ }
223
+ async function createAstroPlan(ctx) {
224
+ const files = [['blocks/QuoteBlock.astro', ASTRO_QUOTE_BLOCK]];
225
+ const componentsJsonPath = join(ctx.cwd, 'components.json');
226
+ const styled = !ctx.noStyled &&
227
+ existsSync(componentsJsonPath) &&
228
+ (await ctx.installStyledPack('@contentbit/astro-pack'));
229
+ // Every config filename Astro resolves (src/content.config.* plus the legacy
230
+ // src/content/config.* location), so we never scaffold a second config that
231
+ // Astro would silently ignore.
232
+ const configCandidates = ['ts', 'mts', 'mjs', 'js'].flatMap((ext) => [
233
+ `src/content.config.${ext}`,
234
+ `src/content/config.${ext}`,
235
+ ]);
236
+ const existingConfig = configCandidates.find((p) => existsSync(join(ctx.cwd, p)));
237
+ if (existingConfig) {
238
+ ctx.io.stdout(`content config exists (${existingConfig}); add this collection manually:`);
239
+ ctx.io.stdout(ASTRO_CONTENT_CONFIG);
240
+ ctx.io.stdout('the example page expects the "articles" collection above');
241
+ }
242
+ else {
243
+ files.push(['src/content.config.ts', ASTRO_CONTENT_CONFIG]);
244
+ }
245
+ if (!ctx.noPage)
246
+ files.push(['src/pages/example.astro', astroPage(styled)]);
247
+ return {
248
+ files,
249
+ nextSteps: [
250
+ ' 2. Start the dev server and open /example to see the article rendered.',
251
+ ' 3. Styled components: pnpm dlx shadcn@latest add @contentbit/astro-pack',
252
+ ],
253
+ };
254
+ }
255
+ export const TARGET_ADAPTERS = {
256
+ react: {
257
+ markdownChoices: ['react-markdown', 'none'],
258
+ runtimeDependencies: reactRuntime,
259
+ createPlan: createReactPlan,
260
+ },
261
+ markdown: {
262
+ markdownChoices: ['none'],
263
+ runtimeDependencies: () => runtimeWith(),
264
+ createPlan: async () => ({
265
+ files: [],
266
+ nextSteps: [' 2. Render it: contentbit render content/example.md'],
267
+ }),
268
+ },
269
+ astro: {
270
+ // Astro projects usually reuse their host Markdown pipeline.
271
+ markdownChoices: ['none'],
272
+ runtimeDependencies: () => runtimeWith('@contentbit/astro'),
273
+ createPlan: createAstroPlan,
274
+ },
275
+ };
276
+ export function detectTarget(deps) {
277
+ const hasReact = Boolean(deps.react);
278
+ const hasAstro = Boolean(deps.astro);
279
+ return hasAstro ? 'astro' : hasReact ? 'react' : 'markdown';
280
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/commands/init.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,WAAW,CAAA;AAianC,wBAAsB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAqPzE"}
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/commands/init.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,WAAW,CAAA;AA2KnC,wBAAsB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CA2LzE"}