Embedding
Script tag
One file renders every diagram on the page. It needs no build step and has no dependencies.
<script src="https://cdn.jsdelivr.net/npm/yuhwa/dist/yuhwa.iife.js"></script>
<pre><code class="language-yuhwa">
web "Web" -> api "API" -> db "DB" [shape: cylinder]
</code></pre>The script renders pre > code.language-yuhwa (and language-yu), pre.yuhwa and div.yuhwa. HTML generated from Markdown therefore works unchanged. Without a theme: setting, each diagram uses paper or midnight to match the reader's colour scheme.
Add data-manual to the script tag to render yourself. The global Yuhwa holds the whole API:
<script src="yuhwa.iife.js" data-manual></script>
<script>
const { svg, diagnostics } = Yuhwa.render('a -> b', { theme: 'pastel' });
Yuhwa.run(document.querySelector('#docs'));
</script>Web component
The same script defines <yuhwa-diagram>:
<yuhwa-diagram theme="pastel"> web "Web" -> api "API" -> db "DB" [shape: cylinder] </yuhwa-diagram>
<yuhwa-diagram src="docs/architecture.yu"></yuhwa-diagram>- Setting
el.source = '…'or changing thethemeorsrcattribute re-renders the diagram. - Each render fires a
yuhwa:renderevent. Itsdetailholds the diagnostics and the detected format. - The diagram renders into light DOM, so page CSS can size it.
- The element gets
role="img"and anaria-labelfrom thetitle:setting. zoommakes it a pan and zoom viewer, see Zoom and pan.render="png"orrender="canvas"shows the diagram as a PNG image or a canvas instead of an inline SVG. The SVG shows first. Then the drawing, with the page's web fonts embedded, replaces it. Script-tag blocks takedata-renderin the same way.
<yuhwa-diagram render="canvas">a -> b</yuhwa-diagram>
<pre class="yuhwa" data-render="png">a -> b</pre>Imports
With src, the element also fetches every import "..." in the file, and their imports, relative to the src URL. Then it renders once.
<!-- docs/checkout.yu starts with: import "./shared/services.yu" -->
<yuhwa-diagram src="docs/checkout.yu"></yuhwa-diagram>Inline source has no file to resolve against. Use src, or call fetchImports yourself. A file that fails to load becomes an error on its import line.
With a bundler, import the module once to register the element:
import 'yuhwa/browser';Zoom and pan
A large diagram in a small column is hard to read. A viewer keeps it on the page at a fixed height and lets the reader look around:
- drag to pan;
- pinch, or Ctrl/⌘ + scroll, to zoom (a plain scroll still scrolls the page);
- double-click to zoom in at the cursor;
+,-,0and the arrow keys once it has focus;- buttons for zoom, reset and full screen.
Web component
Add zoom to <yuhwa-diagram>. Its value is the height: a number of pixels or any CSS length. Without one the viewer keeps the diagram's own height. wheel lets the plain mouse wheel zoom, for pages where the viewer is the whole point.
<yuhwa-diagram src="docs/architecture.yu" zoom="480"></yuhwa-diagram>
<yuhwa-diagram src="docs/architecture.yu" zoom="100vh" wheel></yuhwa-diagram>Script tag
Blocks rendered by the script tag take a data-zoom attribute:
<pre class="yuhwa" data-zoom="400">
…
</pre>Any element
zoomable turns any element that holds a diagram, inline SVG or <img>, into a viewer. It returns a handle with zoom, reset and destroy.
import { zoomable } from 'yuhwa/zoom'; // or Yuhwa.zoomable from the script tag
const viewer = zoomable(document.querySelector('#map'), { height: '480px' });
viewer.zoom(2); // zoom in around the centre
viewer.destroy(); // back to the plain diagram| option | default | effect |
|---|---|---|
height | fixed height, any CSS length; the diagram is fitted inside it | |
min | 1 | smallest zoom; the diagram never shrinks below its fitted size |
max | 8 | largest zoom |
wheel | false | zoom on a plain wheel, not only with Ctrl/⌘ |
controls | true | show the zoom and full-screen buttons |
Docs sites
The docs integrations take a zoom flag on a fence, ```yuhwa zoom=480, and zoom and zoomHeight options. See Zoom and pan there.
On GitHub
A README on GitHub cannot run scripts, so it cannot hold a viewer. It can link to one. yuhwa md --view links each rendered image to the hosted viewer, so a click opens the diagram full window:
npx yuhwa md README.md --viewpnpm dlx yuhwa md README.md --view[](https://ph1p.github.io/yuhwa/view/#z=eNpLVNC1U0gCEckADtgCfQ)The diagram travels in the link after the #. Browsers do not send that part, so it never reaches a server. yuhwa url --view diagram.yu prints such a link for one file. With --view=http://localhost:4749 (or url --view --base …) the links point at your own yuhwa serve instead.
Blocks that import other files get no link, because the viewer only sees the link's text.
yuhwa render big.yu -o big.html writes the same viewer as a single HTML file, for sharing or attaching.
JavaScript and TypeScript
import { render } from 'yuhwa';
const { svg, diagnostics } = render(source, { theme: 'blueprint' });The module has no DOM access and no dependencies. It runs in Node, Deno, Bun, Cloudflare Workers and browsers. render never throws on bad input. Problems come back as diagnostics. See the API reference.
Markdown pipelines
The package ships ready-made plugins for markdown-it and remark, plus one-line setups for VitePress, VuePress, Astro, Starlight, Docusaurus, Vocs, Nextra and more. See Docs sites and frameworks.
import yuhwa from 'yuhwa/markdown-it'; // md.use(yuhwa)
import remarkYuhwa from 'yuhwa/remark'; // remarkPlugins: [remarkYuhwa]Image URLs
yuhwa serve starts an HTTP render service. Anything that shows an image can then show a diagram: wikis, issue trackers, chat, email, slides.
npx yuhwa serve --port 4749
npx yuhwa url diagram.yu # prints the image URL for a filepnpm dlx yuhwa serve --port 4749
pnpm dlx yuhwa url diagram.yu # prints the image URL for a file
| route | input |
|---|---|
GET /svg?src=…&theme=… | URL-encoded source |
GET /svg/<base64url> | base64url source, optional .svg suffix |
GET /yuhwa/svg/<deflate+base64url> | compressed source, Kroki-compatible |
GET /view?src=…, /view/<base64url>, /yuhwa/view/<deflate+base64url> | an HTML page with the diagram in a pan and zoom viewer |
POST /svg | source as the body |
POST /check | source as the body; returns { ok, format, diagnostics } |
GET /llms.txt, GET /guide | language reference |
GET /health | { "ok": true } |
A POST body is the raw source, a JSON object { "source": "…" } (with content-type: application/json) or a form field source=…. ?theme= works on every route.
- Responses allow any origin (CORS).
- GET responses are cacheable forever, because the output is deterministic.
- Errors are reported in the
x-yuhwa-errorsheader and drawn into the SVG. - The service binds to
127.0.0.1. Pass--host 0.0.0.0to expose it. - A Kroki integration that accepts a custom server URL can use the type
yuhwa.