Skip to main content

Get started

Diya is a JavaScript library: render takes Diya text and returns an SVG element.

Try it in the browser​

The Examples page has every example in an editor: change the text and the diagram follows. It's the quickest way to learn the language.

Install​

Diya isn't released yet: the diya-js package on npm is a placeholder. Until the first release, build it from source:

git clone https://github.com/pearmini/diya.git
cd diya
pnpm install
pnpm prepublishOnly # builds dist/diya.umd.js and dist/diya.umd.min.js

Render a diagram​

render returns an SVGSVGElement; add it to the page:

import {render} from "diya-js";

const svg = render(`
Mercury
A B C D

--- relations
stack [A..D] {gap=50}
`);

document.body.append(svg);

The UMD build exposes the same functions as the global diya:

<script src="dist/diya.umd.min.js"></script>
<script>
document.body.append(diya.render("A B C"));
</script>

Diya measures text with a canvas, so it needs a browser (or a DOM with canvas, such as jsdom with a canvas stand-in, in tests).

Generate text from data​

Data-driven diagrams are JavaScript that writes Diya text. The diya tagged template makes that easier: an array interpolates as one line per item.

import {diya, render} from "diya-js";

const steps = [
[3, 1, 2],
[1, 3, 2],
[1, 2, 3],
];
const svg = render(diya`
${steps.map((values) => values.join(" "))}

--- styles
1, 2, 3: circle r=14 fill=#dbeafe // every 1, 2 and 3
`);

Errors​

Invalid text throws a DiyaError whose message starts with the line it's on:

render(`A B
--- relations
arrow A`);
// DiyaError: Line 3: arrow needs at least two targets.

In Markdown​

This site writes its diagrams in Markdown Genji code blocks: a fence written ```diya eval t=diya shows the code and renders the diagram under it. The site's genji.config.js passes the text to render.