@md-plugins/md-plugin-mermaid 0.1.0-beta.20

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.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024-present, MD-PLUGINS
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
13
+ all 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
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # @md-plugins/md-plugin-mermaid
2
+
3
+ A **Markdown-It** plugin that renders Mermaid fenced code blocks. It is designed for Q-Press and Vue-based Markdown pages, while still allowing plain MarkdownIt users to render Mermaid-compatible `<pre>` blocks.
4
+
5
+ ## Features
6
+
7
+ - Converts `mermaid` and `mmd` fenced code blocks into a configurable component.
8
+ - Adds page import statements for Q-Press generated Vue pages.
9
+ - Supports a plain `<pre class="mermaid">` render mode for custom MarkdownIt pipelines.
10
+
11
+ ## Installation
12
+
13
+ Install the plugin via your preferred package manager:
14
+
15
+ ```bash
16
+ # with pnpm:
17
+ pnpm add @md-plugins/md-plugin-mermaid
18
+ # with bun:
19
+ bun add @md-plugins/md-plugin-mermaid
20
+ # with Yarn:
21
+ yarn add @md-plugins/md-plugin-mermaid
22
+ # with npm:
23
+ npm install @md-plugins/md-plugin-mermaid
24
+ ```
25
+
26
+ If you render Mermaid in the browser, install Mermaid in the consuming app:
27
+
28
+ ```bash
29
+ pnpm add mermaid
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### Q-Press or Vue Component Mode
35
+
36
+ ```ts
37
+ import MarkdownIt from 'markdown-it'
38
+ import { mermaidPlugin } from '@md-plugins/md-plugin-mermaid'
39
+
40
+ const md = new MarkdownIt()
41
+ md.use(mermaidPlugin, {
42
+ componentName: 'MarkdownMermaid',
43
+ })
44
+ ```
45
+
46
+ ### Raw MarkdownIt Mode
47
+
48
+ ```ts
49
+ import MarkdownIt from 'markdown-it'
50
+ import { mermaidPlugin } from '@md-plugins/md-plugin-mermaid'
51
+
52
+ const md = new MarkdownIt()
53
+ md.use(mermaidPlugin, {
54
+ renderMode: 'pre',
55
+ })
56
+ ```
57
+
58
+ Then initialize Mermaid in your application after the HTML is mounted.
59
+
60
+ ## Markdown
61
+
62
+ ````markdown
63
+ ```mermaid
64
+ graph TD
65
+ A[Write Markdown] --> B[Render Diagram]
66
+ ```
67
+ ````
68
+
69
+ ## Options
70
+
71
+ | Option | Type | Default | Description |
72
+ | ------------- | -------------------- | ------------------------ | ----------------------------------------------- |
73
+ | languages | string[] | `['mermaid', 'mmd']` | Fence languages treated as Mermaid diagrams. |
74
+ | renderMode | 'component' \| 'pre' | `'component'` | Output mode for Mermaid diagrams. |
75
+ | componentName | string | `'MarkdownMermaid'` | Component used in component mode. |
76
+ | codeProp | string | `'code'` | Component prop that receives Mermaid source. |
77
+ | preClass | string | `'mermaid'` | CSS class used in pre mode. |
78
+ | pageScripts | string[] | Q-Press component import | Page imports added to generated Vue components. |
79
+
80
+ ## Support
81
+
82
+ If md-plugin-mermaid is useful in your workflow and you want to support ongoing maintenance:
83
+
84
+ GitHub Sponsors: https://github.com/sponsors/hawkeye64
85
+ PayPal: https://paypal.me/hawkeye64
86
+
87
+ ## License
88
+
89
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
@@ -0,0 +1,52 @@
1
+ import { PluginWithOptions } from 'markdown-it';
2
+
3
+ type MermaidRenderMode = 'component' | 'pre';
4
+ interface MermaidPluginOptions {
5
+ /**
6
+ * Languages that should be treated as Mermaid diagrams.
7
+ *
8
+ * @default ['mermaid', 'mmd']
9
+ */
10
+ languages?: string[];
11
+ /**
12
+ * Render as a Vue component or as a plain Mermaid pre block.
13
+ *
14
+ * @default 'component'
15
+ */
16
+ renderMode?: MermaidRenderMode;
17
+ /**
18
+ * The Vue component used when renderMode is "component".
19
+ *
20
+ * @default 'MarkdownMermaid'
21
+ */
22
+ componentName?: string;
23
+ /**
24
+ * The prop name used to pass diagram source into the component.
25
+ *
26
+ * @default 'code'
27
+ */
28
+ codeProp?: string;
29
+ /**
30
+ * The class applied to plain pre blocks when renderMode is "pre".
31
+ *
32
+ * @default 'mermaid'
33
+ */
34
+ preClass?: string;
35
+ /**
36
+ * Page-level import statements required by the component render mode.
37
+ */
38
+ pageScripts?: string[];
39
+ }
40
+ declare module '@md-plugins/shared' {
41
+ interface MarkdownItEnv {
42
+ /**
43
+ * An array of page script import statements to include in generated Vue pages.
44
+ */
45
+ pageScripts?: Set<string>;
46
+ }
47
+ }
48
+
49
+ declare const mermaidPlugin: PluginWithOptions<MermaidPluginOptions>;
50
+
51
+ export { mermaidPlugin };
52
+ export type { MermaidPluginOptions, MermaidRenderMode };
@@ -0,0 +1,52 @@
1
+ import { PluginWithOptions } from 'markdown-it';
2
+
3
+ type MermaidRenderMode = 'component' | 'pre';
4
+ interface MermaidPluginOptions {
5
+ /**
6
+ * Languages that should be treated as Mermaid diagrams.
7
+ *
8
+ * @default ['mermaid', 'mmd']
9
+ */
10
+ languages?: string[];
11
+ /**
12
+ * Render as a Vue component or as a plain Mermaid pre block.
13
+ *
14
+ * @default 'component'
15
+ */
16
+ renderMode?: MermaidRenderMode;
17
+ /**
18
+ * The Vue component used when renderMode is "component".
19
+ *
20
+ * @default 'MarkdownMermaid'
21
+ */
22
+ componentName?: string;
23
+ /**
24
+ * The prop name used to pass diagram source into the component.
25
+ *
26
+ * @default 'code'
27
+ */
28
+ codeProp?: string;
29
+ /**
30
+ * The class applied to plain pre blocks when renderMode is "pre".
31
+ *
32
+ * @default 'mermaid'
33
+ */
34
+ preClass?: string;
35
+ /**
36
+ * Page-level import statements required by the component render mode.
37
+ */
38
+ pageScripts?: string[];
39
+ }
40
+ declare module '@md-plugins/shared' {
41
+ interface MarkdownItEnv {
42
+ /**
43
+ * An array of page script import statements to include in generated Vue pages.
44
+ */
45
+ pageScripts?: Set<string>;
46
+ }
47
+ }
48
+
49
+ declare const mermaidPlugin: PluginWithOptions<MermaidPluginOptions>;
50
+
51
+ export { mermaidPlugin };
52
+ export type { MermaidPluginOptions, MermaidRenderMode };
package/dist/index.mjs ADDED
@@ -0,0 +1,54 @@
1
+ import { resolvePluginOptions } from '@md-plugins/shared';
2
+
3
+ const DEFAULT_MERMAID_PLUGIN_OPTIONS = {
4
+ languages: ["mermaid", "mmd"],
5
+ renderMode: "component",
6
+ componentName: "MarkdownMermaid",
7
+ codeProp: "code",
8
+ preClass: "mermaid",
9
+ pageScripts: ["import MarkdownMermaid from '@/.q-press/components/MarkdownMermaid.vue'"]
10
+ };
11
+ function getFenceLang(token) {
12
+ return token.info.trim().split(/\s+/, 1)[0] ?? "";
13
+ }
14
+ function getVueBinding(md, value) {
15
+ return md.utils.escapeHtml(JSON.stringify(value));
16
+ }
17
+ const mermaidPlugin = (md, options) => {
18
+ const resolvedOptions = resolvePluginOptions(
19
+ options,
20
+ "mermaidPlugin",
21
+ DEFAULT_MERMAID_PLUGIN_OPTIONS
22
+ );
23
+ const {
24
+ languages = DEFAULT_MERMAID_PLUGIN_OPTIONS.languages,
25
+ renderMode = DEFAULT_MERMAID_PLUGIN_OPTIONS.renderMode,
26
+ componentName = DEFAULT_MERMAID_PLUGIN_OPTIONS.componentName,
27
+ codeProp = DEFAULT_MERMAID_PLUGIN_OPTIONS.codeProp,
28
+ preClass = DEFAULT_MERMAID_PLUGIN_OPTIONS.preClass,
29
+ pageScripts = DEFAULT_MERMAID_PLUGIN_OPTIONS.pageScripts
30
+ } = resolvedOptions;
31
+ const mermaidLanguages = new Set(languages);
32
+ const originalFenceRender = md.renderer.rules.fence;
33
+ md.renderer.rules.fence = (tokens, idx, options2, env, self) => {
34
+ const token = tokens[idx];
35
+ if (!token) {
36
+ return "";
37
+ }
38
+ if (mermaidLanguages.has(getFenceLang(token)) === false) {
39
+ return originalFenceRender ? originalFenceRender(tokens, idx, options2, env, self) : self.renderToken(tokens, idx, options2);
40
+ }
41
+ if (renderMode === "pre") {
42
+ return `<pre class="${md.utils.escapeHtml(preClass)}"><code>${md.utils.escapeHtml(token.content)}</code></pre>`;
43
+ }
44
+ if (pageScripts.length > 0) {
45
+ env.pageScripts = env.pageScripts || /* @__PURE__ */ new Set();
46
+ for (const script of pageScripts) {
47
+ env.pageScripts.add(script);
48
+ }
49
+ }
50
+ return `<${componentName} :${codeProp}="${getVueBinding(md, token.content)}"></${componentName}>`;
51
+ };
52
+ };
53
+
54
+ export { mermaidPlugin };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@md-plugins/md-plugin-mermaid",
3
+ "version": "0.1.0-beta.20",
4
+ "description": "A markdown-it plugin for rendering Mermaid diagrams.",
5
+ "keywords": [
6
+ "markdown-it",
7
+ "mermaid",
8
+ "quasarframework",
9
+ "types",
10
+ "vue"
11
+ ],
12
+ "homepage": "https://github.com/md-plugins",
13
+ "bugs": {
14
+ "url": "https://github.com/md-plugins/md-plugins/issues"
15
+ },
16
+ "license": "MIT",
17
+ "author": "hawkeye64 <galbraith64@gmail.com>",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/md-plugins/md-plugins.git"
21
+ },
22
+ "files": [
23
+ "./dist"
24
+ ],
25
+ "type": "module",
26
+ "module": "./dist/index.mjs",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "import": {
31
+ "types": "./dist/index.d.mts",
32
+ "default": "./dist/index.mjs"
33
+ }
34
+ }
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "dependencies": {
40
+ "@md-plugins/shared": "0.1.0-beta.20"
41
+ },
42
+ "devDependencies": {
43
+ "@types/markdown-it": "^14.1.2",
44
+ "markdown-it": "^14.2.0"
45
+ },
46
+ "peerDependencies": {
47
+ "markdown-it": "^14.2.0"
48
+ },
49
+ "scripts": {
50
+ "build": "unbuild",
51
+ "clean": "rm -rf dist/ node_modules/",
52
+ "test": "vitest"
53
+ }
54
+ }