# Embed the explorer on any site

The interactive explorer is one self-contained HTML file: no server, no CDN, no account. Since 0.4.0 it also has an
embed mode, so the same file can sit inside a docs page, a blog post or a wiki page without dragging in the full
toolbar, every style and a contents drawer that opens by itself.

The page still gets built once, at build time, by the CLI or the MCP server (headless Chrome pre-renders the
drawings). Embedding only adds a compact file and a tiny loader: nothing is rendered in the visitor's browser beyond
what the explorer already does.

## 1. Build an embed-ready file

```bash
diagramsmith interactive map.json --embed -o static/map.html
```

`--embed` gives you:

- **one style**: the resolved preset (pass `--styles` yourself to include more);
- **no auto-opened Contents drawer**, and a body that fills its container (100% width and height, no margins, no
  scrollbars);
- **a compact toolbar** (`--toolbar min` by default): fit, zoom in/out, fullscreen, and an "Open full view" link when
  you pass `--full-url <url>`.

Useful options:

| Option | What it does |
|---|---|
| `--toolbar full\|min\|none` | How much chrome the embedded page shows. `none` hides the toolbar completely. |
| `--full-url <url>` | Adds an "Open full view" link to the compact toolbar, usually the standalone explorer page. |
| `--bg transparent` | A transparent page background, so the host page's colour shows through. |
| `--preset <name>` | Which of the 7 styles the embed uses. |

The command prints the output size. An embed of `docs/demo/checkout-platform.json` is a small fraction of the same
page built with `--styles all`; a test asserts it stays under 40%.

## 2. Put it on the page

### With the loader (recommended)

Copy the loader into the same place as your diagram file:

```bash
diagramsmith embed-script -o static
```

That writes `static/diagramsmith-embed.js` (about 150 lines, no dependencies, no build step). Then add one script tag
and one element:

```html
<script src="/static/diagramsmith-embed.js" defer></script>

<diagramsmith-explorer
  src="/static/map.html"
  height="520"
  toolbar="min"
  theme="auto"
  title="Checkout platform"
></diagramsmith-explorer>
```

You can place as many elements on a page as you like; each gets its own sandboxed, lazy-loaded iframe.

| Attribute | Meaning |
|---|---|
| `src` | The file built with `--embed`. Required. |
| `title` | Visible title and the iframe's accessible name. Defaults to the file name. |
| `height` | Frame height in pixels. Without it the frame keeps a 16:10 aspect ratio. |
| `toolbar` | `full`, `min` (default) or `none`. |
| `theme` | `auto` (default, follows the viewer), `light` or `dark`. |
| `tour` | Add `tour` to start the guided tour when the page loads. |

### With a plain iframe

No script needed:

```html
<iframe
  src="/static/map.html?toolbar=min"
  title="Checkout platform"
  loading="lazy"
  sandbox="allow-scripts allow-popups"
  allow="fullscreen"
  allowfullscreen
  style="width: 100%; height: 520px; border: 1px solid #ccc; border-radius: 10px;"
></iframe>
```

## 3. Change options at view time

The embedded file also reads URL parameters, so one file can serve several pages:

| Parameter | Values | Effect |
|---|---|---|
| `toolbar` | `none`, `min`, `full` | Overrides the built-in toolbar mode. |
| `theme` | `light`, `dark` | Forces the page theme. |
| `tour` | `1` | Starts the guided tour. |

Unknown or invalid values are ignored; the page still loads.

## 4. Site recipes

Each recipe below is the HTML you paste into that tool. These were **not tested in that tool**; the loader and the
`file://` / GitHub Pages versions are covered by the test suite.

### GitHub Pages

Put `map.html` and `diagramsmith-embed.js` in the folder Pages serves (the repository root, or `docs/`), then in your
`index.html`:

```html
<script src="diagramsmith-embed.js" defer></script>
<diagramsmith-explorer src="map.html" height="520" title="My architecture"></diagramsmith-explorer>
```

This is exactly how the live demo works: <https://zune2004.github.io/diagramsmith/>.

### Docusaurus

Put both files in `static/` (`static/map.html`, `static/diagramsmith-embed.js`). In an MDX page:

```mdx
<script src="/diagramsmith-embed.js" defer></script>

<diagramsmith-explorer src="/map.html" height="520" title="My architecture"></diagramsmith-explorer>
```

If MDX complains about the custom element, wrap the element in a small React component that renders the tag with
`dangerouslySetInnerHTML`, or use the plain iframe from section 2.

### MkDocs Material

Put both files in `docs/` and allow raw HTML in `mkdocs.yml`:

```yaml
markdown_extensions:
  - md_in_html
```

Then in a page:

```html
<script src="diagramsmith-embed.js" defer></script>
<diagramsmith-explorer src="map.html" height="520" title="My architecture"></diagramsmith-explorer>
```

### VitePress

Put both files in `docs/public/` and use the plain iframe in Markdown (VitePress does not run custom element markup
from Markdown reliably):

```html
<iframe src="/map.html?toolbar=min" title="My architecture" loading="lazy"
  sandbox="allow-scripts allow-popups" allow="fullscreen" allowfullscreen
  style="width:100%;height:520px;border:1px solid #ccc;border-radius:10px;"></iframe>
```

## What does not work

- **Sites that strip `<script>` or iframes** (many wikis, some CMS rich-text editors, GitHub issue and PR comments).
  Nothing renders there; use the static PNG/SVG output of the same spec instead.
- **Email.** Mail clients strip scripts and usually iframes.
- **Sites with a strict Content-Security-Policy** that blocks inline scripts or third-party frames. The explorer's
  payload is inline by design, so allow it or host it on a page of its own.
- **`file://` if you move only the HTML file** away from the loader or from its fonts; the explorer embeds its fonts,
  but `src` paths are still relative to the page.

## What the page gives search engines and screen readers

Every page (embedded or not) carries a plain HTML outline, uncompressed, with the title, every node's label and
detail, every edge (`from -> to: label`), nested diagrams as nested lists and the steps in order. It is visually
hidden with the standard visually-hidden pattern, then removed from view (not from the DOM) once the explorer starts.
That keeps the text available to crawlers and assistive technology without reading it twice.
