contentbit 0.5.0 → 0.6.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/README.md +13 -0
- package/dist/bin.js +4807 -501
- package/dist/commands/agents.d.ts +6 -1
- package/dist/commands/agents.d.ts.map +1 -1
- package/dist/commands/brief.d.ts +12 -0
- package/dist/commands/brief.d.ts.map +1 -0
- package/dist/commands/docs.d.ts +6 -1
- package/dist/commands/docs.d.ts.map +1 -1
- package/dist/commands/doctor.d.ts +13 -1
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/init.d.ts +12 -1
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/instructions.d.ts +8 -1
- package/dist/commands/instructions.d.ts.map +1 -1
- package/dist/commands/links.d.ts +7 -1
- package/dist/commands/links.d.ts.map +1 -1
- package/dist/commands/render.d.ts +8 -1
- package/dist/commands/render.d.ts.map +1 -1
- package/dist/commands/stats.d.ts +7 -1
- package/dist/commands/stats.d.ts.map +1 -1
- package/dist/commands/studio.d.ts +13 -1
- package/dist/commands/studio.d.ts.map +1 -1
- package/dist/commands/validate.d.ts +8 -1
- package/dist/commands/validate.d.ts.map +1 -1
- package/dist/content-project.d.ts +2 -22
- package/dist/content-project.d.ts.map +1 -1
- package/dist/link-options.d.ts +5 -0
- package/dist/link-options.d.ts.map +1 -1
- package/dist/load-registry.d.ts +1 -6
- package/dist/load-registry.d.ts.map +1 -1
- package/dist/run.d.ts +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +4805 -499
- package/dist/seo-config.d.ts +12 -0
- package/dist/seo-config.d.ts.map +1 -0
- package/package.json +6 -5
- package/dist/cli-format.js +0 -63
- package/dist/commands/agents.js +0 -73
- package/dist/commands/docs.js +0 -21
- package/dist/commands/doctor.js +0 -149
- package/dist/commands/init-targets.js +0 -280
- package/dist/commands/init.js +0 -343
- package/dist/commands/instructions.js +0 -26
- package/dist/commands/links.js +0 -101
- package/dist/commands/render.js +0 -45
- package/dist/commands/stats.js +0 -48
- package/dist/commands/studio.js +0 -78
- package/dist/commands/validate.js +0 -64
- package/dist/content-project.js +0 -34
- package/dist/link-options.js +0 -31
- package/dist/links-io.js +0 -14
- package/dist/load-registry.js +0 -26
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const DEFAULT_SEO_CONFIG = "contentbit.seo.config.ts";
|
|
2
|
+
export interface LoadSeoConfigInput {
|
|
3
|
+
cwd?: string;
|
|
4
|
+
seoConfig?: string;
|
|
5
|
+
noSeo?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface LoadedSeoConfig {
|
|
8
|
+
config?: unknown;
|
|
9
|
+
path?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function loadSeoConfig(input?: LoadSeoConfigInput): Promise<LoadedSeoConfig>;
|
|
12
|
+
//# sourceMappingURL=seo-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seo-config.d.ts","sourceRoot":"","sources":["../src/seo-config.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,kBAAkB,6BAA6B,CAAA;AAE5D,MAAM,WAAW,kBAAkB;IACjC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,wBAAsB,aAAa,CAAC,KAAK,GAAE,kBAAuB,GAAG,OAAO,CAAC,eAAe,CAAC,CAc5F"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "contentbit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "CLI for Content Blocks: validate, render, and generate LLM authoring instructions from your registry.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -36,10 +36,11 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@clack/prompts": "^1.5.1",
|
|
39
|
+
"commander": "^15.0.0",
|
|
39
40
|
"tinyglobby": "^0.2.10",
|
|
40
|
-
"@contentbit/blocks": "0.
|
|
41
|
-
"@contentbit/core": "0.
|
|
42
|
-
"@contentbit/studio": "0.
|
|
41
|
+
"@contentbit/blocks": "0.6.1",
|
|
42
|
+
"@contentbit/core": "0.6.1",
|
|
43
|
+
"@contentbit/studio": "0.6.1"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@types/node": "^25.0.9",
|
|
@@ -51,7 +52,7 @@
|
|
|
51
52
|
"node": ">=22.18"
|
|
52
53
|
},
|
|
53
54
|
"scripts": {
|
|
54
|
-
"build": "tsc -p tsconfig.build.json
|
|
55
|
+
"build": "node build.mjs && tsc -p tsconfig.build.json --emitDeclarationOnly",
|
|
55
56
|
"test": "vitest run",
|
|
56
57
|
"test:watch": "vitest"
|
|
57
58
|
}
|
package/dist/cli-format.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { isAbsolute, relative } from 'node:path';
|
|
2
|
-
const ANSI = {
|
|
3
|
-
bold: ['\u001b[1m', '\u001b[22m'],
|
|
4
|
-
error: ['\u001b[31m', '\u001b[39m'],
|
|
5
|
-
warning: ['\u001b[33m', '\u001b[39m'],
|
|
6
|
-
info: ['\u001b[36m', '\u001b[39m'],
|
|
7
|
-
success: ['\u001b[32m', '\u001b[39m'],
|
|
8
|
-
muted: ['\u001b[2m', '\u001b[22m'],
|
|
9
|
-
};
|
|
10
|
-
export function color(text, tone) {
|
|
11
|
-
if (!colorsEnabled())
|
|
12
|
-
return text;
|
|
13
|
-
const [open, close] = ANSI[tone];
|
|
14
|
-
return `${open}${text}${close}`;
|
|
15
|
-
}
|
|
16
|
-
export function section(title) {
|
|
17
|
-
return color(title, 'bold');
|
|
18
|
-
}
|
|
19
|
-
export function formatRows(rows) {
|
|
20
|
-
const width = Math.max(...rows.map((row) => row.label.length));
|
|
21
|
-
return rows.map((row) => {
|
|
22
|
-
const value = String(row.value);
|
|
23
|
-
return ` ${color(row.label.padEnd(width), 'muted')} ${row.tone ? color(value, row.tone) : value}`;
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
export function formatCommandList(commands) {
|
|
27
|
-
return commands.map((command) => ` ${color('$', 'muted')} ${command}`);
|
|
28
|
-
}
|
|
29
|
-
export function formatDiagnosticForCli(diagnostic, file) {
|
|
30
|
-
const location = `${displayPath(file)}:${diagnostic.position.start.line}:${diagnostic.position.start.column}`;
|
|
31
|
-
const lines = [
|
|
32
|
-
`${color(location, 'muted')} ${severityLabel(diagnostic.severity)} ${diagnostic.code}`,
|
|
33
|
-
` ${diagnostic.message}`,
|
|
34
|
-
];
|
|
35
|
-
if (diagnostic.hint)
|
|
36
|
-
lines.push(` ${color('hint', 'muted')} ${diagnostic.hint}`);
|
|
37
|
-
return lines.join('\n');
|
|
38
|
-
}
|
|
39
|
-
export function displayPath(file) {
|
|
40
|
-
const path = relative(process.cwd(), file);
|
|
41
|
-
if (path && !path.startsWith('..') && !isAbsolute(path))
|
|
42
|
-
return path;
|
|
43
|
-
return file;
|
|
44
|
-
}
|
|
45
|
-
export function severityLabel(severity) {
|
|
46
|
-
return color(severity, severityTone(severity));
|
|
47
|
-
}
|
|
48
|
-
export function severityTone(severity) {
|
|
49
|
-
if (severity === 'error')
|
|
50
|
-
return 'error';
|
|
51
|
-
if (severity === 'warning')
|
|
52
|
-
return 'warning';
|
|
53
|
-
return 'info';
|
|
54
|
-
}
|
|
55
|
-
function colorsEnabled() {
|
|
56
|
-
if (process.env.VITEST || process.env.NODE_ENV === 'test')
|
|
57
|
-
return false;
|
|
58
|
-
if ('NO_COLOR' in process.env || process.env.FORCE_COLOR === '0')
|
|
59
|
-
return false;
|
|
60
|
-
if (process.env.FORCE_COLOR)
|
|
61
|
-
return true;
|
|
62
|
-
return Boolean(process.stdout.isTTY);
|
|
63
|
-
}
|
package/dist/commands/agents.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
2
|
-
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
|
-
import { parseArgs } from 'node:util';
|
|
5
|
-
import agentsMdTemplate from './agent-templates/AGENTS.md?raw';
|
|
6
|
-
import auditSkill from './agent-templates/contentbit-audit/SKILL.md?raw';
|
|
7
|
-
import authorSkill from './agent-templates/contentbit-author/SKILL.md?raw';
|
|
8
|
-
// These templates are static and project-independent by design: skills fetch
|
|
9
|
-
// live data (authoring guide, stats, diagnostics) by running the CLI, so the
|
|
10
|
-
// registry stays the single source of truth and nothing can drift. Bump the
|
|
11
|
-
// frontmatter version in each SKILL.md when a template changes; `contentbit
|
|
12
|
-
// agents` re-runs overwrite in place.
|
|
13
|
-
const AUTHOR_SKILL = authorSkill;
|
|
14
|
-
const AUDIT_SKILL = auditSkill;
|
|
15
|
-
const AGENTS_MD_BLOCK = agentsMdTemplate.trimEnd();
|
|
16
|
-
const START = '<!-- contentbit:start -->';
|
|
17
|
-
const END = '<!-- contentbit:end -->';
|
|
18
|
-
/** Insert or replace the fenced contentbit block, leaving the rest untouched. */
|
|
19
|
-
function upsertBlock(existing) {
|
|
20
|
-
const start = existing.indexOf(START);
|
|
21
|
-
const end = existing.indexOf(END);
|
|
22
|
-
if (start !== -1 && end !== -1) {
|
|
23
|
-
return existing.slice(0, start) + AGENTS_MD_BLOCK + existing.slice(end + END.length);
|
|
24
|
-
}
|
|
25
|
-
if (existing.trim() === '')
|
|
26
|
-
return `${AGENTS_MD_BLOCK}\n`;
|
|
27
|
-
return `${existing.replace(/\n*$/, '\n\n')}${AGENTS_MD_BLOCK}\n`;
|
|
28
|
-
}
|
|
29
|
-
/** Install or refresh the agent integration. Shared by `agents` and `init`. */
|
|
30
|
-
export async function installAgentIntegration(cwd, options, io) {
|
|
31
|
-
const claude = options.claude ?? existsSync(join(cwd, '.claude'));
|
|
32
|
-
const agentsMd = options.agentsMd ?? true;
|
|
33
|
-
if (agentsMd) {
|
|
34
|
-
const path = join(cwd, 'AGENTS.md');
|
|
35
|
-
let existing = '';
|
|
36
|
-
try {
|
|
37
|
-
existing = await readFile(path, 'utf8');
|
|
38
|
-
}
|
|
39
|
-
catch {
|
|
40
|
-
/* not there yet */
|
|
41
|
-
}
|
|
42
|
-
const created = existing === '';
|
|
43
|
-
await writeFile(path, upsertBlock(existing), 'utf8');
|
|
44
|
-
io.stdout(`${created ? 'created' : 'updated'}: AGENTS.md (contentbit block)`);
|
|
45
|
-
}
|
|
46
|
-
if (claude) {
|
|
47
|
-
const skills = [
|
|
48
|
-
['contentbit-author', AUTHOR_SKILL],
|
|
49
|
-
['contentbit-audit', AUDIT_SKILL],
|
|
50
|
-
];
|
|
51
|
-
for (const [name, content] of skills) {
|
|
52
|
-
const dir = join(cwd, '.claude/skills', name);
|
|
53
|
-
await mkdir(dir, { recursive: true });
|
|
54
|
-
await writeFile(join(dir, 'SKILL.md'), content, 'utf8');
|
|
55
|
-
io.stdout(`installed: .claude/skills/${name}/SKILL.md`);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
export async function agentsCommand(args, io) {
|
|
60
|
-
const { values } = parseArgs({
|
|
61
|
-
args,
|
|
62
|
-
options: {
|
|
63
|
-
claude: { type: 'boolean', default: false },
|
|
64
|
-
'no-agents-md': { type: 'boolean', default: false },
|
|
65
|
-
cwd: { type: 'string', default: process.cwd() },
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
await installAgentIntegration(values.cwd, {
|
|
69
|
-
claude: values.claude || undefined, // false means "detect", not "skip"
|
|
70
|
-
agentsMd: !values['no-agents-md'],
|
|
71
|
-
}, io);
|
|
72
|
-
return 0;
|
|
73
|
-
}
|
package/dist/commands/docs.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { parseArgs } from 'node:util';
|
|
2
|
-
import { loadRegistry } from '../load-registry.js';
|
|
3
|
-
export async function docsCommand(args, io) {
|
|
4
|
-
const { values } = parseArgs({
|
|
5
|
-
args,
|
|
6
|
-
options: {
|
|
7
|
-
registry: { type: 'string' },
|
|
8
|
-
'no-generic-blocks': { type: 'boolean', default: false },
|
|
9
|
-
out: { type: 'string' },
|
|
10
|
-
},
|
|
11
|
-
});
|
|
12
|
-
const registry = await loadRegistry(values.registry, {
|
|
13
|
-
includeGenericBlocks: !values['no-generic-blocks'],
|
|
14
|
-
});
|
|
15
|
-
const guide = registry.toAuthoringGuide({ audience: 'human', includeExamples: true });
|
|
16
|
-
if (values.out)
|
|
17
|
-
await io.writeFile(values.out, guide);
|
|
18
|
-
else
|
|
19
|
-
io.stdout(guide);
|
|
20
|
-
return 0;
|
|
21
|
-
}
|
package/dist/commands/doctor.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_MIN_SECTION_WORDS, } from '@contentbit/core';
|
|
2
|
-
import { parseArgs } from 'node:util';
|
|
3
|
-
import { color, displayPath, formatCommandList, formatRows, section, severityLabel, } from '../cli-format.js';
|
|
4
|
-
import { loadContentProject } from '../content-project.js';
|
|
5
|
-
import { linkResolverOptions } from '../link-options.js';
|
|
6
|
-
export async function doctorCommand(args, io) {
|
|
7
|
-
const { values, positionals } = parseArgs({
|
|
8
|
-
args,
|
|
9
|
-
allowPositionals: true,
|
|
10
|
-
options: {
|
|
11
|
-
registry: { type: 'string' },
|
|
12
|
-
'no-generic-blocks': { type: 'boolean', default: false },
|
|
13
|
-
'strict-warnings': { type: 'boolean', default: false },
|
|
14
|
-
json: { type: 'boolean', default: false },
|
|
15
|
-
'min-section-words': { type: 'string' },
|
|
16
|
-
'link-resolve': { type: 'string' },
|
|
17
|
-
'locale-field': { type: 'string' },
|
|
18
|
-
'slug-field': { type: 'string' },
|
|
19
|
-
'key-field': { type: 'string' },
|
|
20
|
-
'default-locale': { type: 'string' },
|
|
21
|
-
},
|
|
22
|
-
});
|
|
23
|
-
const minSectionWords = parseMinSectionWords(values['min-section-words']);
|
|
24
|
-
if (minSectionWords === null) {
|
|
25
|
-
io.stderr('doctor: --min-section-words must be a non-negative integer.');
|
|
26
|
-
return 2;
|
|
27
|
-
}
|
|
28
|
-
const includeGenericBlocks = !values['no-generic-blocks'];
|
|
29
|
-
const { files, scan } = await loadContentProject({
|
|
30
|
-
cmd: 'doctor',
|
|
31
|
-
positionals,
|
|
32
|
-
registry: values.registry,
|
|
33
|
-
includeGenericBlocks,
|
|
34
|
-
linkOptions: linkResolverOptions(values),
|
|
35
|
-
scan: { minSectionWords },
|
|
36
|
-
});
|
|
37
|
-
const report = {
|
|
38
|
-
files: files.length,
|
|
39
|
-
summary: scan.summary,
|
|
40
|
-
...(scan.linkGraph ? { linkGraph: scan.linkGraph } : {}),
|
|
41
|
-
findings: scan.findings,
|
|
42
|
-
};
|
|
43
|
-
if (values.json)
|
|
44
|
-
io.stdout(JSON.stringify(report, null, 2));
|
|
45
|
-
else
|
|
46
|
-
io.stdout(formatReport(report, positionals, values.registry, includeGenericBlocks, hasAliases(scan.linkInputs)));
|
|
47
|
-
if (report.summary.errors > 0)
|
|
48
|
-
return 1;
|
|
49
|
-
if (report.summary.warnings > 0 && values['strict-warnings'])
|
|
50
|
-
return 1;
|
|
51
|
-
return 0;
|
|
52
|
-
}
|
|
53
|
-
function parseMinSectionWords(value) {
|
|
54
|
-
if (value === undefined)
|
|
55
|
-
return DEFAULT_MIN_SECTION_WORDS;
|
|
56
|
-
if (!/^\d+$/.test(value))
|
|
57
|
-
return null;
|
|
58
|
-
return Number(value);
|
|
59
|
-
}
|
|
60
|
-
function hasAliases(inputs) {
|
|
61
|
-
return inputs.some((input) => Array.isArray(input.data.aliases) && input.data.aliases.length > 0);
|
|
62
|
-
}
|
|
63
|
-
function formatReport(report, globs, registryPath, includeGenericBlocks, aliasFixMayApply) {
|
|
64
|
-
const lines = [];
|
|
65
|
-
lines.push(section('contentbit doctor'));
|
|
66
|
-
lines.push('');
|
|
67
|
-
lines.push(section('Health'));
|
|
68
|
-
lines.push(...formatRows([
|
|
69
|
-
{ label: 'Files', value: report.files },
|
|
70
|
-
{
|
|
71
|
-
label: 'Errors',
|
|
72
|
-
value: report.summary.errors,
|
|
73
|
-
tone: report.summary.errors > 0 ? 'error' : 'success',
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
label: 'Warnings',
|
|
77
|
-
value: report.summary.warnings,
|
|
78
|
-
tone: report.summary.warnings > 0 ? 'warning' : 'success',
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
label: 'Suggestions',
|
|
82
|
-
value: report.summary.suggestions,
|
|
83
|
-
tone: report.summary.suggestions > 0 ? 'info' : 'success',
|
|
84
|
-
},
|
|
85
|
-
]));
|
|
86
|
-
if (report.linkGraph) {
|
|
87
|
-
lines.push('');
|
|
88
|
-
lines.push(section('Link Graph'));
|
|
89
|
-
lines.push(...formatRows([
|
|
90
|
-
{ label: 'Pages', value: report.linkGraph.pages },
|
|
91
|
-
{ label: 'Links', value: report.linkGraph.links },
|
|
92
|
-
{
|
|
93
|
-
label: 'Orphans',
|
|
94
|
-
value: report.linkGraph.orphans,
|
|
95
|
-
tone: report.linkGraph.orphans > 0 ? 'warning' : 'success',
|
|
96
|
-
},
|
|
97
|
-
]));
|
|
98
|
-
}
|
|
99
|
-
lines.push('');
|
|
100
|
-
if (report.findings.length === 0) {
|
|
101
|
-
lines.push(color('No findings. Content looks healthy.', 'success'));
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
lines.push(section('Repair Plan'));
|
|
105
|
-
report.findings.forEach((finding, index) => {
|
|
106
|
-
lines.push(...formatFinding(index + 1, finding));
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
lines.push('');
|
|
110
|
-
lines.push(section('Next Commands'));
|
|
111
|
-
const commands = [
|
|
112
|
-
`contentbit validate ${formatGlobs(globs)}${formatRegistryArgs(registryPath, includeGenericBlocks)}`,
|
|
113
|
-
];
|
|
114
|
-
if (report.linkGraph) {
|
|
115
|
-
commands.push(`contentbit links ${formatGlobs(globs)}`);
|
|
116
|
-
if (aliasFixMayApply && report.summary.errors === 0)
|
|
117
|
-
commands.push(`contentbit links ${formatGlobs(globs)} --fix`);
|
|
118
|
-
}
|
|
119
|
-
commands.push(`contentbit doctor ${formatGlobs(globs)}${formatRegistryArgs(registryPath, includeGenericBlocks)} --json`);
|
|
120
|
-
lines.push(...formatCommandList(commands));
|
|
121
|
-
return lines.join('\n');
|
|
122
|
-
}
|
|
123
|
-
function formatFinding(index, finding) {
|
|
124
|
-
const location = finding.line !== undefined && finding.column !== undefined
|
|
125
|
-
? `${displayPath(finding.file)}:${finding.line}:${finding.column}`
|
|
126
|
-
: displayPath(finding.file);
|
|
127
|
-
const lines = [
|
|
128
|
-
` ${index}. ${severityLabel(finding.severity)} ${finding.source} ${finding.code}`,
|
|
129
|
-
` ${color('at', 'muted')} ${location}`,
|
|
130
|
-
` ${finding.message}`,
|
|
131
|
-
];
|
|
132
|
-
if (finding.hint)
|
|
133
|
-
lines.push(` ${color('hint', 'muted')} ${finding.hint}`);
|
|
134
|
-
return lines;
|
|
135
|
-
}
|
|
136
|
-
function formatGlobs(globs) {
|
|
137
|
-
return globs.map(formatArg).join(' ');
|
|
138
|
-
}
|
|
139
|
-
function formatArg(value) {
|
|
140
|
-
return /\s/.test(value) ? JSON.stringify(value) : value;
|
|
141
|
-
}
|
|
142
|
-
function formatRegistryArgs(registryPath, includeGenericBlocks) {
|
|
143
|
-
const args = [];
|
|
144
|
-
if (registryPath)
|
|
145
|
-
args.push('--registry', formatArg(registryPath));
|
|
146
|
-
if (!includeGenericBlocks)
|
|
147
|
-
args.push('--no-generic-blocks');
|
|
148
|
-
return args.length > 0 ? ` ${args.join(' ')}` : '';
|
|
149
|
-
}
|
|
@@ -1,280 +0,0 @@
|
|
|
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
|
-
}
|