| 开发者 | land0r |
|---|---|
| 更新时间 | 2026年11月9日 03:38 |
| PHP版本: | 7.4 及以上 |
| WordPress版本: | 7.1 |
| 版权: | GPL-3.0-or-later |
| 版权网址: | 版权信息 |
[graphdraw] shortcode for the classic editor, page builders and text widgets.language-mermaid code block from a Markdown import, is shown as a diagram.%%{init}%% directive, counts the same way.themeVariables.$$ are typeset with Temml, as MathML that the browser draws in its own math font. Temml is loaded only on a page whose diagrams have a formula. Mermaid.js keeps its own formulas, drawn by KaTeX.click with a JavaScript callback is dropped, as Mermaid.js does at its strict security level. Links (click A "https://…") work.+, -, 0 and the arrow keys once it has focus.graphdraw_renderer filter. It gets the site's renderer, mermaid or pele, and returns one of them; anything else means Mermaid.js:
add_filter( 'graphdraw_renderer', function ( $renderer ) {
return is_page( 'landing' ) ? 'pele' : $renderer;
} );
Pele's options
A developer can set what Pele may do with the graphdraw_pele_options filter. It applies only when the renderer is Pele. People who write diagrams can't set these options: the site decides what a diagram may do, such as showing links and images.
- links, images: true or false, to allow links and images in diagrams.
- linkSchemes, imageSchemes: lists of the URL schemes they may use, such as https. A list with anything but strings in it is ignored.
- linkRel: the rel of links.
- fontSize, padding, directionBreakpoint: positive numbers. directionBreakpoint is the width in pixels below which a wide diagram may turn vertical.
- autoDirection: true or false, to turn that off.
- limit, outputLimit, maxEdges: positive whole numbers, Pele's limits.
- config: an array of Mermaid configuration used when a diagram sets none.
A key Pele doesn't know, or a value of the wrong type, is ignored. The plugin sets the font, the measuring, the ids and the responsive layout itself, and they win over the filter:
add_filter( 'graphdraw_pele_options', function ( $options ) {
$options['links'] = false;
$options['fontSize'] = 15;
return $options;
} );
Styling Pele diagrams with CSS
Pele draws with CSS variables, so a theme can restyle its diagrams. CSS written for Mermaid.js's SVG doesn't apply to Pele's, which has other elements and classes.
- Colours: --pele-bg, --pele-fg, --pele-muted, --pele-line, --pele-surface, --pele-surface-alt, --pele-border, --pele-accent, --pele-critical, --pele-error, and --pele-series-1 to --pele-series-8 for charts.
- Fonts: --pele-font for labels and --pele-font-mono for monospace labels.
- Weights and corners: --pele-title-weight, --pele-heading-weight, --pele-bold-weight and --pele-radius.
- Classes: .pele on the SVG, and .pele-node, .pele-edge, .pele-edge-label, .pele-cluster, .pele-cluster-label, .pele-label, .pele-title, .pele-marker, .pele-icon and .pele-math on its parts. A node also has a data-id, its id in the code, so [data-id="start"] picks one node. Pele's page about theming lists them all.
The plugin puts the light or dark palette on the diagram's canvas, .graphdraw-diagram__canvas, as inline variables, which beat a rule on the canvas or on an element above it. To change a colour, set the variable on the SVG inside it:
.graphdraw-diagram__canvas > svg {
--pele-accent: #c2410c;
--pele-radius: 8px;
}
Fonts are different: labels are measured in the font before they are drawn. The plugin reads --pele-font and --pele-font-mono on the diagram, .graphdraw-diagram, or else uses the diagram's own font, and writes the font it measured in into the SVG. So set them on .graphdraw-diagram, not on the SVG. A download takes the values the diagram has on the page, so your CSS counts in it too.
Icons:
A developer can register icon packs with the graphdraw_icon_packs filter, for diagrams drawn by either renderer. A pack is the URL of an Iconify JSON file, by the name diagrams use before the colon:
add_filter( 'graphdraw_icon_packs', function ( $packs ) {
$packs['fa'] = 'https://cdn.jsdelivr.net/npm/@iconify-json/fa6-solid@1.2.4/icons.json';
$packs['logos'] = 'https://cdn.jsdelivr.net/npm/@iconify-json/logos@1.2.17/icons.json';
return $packs;
} );
Then a diagram can use fa:fa-car in a label, an icon shape such as A@{ icon: "fa:user" }, and (logos:aws) in an architecture diagram. A pack's name has lower-case letters, digits and hyphens. A pack is fetched only on a page whose diagrams use one of its icons. Icons are drawn as shapes only: scripts, event handlers and outside links in a pack's markup are removed before it reaches the page. People who write diagrams can't add packs.
Shortcode:
Where the block can't be used, write the diagram between [graphdraw] and [/graphdraw]:
[graphdraw caption="Checkout" zoom download]
flowchart LR
A[Cart] --> B{Paid?}
B -->|Yes| C[Done]
[/graphdraw]
Its attributes are the settings of the block:
theme: a Mermaid theme, such as forest or dark. Without one, the theme set for the site.caption: a caption under the diagram.zoom and download: zoom controls and download buttons for visitors. Write them without a value, or as zoom="1".title and description: what screen readers read for the diagram.align: center, wide or full.language-mermaid (under Advanced, in the settings of the block),
- a <pre> element with that class, or whose <code> element has it, as in <pre><code class="language-mermaid">.
The post and the editor keep the code. These diagrams have the theme set for the site, and no controls. To give one its own settings, select the Code block and transform it into a Mermaid Diagram block.
Abilities for AI agents:
On WordPress 6.9 and later, the plugin registers abilities with the WordPress Abilities API, in the "Mermaid diagrams" category. AI agents and automation can use them to work with diagrams, for example through the MCP Adapter or the REST API under wp-abilities/v1:
- graphdraw/list-diagram-types: the diagram types with starter code.
- graphdraw/list-diagrams: the diagrams in one post, or in every post that has some.
- graphdraw/get-diagram: one diagram of a post.
- graphdraw/build-block-markup: the block markup of a diagram, for other abilities that write posts.
- graphdraw/insert-diagram: adds a diagram to a post.
- graphdraw/update-diagram: changes the code or the settings of a diagram.
- graphdraw/delete-diagram: removes a diagram from a post.
- graphdraw/validate-diagram: parses a diagram in the block editor and reports its errors (WordPress 7.0 and later). The server can't parse Mermaid, so the other abilities don't validate code.
A diagram is addressed by the ID of its post and its index among the post's diagrams. Changes need the hash a read returned, so a diagram edited in the meantime is never overwritten, and every change leaves a revision. Each ability is open only to those who could make the same change in the editor: users work with the diagrams of the posts they can edit.
Saved images:
A diagram is drawn in the visitor's browser, so a feed reader, or a browser that runs no JavaScript, shows its Mermaid code instead. Switch on "Save an image of each diagram" under Settings → GraphDraw to give those readers a picture:
- The block editor makes a PNG image of each diagram of a post when the post is opened or saved in it. A post written before gets its images the next time it is opened in the editor.
- Feeds show the image in place of the code. On the site the image is in a <noscript> element, so browsers that run JavaScript never load it, and show the live diagram as before.
- An image is of one version of a diagram. When the code or the theme of a diagram is changed anywhere else, through the abilities for example, its image is removed, and those readers see the code again until the post is opened in the editor.
- The text of the image, for those who can't see it, is the description of the diagram, or else its title.
- The images are files in wp-content/uploads/graphdraw/. They are deleted with their post, and with the plugin.
The diagrams of synced patterns, template parts, widgets and shortcodes get no image.
Libraries:
The diagrams are drawn by Mermaid, by the Mermaid project (MIT licence), or by Pele, by Obsidian (MIT licence). With Pele, formulas are typeset by Temml, by Ron Kok (MIT licence). For a diagram that asks for the ELK layout, Mermaid uses ELK, by the Eclipse Foundation, as elkjs (Eclipse Public License 2.0, which allows the GPL as a secondary licence). All four are included in the plugin. GraphDraw is an independent plugin, not affiliated with or endorsed by the Mermaid project. Mermaid's files also include the libraries Mermaid is built with, such as KaTeX, DOMPurify, D3, dagre and Cytoscape.js, under the MIT, ISC, Apache 2.0 and similar licences.
Source code:
The JavaScript in the plugin's build/ folder is compiled. Its source, with the build scripts, is on GitHub: land0r/graphdraw. Run npm ci && composer install && npm run build there to build it.
/wp-content/plugins/graphdraw-flowcharts-for-mermaid/.Every type of the Mermaid version the plugin has, Mermaid 12.2: flowcharts, sequence, class, state, entity relationship, user journey, Gantt, pie, quadrant, requirement, C4, Sankey, XY, block, packet, kanban, architecture, radar, treemap, use case, agent flow, swimlane, event modeling, Ishikawa, Venn, Cynefin, railroad and tree view diagrams, mind maps, timelines, Wardley maps and Git graphs. Pele draws them all too.
Yes, you can edit Mermaid code directly in the block editor using plain text.
Yes, your diagrams are rendered in real-time as you edit the code.
Yes, it is compatible with most modern WordPress themes.
A page with a diagram gets one script of 8 KB (3.5 KB compressed) and 3.5 KB of styles, and nothing more until a diagram scrolls into view. Then it loads Mermaid, and of Mermaid only what the type of that diagram needs. For a flowchart that is 870 KB of JavaScript, or 240 KB as a server sends it, compressed. Class, state and entity relationship diagrams take as much, sequence diagrams and Gantt charts a little less, and the other types between 190 and 370 KB compressed, except architecture diagrams with 520 KB and use case diagrams with 760 KB. Three quarters of it is the same for every type, so a page with several types loads that part once. Three things are loaded only for a diagram that uses them: formulas (80 KB compressed), the ELK layout (500 KB) and the controls for visitors (4 KB). A page without a diagram loads nothing from the plugin.
If page weight and speed matter more to you than the look of Mermaid.js's themes, yes: choose it under Settings → GraphDraw, look at your diagrams, and switch back if you prefer the old look. Pele draws every diagram type the plugin offers, but in a plainer look of its own, with a light and a dark palette in place of Mermaid's themes. The section "Pele, a lighter renderer" above says what is different.
Yes. Write the formula between $$ in a label. Mermaid.js draws it with KaTeX, Pele with Temml. Either is loaded only on a page whose diagrams have a formula.
Yes, once a developer has registered icon packs with the graphdraw_icon_packs filter. The section "Icons" above shows how. A name that no registered pack has is drawn as before: with Pele, in its own symbol where it has one, such as database in an architecture diagram.
Pele has no named themes, so with Pele the theme settings offer Automatic, Light and Dark. A diagram that had another theme before keeps it, and shows it as Dark if it is Dark, Neo Dark, Redux Dark or Redux Dark Color, and as Light otherwise; switch back to Mermaid.js and it looks as it did. A theme written in the diagram's code counts the same way.
Yes, for one diagram at a time. Start its code with these lines: config: layout: elk Without them, flowcharts and class, state, entity relationship and requirement diagrams get Mermaid's dagre layout. Use case diagrams always get ELK. ELK draws its lines with right angles and often keeps a large diagram tidier, but it is 1.6 MB of JavaScript, twice everything else a flowchart loads, so only the pages with a diagram that asks for it load it.
The Mermaid code of the diagram, since it is the browser that draws it. To show them a picture instead, switch on "Save an image of each diagram" under Settings → GraphDraw, then open your posts in the block editor, which makes the images.
In Elementor, add the Mermaid Diagram widget: it has the block's settings, and its preview shows the diagram as you edit it. Elsewhere, use the [graphdraw] shortcode: put the Mermaid code between [graphdraw] and [/graphdraw]. If a page builder shows the code instead of the diagram, the code has a < that WordPress took for the start of a tag: write it as <.
No. Switch on "Show code marked as Mermaid as diagrams" under Settings → GraphDraw. Code with the language-mermaid class is then shown as a diagram, and the posts stay as they are. The diagram replaces the code before the page is sent, so a script of another plugin that draws such code finds none: it is not drawn twice. To keep some code as code, return false from the graphdraw_show_code_as_diagram filter.
Ask in the plugin's support forum on WordPress.org. Report bugs and suggest features on GitHub: land0r/graphdraw issues. Support covers the plugin itself; help with Mermaid's syntax is in Mermaid's documentation.
[graphdraw] shortcode, a Mermaid Diagram widget for Elementor, and code marked as Mermaid in posts shown as diagrams (off by default).