nizel 0.1.12 → 0.1.14
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 +15 -8
- package/cli/nizel.mjs +81 -0
- package/dist/browser/nizel.iife.js +311 -1
- package/dist/browser/nizel.js +311 -1
- package/dist/browser.d.ts +5 -0
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +5 -0
- package/dist/browser.js.map +1 -1
- package/dist/document.d.ts +12 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +63 -0
- package/dist/document.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/markdown.d.ts +4 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +126 -0
- package/dist/markdown.js.map +1 -0
- package/dist/options.js +4 -0
- package/dist/options.js.map +1 -1
- package/dist/renderer.d.ts +4 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +79 -0
- package/dist/renderer.js.map +1 -0
- package/dist/streaming.d.ts +4 -0
- package/dist/streaming.d.ts.map +1 -0
- package/dist/streaming.js +76 -0
- package/dist/streaming.js.map +1 -0
- package/dist/types.d.ts +36 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/use-nizel.d.ts.map +1 -1
- package/dist/use-nizel.js +7 -0
- package/dist/use-nizel.js.map +1 -1
- package/dist/visit.d.ts +5 -0
- package/dist/visit.d.ts.map +1 -0
- package/dist/visit.js +20 -0
- package/dist/visit.js.map +1 -0
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -19,27 +19,34 @@ const processor = useNizel({
|
|
|
19
19
|
// optional configuration
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
-
const result = processor
|
|
22
|
+
const result = await processor('# Hello {{ name }}', {
|
|
23
23
|
data: { name: 'World' }
|
|
24
24
|
});
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
## Result
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
The processor returns:
|
|
30
30
|
|
|
31
|
-
- `
|
|
32
|
-
- `
|
|
33
|
-
- `
|
|
34
|
-
- `
|
|
31
|
+
- `result`, `html`, `text`, and `ast`
|
|
32
|
+
- `meta` and `frontmatter`
|
|
33
|
+
- `title`, `description`, and `excerpt`
|
|
34
|
+
- `toc`, `headings`, `links`, and `images`
|
|
35
|
+
- `readingTime`
|
|
35
36
|
|
|
36
37
|
## Convenience Methods
|
|
37
38
|
|
|
38
39
|
```js
|
|
39
|
-
processor.parse(markdown);
|
|
40
|
-
processor.render(
|
|
40
|
+
await processor.parse(markdown); // AST only
|
|
41
|
+
processor.render(ast); // AST to HTML
|
|
42
|
+
processor.markdown(ast); // AST to Markdown
|
|
43
|
+
await processor.document(markdown); // portable document
|
|
41
44
|
```
|
|
42
45
|
|
|
46
|
+
Portable documents use the versioned `nizel/document` schema and can be passed
|
|
47
|
+
through `serializeNizelDocument()` and `deserializeNizelDocument()`. Use
|
|
48
|
+
`completeStreamingMarkdown()` before parsing incomplete streamed content.
|
|
49
|
+
|
|
43
50
|
## Features
|
|
44
51
|
|
|
45
52
|
- **CommonMark 0.31** — full spec compliance (652/652 tests)
|
package/cli/nizel.mjs
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { useNizel } from '../dist/index.js';
|
|
6
|
+
|
|
7
|
+
const HELP = `Usage: npx nizel <input.md> [options]
|
|
8
|
+
|
|
9
|
+
Options:
|
|
10
|
+
-o, --output <file> Write HTML to a file instead of stdout
|
|
11
|
+
--fragment Output the rendered HTML fragment only (default)
|
|
12
|
+
--document Wrap output in a complete HTML document
|
|
13
|
+
--title <title> Override the document title used with --document
|
|
14
|
+
-h, --help Show this help
|
|
15
|
+
-v, --version Show the installed Nizel version
|
|
16
|
+
|
|
17
|
+
Examples:
|
|
18
|
+
npx nizel report.md
|
|
19
|
+
npx nizel report.md -o report.html --document
|
|
20
|
+
cat report.md | npx nizel - --document > report.html
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
function parseArgs(argv) {
|
|
24
|
+
const options = { input: null, output: null, document: false, title: null };
|
|
25
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
26
|
+
const arg = argv[index];
|
|
27
|
+
if (arg === '-h' || arg === '--help') return { ...options, help: true };
|
|
28
|
+
if (arg === '-v' || arg === '--version') return { ...options, version: true };
|
|
29
|
+
if (arg === '--document') { options.document = true; continue; }
|
|
30
|
+
if (arg === '--fragment') { options.document = false; continue; }
|
|
31
|
+
if (arg === '-o' || arg === '--output') { options.output = argv[++index]; continue; }
|
|
32
|
+
if (arg === '--title') { options.title = argv[++index]; continue; }
|
|
33
|
+
if (arg.startsWith('-') && arg !== '-') throw new Error(`Unknown option: ${arg}`);
|
|
34
|
+
if (options.input) throw new Error('Only one input file is supported.');
|
|
35
|
+
options.input = arg;
|
|
36
|
+
}
|
|
37
|
+
return options;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function escapeHtml(value) {
|
|
41
|
+
return String(value)
|
|
42
|
+
.replaceAll('&', '&')
|
|
43
|
+
.replaceAll('<', '<')
|
|
44
|
+
.replaceAll('>', '>')
|
|
45
|
+
.replaceAll('"', '"');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function wrapDocument(html, title) {
|
|
49
|
+
return `<!doctype html>\n<html lang="en">\n<head>\n<meta charset="utf-8">\n<meta name="viewport" content="width=device-width, initial-scale=1">\n<title>${escapeHtml(title || 'Nizel document')}</title>\n</head>\n<body>\n${html}\n</body>\n</html>\n`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function readStdin() {
|
|
53
|
+
const chunks = [];
|
|
54
|
+
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
55
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function main() {
|
|
59
|
+
const options = parseArgs(process.argv.slice(2));
|
|
60
|
+
if (options.help) { process.stdout.write(HELP); return; }
|
|
61
|
+
if (options.version) {
|
|
62
|
+
const pkg = JSON.parse(await readFile(new URL('../package.json', import.meta.url), 'utf8'));
|
|
63
|
+
process.stdout.write(`${pkg.version}\n`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!options.input) throw new Error(`Missing input file.\n\n${HELP}`);
|
|
67
|
+
|
|
68
|
+
const markdown = options.input === '-' ? await readStdin() : await readFile(options.input, 'utf8');
|
|
69
|
+
const rendered = await useNizel()(markdown);
|
|
70
|
+
const html = options.document
|
|
71
|
+
? wrapDocument(rendered.html, options.title || rendered.title || rendered.meta?.title)
|
|
72
|
+
: rendered.html;
|
|
73
|
+
|
|
74
|
+
if (options.output) await writeFile(options.output, html, 'utf8');
|
|
75
|
+
else process.stdout.write(html.endsWith('\n') ? html : `${html}\n`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
main().catch((error) => {
|
|
79
|
+
process.stderr.write(`nizel: ${error.message}\n`);
|
|
80
|
+
process.exitCode = 1;
|
|
81
|
+
});
|
|
@@ -21,15 +21,25 @@ var Nizel = (() => {
|
|
|
21
21
|
// src/browser.ts
|
|
22
22
|
var browser_exports = {};
|
|
23
23
|
__export(browser_exports, {
|
|
24
|
+
NIZEL_DOCUMENT_SCHEMA: () => NIZEL_DOCUMENT_SCHEMA,
|
|
25
|
+
NIZEL_DOCUMENT_VERSION: () => NIZEL_DOCUMENT_VERSION,
|
|
26
|
+
assertNizelDocument: () => assertNizelDocument,
|
|
27
|
+
completeStreamingMarkdown: () => completeStreamingMarkdown,
|
|
28
|
+
createNizelDocument: () => createNizelDocument,
|
|
24
29
|
defineBlock: () => defineBlock,
|
|
25
30
|
defineNizelPlugin: () => defineNizelPlugin,
|
|
31
|
+
deserializeNizelDocument: () => deserializeNizelDocument,
|
|
26
32
|
htmlToMarkdown: () => htmlToMarkdown2,
|
|
27
33
|
markdownToHtml: () => markdownToHtml,
|
|
28
34
|
mountMarkdown: () => mountMarkdown,
|
|
29
35
|
nodeToMarkdown: () => nodeToMarkdown,
|
|
36
|
+
renderMarkdown: () => renderMarkdown,
|
|
37
|
+
renderNizelTree: () => renderNizelTree,
|
|
30
38
|
selectionToMarkdown: () => selectionToMarkdown,
|
|
39
|
+
serializeNizelDocument: () => serializeNizelDocument,
|
|
31
40
|
useBrowserNizel: () => useBrowserNizel,
|
|
32
|
-
useNizel: () => useNizel
|
|
41
|
+
useNizel: () => useNizel,
|
|
42
|
+
visitNizel: () => visitNizel
|
|
33
43
|
});
|
|
34
44
|
|
|
35
45
|
// src/html-to-markdown.ts
|
|
@@ -2388,6 +2398,10 @@ ${trimmed}
|
|
|
2388
2398
|
...left.variables ?? {},
|
|
2389
2399
|
...right.variables ?? {}
|
|
2390
2400
|
},
|
|
2401
|
+
meta: {
|
|
2402
|
+
...left.meta ?? {},
|
|
2403
|
+
...right.meta ?? {}
|
|
2404
|
+
},
|
|
2391
2405
|
elements: {
|
|
2392
2406
|
...left.elements ?? {},
|
|
2393
2407
|
...right.elements ?? {}
|
|
@@ -4376,6 +4390,166 @@ ${body}
|
|
|
4376
4390
|
].includes(node.type);
|
|
4377
4391
|
}
|
|
4378
4392
|
|
|
4393
|
+
// src/markdown.ts
|
|
4394
|
+
function renderMarkdown(value, options = {}) {
|
|
4395
|
+
const document2 = "schema" in value ? value : void 0;
|
|
4396
|
+
const body = renderBlocks((document2?.ast ?? value).children, options).trim();
|
|
4397
|
+
if (!document2 || options.frontmatter === false || Object.keys(document2.meta).length === 0) return body;
|
|
4398
|
+
return `---
|
|
4399
|
+
${renderYaml(document2.meta)}
|
|
4400
|
+
---${body ? `
|
|
4401
|
+
|
|
4402
|
+
${body}` : ""}`;
|
|
4403
|
+
}
|
|
4404
|
+
function renderBlocks(nodes, options) {
|
|
4405
|
+
return nodes.map((node) => renderBlock2(node, options)).filter(Boolean).join("\n\n");
|
|
4406
|
+
}
|
|
4407
|
+
function renderBlock2(node, options) {
|
|
4408
|
+
if (node.type === "paragraph") return renderInline2(node.children);
|
|
4409
|
+
if (node.type === "heading") return `${"#".repeat(node.depth)} ${renderInline2(node.children)}`;
|
|
4410
|
+
if (node.type === "thematicBreak") return "---";
|
|
4411
|
+
if (node.type === "html") return node.value;
|
|
4412
|
+
if (node.type === "code") {
|
|
4413
|
+
const marker = options.fence ?? "`";
|
|
4414
|
+
const length = Math.max(3, longestRun2(node.code, marker) + 1);
|
|
4415
|
+
const fence = marker.repeat(length);
|
|
4416
|
+
const meta = node.meta === node.lang ? void 0 : node.meta;
|
|
4417
|
+
const info = [node.lang, meta].filter(Boolean).join(" ");
|
|
4418
|
+
return `${fence}${info}
|
|
4419
|
+
${node.code.replace(/\n$/, "")}
|
|
4420
|
+
${fence}`;
|
|
4421
|
+
}
|
|
4422
|
+
if (node.type === "blockquote") return prefixLines2(renderBlocks(node.children, options), "> ");
|
|
4423
|
+
if (node.type === "list") {
|
|
4424
|
+
return node.children.map((item, index) => renderListItem(item, node.ordered ? `${(node.start ?? 1) + index}.` : options.bullet ?? "-", options)).join("\n");
|
|
4425
|
+
}
|
|
4426
|
+
if (node.type === "table") return renderTable2(node.children, node.align);
|
|
4427
|
+
if (node.type === "customBlock") {
|
|
4428
|
+
const custom = options.customBlocks?.[node.name];
|
|
4429
|
+
if (custom) return custom(node, { render: (children2) => renderBlocks(children2, options) });
|
|
4430
|
+
const args = node.args?.length ? ` ${node.args.join(" ")}` : "";
|
|
4431
|
+
const props = node.props && Object.keys(node.props).length ? `
|
|
4432
|
+
${renderYaml(node.props)}
|
|
4433
|
+
::` : "";
|
|
4434
|
+
const children = node.children?.length ? `
|
|
4435
|
+
${renderBlocks(node.children, options)}` : "";
|
|
4436
|
+
return `::${node.name}${args}${props}${children}
|
|
4437
|
+
::`;
|
|
4438
|
+
}
|
|
4439
|
+
return "";
|
|
4440
|
+
}
|
|
4441
|
+
function renderInline2(nodes) {
|
|
4442
|
+
return nodes.map((node) => {
|
|
4443
|
+
if (node.type === "text") return escapeText(node.value);
|
|
4444
|
+
if (node.type === "lineBreak") return node.hard ? " \n" : "\n";
|
|
4445
|
+
if (node.type === "strong") return `**${renderInline2(node.children)}**`;
|
|
4446
|
+
if (node.type === "emphasis") return `*${renderInline2(node.children)}*`;
|
|
4447
|
+
if (node.type === "delete") return `~~${renderInline2(node.children)}~~`;
|
|
4448
|
+
if (node.type === "inlineCode") return renderCodeSpan(node.code);
|
|
4449
|
+
if (node.type === "link") return `[${renderInline2(node.children)}](${escapeDestination(node.href)}${node.title ? ` "${node.title.replaceAll('"', '\\"')}"` : ""})`;
|
|
4450
|
+
if (node.type === "image") return `}${node.title ? ` "${node.title.replaceAll('"', '\\"')}"` : ""})`;
|
|
4451
|
+
if (node.type === "inlineHtml") return node.value;
|
|
4452
|
+
return "";
|
|
4453
|
+
}).join("");
|
|
4454
|
+
}
|
|
4455
|
+
function renderListItem(item, marker, options) {
|
|
4456
|
+
const checkbox = item.checked === void 0 ? "" : `[${item.checked ? "x" : " "}] `;
|
|
4457
|
+
const body = renderBlocks(item.children, options);
|
|
4458
|
+
const lines = body.split("\n");
|
|
4459
|
+
return `${marker} ${checkbox}${lines[0] ?? ""}${lines.slice(1).map((line) => `
|
|
4460
|
+
${" ".repeat(marker.length + 1)}${line}`).join("")}`;
|
|
4461
|
+
}
|
|
4462
|
+
function renderTable2(rows, align = []) {
|
|
4463
|
+
if (!rows.length) return "";
|
|
4464
|
+
const width = Math.max(...rows.map((row) => row.children.length));
|
|
4465
|
+
const line = (cells) => `| ${Array.from({ length: width }, (_, index) => escapeTable(renderInline2(cells[index]?.children ?? []))).join(" | ")} |`;
|
|
4466
|
+
const divider = `| ${Array.from({ length: width }, (_, index) => align[index] === "center" ? ":---:" : align[index] === "right" ? "---:" : align[index] === "left" ? ":---" : "---").join(" | ")} |`;
|
|
4467
|
+
return [line(rows[0].children), divider, ...rows.slice(1).map((row) => line(row.children))].join("\n");
|
|
4468
|
+
}
|
|
4469
|
+
function renderYaml(value) {
|
|
4470
|
+
return Object.entries(value).flatMap(([key, item]) => {
|
|
4471
|
+
if (Array.isArray(item)) return [`${key}:`, ...item.map((entry) => ` - ${yamlScalar(entry)}`)];
|
|
4472
|
+
return [`${key}: ${yamlScalar(item)}`];
|
|
4473
|
+
}).join("\n");
|
|
4474
|
+
}
|
|
4475
|
+
function yamlScalar(value) {
|
|
4476
|
+
if (value === null) return "null";
|
|
4477
|
+
if (typeof value === "boolean" || typeof value === "number") return String(value);
|
|
4478
|
+
const text = String(value);
|
|
4479
|
+
return text === "" || /^[-?:,[\]{}#&*!|>'"%@`]|[:#]\s|\s$|^(true|false|null|-?\d+(\.\d+)?)$/i.test(text) ? JSON.stringify(text) : text;
|
|
4480
|
+
}
|
|
4481
|
+
function renderCodeSpan(code) {
|
|
4482
|
+
const fence = "`".repeat(Math.max(1, longestRun2(code, "`") + 1));
|
|
4483
|
+
const padding = code.startsWith("`") || code.endsWith("`") ? " " : "";
|
|
4484
|
+
return `${fence}${padding}${code}${padding}${fence}`;
|
|
4485
|
+
}
|
|
4486
|
+
function longestRun2(value, character) {
|
|
4487
|
+
return Math.max(0, ...Array.from(value.matchAll(new RegExp(`\\${character}+`, "g")), (match) => match[0].length));
|
|
4488
|
+
}
|
|
4489
|
+
function prefixLines2(value, prefix) {
|
|
4490
|
+
return value.split("\n").map((line) => line ? `${prefix}${line}` : prefix.trimEnd()).join("\n");
|
|
4491
|
+
}
|
|
4492
|
+
function escapeText(value) {
|
|
4493
|
+
return value.replace(/([\\`*_[\]<>])/g, "\\$1");
|
|
4494
|
+
}
|
|
4495
|
+
function escapeLabel(value) {
|
|
4496
|
+
return value.replace(/([\\\]])/g, "\\$1");
|
|
4497
|
+
}
|
|
4498
|
+
function escapeDestination(value) {
|
|
4499
|
+
return value.replaceAll(" ", "%20").replaceAll(")", "%29");
|
|
4500
|
+
}
|
|
4501
|
+
function escapeTable(value) {
|
|
4502
|
+
return value.replaceAll("|", "\\|").replaceAll("\n", "<br>");
|
|
4503
|
+
}
|
|
4504
|
+
|
|
4505
|
+
// src/document.ts
|
|
4506
|
+
var NIZEL_DOCUMENT_SCHEMA = "nizel/document";
|
|
4507
|
+
var NIZEL_DOCUMENT_VERSION = 1;
|
|
4508
|
+
function createNizelDocument(value, meta) {
|
|
4509
|
+
const ast = isRoot(value) ? value : value.ast;
|
|
4510
|
+
const documentMeta = (isRoot(value) ? meta : value.meta) ?? {};
|
|
4511
|
+
assertJsonValue(ast, "ast");
|
|
4512
|
+
assertJsonValue(documentMeta, "meta");
|
|
4513
|
+
return { schema: NIZEL_DOCUMENT_SCHEMA, version: NIZEL_DOCUMENT_VERSION, ast, meta: documentMeta };
|
|
4514
|
+
}
|
|
4515
|
+
function serializeNizelDocument(document2, space) {
|
|
4516
|
+
assertNizelDocument(document2);
|
|
4517
|
+
return JSON.stringify(document2, void 0, space);
|
|
4518
|
+
}
|
|
4519
|
+
function deserializeNizelDocument(source) {
|
|
4520
|
+
let value;
|
|
4521
|
+
try {
|
|
4522
|
+
value = JSON.parse(source);
|
|
4523
|
+
} catch (error) {
|
|
4524
|
+
throw new TypeError(`Invalid Nizel document JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
4525
|
+
}
|
|
4526
|
+
assertNizelDocument(value);
|
|
4527
|
+
return value;
|
|
4528
|
+
}
|
|
4529
|
+
function assertNizelDocument(value) {
|
|
4530
|
+
if (!value || typeof value !== "object") throw new TypeError("Nizel document must be an object.");
|
|
4531
|
+
const candidate = value;
|
|
4532
|
+
if (candidate.schema !== NIZEL_DOCUMENT_SCHEMA) throw new TypeError("Unsupported Nizel document schema.");
|
|
4533
|
+
if (candidate.version !== NIZEL_DOCUMENT_VERSION) throw new TypeError(`Unsupported Nizel document version: ${String(candidate.version)}.`);
|
|
4534
|
+
if (!isRoot(candidate.ast)) throw new TypeError("Nizel document ast must be a root node.");
|
|
4535
|
+
if (!candidate.meta || typeof candidate.meta !== "object" || Array.isArray(candidate.meta)) {
|
|
4536
|
+
throw new TypeError("Nizel document meta must be an object.");
|
|
4537
|
+
}
|
|
4538
|
+
assertJsonValue(candidate, "document");
|
|
4539
|
+
}
|
|
4540
|
+
function isRoot(value) {
|
|
4541
|
+
return Boolean(value && typeof value === "object" && value.type === "root" && Array.isArray(value.children));
|
|
4542
|
+
}
|
|
4543
|
+
function assertJsonValue(value, path, seen = /* @__PURE__ */ new Set()) {
|
|
4544
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return;
|
|
4545
|
+
if (typeof value === "number" && Number.isFinite(value)) return;
|
|
4546
|
+
if (typeof value !== "object") throw new TypeError(`Nizel ${path} is not JSON-serializable.`);
|
|
4547
|
+
if (seen.has(value)) throw new TypeError(`Nizel ${path} contains a circular reference.`);
|
|
4548
|
+
seen.add(value);
|
|
4549
|
+
for (const [key, child] of Object.entries(value)) assertJsonValue(child, `${path}.${key}`, seen);
|
|
4550
|
+
seen.delete(value);
|
|
4551
|
+
}
|
|
4552
|
+
|
|
4379
4553
|
// src/template.ts
|
|
4380
4554
|
var renderTemplate = (markdown, data, options) => {
|
|
4381
4555
|
const filters = options.filters ?? {};
|
|
@@ -4511,6 +4685,11 @@ ${body}
|
|
|
4511
4685
|
unwrapStandaloneImages: resolved.unwrapStandaloneImages === true
|
|
4512
4686
|
});
|
|
4513
4687
|
};
|
|
4688
|
+
nizel.markdown = (value, markdownOptions) => renderMarkdown(value, markdownOptions);
|
|
4689
|
+
nizel.document = async (markdown, runtimeOptions) => {
|
|
4690
|
+
const result = await process(markdown, runtimeOptions);
|
|
4691
|
+
return createNizelDocument(result);
|
|
4692
|
+
};
|
|
4514
4693
|
return nizel;
|
|
4515
4694
|
function prepareMarkdown(markdown, runtimeOptions = {}) {
|
|
4516
4695
|
const preparedMeta = prepareMeta(markdown, runtimeOptions);
|
|
@@ -4689,6 +4868,137 @@ ${body}
|
|
|
4689
4868
|
return block;
|
|
4690
4869
|
};
|
|
4691
4870
|
|
|
4871
|
+
// src/streaming.ts
|
|
4872
|
+
function completeStreamingMarkdown(source, options = {}) {
|
|
4873
|
+
if (!source) return source;
|
|
4874
|
+
let completed = source;
|
|
4875
|
+
if (options.frontmatter !== false && completed.startsWith("---\n") && !/^---\n[\s\S]*?\n---(?:\n|$)/.test(completed)) {
|
|
4876
|
+
return `${completed.replace(/\n*$/, "")}
|
|
4877
|
+
---`;
|
|
4878
|
+
}
|
|
4879
|
+
const openFence = findOpenCodeFence(completed);
|
|
4880
|
+
if (openFence) return `${completed.replace(/\n*$/, "")}
|
|
4881
|
+
${openFence}`;
|
|
4882
|
+
if (options.customBlocks !== false) {
|
|
4883
|
+
const customClosers = openCustomBlockClosers(completed);
|
|
4884
|
+
if (customClosers.length) completed = `${completed.replace(/\n*$/, "")}
|
|
4885
|
+
${customClosers.join("\n")}`;
|
|
4886
|
+
}
|
|
4887
|
+
completed += inlineClosers(completed);
|
|
4888
|
+
return completed;
|
|
4889
|
+
}
|
|
4890
|
+
function findOpenCodeFence(source) {
|
|
4891
|
+
let open;
|
|
4892
|
+
for (const line of source.split("\n")) {
|
|
4893
|
+
const match = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
|
|
4894
|
+
if (!match) continue;
|
|
4895
|
+
const marker = match[1][0];
|
|
4896
|
+
if (!open) open = { marker, length: match[1].length };
|
|
4897
|
+
else if (marker === open.marker && match[1].length >= open.length && match[2].trim() === "") open = void 0;
|
|
4898
|
+
}
|
|
4899
|
+
return open?.marker.repeat(open.length);
|
|
4900
|
+
}
|
|
4901
|
+
function openCustomBlockClosers(source) {
|
|
4902
|
+
const stack = [];
|
|
4903
|
+
for (const line of source.split("\n")) {
|
|
4904
|
+
const match = /^(:{2,})(?:([A-Za-z][\w-]*)(?:\s|\{|$))?/.exec(line.trim());
|
|
4905
|
+
if (!match) continue;
|
|
4906
|
+
if (match[2]) stack.push(match[1]);
|
|
4907
|
+
else if (stack.length && match[1].length >= stack.at(-1).length) stack.pop();
|
|
4908
|
+
}
|
|
4909
|
+
return stack.reverse();
|
|
4910
|
+
}
|
|
4911
|
+
function inlineClosers(source) {
|
|
4912
|
+
const lastLine = source.split("\n").at(-1) ?? "";
|
|
4913
|
+
if (/^ {0,3}(?:#{1,6}|>|[-+*]\s|\d+\.\s)/.test(lastLine)) return "";
|
|
4914
|
+
const escaped = (index) => index > 0 && lastLine[index - 1] === "\\";
|
|
4915
|
+
let ticks = 0;
|
|
4916
|
+
let strong = 0;
|
|
4917
|
+
let strike = 0;
|
|
4918
|
+
for (let index = 0; index < lastLine.length; index += 1) {
|
|
4919
|
+
if (escaped(index)) continue;
|
|
4920
|
+
if (lastLine[index] === "`") ticks ^= 1;
|
|
4921
|
+
if (!ticks && lastLine.slice(index, index + 2) === "**") {
|
|
4922
|
+
strong ^= 1;
|
|
4923
|
+
index += 1;
|
|
4924
|
+
} else if (!ticks && lastLine.slice(index, index + 2) === "~~") {
|
|
4925
|
+
strike ^= 1;
|
|
4926
|
+
index += 1;
|
|
4927
|
+
}
|
|
4928
|
+
}
|
|
4929
|
+
const link = !ticks && /!?\[[^\]]*\]\([^)]*$/.test(lastLine);
|
|
4930
|
+
return `${link ? ")" : ""}${strike ? "~~" : ""}${strong ? "**" : ""}${ticks ? "`" : ""}`;
|
|
4931
|
+
}
|
|
4932
|
+
|
|
4933
|
+
// src/visit.ts
|
|
4934
|
+
function visitNizel(node, visitor) {
|
|
4935
|
+
let stopped = false;
|
|
4936
|
+
const walk = (current, parent) => {
|
|
4937
|
+
if (stopped) return;
|
|
4938
|
+
const control = visitor(current, parent);
|
|
4939
|
+
if (control === "stop") {
|
|
4940
|
+
stopped = true;
|
|
4941
|
+
return;
|
|
4942
|
+
}
|
|
4943
|
+
if (control === "skip") return;
|
|
4944
|
+
const children = "children" in current && Array.isArray(current.children) ? current.children : [];
|
|
4945
|
+
for (const child of children) walk(child, current);
|
|
4946
|
+
};
|
|
4947
|
+
walk(node);
|
|
4948
|
+
}
|
|
4949
|
+
|
|
4950
|
+
// src/renderer.ts
|
|
4951
|
+
function renderNizelTree(value, options) {
|
|
4952
|
+
const root = "schema" in value ? value.ast : value;
|
|
4953
|
+
const lastText = options.streaming && options.caret ? findLastText(root) : void 0;
|
|
4954
|
+
const render = (node) => {
|
|
4955
|
+
if (node.type === "text") {
|
|
4956
|
+
const output = options.adapter.text(node.value, node);
|
|
4957
|
+
if (node !== lastText || !options.adapter.caret) return output;
|
|
4958
|
+
return options.adapter.element("fragment", {}, [output, options.adapter.caret(caretProps(options.caret))], node);
|
|
4959
|
+
}
|
|
4960
|
+
const descriptor = describeNode(node);
|
|
4961
|
+
const name = options.components?.[descriptor.name] ?? descriptor.name;
|
|
4962
|
+
const children = descriptor.children.map(render);
|
|
4963
|
+
return options.adapter.element(name, descriptor.props, children, node);
|
|
4964
|
+
};
|
|
4965
|
+
return render(root);
|
|
4966
|
+
}
|
|
4967
|
+
function describeNode(node) {
|
|
4968
|
+
if (node.type === "root") return { name: "root", props: {}, children: node.children };
|
|
4969
|
+
if (node.type === "paragraph") return { name: "p", props: {}, children: node.children };
|
|
4970
|
+
if (node.type === "heading") return { name: `h${node.depth}`, props: { id: node.id }, children: node.children };
|
|
4971
|
+
if (node.type === "emphasis") return { name: "em", props: {}, children: node.children };
|
|
4972
|
+
if (node.type === "strong") return { name: "strong", props: {}, children: node.children };
|
|
4973
|
+
if (node.type === "delete") return { name: "del", props: {}, children: node.children };
|
|
4974
|
+
if (node.type === "lineBreak") return { name: "br", props: { hard: node.hard }, children: [] };
|
|
4975
|
+
if (node.type === "inlineCode") return { name: "code", props: { inline: true, code: node.code }, children: [] };
|
|
4976
|
+
if (node.type === "code") return { name: "pre", props: { code: node.code, language: node.lang, meta: node.meta, filename: node.filename, highlightLines: node.highlightLines }, children: [] };
|
|
4977
|
+
if (node.type === "link") return { name: "a", props: { href: node.href, title: node.title, external: node.external }, children: node.children };
|
|
4978
|
+
if (node.type === "image") return { name: "img", props: { src: node.src, alt: node.alt, title: node.title }, children: [] };
|
|
4979
|
+
if (node.type === "inlineHtml" || node.type === "html") return { name: "rawHtml", props: { value: node.value }, children: [] };
|
|
4980
|
+
if (node.type === "list") return { name: node.ordered ? "ol" : "ul", props: { start: node.start, tight: node.tight }, children: node.children };
|
|
4981
|
+
if (node.type === "listItem") return { name: "li", props: { checked: node.checked }, children: node.children };
|
|
4982
|
+
if (node.type === "blockquote") return { name: "blockquote", props: {}, children: node.children };
|
|
4983
|
+
if (node.type === "thematicBreak") return { name: "hr", props: {}, children: [] };
|
|
4984
|
+
if (node.type === "table") return { name: "table", props: { align: node.align }, children: node.children };
|
|
4985
|
+
if (node.type === "tableRow") return { name: "tr", props: {}, children: node.children };
|
|
4986
|
+
if (node.type === "tableCell") return { name: "td", props: {}, children: node.children };
|
|
4987
|
+
return { name: node.name, props: { args: node.args, ...node.props, value: node.value }, children: node.children ?? [] };
|
|
4988
|
+
}
|
|
4989
|
+
function findLastText(root) {
|
|
4990
|
+
let found;
|
|
4991
|
+
const walk = (node) => {
|
|
4992
|
+
if (node.type === "text") found = node;
|
|
4993
|
+
if ("children" in node && Array.isArray(node.children)) node.children.forEach((child) => walk(child));
|
|
4994
|
+
};
|
|
4995
|
+
walk(root);
|
|
4996
|
+
return found;
|
|
4997
|
+
}
|
|
4998
|
+
function caretProps(value) {
|
|
4999
|
+
return value && typeof value === "object" ? value : {};
|
|
5000
|
+
}
|
|
5001
|
+
|
|
4692
5002
|
// src/browser.ts
|
|
4693
5003
|
function useBrowserNizel(options = {}) {
|
|
4694
5004
|
const processor = useNizel(options);
|