@p10node/diagram 0.0.0-stage → 0.9.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 p10node
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,78 @@
1
- # Temporary Holding Version
1
+ # @p10node/diagram
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
+ Mermaid, PlantUML and Graphviz DOT text → SVG. No DOM needed (browser, Web Worker, Node, Bun), no server, no WebAssembly, no dependency.
4
+
5
+ Part of [office-engine](https://github.com/p10node/office-engine); single-language packages: [`@p10node/mermaid`](../mermaid), [`@p10node/plantuml`](../plantuml), [`@p10node/graphviz`](../graphviz). Markdown with diagrams: [`@p10node/markdown`](../markdown).
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm i @p10node/diagram
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```ts
16
+ import { detect, render, renderEach, renderPage, renderTo, toPng } from '@p10node/diagram'
17
+
18
+ const { svg, width, height } = render('graph LR\n A --> B') // language detected
19
+ render('A -> B : hi', 'plantuml') // or given
20
+ renderEach(pumlFileText) // every @startuml block, every DOT graph
21
+ detect('digraph { a -> b }') // 'graphviz'
22
+
23
+ renderTo(document.getElementById('out')!, source) // into an element
24
+ renderPage() // every diagram block of the page
25
+ const png = await toPng(svg, 2) // PNG Blob, 2× size (browser)
26
+ ```
27
+
28
+ `renderPage(root = document)` draws `<pre class="mermaid">`, `<div class="plantuml">`, `<pre class="graphviz">`, `<pre><code class="language-mermaid | plantuml | puml | dot | graphviz">` and `<pre class="diagram">` (language detected). Each becomes `<figure class="diagram diagram-<lang>">`; a block that cannot be drawn stays, with a `data-diagram-error` attribute. It returns how many it drew.
29
+
30
+ From a CDN, no build step:
31
+
32
+ ```html
33
+ <pre class="mermaid">sequenceDiagram
34
+ Alice->>Bob: Hello</pre>
35
+ <script type="module">
36
+ import { renderPage } from 'https://cdn.jsdelivr.net/npm/@p10node/diagram/dist/index.js'
37
+ renderPage()
38
+ </script>
39
+ ```
40
+
41
+ ## API
42
+
43
+ | Function | Returns | Notes |
44
+ |------------------------------------|-----------------------|--------------------------------------------------------------------|
45
+ | `render(source, lang?)` | `Diagram` | The first drawing; `lang`: `'mermaid' \| 'plantuml' \| 'graphviz'` |
46
+ | `renderEach(source, lang?)` | `Diagram[]` | Every drawing of the source |
47
+ | `detect(source)` | `DiagramLang \| null` | From `@startuml`, `digraph {`, a Mermaid header |
48
+ | `renderTo(element, source, lang?)` | `Diagram[]` | Replaces the element's content (browser) |
49
+ | `renderPage(root?)` | `number` | Draws the page's diagram blocks (browser) |
50
+ | `toPng(svg, scale = 2)` | `Promise<Blob>` | PNG of a drawing (browser) |
51
+
52
+ `Diagram` is `{ svg: string; width: number; height: number }` (CSS pixels). A source that cannot be drawn throws a `DiagramError` with the reason.
53
+
54
+ ## What is drawn
55
+
56
+ | Language | Diagrams |
57
+ |----------|---------------------------------------------------------------------------------------------------------|
58
+ | Mermaid | flowchart / graph (subgraphs, `classDef`, `style`), sequence, class, state, ER, pie, Gantt, mind map |
59
+ | PlantUML | sequence, class / object, use case, component / deployment, state, activity, mind map, WBS, `@startdot` |
60
+ | Graphviz | graph / digraph, clusters, default attributes, edge chains, `rank=same`, record and HTML labels (text) |
61
+
62
+ Laid out by a built-in layered layout (like Mermaid's and Graphviz's `dot`), so drawings look close to, not exactly like, the originals. Not drawn: other Mermaid kinds (git graph, journey, timeline, C4…), other PlantUML kinds (Gantt, JSON / YAML, salt, timing), Graphviz engines other than `dot`.
63
+
64
+ ## Showing the SVG
65
+
66
+ The SVG has a fixed `width` / `height`; to fit a column:
67
+
68
+ ```css
69
+ figure.diagram svg { max-width: 100%; height: auto; }
70
+ ```
71
+
72
+ ## Safety
73
+
74
+ Text from the source is escaped; only plain colors are kept from styles; the SVG has no scripts, links, `<foreignObject>` or external references. `renderTo` / `renderPage` only ever insert SVG built this way.
75
+
76
+ ## License
77
+
78
+ MIT
@@ -0,0 +1,28 @@
1
+ export type DiagramLang = 'mermaid' | 'plantuml' | 'graphviz';
2
+ /** A drawing: a self-contained SVG document and its size in CSS pixels. */
3
+ export interface Diagram {
4
+ svg: string;
5
+ width: number;
6
+ height: number;
7
+ }
8
+ /** The source cannot be drawn (unknown or unsupported diagram kind, syntax the reader does not follow). */
9
+ export declare class DiagramError extends Error {
10
+ constructor(message: string);
11
+ }
12
+ /** The language of a diagram source: `@startuml`, `digraph {`, a Mermaid header (`graph TD`, `sequenceDiagram`…), or null. */
13
+ export declare function detect(source: string): DiagramLang | null;
14
+ /** Every drawing of a source (a PlantUML file or a DOT file may hold several). `lang`: detected when left out. */
15
+ export declare function renderEach(source: string, lang?: DiagramLang): Diagram[];
16
+ /** The (first) drawing of a source. */
17
+ export declare function render(source: string, lang?: DiagramLang): Diagram;
18
+ /** Draws a source into an element (its content is replaced). */
19
+ export declare function renderTo(target: Element, source: string, lang?: DiagramLang): Diagram[];
20
+ /**
21
+ * Draws the diagram blocks of a page: `<pre class="mermaid">`, `<div class="plantuml">`, `<pre><code
22
+ * class="language-dot">`, `<pre class="diagram">` (language detected)… Each becomes a
23
+ * `<figure class="diagram diagram-<lang>">`; a block that cannot be drawn stays and gets a `data-diagram-error`
24
+ * attribute with the reason. Returns how many were drawn. Browser only.
25
+ */
26
+ export declare function renderPage(root?: ParentNode): number;
27
+ /** A PNG of a drawing's SVG, `scale` times its size. Browser only. */
28
+ export declare function toPng(svg: string, scale?: number): Promise<Blob>;