yuhwa 0.0.0-stage → 0.0.1
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 +172 -2
- package/client.d.ts +9 -0
- package/dist/astro.d.ts +17 -0
- package/dist/astro.js +20 -0
- package/dist/browser.d.ts +31 -0
- package/dist/browser.js +158 -0
- package/dist/cli.js +11827 -0
- package/dist/editor/assets/index-BWzT0Wd7.js +585 -0
- package/dist/editor/assets/index-DaWl0OPj.css +1 -0
- package/dist/editor/assets/lsp.worker-DSWloYB1.js +26 -0
- package/dist/editor/index.html +240 -0
- package/dist/figure-CYW54wev.d.ts +27 -0
- package/dist/index-PDbKFm2X.d.ts +1072 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/ironmark.d.ts +33 -0
- package/dist/ironmark.js +73 -0
- package/dist/llms.txt +218 -0
- package/dist/markdown-it.d.ts +21 -0
- package/dist/markdown-it.js +36 -0
- package/dist/node-DBf7JVVp.js +108 -0
- package/dist/remark.d.ts +14 -0
- package/dist/remark.js +42 -0
- package/dist/src-BNOnUjQh.js +11444 -0
- package/dist/style.css +48 -0
- package/dist/vite.d.ts +12 -0
- package/dist/vite.js +55 -0
- package/dist/vuepress.d.ts +20 -0
- package/dist/vuepress.js +17 -0
- package/dist/yuhwa.iife.js +261 -0
- package/package.json +113 -3
package/README.md
CHANGED
|
@@ -1,3 +1,173 @@
|
|
|
1
|
-
#
|
|
1
|
+
# yuhwa
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Diagrams that look good and stay where you put them.
|
|
4
|
+
|
|
5
|
+
yuhwa is a text diagram language for Markdown and documentation, built as a
|
|
6
|
+
friendlier alternative to Mermaid.
|
|
7
|
+
|
|
8
|
+
**[Documentation](apps/docs/index.md)** ·
|
|
9
|
+
**[Editor](apps/docs/guide/editor.md)** ·
|
|
10
|
+
**[Syntax](apps/docs/guide/syntax.md)**
|
|
11
|
+
|
|
12
|
+
```yuhwa
|
|
13
|
+
title: Checkout flow
|
|
14
|
+
|
|
15
|
+
.svc [blue]
|
|
16
|
+
.store [cylinder, amber]
|
|
17
|
+
|
|
18
|
+
user "Customer" [circle, purple]
|
|
19
|
+
|
|
20
|
+
group core "Core services" [blue] {
|
|
21
|
+
cart "Cart" .svc
|
|
22
|
+
orders "Orders" .svc
|
|
23
|
+
pay "Payments" .svc [green]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
user -> cart: add item
|
|
27
|
+
user -> orders: checkout
|
|
28
|
+
orders -> pay: [flow]
|
|
29
|
+
cart, orders -> db "Postgres" .store
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- **Readable by default.** Clean auto-layout, soft colours, twelve themes, or define your own.
|
|
33
|
+
- **More than flowcharts.** Sequence diagrams, timelines, charts and git
|
|
34
|
+
branch histories sit next to the graph in one file. `import` splits a
|
|
35
|
+
diagram over files.
|
|
36
|
+
- **Presentation-ready.** Cards with charts, stats, progress bars and tables,
|
|
37
|
+
browser and terminal window frames, footers. All of it as plugins.
|
|
38
|
+
- **Styleable everywhere.** Tones, shapes, borders, icons, line styles, arrow
|
|
39
|
+
heads and animation, with reusable classes.
|
|
40
|
+
- **Hand-tunable.** Drag boxes and bend edges in the visual editor. Positions
|
|
41
|
+
are written back into the source as a small `layout {}` block. The editor
|
|
42
|
+
also imports Mermaid and JSON and holds several files as tabs.
|
|
43
|
+
- **Made for AI agents.** `yuhwa guide` prints the language reference and
|
|
44
|
+
`yuhwa check --json` returns diagnostics that name the fix.
|
|
45
|
+
- **Embeds anywhere.** A script tag, a web component, an ES module, an image
|
|
46
|
+
URL service, or static SVGs for GitHub via `yuhwa md`.
|
|
47
|
+
- **Speaks Mermaid.** Mermaid flowcharts render as-is and convert to yuhwa.
|
|
48
|
+
- **No runtime dependencies.** TypeScript, deterministic output.
|
|
49
|
+
|
|
50
|
+
## Quick start
|
|
51
|
+
|
|
52
|
+
Node.js 20 or later.
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
npm install -g yuhwa
|
|
56
|
+
echo 'web -> api -> db [cylinder]' > hello.yuhwa
|
|
57
|
+
yuhwa render hello.yuhwa -o hello.svg # SVG
|
|
58
|
+
yuhwa check hello.yuhwa # diagnostics, exit 1 on errors
|
|
59
|
+
yuhwa edit hello.yuhwa # visual editor, saves back to the file
|
|
60
|
+
yuhwa md README.md # render the diagrams in a Markdown file
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Or skip the install: `npx yuhwa <command>`. Step by step:
|
|
64
|
+
[getting started](apps/docs/guide/getting-started.md).
|
|
65
|
+
|
|
66
|
+
```html
|
|
67
|
+
<script src="https://cdn.jsdelivr.net/npm/yuhwa/dist/yuhwa.iife.js"></script>
|
|
68
|
+
<yuhwa-diagram>web -> api -> db [shape: cylinder]</yuhwa-diagram>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import { render } from 'yuhwa';
|
|
73
|
+
const { svg, diagnostics } = render('a -> b -> c', { theme: 'midnight' });
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Docs sites
|
|
77
|
+
|
|
78
|
+
Write ` ```yuhwa ` blocks in Markdown. A build plugin turns them into SVG
|
|
79
|
+
figures with light and dark variants, and a broken diagram fails the build.
|
|
80
|
+
|
|
81
|
+
| site generator | plugin |
|
|
82
|
+
| --- | --- |
|
|
83
|
+
| VitePress, Eleventy, any markdown-it | `yuhwa/markdown-it` |
|
|
84
|
+
| VuePress 2 | `yuhwa/vuepress` |
|
|
85
|
+
| Astro, Starlight | `yuhwa/astro` |
|
|
86
|
+
| Docusaurus, Vocs, Nextra, Fumadocs, Rspress, any MDX | `yuhwa/remark` |
|
|
87
|
+
| ironmark (`.md`, `.mdx`, runtime) | `yuhwa/ironmark` |
|
|
88
|
+
| Vite apps (React, Vue, Svelte, Solid) | `yuhwa/vite` |
|
|
89
|
+
| Docsify, Hugo, Jekyll | the script tag |
|
|
90
|
+
| GitHub, GitLab | `yuhwa md` writes SVG files |
|
|
91
|
+
|
|
92
|
+
Setup for each: [integrations](apps/docs/guide/integrations.md).
|
|
93
|
+
|
|
94
|
+
## AI agents
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
npx yuhwa guide # language reference, written for agents
|
|
98
|
+
npx yuhwa check x.yuhwa --json # diagnostics that name the fix
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Rules-file and skill snippets: [ai](apps/docs/guide/ai.md).
|
|
102
|
+
Quick reference: [cheatsheet](apps/docs/guide/cheatsheet.md).
|
|
103
|
+
|
|
104
|
+
## Editors
|
|
105
|
+
|
|
106
|
+
`yuhwa-lsp` is a language server: diagnostics, quick fixes,
|
|
107
|
+
completion, hover, rename, formatting and colouring.
|
|
108
|
+
|
|
109
|
+
- **VS Code, Cursor, VSCodium:** `pnpm --filter ./packages/vscode package`, then
|
|
110
|
+
`code --install-extension packages/vscode/yuhwa.vsix`. The server is
|
|
111
|
+
bundled.
|
|
112
|
+
- **Neovim, Helix, Sublime, Emacs, JetBrains, Vim, Kate:** install the
|
|
113
|
+
server, then point the editor at `yuhwa-lsp --stdio` for `.yuhwa` files.
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
npm install -g yuhwa-lsp
|
|
117
|
+
yuhwa-lsp --version
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Config snippet per editor and troubleshooting:
|
|
121
|
+
[editor setup](apps/docs/guide/lsp.md).
|
|
122
|
+
|
|
123
|
+
## Development
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
pnpm install
|
|
127
|
+
pnpm dev # editor with hot reload
|
|
128
|
+
pnpm docs:dev # documentation site
|
|
129
|
+
pnpm check # typecheck, tests, build
|
|
130
|
+
pnpm docs:build # library, editor and docs into apps/docs/.vitepress/dist
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
A pnpm workspace. `packages/core` and `packages/lsp` are published.
|
|
134
|
+
The apps import them and read their `src` through the `yuhwa-source` export
|
|
135
|
+
condition, so changes show up without a package build.
|
|
136
|
+
|
|
137
|
+
| path | contents |
|
|
138
|
+
| --- | --- |
|
|
139
|
+
| `packages/core/src/core` | parser, linter, layout, router, SVG renderer |
|
|
140
|
+
| `packages/core/src/plugins` | built-in plugins: themes, card rows, tables, frames, footer, and the sequence, timeline, chart and git panels |
|
|
141
|
+
| `packages/core/src/convert` | Mermaid and JSON input |
|
|
142
|
+
| `packages/core/src/integrations` | docs-site plugins (markdown-it, remark, Astro, VuePress, Vite, ironmark) |
|
|
143
|
+
| `packages/core/src/cli` | CLI and HTTP render service |
|
|
144
|
+
| `packages/core/src/browser.ts` | script build and `<yuhwa-diagram>` |
|
|
145
|
+
| `packages/lsp` | language server, for editors and the browser |
|
|
146
|
+
| `apps/editor` | visual editor (Vite); builds into `packages/core/dist/editor` |
|
|
147
|
+
| `apps/docs` | documentation site (VitePress) |
|
|
148
|
+
|
|
149
|
+
### Releases
|
|
150
|
+
|
|
151
|
+
[semantic-release](https://semantic-release.gitbook.io/) publishes from `main`. Write [conventional commits](https://www.conventionalcommits.org/):
|
|
152
|
+
|
|
153
|
+
- `feat:` publishes a minor version.
|
|
154
|
+
- `fix:`, `perf:`, `refactor:` and `revert:` publish a patch version.
|
|
155
|
+
- `feat!:` or a `BREAKING CHANGE:` footer publishes a major version.
|
|
156
|
+
- `docs:`, `chore:`, `test:` and `ci:` publish nothing.
|
|
157
|
+
|
|
158
|
+
All three packages share one version. Each push to `main` runs CI first. When a commit warrants a release, the release job:
|
|
159
|
+
|
|
160
|
+
1. sets the version in `packages/core`, `packages/lsp` and `packages/vscode`, and builds,
|
|
161
|
+
2. publishes `yuhwa` and `yuhwa-lsp` to npm through trusted publishing (no token),
|
|
162
|
+
3. publishes the extension to the VS Code Marketplace (skipped while `VSCE_PAT` is unset),
|
|
163
|
+
4. tags `vX.Y.Z` and writes a GitHub release with the `.vsix` attached,
|
|
164
|
+
5. commits the new version back to the three `package.json` files,
|
|
165
|
+
6. redeploys the docs.
|
|
166
|
+
|
|
167
|
+
`scripts/release.mjs` does steps 1 to 3.
|
|
168
|
+
|
|
169
|
+
`pnpm release:dry` previews the next version.
|
|
170
|
+
|
|
171
|
+
## License
|
|
172
|
+
|
|
173
|
+
MIT
|
package/client.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Types for `.yuhwa` imports through `yuhwa/vite`.
|
|
2
|
+
// Reference once: /// <reference types="yuhwa/client" />
|
|
3
|
+
declare module '*.yuhwa' {
|
|
4
|
+
const svg: string;
|
|
5
|
+
export default svg;
|
|
6
|
+
export const darkSvg: string;
|
|
7
|
+
export const source: string;
|
|
8
|
+
export const title: string;
|
|
9
|
+
}
|
package/dist/astro.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { t as yuhwaDocsOptions } from "./figure-CYW54wev.js";
|
|
2
|
+
interface SetupParams {
|
|
3
|
+
updateConfig: (config: {
|
|
4
|
+
markdown: {
|
|
5
|
+
remarkPlugins: unknown[];
|
|
6
|
+
};
|
|
7
|
+
}) => unknown;
|
|
8
|
+
injectScript: (stage: "page-ssr", content: string) => void;
|
|
9
|
+
}
|
|
10
|
+
export interface AstroIntegration {
|
|
11
|
+
name: string;
|
|
12
|
+
hooks: {
|
|
13
|
+
"astro:config:setup": (params: SetupParams) => void;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export default function yuhwa(opts?: yuhwaDocsOptions): AstroIntegration;
|
|
17
|
+
export type { yuhwaDocsOptions };
|
package/dist/astro.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import remarkyuhwa from "./remark.js";
|
|
2
|
+
// src/integrations/astro.ts
|
|
3
|
+
/**
|
|
4
|
+
* Astro integration, also for Starlight: renders ```yuhwa blocks in .md and
|
|
5
|
+
* .mdx and adds the light/dark stylesheet.
|
|
6
|
+
*
|
|
7
|
+
* // astro.config.mjs
|
|
8
|
+
* import yuhwa from 'yuhwa/astro';
|
|
9
|
+
* export default defineConfig({ integrations: [starlight({...}), yuhwa()] });
|
|
10
|
+
*/
|
|
11
|
+
function yuhwa(opts = {}) {
|
|
12
|
+
return {
|
|
13
|
+
name: "yuhwa",
|
|
14
|
+
hooks: { "astro:config:setup": ({ updateConfig, injectScript }) => {
|
|
15
|
+
updateConfig({ markdown: { remarkPlugins: [[remarkyuhwa, opts]] } });
|
|
16
|
+
injectScript("page-ssr", `import 'yuhwa/style.css';`);
|
|
17
|
+
} }
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export { yuhwa as default };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drop-in browser runtime: <script src="yuhwa.js"><\/script>
|
|
3
|
+
* Renders every `pre > code.language-yuhwa` (or `.lang-yuhwa`), `pre.yuhwa` and
|
|
4
|
+
* `div.yuhwa` element on the page, and defines the <yuhwa-diagram>
|
|
5
|
+
* custom element:
|
|
6
|
+
*
|
|
7
|
+
* <yuhwa-diagram theme="midnight">a -> b -> c</yuhwa-diagram>
|
|
8
|
+
* <yuhwa-diagram src="docs/arch.yuhwa"></yuhwa-diagram>
|
|
9
|
+
* el.source = 'a -> b' // re-renders
|
|
10
|
+
*/
|
|
11
|
+
import { $ as ParseOptions, $t as PanelKind, A as MdBlock, An as registerTheme, At as LayoutOverrides, B as setProp, Bt as Point, C as NODE_PROPS, Cn as TONES, Ct as HeadKind, D as ConvertResult, Dn as getTheme, Dt as LaidNode, E as lint, En as createTheme, Et as LaidGroup, F as replaceBlockSource, Ft as PanelOverride, G as SHAPES, Gt as Side, H as writeLayout, Ht as ResolvedEdgeStyle, I as editPart, It as PanelPart, J as fromJSON, Jt as groupPath, K as DiagramJSON, Kt as SourceRef, L as formatLayout, Lt as PartAction, M as MdResult, Mn as approxMeasure, Mt as NodeOverride, N as findBlocks, Nn as canvasMeasure, Nt as PanelDecl, O as fromMermaid, On as isToneOrHex, Ot as LaidPanel, P as processMarkdown, Pt as PanelHandle, Q as PANELS, Qt as PanelCtx, R as setLabel, Rt as PartChange, S as GROUP_PROPS, Sn as THEME_NAMES, St as GroupDecl, T as PANEL_PROPS, Tn as ThemeInput, Tt as LaidEdge, U as dashFor, Ut as ResolvedNodeStyle, V as setSetting, Vt as Props, W as lineStyle, Wt as ShapeName, X as KNOWN_SETTINGS, Xt as Decoration, Y as renderSvg, Yt as layout, Z as LexedLine, Zt as FrameKind, _ as themes, _n as tspans, _t as Diagnostic, an as recolor, at as joinPath, b as EDGE_PROPS, bn as THEMES, bt as EdgeDecl, c as git, cn as yuhwaPlugin, ct as parse, d as sequence, dn as renderHead, dt as setLineProp, en as RowCtx, et as SETTING_VALUES, f as legend, fn as InkColors, ft as setTrailingProp, g as charts, gn as segsW, gt as Curve, h as tables, hn as renderSegs, ht as Component, i as hash, in as perPass, it as isyuhwaPath, j as MdOptions, jn as themeNames, jt as NodeDecl, k as isMermaid, kn as ramp, kt as Layout, l as chart, ln as MarkerKind, lt as parseProps, m as frames, mn as parseInline, mt as Box, n as Options, nn as RowPaint, nt as TokKind, o as toSource, on as registry, ot as lex, p as footer, pn as Seg, pt as splitTrailingProps, q as JsonResult, qt as GRID, r as Result, rn as StatementKind, rt as blockBraces, s as builtins, sn as use, st as onlyColor, t as InputFormat, tn as RowKind, tt as Tok, u as timeline, un as ShapeKind, ut as serializeProps, v as GUIDE, vn as LineStyle, vt as Diagram, w as NUMBERS, wn as Theme, wt as LabelRef, x as ENUMS, xn as THEME_KEYS, xt as EdgeOverride, y as BOOLS, yn as RAMPS, yt as Direction, z as setPartTone, zt as PartEdit } from "./index-PDbKFm2X.js";
|
|
12
|
+
export declare function render(source: string, opts?: Options): Result;
|
|
13
|
+
/**
|
|
14
|
+
* Fetches every file that `source` imports, and what those import, so
|
|
15
|
+
* `render` can resolve them synchronously. Paths join against `file` (a URL
|
|
16
|
+
* path such as `/docs/arch.yuhwa`); a file that fails to load is left out and
|
|
17
|
+
* shows up as a diagnostic on its import line.
|
|
18
|
+
*/
|
|
19
|
+
export declare function fetchImports(source: string, file: string, load?: (path: string) => Promise<string>): Promise<Record<string, string>>;
|
|
20
|
+
export declare function run(root?: ParentNode, opts?: Options): void;
|
|
21
|
+
declare const Base: typeof HTMLElement;
|
|
22
|
+
/** <yuhwa-diagram [theme] [src]>source</yuhwa-diagram>; fires `yuhwa:render` with the diagnostics */
|
|
23
|
+
export declare class yuhwaDiagram extends Base {
|
|
24
|
+
#private;
|
|
25
|
+
static observedAttributes: string[];
|
|
26
|
+
get source(): string;
|
|
27
|
+
set source(v: string);
|
|
28
|
+
connectedCallback(): void;
|
|
29
|
+
attributeChangedCallback(): void;
|
|
30
|
+
}
|
|
31
|
+
export { BOOLS, type Box, type Component, type ConvertResult, type Curve, type Decoration, type Diagnostic, type Diagram, type DiagramJSON, type Direction, EDGE_PROPS, ENUMS, type EdgeDecl, type EdgeOverride, type FrameKind, GRID, GROUP_PROPS, GUIDE, type GroupDecl, type HeadKind, type InkColors, InputFormat, type JsonResult, KNOWN_SETTINGS, type LabelRef, type LaidEdge, type LaidGroup, type LaidNode, type LaidPanel, type Layout, type LayoutOverrides, type LexedLine, type LineStyle, type MarkerKind, type MdBlock, type MdOptions, type MdResult, NODE_PROPS, NUMBERS, type NodeDecl, type NodeOverride, Options, PANELS, PANEL_PROPS, type PanelCtx, type PanelDecl, type PanelHandle, type PanelKind, type PanelOverride, type PanelPart, type ParseOptions, type PartAction, type PartChange, type PartEdit, type Point, type Props, RAMPS, type ResolvedEdgeStyle, type ResolvedNodeStyle, Result, type RowCtx, type RowKind, type RowPaint, SETTING_VALUES, SHAPES, type Seg, type ShapeKind, type ShapeName, type Side, type SourceRef, type StatementKind, THEMES, THEME_KEYS, THEME_NAMES, TONES, type Theme, type ThemeInput, type Tok, type TokKind, approxMeasure, blockBraces, builtins, canvasMeasure, chart, charts, createTheme, dashFor, editPart, findBlocks, footer, formatLayout, frames, fromJSON, fromMermaid, getTheme, git, groupPath, hash, isMermaid, isToneOrHex, isyuhwaPath, joinPath, layout, legend, lex, lineStyle, lint, onlyColor, parse, parseInline, parseProps, perPass, processMarkdown, ramp, recolor, registerTheme, registry, renderHead, renderSegs, renderSvg, replaceBlockSource, segsW, sequence, serializeProps, setLabel, setLineProp, setPartTone, setProp, setSetting, setTrailingProp, splitTrailingProps, tables, themeNames, themes, timeline, toSource, tspans, use, writeLayout, type yuhwaPlugin };
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { $ as BOOLS, A as footer, B as PANELS, C as git, Ct as ramp, D as legend, E as sequence, F as perPass, G as lex, H as blockBraces, I as recolor, J as parseProps, K as onlyColor, L as registry, M as tables, N as charts, O as approxMeasure, P as themes, Q as splitTrailingProps, R as use, S as builtins, St as isToneOrHex, T as timeline, Tt as themeNames, U as isyuhwaPath, V as SETTING_VALUES, W as joinPath, X as setLineProp, Y as serializeProps, Z as setTrailingProp, _ as fromJSON, _t as THEME_KEYS, a as findBlocks, at as PANEL_PROPS, b as groupPath, bt as createTheme, c as editPart, ct as renderHead, d as setPartTone, dt as parseInline, et as EDGE_PROPS, f as setProp, ft as renderSegs, g as isMermaid, gt as THEMES, h as fromMermaid, ht as RAMPS, i as GUIDE, it as NUMBERS, j as frames, k as canvasMeasure, l as formatLayout, lt as dashFor, m as writeLayout, mt as tspans, n as render$1, nt as GROUP_PROPS, o as processMarkdown, ot as lint, p as setSetting, pt as segsW, q as parse, r as toSource, rt as NODE_PROPS, s as replaceBlockSource, st as SHAPES, t as hash, tt as ENUMS, u as setLabel, ut as lineStyle, v as renderSvg, vt as THEME_NAMES, w as chart, wt as registerTheme, x as layout, xt as getTheme, y as GRID, yt as TONES, z as KNOWN_SETTINGS } from "./src-BNOnUjQh.js";
|
|
2
|
+
// src/browser.ts
|
|
3
|
+
/**
|
|
4
|
+
* Drop-in browser runtime: <script src="yuhwa.js"><\/script>
|
|
5
|
+
* Renders every `pre > code.language-yuhwa` (or `.lang-yuhwa`), `pre.yuhwa` and
|
|
6
|
+
* `div.yuhwa` element on the page, and defines the <yuhwa-diagram>
|
|
7
|
+
* custom element:
|
|
8
|
+
*
|
|
9
|
+
* <yuhwa-diagram theme="midnight">a -> b -> c</yuhwa-diagram>
|
|
10
|
+
* <yuhwa-diagram src="docs/arch.yuhwa"></yuhwa-diagram>
|
|
11
|
+
* el.source = 'a -> b' // re-renders
|
|
12
|
+
*/
|
|
13
|
+
const SELECTOR = "pre > code.language-yuhwa, pre > code.lang-yuhwa, pre.yuhwa, div.yuhwa:not([data-rendered])";
|
|
14
|
+
function prefersDark() {
|
|
15
|
+
return typeof matchMedia === "function" && matchMedia("(prefers-color-scheme: dark)").matches;
|
|
16
|
+
}
|
|
17
|
+
function render(source, opts = {}) {
|
|
18
|
+
let theme = opts.theme;
|
|
19
|
+
if (!theme || theme === "auto") {
|
|
20
|
+
const m = source.match(/^\s*theme\s*:\s*(\S+)/m);
|
|
21
|
+
if (!m || m[1] === "auto") theme = prefersDark() ? "midnight" : "paper";
|
|
22
|
+
}
|
|
23
|
+
return render$1(source, {
|
|
24
|
+
measureFor: (t) => canvasMeasure(t.font),
|
|
25
|
+
...opts,
|
|
26
|
+
theme
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const IMPORT_RE = /^\s*import\s+(?:\{[^}]*\}\s*from\s+)?"([^"]+)"/gm;
|
|
30
|
+
/**
|
|
31
|
+
* Fetches every file that `source` imports, and what those import, so
|
|
32
|
+
* `render` can resolve them synchronously. Paths join against `file` (a URL
|
|
33
|
+
* path such as `/docs/arch.yuhwa`); a file that fails to load is left out and
|
|
34
|
+
* shows up as a diagnostic on its import line.
|
|
35
|
+
*/
|
|
36
|
+
async function fetchImports(source, file, load = fetchText) {
|
|
37
|
+
const files = {};
|
|
38
|
+
const queue = [[source, file]];
|
|
39
|
+
while (queue.length) {
|
|
40
|
+
const [text, from] = queue.shift();
|
|
41
|
+
const paths = [...text.matchAll(IMPORT_RE)].map((m) => joinPath(from, m[1])).filter((p) => isyuhwaPath(p) && !(p in files));
|
|
42
|
+
await Promise.all(paths.map(async (p) => {
|
|
43
|
+
files[p] = "";
|
|
44
|
+
try {
|
|
45
|
+
files[p] = await load(p);
|
|
46
|
+
queue.push([files[p], p]);
|
|
47
|
+
} catch {
|
|
48
|
+
delete files[p];
|
|
49
|
+
}
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
return files;
|
|
53
|
+
}
|
|
54
|
+
const fetchText = (url) => fetch(url).then((r) => r.ok ? r.text() : Promise.reject(/* @__PURE__ */ new Error(`${url}: HTTP ${r.status}`)));
|
|
55
|
+
function fit(svg) {
|
|
56
|
+
if (!svg) return;
|
|
57
|
+
svg.removeAttribute("height");
|
|
58
|
+
svg.style.maxWidth = "100%";
|
|
59
|
+
svg.style.height = "auto";
|
|
60
|
+
svg.style.display = "block";
|
|
61
|
+
}
|
|
62
|
+
function run(root = document, opts = {}) {
|
|
63
|
+
root.querySelectorAll(SELECTOR).forEach((el) => {
|
|
64
|
+
const host = el.tagName === "CODE" ? el.parentElement : el;
|
|
65
|
+
const source = el.textContent ?? "";
|
|
66
|
+
const out = document.createElement("div");
|
|
67
|
+
out.className = "yuhwa";
|
|
68
|
+
out.dataset.rendered = "true";
|
|
69
|
+
out.dataset.source = source;
|
|
70
|
+
try {
|
|
71
|
+
const r = render(source, {
|
|
72
|
+
theme: host.dataset.theme,
|
|
73
|
+
...opts
|
|
74
|
+
});
|
|
75
|
+
out.innerHTML = r.svg;
|
|
76
|
+
fit(out.querySelector("svg"));
|
|
77
|
+
for (const d of r.diagnostics) console.warn(`yuhwa:${d.line}: ${d.message}`);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
out.textContent = `yuhwa error: ${e.message}`;
|
|
80
|
+
out.style.color = "crimson";
|
|
81
|
+
}
|
|
82
|
+
host.replaceWith(out);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const Base = typeof HTMLElement === "undefined" ? class {} : HTMLElement;
|
|
86
|
+
/** <yuhwa-diagram [theme] [src]>source</yuhwa-diagram>; fires `yuhwa:render` with the diagnostics */
|
|
87
|
+
var yuhwaDiagram = class extends Base {
|
|
88
|
+
static observedAttributes = ["theme", "src"];
|
|
89
|
+
#source = null;
|
|
90
|
+
#fetched = "";
|
|
91
|
+
#file;
|
|
92
|
+
#files = {};
|
|
93
|
+
get source() {
|
|
94
|
+
return this.#source ?? "";
|
|
95
|
+
}
|
|
96
|
+
set source(v) {
|
|
97
|
+
this.#source = v;
|
|
98
|
+
this.#draw();
|
|
99
|
+
}
|
|
100
|
+
connectedCallback() {
|
|
101
|
+
if (document.readyState === "loading" && !this.textContent?.trim() && !this.hasAttribute("src")) return void document.addEventListener("DOMContentLoaded", () => this.connectedCallback(), { once: true });
|
|
102
|
+
if (this.#source === null && !this.hasAttribute("src")) this.#source = this.textContent ?? "";
|
|
103
|
+
this.#update();
|
|
104
|
+
}
|
|
105
|
+
attributeChangedCallback() {
|
|
106
|
+
if (this.isConnected) this.#update();
|
|
107
|
+
}
|
|
108
|
+
#update() {
|
|
109
|
+
const src = this.getAttribute("src");
|
|
110
|
+
if (!src || src === this.#fetched) return this.#draw();
|
|
111
|
+
this.#fetched = src;
|
|
112
|
+
const url = new URL(src, location.href);
|
|
113
|
+
const file = url.origin === location.origin ? url.pathname : url.href;
|
|
114
|
+
fetchText(url.href).then(async (t) => {
|
|
115
|
+
this.#files = await fetchImports(t, file);
|
|
116
|
+
this.#file = file;
|
|
117
|
+
this.source = t;
|
|
118
|
+
}).catch((e) => this.#fail(e.message));
|
|
119
|
+
}
|
|
120
|
+
#draw() {
|
|
121
|
+
if (this.#source === null) return;
|
|
122
|
+
try {
|
|
123
|
+
const files = this.#files;
|
|
124
|
+
const r = render(this.#source, {
|
|
125
|
+
theme: this.getAttribute("theme") ?? void 0,
|
|
126
|
+
file: this.#file,
|
|
127
|
+
resolve: (p) => files[p]
|
|
128
|
+
});
|
|
129
|
+
this.innerHTML = r.svg;
|
|
130
|
+
fit(this.querySelector("svg"));
|
|
131
|
+
this.setAttribute("role", "img");
|
|
132
|
+
if (!this.hasAttribute("aria-label") || this.dataset.autoLabel) {
|
|
133
|
+
const t = r.diagram.settings.title;
|
|
134
|
+
if (t) this.setAttribute("aria-label", t), this.dataset.autoLabel = "1";
|
|
135
|
+
else if (this.dataset.autoLabel) this.removeAttribute("aria-label"), delete this.dataset.autoLabel;
|
|
136
|
+
}
|
|
137
|
+
for (const d of r.diagnostics) console.warn(`yuhwa:${d.line}: ${d.message}`);
|
|
138
|
+
this.dispatchEvent(new CustomEvent("yuhwa:render", { detail: {
|
|
139
|
+
diagnostics: r.diagnostics,
|
|
140
|
+
format: r.format
|
|
141
|
+
} }));
|
|
142
|
+
} catch (e) {
|
|
143
|
+
this.#fail(e.message);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
#fail(msg) {
|
|
147
|
+
this.textContent = `yuhwa error: ${msg}`;
|
|
148
|
+
this.style.color = "crimson";
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
if (typeof customElements !== "undefined" && !customElements.get("yuhwa-diagram")) customElements.define("yuhwa-diagram", yuhwaDiagram);
|
|
152
|
+
if (typeof document !== "undefined") {
|
|
153
|
+
if (!document.currentScript?.hasAttribute("data-manual")) {
|
|
154
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () => run());
|
|
155
|
+
else run();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
export { BOOLS, EDGE_PROPS, ENUMS, GRID, GROUP_PROPS, GUIDE, KNOWN_SETTINGS, NODE_PROPS, NUMBERS, PANELS, PANEL_PROPS, RAMPS, SETTING_VALUES, SHAPES, THEMES, THEME_KEYS, THEME_NAMES, TONES, approxMeasure, blockBraces, builtins, canvasMeasure, chart, charts, createTheme, dashFor, editPart, fetchImports, findBlocks, footer, formatLayout, frames, fromJSON, fromMermaid, getTheme, git, groupPath, hash, isMermaid, isToneOrHex, isyuhwaPath, joinPath, layout, legend, lex, lineStyle, lint, onlyColor, parse, parseInline, parseProps, perPass, processMarkdown, ramp, recolor, registerTheme, registry, render, renderHead, renderSegs, renderSvg, replaceBlockSource, run, segsW, sequence, serializeProps, setLabel, setLineProp, setPartTone, setProp, setSetting, setTrailingProp, splitTrailingProps, tables, themeNames, themes, timeline, toSource, tspans, use, writeLayout, yuhwaDiagram };
|