|
| 1 | +--- |
| 2 | +title: Diagrams |
| 3 | +description: Mermaid diagram blocks for BlockNote — rendered as diagrams in the editor, and exportable to Markdown, PDF, DOCX, ODT, and email. |
| 4 | +--- |
| 5 | + |
| 6 | +# Diagrams |
| 7 | + |
| 8 | +The `@blocknote/diagram-block` package adds a **diagram block**: authored as [Mermaid](https://mermaid.js.org/) source in a source popup, rendered as the diagram it describes — flowcharts, sequence diagrams, Gantt charts, and everything else Mermaid supports. |
| 9 | + |
| 10 | +<Callout type="info"> |
| 11 | + This block is only available in React (`@blocknote/react`). |
| 12 | +</Callout> |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install @blocknote/diagram-block |
| 16 | +``` |
| 17 | + |
| 18 | +## Adding to your editor |
| 19 | + |
| 20 | +The package exports `createReactDiagramBlockSpec`. Add it to your schema's `blockSpecs`: |
| 21 | + |
| 22 | +```tsx |
| 23 | +import { BlockNoteSchema } from "@blocknote/core"; |
| 24 | +import { createReactDiagramBlockSpec } from "@blocknote/diagram-block"; |
| 25 | + |
| 26 | +const schema = BlockNoteSchema.create().extend({ |
| 27 | + blockSpecs: { |
| 28 | + // Adds the Diagram block to the schema. |
| 29 | + diagram: createReactDiagramBlockSpec(), |
| 30 | + }, |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +To highlight the Mermaid source in the popup, add the [syntax highlighting](/docs/features/blocks/code-blocks#syntax-highlighting) extension to your editor. The diagram block already declares its source language (`mermaid`), so no per-block configuration is needed: |
| 35 | + |
| 36 | +```tsx |
| 37 | +import { syntaxHighlighter } from "@blocknote/code-block"; |
| 38 | + |
| 39 | +const editor = useCreateBlockNote({ |
| 40 | + schema, |
| 41 | + extensions: [syntaxHighlighter], |
| 42 | +}); |
| 43 | +``` |
| 44 | + |
| 45 | +## Menu items & localization |
| 46 | + |
| 47 | +Because the diagram spec lives in an optional package, its editor integrations are opt-in too — the package exports everything needed: |
| 48 | + |
| 49 | +```tsx |
| 50 | +import { |
| 51 | + getDiagramSlashMenuItems, // Slash Menu item for inserting a diagram |
| 52 | + getDiagramBlockTypeSelectItems, // Block Type Select item for the Formatting Toolbar |
| 53 | + locales as diagramLocales, // dictionary strings, merged under the `diagram` key |
| 54 | +} from "@blocknote/diagram-block"; |
| 55 | +``` |
| 56 | + |
| 57 | +- `getDiagramSlashMenuItems(editor)` returns a [Slash Menu](/docs/react/components/suggestion-menus#slash-menu) item for inserting a diagram — combine it with the default items via `combineByGroup`. |
| 58 | +- `getDiagramBlockTypeSelectItems(editor)` returns a [Block Type Select](/docs/react/components/formatting-toolbar) item for turning a block into a diagram, to spread alongside the defaults. |
| 59 | +- `diagramLocales` translates the diagram strings — merge a locale into the editor's `dictionary` under the `diagram` key (see [Localization](/docs/features/localization)); without one, the bundled English strings are used. |
| 60 | + |
| 61 | +The example below wires them all up. |
| 62 | + |
| 63 | +## Example |
| 64 | + |
| 65 | +<Example name="custom-schema/diagram-block" /> |
| 66 | + |
| 67 | +## Exporting |
| 68 | + |
| 69 | +Diagrams export to every format BlockNote supports. [Markdown](/docs/features/export/markdown) works out of the box — diagrams export as ` ```mermaid ` fenced code blocks, their common Markdown notation. |
| 70 | + |
| 71 | +The [PDF](/docs/features/export/pdf), [DOCX](/docs/features/export/docx), [ODT](/docs/features/export/odt), and [email](/docs/features/export/email) exporters embed the diagram as an image via their mappings — they live as subpaths of this package, and each exports a `createDiagramBlockMapping` factory to spread into the exporter's default mappings. The [DOCX exporter](/docs/features/export/docx) shown here; the [PDF](/docs/features/export/pdf), [ODT](/docs/features/export/odt), and [email](/docs/features/export/email) exporters work the same way with their respective subpaths: |
| 72 | + |
| 73 | +```typescript |
| 74 | +import { |
| 75 | + DOCXExporter, |
| 76 | + docxDefaultSchemaMappings, |
| 77 | +} from "@blocknote/xl-docx-exporter"; |
| 78 | +import { createDiagramBlockMapping } from "@blocknote/diagram-block/docx-exporter"; |
| 79 | +// ...or "@blocknote/diagram-block/pdf-exporter", |
| 80 | +// "@blocknote/diagram-block/odt-exporter", |
| 81 | +// "@blocknote/diagram-block/email-exporter" |
| 82 | + |
| 83 | +const exporter = new DOCXExporter(editor.schema, { |
| 84 | + ...docxDefaultSchemaMappings, |
| 85 | + blockMapping: { |
| 86 | + ...docxDefaultSchemaMappings.blockMapping, |
| 87 | + diagram: createDiagramBlockMapping(), |
| 88 | + }, |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +The factory takes one option: |
| 93 | + |
| 94 | +```typescript |
| 95 | +createDiagramBlockMapping(options?: { |
| 96 | + /** |
| 97 | + * Renders the Mermaid source to an image. Defaults to the built-in |
| 98 | + * Mermaid renderer, which only works in the browser - see "Exporting |
| 99 | + * server-side" below. |
| 100 | + */ |
| 101 | + renderDiagram?: RenderDiagram; |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +Invalid Mermaid sources render an error placeholder identifying the offending source, mirroring the editor. |
| 106 | + |
| 107 | +The email subpath's factory additionally takes an `imageDelivery` option: some email clients don't display the default data URL images, and the generated images can be delivered as inline `cid:` attachments instead — see [image delivery](/docs/features/export/email#math--diagram-blocks) on the email page. |
| 108 | + |
| 109 | +### Exporting server-side |
| 110 | + |
| 111 | +Rendering Mermaid source to an image requires a browser, so the built-in renderer only works for client-side exports. When exporting server-side, pass a `renderDiagram` function to `createDiagramBlockMapping` — without one, a server-side export throws: |
| 112 | + |
| 113 | +```typescript |
| 114 | +import { createDiagramBlockMapping } from "@blocknote/diagram-block/docx-exporter"; |
| 115 | +import type { RenderDiagram } from "@blocknote/diagram-block/docx-exporter"; |
| 116 | + |
| 117 | +const renderDiagram: RenderDiagram = async (source) => { |
| 118 | + // Render the Mermaid source to an image with your renderer of choice. |
| 119 | + return { |
| 120 | + image: { data: pngBytes, mimeType: "image/png", width, height }, |
| 121 | + }; |
| 122 | +}; |
| 123 | + |
| 124 | +createDiagramBlockMapping({ renderDiagram }); |
| 125 | +``` |
| 126 | + |
| 127 | +Common choices for the server-side renderer are [`@mermaid-js/mermaid-cli`](https://github.com/mermaid-js/mermaid-cli) (renders in a headless browser) or a [Kroki](https://kroki.io) server. Invalid Mermaid source is an expected failure — return it as `{ error }` rather than throwing, and the export renders the error placeholder for that block instead of failing. |
0 commit comments