hexo-markdown-mermaid 0.0.1

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,95 @@
1
+ # hexo-markdown-mermaid
2
+
3
+ A Hexo plugin for rendering Mermaid diagrams in your posts.
4
+
5
+ [简体中文](./README.zh-CN.md)
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install hexo-markdown-mermaid
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ Add mermaid code blocks in your posts:
16
+
17
+ ````markdown
18
+ ```mermaid
19
+ graph TD
20
+ A-->B
21
+ A-->C
22
+ B-->D
23
+ C-->D
24
+ ```
25
+ ````
26
+
27
+ ## Configuration
28
+
29
+ ### With Syntax Highlighter (Recommended)
30
+
31
+ Only highlight.js is supported. exclude mermaid so Mermaid.js can render it:
32
+
33
+ ```yaml
34
+ highlight:
35
+ enable: true
36
+ exclude_languages:
37
+ - mermaid
38
+ ```
39
+
40
+ :::warning
41
+ PrismJS is not supported as it does not have `exclude_languages` option.
42
+ :::
43
+
44
+ ### Mermaid Options
45
+
46
+ ```yaml
47
+ mermaid:
48
+ version: 11
49
+ startOnLoad: true
50
+ theme: default
51
+ # Other mermaid config options...
52
+ ```
53
+
54
+ #### Available Options
55
+
56
+ | Option | Description | Default |
57
+ |--------|-------------|---------|
58
+ | `version` | Mermaid version | 11 |
59
+ | `startOnLoad` | Auto-render on page load | true |
60
+ | Other options | See [Mermaid Config](https://mermaid.js.org/config/schema-docs/config.html) | - |
61
+
62
+ ## License
63
+
64
+ MIT
65
+
66
+ ---
67
+
68
+ ## Technical Details
69
+
70
+ ### Why render in browser instead of build-time?
71
+
72
+ Mermaid CLI (mermaid-cli) can pre-render diagrams at build time, but it has heavy dependencies (requires Puppeteer or similar), making it unsuitable for most Hexo setups.
73
+
74
+ This plugin chooses browser-side rendering for simplicity and broad compatibility.
75
+
76
+ ### Why exclude mermaid from syntax highlighter?
77
+
78
+ When using highlight.js with Hexo's `highlight.enable: true`, code blocks are pre-rendered to HTML with each line wrapped in `<span class="line">`:
79
+
80
+ ```html
81
+ <pre><span class="line">graph TD</span>
82
+ <span class="line">A--&gt;B</span></pre>
83
+ ```
84
+
85
+ Mermaid.js searches for elements with class `.mermaid`. After excluding mermaid, code blocks will be rendered as:
86
+
87
+ ```html
88
+ <pre><code class="highlight mermaid">graph TD\nA--&gt;B</code></pre>
89
+ ```
90
+
91
+ This allows Mermaid.js to find and render the diagrams correctly.
92
+
93
+ ### Mermaid Selector
94
+
95
+ This plugin uses Mermaid's default query selector `.mermaid`. See [Mermaid RunOptions](https://mermaid.js.org/config/setup/mermaid/interfaces/RunOptions.html#queryselector).
@@ -0,0 +1,93 @@
1
+ # hexo-markdown-mermaid
2
+
3
+ Hexo 插件,用于在文章中渲染 Mermaid 图表。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install hexo-markdown-mermaid
9
+ ```
10
+
11
+ ## 快速开始
12
+
13
+ 在文章中添加 mermaid 代码块:
14
+
15
+ ````markdown
16
+ ```mermaid
17
+ graph TD
18
+ A-->B
19
+ A-->C
20
+ B-->D
21
+ C-->D
22
+ ```
23
+ ````
24
+
25
+ ## 配置
26
+
27
+ ### 使用语法高亮器(推荐)
28
+
29
+ 仅支持 highlight.js。需排除 mermaid 以便 Mermaid.js 渲染:
30
+
31
+ ```yaml
32
+ highlight:
33
+ enable: true
34
+ exclude_languages:
35
+ - mermaid
36
+ ```
37
+
38
+ :::warning
39
+ PrismJS 不支持,因为没有 `exclude_languages` 选项。
40
+ :::
41
+
42
+ ### Mermaid 选项
43
+
44
+ ```yaml
45
+ mermaid:
46
+ version: 11
47
+ startOnLoad: true
48
+ theme: default
49
+ # 其他 mermaid 配置选项...
50
+ ```
51
+
52
+ #### 可用选项
53
+
54
+ | 选项 | 描述 | 默认值 |
55
+ |------|------|--------|
56
+ | `version` | Mermaid 版本 | 11 |
57
+ | `startOnLoad` | 页面加载时自动渲染 | true |
58
+ | 其他选项 | 参阅 [Mermaid 配置](https://mermaid.js.org/config/schema-docs/config.html) | - |
59
+
60
+ ## 许可
61
+
62
+ MIT
63
+
64
+ ---
65
+
66
+ ## 技术细节
67
+
68
+ ### 为什么在浏览器端渲染而非构建时?
69
+
70
+ Mermaid CLI 可以在构建时预渲染图表,但它依赖较重(需要 Puppeteer 或类似工具),不适合大多数 Hexo 环境。
71
+
72
+ 本插件选择浏览器端渲染,以保持简洁和广泛兼容性。
73
+
74
+ ### 为什么要排除 mermaid?
75
+
76
+ 使用 highlight.js 且 Hexo 配置 `highlight.enable: true` 时,代码块会被预渲染为 HTML,每行被包装在 `<span class="line">` 中:
77
+
78
+ ```html
79
+ <pre><span class="line">graph TD</span>
80
+ <span class="line">A--&gt;B</span></pre>
81
+ ```
82
+
83
+ Mermaid.js 查找 class 为 `.mermaid` 的元素。排除后,代码块会渲染为:
84
+
85
+ ```html
86
+ <pre><code class="highlight mermaid">graph TD\nA--&gt;B</code></pre>
87
+ ```
88
+
89
+ 这样 Mermaid.js 就能正确找到并渲染图表。
90
+
91
+ ### Mermaid 选择器
92
+
93
+ 本插件使用 Mermaid 默认的查询选择器 `.mermaid`。参阅 [Mermaid RunOptions](https://mermaid.js.org/config/setup/mermaid/interfaces/RunOptions.html#queryselector)。
package/index.js ADDED
@@ -0,0 +1,14 @@
1
+ const mermaidVersion = hexo.config.mermaid?.version || '11';
2
+ const { version: _version, ...userConfig } = hexo.config.mermaid || {};
3
+ const mermaidConfig = Object.assign({
4
+ startOnLoad: true
5
+ }, userConfig);
6
+
7
+ hexo.extend.injector.register('body_end', `
8
+ <script src="https://cdn.jsdelivr.net/npm/mermaid@${mermaidVersion}/dist/mermaid.min.js"></script>
9
+ <script>
10
+ document.addEventListener('DOMContentLoaded', function() {
11
+ mermaid.initialize(${JSON.stringify(mermaidConfig)});
12
+ });
13
+ </script>
14
+ `, 'default');
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "hexo-markdown-mermaid",
3
+ "version": "0.0.1",
4
+ "description": "A Hexo plugin for rendering Mermaid diagrams",
5
+ "main": "index.js",
6
+ "keywords": ["hexo", "mermaid", "plugin"],
7
+ "license": "MIT",
8
+ "devDependencies": {
9
+ "chai": "^4.4.1",
10
+ "mocha": "^10.4.0"
11
+ },
12
+ "scripts": {
13
+ "test": "mocha"
14
+ }
15
+ }
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ require('chai').should();
4
+
5
+ const mockHexo = (config = {}) => {
6
+ const registeredInjectors = [];
7
+
8
+ const mock = {
9
+ extend: {},
10
+ config: Object.assign({}, config)
11
+ };
12
+ mock.extend.injector = {
13
+ register: function(position, content, priority) {
14
+ registeredInjectors.push({ position, content, priority });
15
+ }
16
+ };
17
+ mock._registeredInjectors = registeredInjectors;
18
+ return mock;
19
+ };
20
+
21
+ function loadPlugin(hexoMock) {
22
+ global.hexo = hexoMock;
23
+ delete require.cache[require.resolve('../index.js')];
24
+ require('../index.js');
25
+ }
26
+
27
+ describe('hexo-mermaid', function() {
28
+ this.timeout(10000);
29
+
30
+ describe('injector', function() {
31
+ it('should register body_end injector with mermaid script', function() {
32
+ const hexo = mockHexo({});
33
+ loadPlugin(hexo);
34
+ const injectors = hexo._registeredInjectors;
35
+ const mermaidInjector = injectors.find(i => i.position === 'body_end' && i.content.includes('mermaid'));
36
+ mermaidInjector.should.be.an('object');
37
+ mermaidInjector.priority.should.equal('default');
38
+ });
39
+
40
+ it('should use default config when no mermaid config', function() {
41
+ const hexo = mockHexo({});
42
+ loadPlugin(hexo);
43
+ const injector = hexo._registeredInjectors[0];
44
+ injector.content.should.include('"startOnLoad":true');
45
+ });
46
+
47
+ it('should merge user config with default config', function() {
48
+ const hexo = mockHexo({ mermaid: { theme: 'forest', fontFamily: 'Arial' } });
49
+ loadPlugin(hexo);
50
+ const injector = hexo._registeredInjectors[0];
51
+ injector.content.should.include('"theme":"forest"');
52
+ injector.content.should.include('"fontFamily":"Arial"');
53
+ injector.content.should.include('"startOnLoad":true');
54
+ });
55
+
56
+ it('should use default version 11 when no version config', function() {
57
+ const hexo = mockHexo({});
58
+ loadPlugin(hexo);
59
+ const injector = hexo._registeredInjectors[0];
60
+ injector.content.should.include('mermaid@11/dist/mermaid.min.js');
61
+ });
62
+
63
+ it('should use custom version when specified', function() {
64
+ const hexo = mockHexo({ mermaid: { version: '10' } });
65
+ loadPlugin(hexo);
66
+ const injector = hexo._registeredInjectors[0];
67
+ injector.content.should.include('mermaid@10/dist/mermaid.min.js');
68
+ });
69
+ });
70
+ });