@docmd/plugin-mermaid 0.4.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 docmd (docmd.io)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @docmd/plugin-mermaid
2
+
3
+ Adds [Mermaid.js](https://mermaid.js.org/) diagram support to **docmd**.
4
+
5
+ ## Usage
6
+ Use the `mermaid` code fence in your Markdown:
7
+
8
+ ````
9
+ ```mermaid
10
+ graph TD;
11
+ A-->B;
12
+ A-->C;
13
+ B-->D;
14
+ C-->D;
15
+ ```
16
+ ````
@@ -0,0 +1,52 @@
1
+ // packages/plugins/mermaid/assets/init-mermaid.js
2
+
3
+ import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
4
+
5
+ (async function () {
6
+ 'use strict';
7
+
8
+ // 1. Initialize
9
+ const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default';
10
+
11
+ mermaid.initialize({
12
+ startOnLoad: false,
13
+ theme: theme,
14
+ securityLevel: 'loose',
15
+ fontFamily: 'inherit'
16
+ });
17
+
18
+ // 2. Render Function
19
+ async function render() {
20
+ try {
21
+ // Mermaid 10/11 API: run() handles finding .mermaid classes
22
+ await mermaid.run({
23
+ querySelector: '.mermaid'
24
+ });
25
+ } catch (e) {
26
+ console.error('Mermaid rendering failed:', e);
27
+ }
28
+ }
29
+
30
+ // 3. Theme Observer
31
+ const observer = new MutationObserver(async (mutations) => {
32
+ for (const m of mutations) {
33
+ if (m.attributeName === 'data-theme') {
34
+ // Reload to force re-render with new theme variables
35
+ // (Mermaid doesn't support dynamic theme swapping easily without re-parsing)
36
+ location.reload();
37
+ }
38
+ }
39
+ });
40
+
41
+ observer.observe(document.documentElement, {
42
+ attributes: true,
43
+ attributeFilter: ['data-theme']
44
+ });
45
+
46
+ // 4. Boot
47
+ if (document.readyState === 'loading') {
48
+ document.addEventListener('DOMContentLoaded', render);
49
+ } else {
50
+ render();
51
+ }
52
+ })();
package/index.js ADDED
@@ -0,0 +1,30 @@
1
+ // packages/plugins/mermaid/index.js
2
+ const path = require('path');
3
+
4
+ function markdownSetup(md) {
5
+ const defaultFence = md.renderer.rules.fence;
6
+ md.renderer.rules.fence = (tokens, idx, options, env, self) => {
7
+ const token = tokens[idx];
8
+ const info = token.info.trim();
9
+ if (info === 'mermaid') {
10
+ return `<div class="mermaid">${token.content}</div>`;
11
+ }
12
+ return defaultFence(tokens, idx, options, env, self);
13
+ };
14
+ }
15
+
16
+ function getAssets() {
17
+ return [
18
+ // REMOVED: The direct CDN link to mermaid.min.js
19
+ // We now rely on init-mermaid.js to import it as a module
20
+ {
21
+ src: path.join(__dirname, 'assets/init-mermaid.js'),
22
+ dest: 'assets/js/init-mermaid.js',
23
+ type: 'js',
24
+ location: 'body',
25
+ attributes: { type: 'module' }
26
+ }
27
+ ];
28
+ }
29
+
30
+ module.exports = { markdownSetup, getAssets };
package/package.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@docmd/plugin-mermaid",
3
+ "version": "0.4.0",
4
+ "description": "Mermaid diagram support plugin for docmd",
5
+ "main": "index.js",
6
+ "license": "MIT"
7
+ }