Skip to content

Embedding ​

Script tag ​

One file renders every diagram on the page. It needs no build step and has no dependencies.

html
<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:

html
<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>:

html
<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 the theme or src attribute re-renders the diagram.
  • Each render fires a yuhwa:render event. Its detail holds the diagnostics and the detected format.
  • The diagram renders into light DOM, so page CSS can size it.
  • The element gets role="img" and an aria-label from the title: setting.
  • zoom makes it a pan and zoom viewer, see Zoom and pan.
  • render="png" or render="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 take data-render in the same way.
html
<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.

html
<!-- 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:

js
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;
  • +, -, 0 and 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.

html
<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:

html
<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.

js
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
optiondefaulteffect
heightfixed height, any CSS length; the diagram is fitted inside it
min1smallest zoom; the diagram never shrinks below its fitted size
max8largest zoom
wheelfalsezoom on a plain wheel, not only with Ctrl/⌘
controlstrueshow 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:

sh
npx yuhwa md README.md --view
sh
pnpm dlx yuhwa md README.md --view
md
[![architecture](diagrams/readme-1.svg)](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 ​

js
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.

js
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.

sh
npx yuhwa serve --port 4749
npx yuhwa url diagram.yu   # prints the image URL for a file
sh
pnpm dlx yuhwa serve --port 4749
pnpm dlx yuhwa url diagram.yu   # prints the image URL for a file
md
![flow](http://localhost:4749/svg?src=a%20-%3E%20b%20-%3E%20c)
![flow](http://localhost:4749/yuhwa/svg/eNpLVNC1U0gCEckADtgCfQ)
routeinput
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 /svgsource as the body
POST /checksource as the body; returns { ok, format, diagnostics }
GET /llms.txt, GET /guidelanguage 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-errors header and drawn into the SVG.
  • The service binds to 127.0.0.1. Pass --host 0.0.0.0 to expose it.
  • A Kroki integration that accepts a custom server URL can use the type yuhwa.

Released under the MIT License.