mdast-pele 0.0.0-stage → 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 torben haack
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 CHANGED
@@ -1,3 +1,44 @@
1
- # Temporary Holding Version
1
+ # mdast-pele
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Render Mermaid fences to static SVG with [Pele](https://pele.run) in remark and Sätteri pipelines.
4
+
5
+ The browser needs no JavaScript. The generated SVG is themed with CSS variables.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install mdast-pele
11
+ ```
12
+
13
+ ## Remark
14
+
15
+ ````js
16
+ import { unified } from 'unified';
17
+ import remarkParse from 'remark-parse';
18
+ import { remarkPele } from 'mdast-pele/remark';
19
+ import remarkRehype from 'remark-rehype';
20
+ import rehypeStringify from 'rehype-stringify';
21
+
22
+ const html = unified()
23
+ .use(remarkParse)
24
+ .use(remarkPele)
25
+ .use(remarkRehype, { allowDangerousHtml: true })
26
+ .use(rehypeStringify, { allowDangerousHtml: true })
27
+ .processSync('```mermaid\nflowchart LR\n A --> B\n```')
28
+ .toString();
29
+ ````
30
+
31
+ ## Sätteri
32
+
33
+ ```js
34
+ import { markdownToHtml } from 'satteri';
35
+ import { satteriPele } from 'mdast-pele/satteri';
36
+
37
+ const { html } = markdownToHtml(source, { mdastPlugins: [satteriPele()] });
38
+ ```
39
+
40
+ ## Options
41
+
42
+ `langs`, `errorMode`, `zoom`, `wrapper`, `className`, `caption` and all Pele `render()` options are supported. Add `zoom` to a fence to mark it for pan and zoom.
43
+
44
+ Full docs: https://t128n.github.io/pele-x/
@@ -0,0 +1,8 @@
1
+ import { a as RenderOptions, i as PelePluginOptions, n as FenceContext, o as RenderResult, r as FenceMeta, t as ErrorMode } from "./types-BI4iEutF.js";
2
+ export declare function renderDiagram(source: string, options?: RenderOptions): RenderResult;
3
+ export declare function renderFenceToHtml(source: string, meta: FenceMeta, context: FenceContext, options?: PelePluginOptions): string;
4
+ export declare function clearRenderCache(): void;
5
+ export declare function diagramId(fileURL: string | undefined, index: number, source: string): string;
6
+ export declare function parseFenceMeta(meta: string | null | undefined): FenceMeta;
7
+ export declare function isPeleLang(lang: string | null | undefined, options?: PelePluginOptions): boolean;
8
+ export type { ErrorMode, FenceContext, FenceMeta, PelePluginOptions, RenderOptions, RenderResult };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { r as clearRenderCache, o as diagramId, t as isPeleLang, n as parseFenceMeta, i as renderDiagram, a as renderFenceToHtml } from "./meta-C5f6wlQq.js";
@@ -0,0 +1,141 @@
1
+ import { PeleError, render } from "pele";
2
+ // src/errors.ts
3
+ function escapeHtml(value) {
4
+ return value.replace(/[&<>"']/g, (character) => {
5
+ switch (character) {
6
+ case "&": return "&amp;";
7
+ case "<": return "&lt;";
8
+ case ">": return "&gt;";
9
+ case "\"": return "&quot;";
10
+ default: return "&#39;";
11
+ }
12
+ });
13
+ }
14
+ function formatError(error, context) {
15
+ const message = error instanceof Error ? error.message : String(error);
16
+ const errorLine = error instanceof PeleError && error.line > 0 ? error.line : void 0;
17
+ const line = context.line !== void 0 && errorLine !== void 0 ? context.line + errorLine - 1 : context.line ?? errorLine;
18
+ const location = context.fileURL ? `${context.fileURL}${line === void 0 ? "" : `:${line}`}` : line === void 0 ? "" : `line ${line}`;
19
+ return location ? `${location}: ${message}` : message;
20
+ }
21
+ function contextualError(error, context) {
22
+ const message = formatError(error, context);
23
+ if (error instanceof PeleError) return new PeleError(message, error.code, {
24
+ type: error.type,
25
+ mermaidType: error.mermaidType,
26
+ line: error.line,
27
+ column: error.column,
28
+ snippet: error.snippet
29
+ });
30
+ return new Error(message, { cause: error });
31
+ }
32
+ // src/id.ts
33
+ function hash(value) {
34
+ let result = 2166136261;
35
+ for (let index = 0; index < value.length; index++) {
36
+ result ^= value.charCodeAt(index);
37
+ result = Math.imul(result, 16777619);
38
+ }
39
+ return (result >>> 0).toString(36).padStart(7, "0");
40
+ }
41
+ function diagramId(fileURL, index, source) {
42
+ return `pele-${hash(`${fileURL ?? ""}\0${source}`)}-${index.toString(36)}`;
43
+ }
44
+ // src/core.ts
45
+ const cacheLimit = 100;
46
+ const cache = /* @__PURE__ */ new Map();
47
+ const functionIds = /* @__PURE__ */ new WeakMap();
48
+ let nextFunctionId = 0;
49
+ function functionId(value) {
50
+ let id = functionIds.get(value);
51
+ if (id === void 0) {
52
+ id = ++nextFunctionId;
53
+ functionIds.set(value, id);
54
+ }
55
+ return id;
56
+ }
57
+ function cacheKey(source, meta, idPrefix, marked, options) {
58
+ try {
59
+ return JSON.stringify([
60
+ source,
61
+ meta.title,
62
+ idPrefix,
63
+ marked,
64
+ options
65
+ ], (_key, value) => {
66
+ if (typeof value === "function") return `function:${functionId(value)}`;
67
+ if (typeof value === "bigint") return `bigint:${value.toString()}`;
68
+ return value;
69
+ });
70
+ } catch {
71
+ return null;
72
+ }
73
+ }
74
+ function cached(key) {
75
+ const value = cache.get(key);
76
+ if (value !== void 0) {
77
+ cache.delete(key);
78
+ cache.set(key, value);
79
+ }
80
+ return value;
81
+ }
82
+ function store(key, value) {
83
+ cache.set(key, value);
84
+ if (cache.size > cacheLimit) {
85
+ const oldest = cache.keys().next().value;
86
+ if (oldest !== void 0) cache.delete(oldest);
87
+ }
88
+ }
89
+ function renderDiagram(source, options) {
90
+ return render(source, options);
91
+ }
92
+ function renderFenceToHtml(source, meta, context, options) {
93
+ const errorMode = options?.errorMode ?? "throw";
94
+ const marked = options?.zoom === true || options?.zoom === "marked" && meta.zoom;
95
+ const idPrefix = diagramId(context.fileURL, context.index, source);
96
+ const renderOptions = {
97
+ ...options,
98
+ idPrefix
99
+ };
100
+ delete renderOptions.languages;
101
+ delete renderOptions.errorMode;
102
+ delete renderOptions.zoom;
103
+ const key = cacheKey(source, meta, idPrefix, marked, renderOptions);
104
+ if (key !== null) {
105
+ const hit = cached(key);
106
+ if (hit !== void 0) return hit;
107
+ }
108
+ try {
109
+ const { svg } = renderDiagram(source, renderOptions);
110
+ const caption = meta.title === void 0 ? "" : `<figcaption>${escapeHtml(meta.title)}</figcaption>`;
111
+ const html = `<figure class="pele-diagram"${marked ? " data-pele-zoom" : ""}>${svg}${caption}</figure>`;
112
+ if (key !== null) store(key, html);
113
+ return html;
114
+ } catch (error) {
115
+ const message = formatError(error, context);
116
+ if (errorMode === "warn") context.report?.(message);
117
+ if (errorMode === "throw") throw contextualError(error, context);
118
+ return `<pre class="pele-error" role="alert">${escapeHtml(message)}</pre>`;
119
+ }
120
+ }
121
+ function clearRenderCache() {
122
+ cache.clear();
123
+ }
124
+ // src/meta.ts
125
+ const titlePattern = /(?:^|\s)title\s*=\s*(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)')/;
126
+ const quotedPattern = /"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'/g;
127
+ const zoomPattern = /(?:^|\s)zoom(?=\s|$)/;
128
+ const defaultLanguages = ["mermaid", "pele"];
129
+ function parseFenceMeta(meta) {
130
+ if (!meta) return { zoom: false };
131
+ const titleMatch = titlePattern.exec(meta);
132
+ const title = titleMatch?.[1] ?? titleMatch?.[2];
133
+ const unquoted = meta.replace(quotedPattern, "");
134
+ const result = { zoom: zoomPattern.test(unquoted) };
135
+ if (title !== void 0) result.title = title.replace(/\\(["'\\])/g, "$1");
136
+ return result;
137
+ }
138
+ function isPeleLang(lang, options) {
139
+ return lang != null && (options?.languages ?? defaultLanguages).includes(lang);
140
+ }
141
+ export { renderFenceToHtml as a, renderDiagram as i, parseFenceMeta as n, diagramId as o, clearRenderCache as r, isPeleLang as t };
@@ -0,0 +1,7 @@
1
+ import { i as PelePluginOptions } from "./types-BI4iEutF.js";
2
+ import { Root } from "mdast";
3
+ export type RemarkTransformer = (tree: Root, file?: {
4
+ path?: string;
5
+ history?: string[];
6
+ }) => void;
7
+ export declare function remarkPele(options?: PelePluginOptions): RemarkTransformer;
package/dist/remark.js ADDED
@@ -0,0 +1,22 @@
1
+ import { a as renderFenceToHtml, n as parseFenceMeta, t as isPeleLang } from "./meta-C5f6wlQq.js";
2
+ import { visit } from "unist-util-visit";
3
+ // src/remark.ts
4
+ function remarkPele(options) {
5
+ return (tree, file) => {
6
+ const fileURL = file?.path ?? file?.history?.[0];
7
+ visit(tree, "code", (node, index, parent) => {
8
+ if (index == null || !parent || !isPeleLang(node.lang, options)) return;
9
+ const value = renderFenceToHtml(node.value, parseFenceMeta(node.meta), {
10
+ index,
11
+ fileURL,
12
+ line: node.position?.start.line
13
+ }, options);
14
+ parent.children[index] = {
15
+ type: "html",
16
+ value,
17
+ position: node.position
18
+ };
19
+ });
20
+ };
21
+ }
22
+ export { remarkPele };