@kekonic/diagrams-markdown-it 1.0.0-rc.4
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/README.md +17 -0
- package/dist/index.d.mts +29 -0
- package/dist/index.mjs +70 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kekonic
|
|
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/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# `@kekonic/diagrams-markdown-it`
|
|
2
|
+
|
|
3
|
+
Render fenced `kdiagrams` blocks to static, accessible SVG in Markdown-it hosts.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import MarkdownIt from "markdown-it";
|
|
7
|
+
import { kdiagramMarkdownIt, renderKDiagramMarkdown } from "@kekonic/diagrams-markdown-it";
|
|
8
|
+
|
|
9
|
+
const md = new MarkdownIt();
|
|
10
|
+
kdiagramMarkdownIt(md);
|
|
11
|
+
const html = await renderKDiagramMarkdown(md, markdown);
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
KDiagram layout is asynchronous, so use `renderKDiagramMarkdown()` rather than calling
|
|
15
|
+
`md.render()` directly. Static SVG with resolved theme tokens is the default. Diagnostics are
|
|
16
|
+
available on `env.kdiagramDiagnostics` with both KDiagram source ranges and one-based Markdown
|
|
17
|
+
lines.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Diagnostic, RenderOptions } from "@kekonic/diagrams";
|
|
2
|
+
import MarkdownIt from "markdown-it";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
declare const RENDERED: unique symbol;
|
|
6
|
+
type KDiagramFenceDiagnostic = Diagnostic & {
|
|
7
|
+
markdownLine: number;
|
|
8
|
+
markdownEndLine: number;
|
|
9
|
+
};
|
|
10
|
+
type KDiagramMarkdownEnvironment = {
|
|
11
|
+
[RENDERED]?: Map<number, string>;
|
|
12
|
+
kdiagramDiagnostics?: KDiagramFenceDiagnostic[];
|
|
13
|
+
};
|
|
14
|
+
type KDiagramMarkdownItOptions = {
|
|
15
|
+
className?: string;
|
|
16
|
+
renderOptions?: RenderOptions;
|
|
17
|
+
onDiagnostic?: (diagnostic: KDiagramFenceDiagnostic) => void;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Register the synchronous fence rule used by Markdown-it. Call
|
|
21
|
+
* {@link renderKDiagramMarkdown} to perform KDiagram's asynchronous layout pass before rendering.
|
|
22
|
+
*/
|
|
23
|
+
declare function kdiagramMarkdownIt(md: MarkdownIt, options?: KDiagramMarkdownItOptions): void;
|
|
24
|
+
declare function renderKDiagramMarkdown(md: MarkdownIt, markdown: string, env?: KDiagramMarkdownEnvironment, options?: KDiagramMarkdownItOptions): Promise<string>;
|
|
25
|
+
declare function isKDiagramFence(info: string): boolean;
|
|
26
|
+
declare function encodeKDiagramFenceSource(source: string): string;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { KDiagramFenceDiagnostic, KDiagramMarkdownEnvironment, KDiagramMarkdownItOptions, encodeKDiagramFenceSource, isKDiagramFence, kdiagramMarkdownIt, renderKDiagramMarkdown };
|
|
29
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { escapeKDiagramAttribute, escapeKDiagramHtml, formatKDiagramBuildError, offsetKDiagramDiagnostic, renderKDiagramForBuild } from "@kekonic/diagrams-build";
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
const RENDERED = Symbol("kdiagram-rendered-fences");
|
|
4
|
+
const REGISTERED_OPTIONS = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
/**
|
|
6
|
+
* Register the synchronous fence rule used by Markdown-it. Call
|
|
7
|
+
* {@link renderKDiagramMarkdown} to perform KDiagram's asynchronous layout pass before rendering.
|
|
8
|
+
*/
|
|
9
|
+
function kdiagramMarkdownIt(md, options = {}) {
|
|
10
|
+
REGISTERED_OPTIONS.set(md, options);
|
|
11
|
+
const fallback = md.renderer.rules.fence;
|
|
12
|
+
md.renderer.rules.fence = (tokens, index, renderOptions, env, self) => {
|
|
13
|
+
if (fenceLanguage(tokens[index]) !== "kdiagram") return fallback ? fallback(tokens, index, renderOptions, env, self) : self.renderToken(tokens, index, renderOptions);
|
|
14
|
+
const rendered = env[RENDERED]?.get(index);
|
|
15
|
+
if (rendered) return rendered;
|
|
16
|
+
return `<pre class="kdiagram-error"><code>KDiagram fence was not prepared. Use renderKDiagramMarkdown().</code></pre>`;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
async function renderKDiagramMarkdown(md, markdown, env = {}, options = {}) {
|
|
20
|
+
const registered = REGISTERED_OPTIONS.get(md);
|
|
21
|
+
const effectiveOptions = {
|
|
22
|
+
...registered,
|
|
23
|
+
...options,
|
|
24
|
+
renderOptions: {
|
|
25
|
+
...registered?.renderOptions,
|
|
26
|
+
...options.renderOptions
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const tokens = md.parse(markdown, env);
|
|
30
|
+
const rendered = /* @__PURE__ */ new Map();
|
|
31
|
+
const diagnostics = [];
|
|
32
|
+
await Promise.all(tokens.map(async (token, index) => {
|
|
33
|
+
if (fenceLanguage(token) !== "kdiagram") return;
|
|
34
|
+
const result = await renderKDiagramForBuild(token.content, { renderOptions: effectiveOptions.renderOptions });
|
|
35
|
+
const mapped = result.diagnostics.map((diagnostic) => mapDiagnostic(token, diagnostic));
|
|
36
|
+
diagnostics.push(...mapped);
|
|
37
|
+
for (const diagnostic of mapped) effectiveOptions.onDiagnostic?.(diagnostic);
|
|
38
|
+
if (result.svg) rendered.set(index, `<figure class="${escapeKDiagramAttribute(effectiveOptions.className ?? "k-diagram")}">${result.svg}</figure>`);
|
|
39
|
+
else {
|
|
40
|
+
const message = formatKDiagramBuildError(mapped);
|
|
41
|
+
rendered.set(index, `<pre class="kdiagram-error"><code>${escapeKDiagramHtml(message)}</code></pre>`);
|
|
42
|
+
}
|
|
43
|
+
}));
|
|
44
|
+
diagnostics.sort((a, b) => a.markdownLine - b.markdownLine || a.markdownEndLine - b.markdownEndLine);
|
|
45
|
+
env[RENDERED] = rendered;
|
|
46
|
+
env.kdiagramDiagnostics = diagnostics;
|
|
47
|
+
return md.renderer.render(tokens, md.options, env);
|
|
48
|
+
}
|
|
49
|
+
function fenceLanguage(token) {
|
|
50
|
+
if (!token || token.type !== "fence") return void 0;
|
|
51
|
+
return token.info.trim().split(/\s+/, 1)[0]?.toLowerCase();
|
|
52
|
+
}
|
|
53
|
+
function isKDiagramFence(info) {
|
|
54
|
+
return info.trim().split(/\s+/, 1)[0]?.toLowerCase() === "kdiagram";
|
|
55
|
+
}
|
|
56
|
+
function encodeKDiagramFenceSource(source) {
|
|
57
|
+
return encodeURIComponent(source);
|
|
58
|
+
}
|
|
59
|
+
function mapDiagnostic(token, diagnostic) {
|
|
60
|
+
const mapped = offsetKDiagramDiagnostic(diagnostic, (token.map?.[0] ?? 0) + 2);
|
|
61
|
+
return {
|
|
62
|
+
...diagnostic,
|
|
63
|
+
markdownLine: mapped.hostLine,
|
|
64
|
+
markdownEndLine: mapped.hostEndLine
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { encodeKDiagramFenceSource, isKDiagramFence, kdiagramMarkdownIt, renderKDiagramMarkdown };
|
|
69
|
+
|
|
70
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Diagnostic, RenderOptions } from \"@kekonic/diagrams\";\nimport {\n escapeKDiagramAttribute,\n escapeKDiagramHtml,\n formatKDiagramBuildError,\n offsetKDiagramDiagnostic,\n renderKDiagramForBuild,\n} from \"@kekonic/diagrams-build\";\nimport type MarkdownIt from \"markdown-it\";\nimport type Token from \"markdown-it/lib/token.mjs\";\n\nconst RENDERED = Symbol(\"kdiagram-rendered-fences\");\nconst REGISTERED_OPTIONS = new WeakMap<MarkdownIt, KDiagramMarkdownItOptions>();\n\nexport type KDiagramFenceDiagnostic = Diagnostic & {\n markdownLine: number;\n markdownEndLine: number;\n};\n\nexport type KDiagramMarkdownEnvironment = {\n [RENDERED]?: Map<number, string>;\n kdiagramDiagnostics?: KDiagramFenceDiagnostic[];\n};\n\nexport type KDiagramMarkdownItOptions = {\n className?: string;\n renderOptions?: RenderOptions;\n onDiagnostic?: (diagnostic: KDiagramFenceDiagnostic) => void;\n};\n\n/**\n * Register the synchronous fence rule used by Markdown-it. Call\n * {@link renderKDiagramMarkdown} to perform KDiagram's asynchronous layout pass before rendering.\n */\nexport function kdiagramMarkdownIt(md: MarkdownIt, options: KDiagramMarkdownItOptions = {}): void {\n REGISTERED_OPTIONS.set(md, options);\n const fallback = md.renderer.rules.fence;\n md.renderer.rules.fence = (tokens, index, renderOptions, env, self) => {\n if (fenceLanguage(tokens[index]) !== \"kdiagram\") {\n return fallback\n ? fallback(tokens, index, renderOptions, env, self)\n : self.renderToken(tokens, index, renderOptions);\n }\n const rendered = (env as KDiagramMarkdownEnvironment)[RENDERED]?.get(index);\n if (rendered) return rendered;\n return `<pre class=\"kdiagram-error\"><code>KDiagram fence was not prepared. Use renderKDiagramMarkdown().</code></pre>`;\n };\n}\n\nexport async function renderKDiagramMarkdown(\n md: MarkdownIt,\n markdown: string,\n env: KDiagramMarkdownEnvironment = {},\n options: KDiagramMarkdownItOptions = {},\n): Promise<string> {\n const registered = REGISTERED_OPTIONS.get(md);\n const effectiveOptions: KDiagramMarkdownItOptions = {\n ...registered,\n ...options,\n renderOptions: { ...registered?.renderOptions, ...options.renderOptions },\n };\n const tokens = md.parse(markdown, env);\n const rendered = new Map<number, string>();\n const diagnostics: KDiagramFenceDiagnostic[] = [];\n\n await Promise.all(\n tokens.map(async (token, index) => {\n if (fenceLanguage(token) !== \"kdiagram\") return;\n const result = await renderKDiagramForBuild(token.content, {\n renderOptions: effectiveOptions.renderOptions,\n });\n const mapped = result.diagnostics.map((diagnostic) => mapDiagnostic(token, diagnostic));\n diagnostics.push(...mapped);\n for (const diagnostic of mapped) effectiveOptions.onDiagnostic?.(diagnostic);\n if (result.svg) {\n rendered.set(\n index,\n `<figure class=\"${escapeKDiagramAttribute(effectiveOptions.className ?? \"k-diagram\")}\">${result.svg}</figure>`,\n );\n } else {\n const message = formatKDiagramBuildError(mapped);\n rendered.set(\n index,\n `<pre class=\"kdiagram-error\"><code>${escapeKDiagramHtml(message)}</code></pre>`,\n );\n }\n }),\n );\n\n diagnostics.sort(\n (a, b) => a.markdownLine - b.markdownLine || a.markdownEndLine - b.markdownEndLine,\n );\n env[RENDERED] = rendered;\n env.kdiagramDiagnostics = diagnostics;\n return md.renderer.render(tokens, md.options, env);\n}\n\nfunction fenceLanguage(token: Token | undefined): string | undefined {\n if (!token || token.type !== \"fence\") return undefined;\n return token.info.trim().split(/\\s+/, 1)[0]?.toLowerCase();\n}\n\nexport function isKDiagramFence(info: string): boolean {\n return info.trim().split(/\\s+/, 1)[0]?.toLowerCase() === \"kdiagram\";\n}\n\nexport function encodeKDiagramFenceSource(source: string): string {\n return encodeURIComponent(source);\n}\n\nfunction mapDiagnostic(token: Token, diagnostic: Diagnostic): KDiagramFenceDiagnostic {\n const contentStart = (token.map?.[0] ?? 0) + 2;\n const mapped = offsetKDiagramDiagnostic(diagnostic, contentStart);\n return {\n ...diagnostic,\n markdownLine: mapped.hostLine,\n markdownEndLine: mapped.hostEndLine,\n };\n}\n"],"mappings":";;AAWA,MAAM,WAAW,OAAO,0BAA0B;AAClD,MAAM,qCAAqB,IAAI,QAA+C;;;;;AAsB9E,SAAgB,mBAAmB,IAAgB,UAAqC,CAAC,GAAS;CAChG,mBAAmB,IAAI,IAAI,OAAO;CAClC,MAAM,WAAW,GAAG,SAAS,MAAM;CACnC,GAAG,SAAS,MAAM,SAAS,QAAQ,OAAO,eAAe,KAAK,SAAS;EACrE,IAAI,cAAc,OAAO,MAAM,MAAM,YACnC,OAAO,WACH,SAAS,QAAQ,OAAO,eAAe,KAAK,IAAI,IAChD,KAAK,YAAY,QAAQ,OAAO,aAAa;EAEnD,MAAM,WAAY,IAAoC,SAAS,EAAE,IAAI,KAAK;EAC1E,IAAI,UAAU,OAAO;EACrB,OAAO;CACT;AACF;AAEA,eAAsB,uBACpB,IACA,UACA,MAAmC,CAAC,GACpC,UAAqC,CAAC,GACrB;CACjB,MAAM,aAAa,mBAAmB,IAAI,EAAE;CAC5C,MAAM,mBAA8C;EAClD,GAAG;EACH,GAAG;EACH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AAM;AAClC;AAEA,SAAS,cAAc,OAAc,YAAiD;CAEpF,MAAM,SAAS,yBAAyB,aADlB,MAAM,MAAM,MAAM,KAAK,CACmB;CAChE,OAAO;EACL,GAAG;EACH,cAAc,OAAO;EACrB,iBAAiB,OAAO;CAC1B;AACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kekonic/diagrams-markdown-it",
|
|
3
|
+
"version": "1.0.0-rc.4",
|
|
4
|
+
"description": "Static KDiagram diagrams for Markdown-it hosts.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"diagram",
|
|
7
|
+
"kdiagram",
|
|
8
|
+
"markdown-it",
|
|
9
|
+
"vitepress"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/kekonic/diagrams#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/kekonic/diagrams/issues"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/kekonic/diagrams.git",
|
|
19
|
+
"directory": "packages/diagrams-markdown-it"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.mts",
|
|
28
|
+
"import": "./dist/index.mjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@kekonic/diagrams": "1.0.0-rc.4",
|
|
36
|
+
"@kekonic/diagrams-build": "1.0.0-rc.4"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/markdown-it": "^14.1.2",
|
|
40
|
+
"markdown-it": "^14.1.0",
|
|
41
|
+
"typescript": "^5",
|
|
42
|
+
"vitest": "^4"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"markdown-it": ">=14"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22.18.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "vp pack",
|
|
52
|
+
"test": "vp test"
|
|
53
|
+
}
|
|
54
|
+
}
|