@boceto/remark 0.1.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/LICENSE +21 -0
- package/dist/index.cjs +68 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +42 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Maravilla Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var unistUtilVisit = require('unist-util-visit');
|
|
4
|
+
var core = require('@boceto/core');
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
function remarkBoceto(options = {}) {
|
|
8
|
+
const mode = options.mode ?? "wc";
|
|
9
|
+
const tag = options.tag ?? "boceto-view";
|
|
10
|
+
const extraAttrs = options.attributes ?? {};
|
|
11
|
+
const width = options.width ?? 860;
|
|
12
|
+
const height = options.height ?? 600;
|
|
13
|
+
const svgRenderer = mode === "svg" ? new core.SvgRenderer() : null;
|
|
14
|
+
if (mode !== "svg") {
|
|
15
|
+
return (tree) => {
|
|
16
|
+
unistUtilVisit.visit(tree, "code", (node, index, parent) => {
|
|
17
|
+
if (!parent || index == null) return;
|
|
18
|
+
if ((node.lang ?? "") !== "boceto") return;
|
|
19
|
+
const source = node.value;
|
|
20
|
+
const meta = node.meta ?? null;
|
|
21
|
+
let html;
|
|
22
|
+
if (options.render) {
|
|
23
|
+
html = options.render(source, { lang: "boceto", meta });
|
|
24
|
+
} else {
|
|
25
|
+
const attrs = { ...extraAttrs, code: source };
|
|
26
|
+
if (meta) attrs["data-page"] = meta.trim();
|
|
27
|
+
html = renderTag(tag, attrs);
|
|
28
|
+
}
|
|
29
|
+
parent.children[index] = { type: "html", value: html };
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return async (tree) => {
|
|
34
|
+
const targets = [];
|
|
35
|
+
unistUtilVisit.visit(tree, "code", (node, index, parent) => {
|
|
36
|
+
if (!parent || index == null) return;
|
|
37
|
+
if ((node.lang ?? "") !== "boceto") return;
|
|
38
|
+
targets.push({ node, index, parent });
|
|
39
|
+
});
|
|
40
|
+
if (targets.length === 0) return;
|
|
41
|
+
await core.initYoga();
|
|
42
|
+
for (const { node, index, parent } of targets) {
|
|
43
|
+
const source = node.value;
|
|
44
|
+
const meta = node.meta ?? null;
|
|
45
|
+
let html;
|
|
46
|
+
if (options.render) {
|
|
47
|
+
html = options.render(source, { lang: "boceto", meta });
|
|
48
|
+
} else {
|
|
49
|
+
const wrapped = "```boceto" + (meta ? ":" + meta : "") + "\n" + source + "\n```";
|
|
50
|
+
const doc = core.applyFlexLayout(core.parse(wrapped));
|
|
51
|
+
html = svgRenderer.renderToString(doc, { width, height });
|
|
52
|
+
}
|
|
53
|
+
parent.children[index] = { type: "html", value: html };
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function renderTag(tag, attrs) {
|
|
58
|
+
const parts = [tag];
|
|
59
|
+
for (const [k, v] of Object.entries(attrs)) parts.push(`${k}="${escapeAttr(v)}"`);
|
|
60
|
+
return `<${parts.join(" ")}></${tag}>`;
|
|
61
|
+
}
|
|
62
|
+
function escapeAttr(s) {
|
|
63
|
+
return s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = remarkBoceto;
|
|
67
|
+
//# sourceMappingURL=index.cjs.map
|
|
68
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["SvgRenderer","visit","initYoga","applyFlexLayout","parse"],"mappings":";;;;;;AAwCe,SAAR,YAAA,CACL,OAAA,GAA+B,EAAC,EACM;AACtC,EAAA,MAAM,IAAA,GAAO,QAAQ,IAAA,IAAQ,IAAA;AAC7B,EAAA,MAAM,GAAA,GAAM,QAAQ,GAAA,IAAO,aAAA;AAC3B,EAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,UAAA,IAAc,EAAC;AAC1C,EAAA,MAAM,KAAA,GAAQ,QAAQ,KAAA,IAAS,GAAA;AAC/B,EAAA,MAAM,MAAA,GAAS,QAAQ,MAAA,IAAU,GAAA;AACjC,EAAA,MAAM,WAAA,GAAc,IAAA,KAAS,KAAA,GAAQ,IAAIA,kBAAY,GAAI,IAAA;AAEzD,EAAA,IAAI,SAAS,KAAA,E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,EAAA,OAAO,IAAI,KAAA,CAAM,IAAA,CAAK,GAAG,CAAC,MAAM,GAAG,CAAA,CAAA,CAAA;AACrC;AAEA,SAAS,WAAW,CAAA,EAAmB;AACrC,EAAA,OAAO,CAAA,CAAE,OAAA,CAAQ,IAAA,EAAM,OAAO,EAAE,OAAA,CAAQ,IAAA,EAAM,QAAQ,CAAA,CAAE,QAAQ,IAAA,EAAM,MAAM,CAAA,CAAE,OAAA,CAAQ,MAAM,MAAM,CAAA;AACpG","file":"index.cjs","sourcesContent":["import type { Code, Html, Root } from 'mdast'\nimport { visit } from 'unist-util-visit'\nimport { applyFlexLayout, initYoga, parse, SvgRenderer } from '@boceto/core'\n\nexport interface RemarkBocetoOptions {\n /**\n * Output mode.\n * - `'wc'` (default): emit `<boceto-view>` custom element. Requires the WC\n * runtime in the browser.\n * - `'svg'`: parse the source and inline a complete `<svg>` document.\n * Renders with **zero JS at runtime** — works in GitHub READMEs, RSS\n * readers, and SSGs.\n */\n mode?: 'wc' | 'svg'\n /** Tag to emit in `'wc'` mode. Default `'boceto-view'`. Use `'boceto-edit'` for editable blocks. */\n tag?: string\n /** Extra static attributes to attach to every emitted element (`'wc'` mode only). */\n attributes?: Record<string, string>\n /** SVG render dimensions (`'svg'` mode only). Defaults: 860 × 600. */\n width?: number\n height?: number\n /**\n * Receive the boceto source and return arbitrary HTML. If provided, all\n * other options are ignored. Use this if you want a custom wrapper or to\n * swap renderers entirely.\n */\n render?: (source: string, info: { lang: string; meta: string | null }) => string\n}\n\nexport type Plugin = () => (tree: Root) => void | Promise<void>\n\n/**\n * remark plugin that transforms `code` nodes whose language is `boceto`\n * into `html` nodes.\n *\n * - `mode: 'wc'` (default): emits `<boceto-view code=\"…\">`.\n * - `mode: 'svg'`: emits a full `<svg>…</svg>` rendered server-side. The\n * transformer is async in this mode so it can `await initYoga()` once\n * before resolving FlexContainer layout.\n */\nexport default function remarkBoceto(\n options: RemarkBocetoOptions = {},\n): (tree: Root) => void | Promise<void> {\n const mode = options.mode ?? 'wc'\n const tag = options.tag ?? 'boceto-view'\n const extraAttrs = options.attributes ?? {}\n const width = options.width ?? 860\n const height = options.height ?? 600\n const svgRenderer = mode === 'svg' ? new SvgRenderer() : null\n\n if (mode !== 'svg') {\n // Synchronous transformer for WC mode — no layout to resolve.\n return (tree) => {\n visit(tree, 'code', (node: Code, index, parent) => {\n if (!parent || index == null) return\n if ((node.lang ?? '') !== 'boceto') return\n const source = node.value\n const meta = node.meta ?? null\n let html: string\n if (options.render) {\n html = options.render(source, { lang: 'boceto', meta })\n } else {\n const attrs: Record<string, string> = { ...extraAttrs, code: source }\n if (meta) attrs['data-page'] = meta.trim()\n html = renderTag(tag, attrs)\n }\n parent.children[index] = { type: 'html', value: html } as Html\n })\n }\n }\n\n // SVG mode: collect every boceto block, ensure Yoga is loaded once, then\n // parse + lay out + render each in place.\n return async (tree) => {\n const targets: Array<{ node: Code; index: number; parent: Root | Code['data'] }> = []\n visit(tree, 'code', (node: Code, index, parent) => {\n if (!parent || index == null) return\n if ((node.lang ?? '') !== 'boceto') return\n targets.push({ node, index, parent: parent as Root })\n })\n if (targets.length === 0) return\n await initYoga()\n for (const { node, index, parent } of targets) {\n const source = node.value\n const meta = node.meta ?? null\n let html: string\n if (options.render) {\n html = options.render(source, { lang: 'boceto', meta })\n } else {\n const wrapped = '```boceto' + (meta ? ':' + meta : '') + '\\n' + source + '\\n```'\n const doc = applyFlexLayout(parse(wrapped))\n html = svgRenderer!.renderToString(doc, { width, height })\n }\n ;(parent as Root).children[index] = { type: 'html', value: html } as Html\n }\n }\n}\n\nfunction renderTag(tag: string, attrs: Record<string, string>): string {\n const parts = [tag]\n for (const [k, v] of Object.entries(attrs)) parts.push(`${k}=\"${escapeAttr(v)}\"`)\n return `<${parts.join(' ')}></${tag}>`\n}\n\nfunction escapeAttr(s: string): string {\n return s.replace(/&/g, '&').replace(/\"/g, '"').replace(/</g, '<').replace(/>/g, '>')\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Root } from 'mdast';
|
|
2
|
+
|
|
3
|
+
interface RemarkBocetoOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Output mode.
|
|
6
|
+
* - `'wc'` (default): emit `<boceto-view>` custom element. Requires the WC
|
|
7
|
+
* runtime in the browser.
|
|
8
|
+
* - `'svg'`: parse the source and inline a complete `<svg>` document.
|
|
9
|
+
* Renders with **zero JS at runtime** — works in GitHub READMEs, RSS
|
|
10
|
+
* readers, and SSGs.
|
|
11
|
+
*/
|
|
12
|
+
mode?: 'wc' | 'svg';
|
|
13
|
+
/** Tag to emit in `'wc'` mode. Default `'boceto-view'`. Use `'boceto-edit'` for editable blocks. */
|
|
14
|
+
tag?: string;
|
|
15
|
+
/** Extra static attributes to attach to every emitted element (`'wc'` mode only). */
|
|
16
|
+
attributes?: Record<string, string>;
|
|
17
|
+
/** SVG render dimensions (`'svg'` mode only). Defaults: 860 × 600. */
|
|
18
|
+
width?: number;
|
|
19
|
+
height?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Receive the boceto source and return arbitrary HTML. If provided, all
|
|
22
|
+
* other options are ignored. Use this if you want a custom wrapper or to
|
|
23
|
+
* swap renderers entirely.
|
|
24
|
+
*/
|
|
25
|
+
render?: (source: string, info: {
|
|
26
|
+
lang: string;
|
|
27
|
+
meta: string | null;
|
|
28
|
+
}) => string;
|
|
29
|
+
}
|
|
30
|
+
type Plugin = () => (tree: Root) => void | Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* remark plugin that transforms `code` nodes whose language is `boceto`
|
|
33
|
+
* into `html` nodes.
|
|
34
|
+
*
|
|
35
|
+
* - `mode: 'wc'` (default): emits `<boceto-view code="…">`.
|
|
36
|
+
* - `mode: 'svg'`: emits a full `<svg>…</svg>` rendered server-side. The
|
|
37
|
+
* transformer is async in this mode so it can `await initYoga()` once
|
|
38
|
+
* before resolving FlexContainer layout.
|
|
39
|
+
*/
|
|
40
|
+
declare function remarkBoceto(options?: RemarkBocetoOptions): (tree: Root) => void | Promise<void>;
|
|
41
|
+
|
|
42
|
+
export { type Plugin, type RemarkBocetoOptions, remarkBoceto as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Root } from 'mdast';
|
|
2
|
+
|
|
3
|
+
interface RemarkBocetoOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Output mode.
|
|
6
|
+
* - `'wc'` (default): emit `<boceto-view>` custom element. Requires the WC
|
|
7
|
+
* runtime in the browser.
|
|
8
|
+
* - `'svg'`: parse the source and inline a complete `<svg>` document.
|
|
9
|
+
* Renders with **zero JS at runtime** — works in GitHub READMEs, RSS
|
|
10
|
+
* readers, and SSGs.
|
|
11
|
+
*/
|
|
12
|
+
mode?: 'wc' | 'svg';
|
|
13
|
+
/** Tag to emit in `'wc'` mode. Default `'boceto-view'`. Use `'boceto-edit'` for editable blocks. */
|
|
14
|
+
tag?: string;
|
|
15
|
+
/** Extra static attributes to attach to every emitted element (`'wc'` mode only). */
|
|
16
|
+
attributes?: Record<string, string>;
|
|
17
|
+
/** SVG render dimensions (`'svg'` mode only). Defaults: 860 × 600. */
|
|
18
|
+
width?: number;
|
|
19
|
+
height?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Receive the boceto source and return arbitrary HTML. If provided, all
|
|
22
|
+
* other options are ignored. Use this if you want a custom wrapper or to
|
|
23
|
+
* swap renderers entirely.
|
|
24
|
+
*/
|
|
25
|
+
render?: (source: string, info: {
|
|
26
|
+
lang: string;
|
|
27
|
+
meta: string | null;
|
|
28
|
+
}) => string;
|
|
29
|
+
}
|
|
30
|
+
type Plugin = () => (tree: Root) => void | Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* remark plugin that transforms `code` nodes whose language is `boceto`
|
|
33
|
+
* into `html` nodes.
|
|
34
|
+
*
|
|
35
|
+
* - `mode: 'wc'` (default): emits `<boceto-view code="…">`.
|
|
36
|
+
* - `mode: 'svg'`: emits a full `<svg>…</svg>` rendered server-side. The
|
|
37
|
+
* transformer is async in this mode so it can `await initYoga()` once
|
|
38
|
+
* before resolving FlexContainer layout.
|
|
39
|
+
*/
|
|
40
|
+
declare function remarkBoceto(options?: RemarkBocetoOptions): (tree: Root) => void | Promise<void>;
|
|
41
|
+
|
|
42
|
+
export { type Plugin, type RemarkBocetoOptions, remarkBoceto as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { visit } from 'unist-util-visit';
|
|
2
|
+
import { SvgRenderer, initYoga, applyFlexLayout, parse } from '@boceto/core';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
function remarkBoceto(options = {}) {
|
|
6
|
+
const mode = options.mode ?? "wc";
|
|
7
|
+
const tag = options.tag ?? "boceto-view";
|
|
8
|
+
const extraAttrs = options.attributes ?? {};
|
|
9
|
+
const width = options.width ?? 860;
|
|
10
|
+
const height = options.height ?? 600;
|
|
11
|
+
const svgRenderer = mode === "svg" ? new SvgRenderer() : null;
|
|
12
|
+
if (mode !== "svg") {
|
|
13
|
+
return (tree) => {
|
|
14
|
+
visit(tree, "code", (node, index, parent) => {
|
|
15
|
+
if (!parent || index == null) return;
|
|
16
|
+
if ((node.lang ?? "") !== "boceto") return;
|
|
17
|
+
const source = node.value;
|
|
18
|
+
const meta = node.meta ?? null;
|
|
19
|
+
let html;
|
|
20
|
+
if (options.render) {
|
|
21
|
+
html = options.render(source, { lang: "boceto", meta });
|
|
22
|
+
} else {
|
|
23
|
+
const attrs = { ...extraAttrs, code: source };
|
|
24
|
+
if (meta) attrs["data-page"] = meta.trim();
|
|
25
|
+
html = renderTag(tag, attrs);
|
|
26
|
+
}
|
|
27
|
+
parent.children[index] = { type: "html", value: html };
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return async (tree) => {
|
|
32
|
+
const targets = [];
|
|
33
|
+
visit(tree, "code", (node, index, parent) => {
|
|
34
|
+
if (!parent || index == null) return;
|
|
35
|
+
if ((node.lang ?? "") !== "boceto") return;
|
|
36
|
+
targets.push({ node, index, parent });
|
|
37
|
+
});
|
|
38
|
+
if (targets.length === 0) return;
|
|
39
|
+
await initYoga();
|
|
40
|
+
for (const { node, index, parent } of targets) {
|
|
41
|
+
const source = node.value;
|
|
42
|
+
const meta = node.meta ?? null;
|
|
43
|
+
let html;
|
|
44
|
+
if (options.render) {
|
|
45
|
+
html = options.render(source, { lang: "boceto", meta });
|
|
46
|
+
} else {
|
|
47
|
+
const wrapped = "```boceto" + (meta ? ":" + meta : "") + "\n" + source + "\n```";
|
|
48
|
+
const doc = applyFlexLayout(parse(wrapped));
|
|
49
|
+
html = svgRenderer.renderToString(doc, { width, height });
|
|
50
|
+
}
|
|
51
|
+
parent.children[index] = { type: "html", value: html };
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function renderTag(tag, attrs) {
|
|
56
|
+
const parts = [tag];
|
|
57
|
+
for (const [k, v] of Object.entries(attrs)) parts.push(`${k}="${escapeAttr(v)}"`);
|
|
58
|
+
return `<${parts.join(" ")}></${tag}>`;
|
|
59
|
+
}
|
|
60
|
+
function escapeAttr(s) {
|
|
61
|
+
return s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { remarkBoceto as default };
|
|
65
|
+
//# sourceMappingURL=index.js.map
|
|
66
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAwCe,SAAR,YAAA,CACL,OAAA,GAA+B,EAAC,EACM;AACtC,EAAA,MAAM,IAAA,GAAO,QAAQ,IAAA,IAAQ,IAAA;AAC7B,EAAA,MAAM,GAAA,GAAM,QAAQ,GAAA,IAAO,aAAA;AAC3B,EAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,UAAA,IAAc,EAAC;AAC1C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CAAA;AAClB,EAAA,KAAA,MAAW,CAAC,CAAA,EAAG,CAAC,CAAA,IAAK,MAAA,CAAO,QAAQ,KAAK,CAAA,EAAG,KAAA,CAAM,IAAA,CAAK,GAAG,CAAC,CAAA,EAAA,EAAK,UAAA,CAAW,CAAC,CAAC,CAAA,CAAA,CAAG,CAAA;AAChF,EAAA,OAAO,IAAI,KAAA,CAAM,IAAA,CAAK,GAAG,CAAC,MAAM,GAAG,CAAA,CAAA,CAAA;AACrC;AAEA,SAAS,WAAW,CAAA,EAAmB;AACrC,EAAA,OAAO,CAAA,CAAE,OAAA,CAAQ,IAAA,EAAM,OAAO,EAAE,OAAA,CAAQ,IAAA,EAAM,QAAQ,CAAA,CAAE,QAAQ,IAAA,EAAM,MAAM,CAAA,CAAE,OAAA,CAAQ,MAAM,MAAM,CAAA;AACpG","file":"index.js","sourcesContent":["import type { Code, Html, Root } from 'mdast'\nimport { visit } from 'unist-util-visit'\nimport { applyFlexLayout, initYoga, parse, SvgRenderer } from '@boceto/core'\n\nexport interface RemarkBocetoOptions {\n /**\n * Output mode.\n * - `'wc'` (default): emit `<boceto-view>` custom element. Requires the WC\n * runtime in the browser.\n * - `'svg'`: parse the source and inline a complete `<svg>` document.\n * Renders with **zero JS at runtime** — works in GitHub READMEs, RSS\n * readers, and SSGs.\n */\n mode?: 'wc' | 'svg'\n /** Tag to emit in `'wc'` mode. Default `'boceto-view'`. Use `'boceto-edit'` for editable blocks. */\n tag?: string\n /** Extra static attributes to attach to every emitted element (`'wc'` mode only). */\n attributes?: Record<string, string>\n /** SVG render dimensions (`'svg'` mode only). Defaults: 860 × 600. */\n width?: number\n height?: number\n /**\n * Receive the boceto source and return arbitrary HTML. If provided, all\n * other options are ignored. Use this if you want a custom wrapper or to\n * swap renderers entirely.\n */\n render?: (source: string, info: { lang: string; meta: string | null }) => string\n}\n\nexport type Plugin = () => (tree: Root) => void | Promise<void>\n\n/**\n * remark plugin that transforms `code` nodes whose language is `boceto`\n * into `html` nodes.\n *\n * - `mode: 'wc'` (default): emits `<boceto-view code=\"…\">`.\n * - `mode: 'svg'`: emits a full `<svg>…</svg>` rendered server-side. The\n * transformer is async in this mode so it can `await initYoga()` once\n * before resolving FlexContainer layout.\n */\nexport default function remarkBoceto(\n options: RemarkBocetoOptions = {},\n): (tree: Root) => void | Promise<void> {\n const mode = options.mode ?? 'wc'\n const tag = options.tag ?? 'boceto-view'\n const extraAttrs = options.attributes ?? {}\n const width = options.width ?? 860\n const height = options.height ?? 600\n const svgRenderer = mode === 'svg' ? new SvgRenderer() : null\n\n if (mode !== 'svg') {\n // Synchronous transformer for WC mode — no layout to resolve.\n return (tree) => {\n visit(tree, 'code', (node: Code, index, parent) => {\n if (!parent || index == null) return\n if ((node.lang ?? '') !== 'boceto') return\n const source = node.value\n const meta = node.meta ?? null\n let html: string\n if (options.render) {\n html = options.render(source, { lang: 'boceto', meta })\n } else {\n const attrs: Record<string, string> = { ...extraAttrs, code: source }\n if (meta) attrs['data-page'] = meta.trim()\n html = renderTag(tag, attrs)\n }\n parent.children[index] = { type: 'html', value: html } as Html\n })\n }\n }\n\n // SVG mode: collect every boceto block, ensure Yoga is loaded once, then\n // parse + lay out + render each in place.\n return async (tree) => {\n const targets: Array<{ node: Code; index: number; parent: Root | Code['data'] }> = []\n visit(tree, 'code', (node: Code, index, parent) => {\n if (!parent || index == null) return\n if ((node.lang ?? '') !== 'boceto') return\n targets.push({ node, index, parent: parent as Root })\n })\n if (targets.length === 0) return\n await initYoga()\n for (const { node, index, parent } of targets) {\n const source = node.value\n const meta = node.meta ?? null\n let html: string\n if (options.render) {\n html = options.render(source, { lang: 'boceto', meta })\n } else {\n const wrapped = '```boceto' + (meta ? ':' + meta : '') + '\\n' + source + '\\n```'\n const doc = applyFlexLayout(parse(wrapped))\n html = svgRenderer!.renderToString(doc, { width, height })\n }\n ;(parent as Root).children[index] = { type: 'html', value: html } as Html\n }\n }\n}\n\nfunction renderTag(tag: string, attrs: Record<string, string>): string {\n const parts = [tag]\n for (const [k, v] of Object.entries(attrs)) parts.push(`${k}=\"${escapeAttr(v)}\"`)\n return `<${parts.join(' ')}></${tag}>`\n}\n\nfunction escapeAttr(s: string): string {\n return s.replace(/&/g, '&').replace(/\"/g, '"').replace(/</g, '<').replace(/>/g, '>')\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@boceto/remark",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "remark plugin: render fenced ```boceto blocks as <boceto-view> custom elements.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Maravilla Labs",
|
|
7
|
+
"homepage": "https://maravilla-labs.github.io/boceto/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/maravilla-labs/boceto.git",
|
|
11
|
+
"directory": "packages/remark-boceto"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/maravilla-labs/boceto/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"boceto",
|
|
18
|
+
"wireframe",
|
|
19
|
+
"remark",
|
|
20
|
+
"unified",
|
|
21
|
+
"markdown",
|
|
22
|
+
"plugin"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"main": "./dist/index.cjs",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.js",
|
|
33
|
+
"require": "./dist/index.cjs"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"README.md"
|
|
39
|
+
],
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"unified": "^11.0.0"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"unist-util-visit": "^5.0.0",
|
|
45
|
+
"@boceto/core": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/mdast": "^4.0.4",
|
|
49
|
+
"remark": "^15.0.1",
|
|
50
|
+
"remark-html": "^16.0.1",
|
|
51
|
+
"tsup": "^8.3.0",
|
|
52
|
+
"typescript": "^5.6.3",
|
|
53
|
+
"unified": "^11.0.5",
|
|
54
|
+
"unist-util-visit": "^5.0.0",
|
|
55
|
+
"vitest": "^2.1.4"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsup",
|
|
62
|
+
"dev": "tsup --watch",
|
|
63
|
+
"test": "vitest run"
|
|
64
|
+
}
|
|
65
|
+
}
|