@benjilegnard/reveal.js-mermaid-plugin 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # @benjilegnard/reveal.js-mermaid-plugin
2
+
3
+ This is a [reveal.js](https://revealjs.com/) plugin to render [mermaid](http://mermaid.ai) diagrams.
4
+
5
+ Its main goal is to have modern ESM support for mermaid, allow you to embed any mermaid version (>=10), and use internal reveal.js types for typed configuration.
6
+
7
+ ## Demo
8
+
9
+ Visible on <https://benjilegnard.github.io/reveal.js-mermaid-plugin/>
10
+
11
+ ## Installation
12
+
13
+ ```sh
14
+ npm i @benjilegnard/reveal.js-mermaid-plugin mermaid reveal.js
15
+ ```
16
+
17
+ `mermaid` (>= 10) and `reveal.js` (>= 6) are peer dependencies.
18
+
19
+ ## Usage
20
+
21
+ Register the plugin **after** `Markdown` and **before** `Highlight`: it needs the
22
+ markdown to be converted, and must replace the code blocks before highlight.js
23
+ processes them.
24
+
25
+ ```ts
26
+ import Reveal from "reveal.js";
27
+ import Markdown from "reveal.js/plugin/markdown";
28
+ import Highlight from "reveal.js/plugin/highlight";
29
+ import Mermaid from "@benjilegnard/reveal.js-mermaid-plugin";
30
+
31
+ const deck = new Reveal({
32
+ plugins: [Markdown, Mermaid, Highlight],
33
+ });
34
+
35
+ deck.initialize({
36
+ // passed as is to mermaid.initialize()
37
+ mermaid: {
38
+ theme: "dark",
39
+ // ... pass here any mermaid configuration
40
+ },
41
+ });
42
+ ```
43
+
44
+ Then write mermaid code blocks in your markdown slides:
45
+
46
+ ````md
47
+ ```mermaid
48
+ flowchart LR
49
+ A --> B
50
+ ```
51
+ ````
52
+
53
+ Each block is replaced by a `<div class="mermaid">` containing the inline SVG.
54
+ If a diagram fails to render, the error is logged in the console, the source
55
+ stays visible and its `<pre>` gets a `mermaid-error` class.
56
+
57
+ Without the Markdown plugin, you just need to have html `<pre>`+`<code>` blocks:
58
+
59
+ ```html
60
+ <pre>
61
+ <code class="mermaid">
62
+ flowchart LR
63
+ A --> B
64
+ </code>
65
+ </pre>
66
+ ```
67
+
68
+ ## TypeScript
69
+
70
+ Importing the plugin augments the reveal.js `RevealConfig` interface with a typed
71
+ `mermaid` key (`MermaidConfig` from mermaid), so `deck.initialize({ mermaid: ... })`
72
+ is type checked.
73
+
74
+ `reveal.js` type definitions use extensionless relative imports: they only resolve
75
+ with `"moduleResolution": "bundler"` (the Vite default). With `node16`/`nodenext`,
76
+ `RevealConfig` resolves to `any` and no option is checked.
77
+
78
+ ## Alternatives
79
+
80
+ - <https://github.com/zjffun/reveal.js-mermaid-plugin> : (embeds an hard-coded old version of mermaidjs)
81
+ - <https://github.com/ludwick/reveal.js-mermaid-plugin> : (retired)
@@ -0,0 +1,30 @@
1
+ import type { RevealPlugin } from "reveal.js";
2
+ import { type MermaidConfig } from "mermaid";
3
+ /**
4
+ * Options of the plugin, set with the `mermaid` key of the reveal.js config.
5
+ * Passed as is to `mermaid.initialize()`.
6
+ */
7
+ export type MermaidPluginConfig = MermaidConfig;
8
+ export interface MermaidPlugin extends RevealPlugin {
9
+ id: "mermaid";
10
+ }
11
+ declare module "reveal.js" {
12
+ interface RevealConfig {
13
+ /**
14
+ * Mermaid plugin configuration
15
+ *
16
+ * @see {@link https://mermaid.js.org/config/schema-docs/config.html}
17
+ */
18
+ mermaid?: MermaidPluginConfig;
19
+ }
20
+ }
21
+ /**
22
+ * Renders ```mermaid code blocks produced by the markdown plugin
23
+ * (marked outputs them as <pre><code class="mermaid">) into inline SVG.
24
+ *
25
+ * Must be registered after Markdown and before Highlight, so it runs once
26
+ * slides are converted and before highlight.js touches the code blocks.
27
+ */
28
+ declare const Mermaid: () => MermaidPlugin;
29
+ export default Mermaid;
30
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAa,YAAY,EAAE,MAAM,WAAW,CAAC;AACzD,OAAgB,EAAE,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAEtD;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,aAAa,CAAC;AAEhD,MAAM,WAAW,aAAc,SAAQ,YAAY;IACjD,EAAE,EAAE,SAAS,CAAC;CACf;AAED,OAAO,QAAQ,WAAW,CAAC;IACzB,UAAU,YAAY;QACpB;;;;WAIG;QACH,OAAO,CAAC,EAAE,mBAAmB,CAAC;KAC/B;CACF;AAED;;;;;;GAMG;AACH,QAAA,MAAM,OAAO,QAAO,aAkClB,CAAC;eAEY,OAAO"}
package/dist/index.js ADDED
@@ -0,0 +1,36 @@
1
+ import mermaid, {} from "mermaid";
2
+ /**
3
+ * Renders ```mermaid code blocks produced by the markdown plugin
4
+ * (marked outputs them as <pre><code class="mermaid">) into inline SVG.
5
+ *
6
+ * Must be registered after Markdown and before Highlight, so it runs once
7
+ * slides are converted and before highlight.js touches the code blocks.
8
+ */
9
+ const Mermaid = () => ({
10
+ id: "mermaid",
11
+ async init(deck) {
12
+ mermaid.initialize({ startOnLoad: false, ...deck.getConfig().mermaid });
13
+ const blocks = deck
14
+ .getRevealElement()
15
+ ?.querySelectorAll("pre > code.mermaid, pre > code.language-mermaid") ?? [];
16
+ let index = 0;
17
+ for (const code of blocks) {
18
+ const pre = code.parentElement;
19
+ const container = document.createElement("div");
20
+ container.classList.add("mermaid");
21
+ try {
22
+ const { svg, bindFunctions } = await mermaid.render(`reveal-mermaid-${index++}`, code.textContent ?? "");
23
+ container.innerHTML = svg;
24
+ pre.replaceWith(container);
25
+ bindFunctions?.(container);
26
+ }
27
+ catch (error) {
28
+ // Keep the source visible so the error is easy to spot on the slide
29
+ console.error("[mermaid] failed to render diagram", error);
30
+ pre.classList.add("mermaid-error");
31
+ }
32
+ }
33
+ },
34
+ });
35
+ export default Mermaid;
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"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ACH,CAAC;CACF,CAAC,CAAC;AAEH,eAAe,OAAO,CAAC"}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@benjilegnard/reveal.js-mermaid-plugin",
3
+ "version": "0.2.0",
4
+ "description": "reveal.js plugin rendering mermaid diagrams from code blocks as inline SVG",
5
+ "keywords": [
6
+ "diagram",
7
+ "markdown",
8
+ "mermaid",
9
+ "reveal.js",
10
+ "reveal.js-plugin",
11
+ "slides"
12
+ ],
13
+ "license": "MIT",
14
+ "author": "Benjamin Legrand",
15
+ "files": [
16
+ "dist",
17
+ "src"
18
+ ],
19
+ "type": "module",
20
+ "sideEffects": false,
21
+ "main": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ },
28
+ "./package.json": "./package.json"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "devDependencies": {
34
+ "mermaid": "^12.0.0",
35
+ "reveal.js": "^6.0.2",
36
+ "typescript": "^7.0.2"
37
+ },
38
+ "peerDependencies": {
39
+ "mermaid": ">=10",
40
+ "reveal.js": ">=6"
41
+ },
42
+ "devEngines": {
43
+ "packageManager": {
44
+ "name": "pnpm",
45
+ "version": "11.24.0",
46
+ "onFail": "download"
47
+ }
48
+ },
49
+ "scripts": {
50
+ "build": "tsc",
51
+ "dev": "tsc --watch",
52
+ "typecheck": "tsc --noEmit"
53
+ }
54
+ }
package/src/index.ts ADDED
@@ -0,0 +1,68 @@
1
+ import type { RevealApi, RevealPlugin } from "reveal.js";
2
+ import mermaid, { type MermaidConfig } from "mermaid";
3
+
4
+ /**
5
+ * Options of the plugin, set with the `mermaid` key of the reveal.js config.
6
+ * Passed as is to `mermaid.initialize()`.
7
+ */
8
+ export type MermaidPluginConfig = MermaidConfig;
9
+
10
+ export interface MermaidPlugin extends RevealPlugin {
11
+ id: "mermaid";
12
+ }
13
+
14
+ declare module "reveal.js" {
15
+ interface RevealConfig {
16
+ /**
17
+ * Mermaid plugin configuration
18
+ *
19
+ * @see {@link https://mermaid.js.org/config/schema-docs/config.html}
20
+ */
21
+ mermaid?: MermaidPluginConfig;
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Renders ```mermaid code blocks produced by the markdown plugin
27
+ * (marked outputs them as <pre><code class="mermaid">) into inline SVG.
28
+ *
29
+ * Must be registered after Markdown and before Highlight, so it runs once
30
+ * slides are converted and before highlight.js touches the code blocks.
31
+ */
32
+ const Mermaid = (): MermaidPlugin => ({
33
+ id: "mermaid",
34
+
35
+ async init(deck: RevealApi) {
36
+ mermaid.initialize({ startOnLoad: false, ...deck.getConfig().mermaid });
37
+
38
+ const blocks =
39
+ deck
40
+ .getRevealElement()
41
+ ?.querySelectorAll<HTMLElement>(
42
+ "pre > code.mermaid, pre > code.language-mermaid",
43
+ ) ?? [];
44
+
45
+ let index = 0;
46
+ for (const code of blocks) {
47
+ const pre = code.parentElement!;
48
+ const container = document.createElement("div");
49
+ container.classList.add("mermaid");
50
+
51
+ try {
52
+ const { svg, bindFunctions } = await mermaid.render(
53
+ `reveal-mermaid-${index++}`,
54
+ code.textContent ?? "",
55
+ );
56
+ container.innerHTML = svg;
57
+ pre.replaceWith(container);
58
+ bindFunctions?.(container);
59
+ } catch (error) {
60
+ // Keep the source visible so the error is easy to spot on the slide
61
+ console.error("[mermaid] failed to render diagram", error);
62
+ pre.classList.add("mermaid-error");
63
+ }
64
+ }
65
+ },
66
+ });
67
+
68
+ export default Mermaid;