@dxos/plugin-mermaid 0.8.4-main.fd6878d → 0.9.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 +102 -5
- package/PLUGIN.mdl +260 -0
- package/README.md +1 -1
- package/dist/lib/neutral/MermaidPlugin.mjs +29 -0
- package/dist/lib/neutral/MermaidPlugin.mjs.map +7 -0
- package/dist/lib/neutral/capabilities/index.mjs +7 -0
- package/dist/lib/neutral/capabilities/index.mjs.map +7 -0
- package/dist/lib/neutral/chunk-WD2YFG2L.mjs +36 -0
- package/dist/lib/neutral/chunk-WD2YFG2L.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +7 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/markdown-extension-7YWEUNWO.mjs +172 -0
- package/dist/lib/neutral/markdown-extension-7YWEUNWO.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/meta.mjs +7 -0
- package/dist/lib/neutral/meta.mjs.map +7 -0
- package/dist/lib/neutral/plugin.mjs +11 -0
- package/dist/lib/neutral/plugin.mjs.map +7 -0
- package/dist/types/src/MermaidPlugin.d.ts +3 -1
- package/dist/types/src/MermaidPlugin.d.ts.map +1 -1
- package/dist/types/src/MermaidPlugin.test.d.ts +2 -0
- package/dist/types/src/MermaidPlugin.test.d.ts.map +1 -0
- package/dist/types/src/capabilities/index.d.ts +3 -0
- package/dist/types/src/capabilities/index.d.ts.map +1 -0
- package/dist/types/src/capabilities/markdown-extension.d.ts +5 -0
- package/dist/types/src/capabilities/markdown-extension.d.ts.map +1 -0
- package/dist/types/src/extensions/index.d.ts +1 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/mermaid-extension.d.ts +7 -0
- package/dist/types/src/extensions/mermaid-extension.d.ts.map +1 -0
- package/dist/types/src/extensions/mermaid.stories.d.ts +11 -11
- package/dist/types/src/extensions/mermaid.stories.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +0 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/meta.d.ts +2 -3
- package/dist/types/src/meta.d.ts.map +1 -1
- package/dist/types/src/plugin.d.ts +3 -0
- package/dist/types/src/plugin.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +59 -31
- package/src/MermaidPlugin.test.ts +30 -0
- package/src/MermaidPlugin.tsx +21 -12
- package/src/capabilities/index.ts +7 -0
- package/src/capabilities/markdown-extension.ts +14 -0
- package/src/extensions/index.ts +1 -1
- package/src/extensions/mermaid-extension.ts +215 -0
- package/src/extensions/mermaid.stories.tsx +20 -21
- package/src/index.ts +0 -1
- package/src/meta.ts +27 -7
- package/src/plugin.ts +9 -0
- package/src/vite-env.d.ts +10 -0
- package/dist/lib/browser/index.mjs +0 -136
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/types/src/extensions/mermaid.d.ts +0 -4
- package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
- package/src/extensions/mermaid.ts +0 -155
package/package.json
CHANGED
|
@@ -1,57 +1,85 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/plugin-mermaid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Mermaid markdown plugin",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
-
"
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
13
|
"sideEffects": true,
|
|
10
14
|
"type": "module",
|
|
15
|
+
"imports": {
|
|
16
|
+
"#capabilities": {
|
|
17
|
+
"source": "./src/capabilities/index.ts",
|
|
18
|
+
"types": "./dist/types/src/capabilities/index.d.ts",
|
|
19
|
+
"default": "./dist/lib/neutral/capabilities/index.mjs"
|
|
20
|
+
},
|
|
21
|
+
"#meta": {
|
|
22
|
+
"source": "./src/meta.ts",
|
|
23
|
+
"types": "./dist/types/src/meta.d.ts",
|
|
24
|
+
"default": "./dist/lib/neutral/meta.mjs"
|
|
25
|
+
},
|
|
26
|
+
"#plugin": {
|
|
27
|
+
"source": "./src/MermaidPlugin.tsx",
|
|
28
|
+
"types": "./dist/types/src/MermaidPlugin.d.ts",
|
|
29
|
+
"default": "./dist/lib/neutral/MermaidPlugin.mjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
11
32
|
"exports": {
|
|
12
33
|
".": {
|
|
13
34
|
"source": "./src/index.ts",
|
|
14
35
|
"types": "./dist/types/src/index.d.ts",
|
|
15
|
-
"
|
|
36
|
+
"default": "./dist/lib/neutral/index.mjs"
|
|
37
|
+
},
|
|
38
|
+
"./assets/PLUGIN.mdl": "./PLUGIN.mdl",
|
|
39
|
+
"./plugin": {
|
|
40
|
+
"source": "./src/plugin.ts",
|
|
41
|
+
"types": "./dist/types/src/plugin.d.ts",
|
|
42
|
+
"default": "./dist/lib/neutral/plugin.mjs"
|
|
16
43
|
}
|
|
17
44
|
},
|
|
18
45
|
"types": "dist/types/src/index.d.ts",
|
|
19
|
-
"typesVersions": {
|
|
20
|
-
"*": {}
|
|
21
|
-
},
|
|
22
46
|
"files": [
|
|
23
47
|
"dist",
|
|
24
|
-
"src"
|
|
48
|
+
"src",
|
|
49
|
+
"PLUGIN.mdl"
|
|
25
50
|
],
|
|
26
51
|
"dependencies": {
|
|
27
|
-
"@codemirror/language": "^6.
|
|
28
|
-
"@codemirror/state": "^6.
|
|
29
|
-
"@codemirror/view": "^6.
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/plugin-
|
|
36
|
-
"@dxos/
|
|
52
|
+
"@codemirror/language": "^6.12.3",
|
|
53
|
+
"@codemirror/state": "^6.6.0",
|
|
54
|
+
"@codemirror/view": "^6.43.0",
|
|
55
|
+
"mermaid": "^10.9.4",
|
|
56
|
+
"@dxos/app-framework": "0.9.0",
|
|
57
|
+
"@dxos/app-toolkit": "0.9.0",
|
|
58
|
+
"@dxos/operation": "0.9.0",
|
|
59
|
+
"@dxos/react-ui-editor": "0.9.0",
|
|
60
|
+
"@dxos/plugin-markdown": "0.9.0",
|
|
61
|
+
"@dxos/util": "0.9.0",
|
|
62
|
+
"@dxos/keys": "0.9.0",
|
|
63
|
+
"@dxos/ui-editor": "0.9.0"
|
|
37
64
|
},
|
|
38
65
|
"devDependencies": {
|
|
39
|
-
"@types/react": "~
|
|
40
|
-
"@types/react-dom": "~
|
|
41
|
-
"
|
|
42
|
-
"react
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/react-ui": "0.
|
|
47
|
-
"@dxos/
|
|
66
|
+
"@types/react": "~19.2.7",
|
|
67
|
+
"@types/react-dom": "~19.2.3",
|
|
68
|
+
"effect": "3.21.3",
|
|
69
|
+
"react": "~19.2.3",
|
|
70
|
+
"react-dom": "~19.2.3",
|
|
71
|
+
"vite": "^8.0.16",
|
|
72
|
+
"@dxos/echo": "0.9.0",
|
|
73
|
+
"@dxos/react-ui": "0.9.0",
|
|
74
|
+
"@dxos/storybook-utils": "0.9.0",
|
|
75
|
+
"@dxos/ui-theme": "0.9.0"
|
|
48
76
|
},
|
|
49
77
|
"peerDependencies": {
|
|
50
|
-
"effect": "
|
|
51
|
-
"react": "~
|
|
52
|
-
"react-dom": "~
|
|
53
|
-
"@dxos/react-ui": "0.
|
|
54
|
-
"@dxos/
|
|
78
|
+
"effect": "3.21.3",
|
|
79
|
+
"react": "~19.2.3",
|
|
80
|
+
"react-dom": "~19.2.3",
|
|
81
|
+
"@dxos/react-ui": "0.9.0",
|
|
82
|
+
"@dxos/ui-theme": "0.9.0"
|
|
55
83
|
},
|
|
56
84
|
"publishConfig": {
|
|
57
85
|
"access": "public"
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { ProcessManagerPlugin } from '@dxos/app-framework';
|
|
8
|
+
import { createTestApp } from '@dxos/app-framework/testing';
|
|
9
|
+
import { MarkdownEvents } from '@dxos/plugin-markdown';
|
|
10
|
+
|
|
11
|
+
import { MermaidPlugin } from '#plugin';
|
|
12
|
+
|
|
13
|
+
import { meta } from './meta';
|
|
14
|
+
|
|
15
|
+
const moduleId = (name: string) => `${meta.id}.module.${name}`;
|
|
16
|
+
|
|
17
|
+
describe('MermaidPlugin', () => {
|
|
18
|
+
test('modules activate on the expected events', async ({ expect }) => {
|
|
19
|
+
// Use createTestApp directly — only ProcessManagerPlugin and ProcessManagerPlugin are needed.
|
|
20
|
+
// MermaidPlugin has no surface module so SetupReactSurface firing harmlessly is fine.
|
|
21
|
+
await using harness = await createTestApp({
|
|
22
|
+
plugins: [ProcessManagerPlugin(), MermaidPlugin()],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// The markdown module activates on MarkdownEvents.SetupExtensions,
|
|
26
|
+
// which is fired by MarkdownPlugin before its ReactSurface activates.
|
|
27
|
+
await harness.fire(MarkdownEvents.SetupExtensions);
|
|
28
|
+
expect(harness.manager.getActive()).toContain(moduleId('markdown'));
|
|
29
|
+
});
|
|
30
|
+
});
|
package/src/MermaidPlugin.tsx
CHANGED
|
@@ -2,17 +2,26 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { Plugin } from '@dxos/app-framework';
|
|
6
|
+
import { AppPlugin } from '@dxos/app-toolkit';
|
|
7
|
+
import { MarkdownEvents } from '@dxos/plugin-markdown';
|
|
7
8
|
|
|
8
|
-
import {
|
|
9
|
-
import { meta } from '
|
|
9
|
+
import { MarkdownExtension } from '#capabilities';
|
|
10
|
+
import { meta } from '#meta';
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
// eslint-disable-next-line import/no-relative-packages
|
|
13
|
+
import pluginSpec from '../PLUGIN.mdl?raw';
|
|
14
|
+
|
|
15
|
+
export const MermaidPlugin = Plugin.define(meta).pipe(
|
|
16
|
+
Plugin.addModule({
|
|
17
|
+
id: 'markdown',
|
|
18
|
+
activatesOn: MarkdownEvents.SetupExtensions,
|
|
19
|
+
activate: MarkdownExtension,
|
|
20
|
+
}),
|
|
21
|
+
AppPlugin.addPluginAssetModule({
|
|
22
|
+
asset: { pluginId: meta.id, path: 'PLUGIN.mdl', content: pluginSpec, mimeType: 'application/x-mdl' },
|
|
23
|
+
}),
|
|
24
|
+
Plugin.make,
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
export default MermaidPlugin;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
|
|
7
|
+
import { Capability } from '@dxos/app-framework';
|
|
8
|
+
import { MarkdownCapabilities } from '@dxos/plugin-markdown/types';
|
|
9
|
+
|
|
10
|
+
import { mermaid } from '../extensions';
|
|
11
|
+
|
|
12
|
+
export default Capability.makeModule(() =>
|
|
13
|
+
Effect.succeed(Capability.contributes(MarkdownCapabilities.ExtensionProvider, [mermaid])),
|
|
14
|
+
);
|
package/src/extensions/index.ts
CHANGED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { type Extension } from '@codemirror/state';
|
|
7
|
+
import { Decoration, type DecorationSet, EditorView, ViewPlugin, type ViewUpdate, WidgetType } from '@codemirror/view';
|
|
8
|
+
import _mermaid from 'mermaid';
|
|
9
|
+
|
|
10
|
+
export type MermaidOptions = {};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Extension to create mermaid diagrams.
|
|
14
|
+
*/
|
|
15
|
+
export const mermaid = (_options: MermaidOptions = {}): Extension => {
|
|
16
|
+
return [
|
|
17
|
+
ViewPlugin.fromClass(
|
|
18
|
+
class {
|
|
19
|
+
decorations: DecorationSet;
|
|
20
|
+
|
|
21
|
+
constructor(view: EditorView) {
|
|
22
|
+
this.decorations = this.buildDecorations(view);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
update(update: ViewUpdate) {
|
|
26
|
+
// Always rebuild decorations when selection changes to handle arrow key navigation.
|
|
27
|
+
if (update.docChanged || update.viewportChanged || update.selectionSet || update.focusChanged) {
|
|
28
|
+
this.decorations = this.buildDecorations(update.view);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
buildDecorations(view: EditorView): DecorationSet {
|
|
33
|
+
const decorations: Array<{ from: number; to: number; decoration: Decoration }> = [];
|
|
34
|
+
|
|
35
|
+
syntaxTree(view.state).iterate({
|
|
36
|
+
enter: (node) => {
|
|
37
|
+
if (node.name === 'FencedCode') {
|
|
38
|
+
const cursor = view.state.selection.main.head;
|
|
39
|
+
// Show widget when cursor is outside the code block.
|
|
40
|
+
// Add a buffer of 1 position before and after to handle edge cases.
|
|
41
|
+
const showWidget = view.state.readOnly || cursor < node.from - 1 || cursor > node.to + 1;
|
|
42
|
+
if (showWidget) {
|
|
43
|
+
const info = node.node.getChild('CodeInfo');
|
|
44
|
+
if (info) {
|
|
45
|
+
const type = view.state.sliceDoc(info.from, info.to);
|
|
46
|
+
const text = node.node.getChild('CodeText');
|
|
47
|
+
if (type === 'mermaid' && text) {
|
|
48
|
+
const content = view.state.sliceDoc(text.from, text.to).trim();
|
|
49
|
+
const label = content.split(' ')[0];
|
|
50
|
+
|
|
51
|
+
// Create widget.
|
|
52
|
+
const widget = new MermaidWidget(`mermaid-${node.from}`, content, label);
|
|
53
|
+
|
|
54
|
+
// Find the line after the code block to place the widget.
|
|
55
|
+
const endLine = view.state.doc.lineAt(node.to);
|
|
56
|
+
const nextLinePos = endLine.to + 1;
|
|
57
|
+
|
|
58
|
+
// Add widget after the code block.
|
|
59
|
+
if (nextLinePos <= view.state.doc.length) {
|
|
60
|
+
decorations.push({
|
|
61
|
+
from: nextLinePos,
|
|
62
|
+
to: nextLinePos,
|
|
63
|
+
decoration: Decoration.widget({
|
|
64
|
+
widget,
|
|
65
|
+
}),
|
|
66
|
+
});
|
|
67
|
+
} else {
|
|
68
|
+
// Check if at the end of the document.
|
|
69
|
+
decorations.push({
|
|
70
|
+
from: endLine.to,
|
|
71
|
+
to: endLine.to,
|
|
72
|
+
decoration: Decoration.widget({
|
|
73
|
+
widget,
|
|
74
|
+
side: 1,
|
|
75
|
+
}),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Hide each line of the code block.
|
|
80
|
+
const startLine = view.state.doc.lineAt(node.from);
|
|
81
|
+
for (let lineNum = startLine.number; lineNum <= endLine.number; lineNum++) {
|
|
82
|
+
const line = view.state.doc.line(lineNum);
|
|
83
|
+
decorations.push({
|
|
84
|
+
from: line.from,
|
|
85
|
+
to: line.from,
|
|
86
|
+
decoration: Decoration.line({
|
|
87
|
+
class: 'cm-mermaid-hidden',
|
|
88
|
+
}),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
return Decoration.set(
|
|
99
|
+
decorations.sort((a, b) => a.from - b.from || a.to - b.to).map((d) => d.decoration.range(d.from, d.to)),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
decorations: (v) => v.decorations,
|
|
105
|
+
},
|
|
106
|
+
),
|
|
107
|
+
EditorView.theme({
|
|
108
|
+
'& .cm-mermaid': {
|
|
109
|
+
position: 'relative',
|
|
110
|
+
display: 'inline-flex',
|
|
111
|
+
width: '100%',
|
|
112
|
+
maring: '4px 0',
|
|
113
|
+
padding: '16px',
|
|
114
|
+
justifyContent: 'center',
|
|
115
|
+
backgroundColor: 'var(--color-group-surface)',
|
|
116
|
+
borderRadius: '8px',
|
|
117
|
+
},
|
|
118
|
+
'& .cm-mermaid-label': {
|
|
119
|
+
position: 'absolute',
|
|
120
|
+
right: '16px',
|
|
121
|
+
fontFamily: 'unset',
|
|
122
|
+
color: 'var(--color-subdued)',
|
|
123
|
+
},
|
|
124
|
+
'& .cm-mermaid-error': {
|
|
125
|
+
display: 'inline-block',
|
|
126
|
+
color: 'var(--color-error)',
|
|
127
|
+
},
|
|
128
|
+
'& .cm-mermaid-hidden': {
|
|
129
|
+
display: 'none !important',
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
];
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
class MermaidWidget extends WidgetType {
|
|
136
|
+
_svg: string | undefined;
|
|
137
|
+
_error: string | undefined;
|
|
138
|
+
|
|
139
|
+
// TODO(burdon): Mermaid API requires unique id.
|
|
140
|
+
constructor(
|
|
141
|
+
private readonly _id: string,
|
|
142
|
+
private readonly _source: string,
|
|
143
|
+
private readonly _label?: string,
|
|
144
|
+
) {
|
|
145
|
+
super();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
override eq(other: this) {
|
|
149
|
+
return this._source === other._source;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
override ignoreEvent(ev: Event) {
|
|
153
|
+
return !/^mouse/.test(ev.type);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
override toDOM(view: EditorView) {
|
|
157
|
+
const div = document.createElement('div');
|
|
158
|
+
div.className = 'cm-mermaid';
|
|
159
|
+
|
|
160
|
+
setTimeout(async () => {
|
|
161
|
+
// https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts
|
|
162
|
+
_mermaid.initialize({
|
|
163
|
+
darkMode: view.state.facet(EditorView.darkTheme),
|
|
164
|
+
theme: 'neutral',
|
|
165
|
+
// TODO(burdon): Styles.
|
|
166
|
+
// NOTE: Must specify 'base' in order to override.
|
|
167
|
+
// theme: 'base',
|
|
168
|
+
// themeVariables: {
|
|
169
|
+
// primaryColor: getToken('extend.colors.red.100'),
|
|
170
|
+
// primaryBorderColor: getToken('extend.colors.neutral.200'),
|
|
171
|
+
// },
|
|
172
|
+
// https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/flowchart/styles.ts
|
|
173
|
+
// https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/sequence/styles.js
|
|
174
|
+
// https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/state/styles.js
|
|
175
|
+
// themeCSS: '.node rect { fill: red; }',
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// TODO(burdon): Cache?
|
|
179
|
+
const svg = await this.render(div);
|
|
180
|
+
if (this._error) {
|
|
181
|
+
div.className = 'cm-mermaid-error';
|
|
182
|
+
div.innerText = this._error;
|
|
183
|
+
} else {
|
|
184
|
+
div.className = 'cm-mermaid';
|
|
185
|
+
div.innerHTML = svg!;
|
|
186
|
+
|
|
187
|
+
if (this._label) {
|
|
188
|
+
const label = document.createElement('span');
|
|
189
|
+
label.innerText = this._label;
|
|
190
|
+
label.className = 'cm-mermaid-label';
|
|
191
|
+
div.appendChild(label);
|
|
192
|
+
view.requestMeasure();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
return div;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async render(_container: Element): Promise<string | undefined> {
|
|
201
|
+
try {
|
|
202
|
+
// https://github.com/mermaid-js/mermaid
|
|
203
|
+
const valid = await _mermaid.parse(this._source);
|
|
204
|
+
if (valid) {
|
|
205
|
+
const result = await _mermaid.render(this._id, this._source);
|
|
206
|
+
this._error = undefined;
|
|
207
|
+
this._svg = result.svg;
|
|
208
|
+
return result.svg;
|
|
209
|
+
}
|
|
210
|
+
} catch (err: any) {
|
|
211
|
+
this._error = String(err);
|
|
212
|
+
this._svg = undefined;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -2,39 +2,36 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@
|
|
6
|
-
|
|
7
|
-
import { type Meta } from '@storybook/react-vite';
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
8
6
|
import React from 'react';
|
|
9
7
|
|
|
10
8
|
import { useThemeContext } from '@dxos/react-ui';
|
|
9
|
+
import { useTextEditor } from '@dxos/react-ui-editor';
|
|
10
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
11
11
|
import {
|
|
12
12
|
createBasicExtensions,
|
|
13
13
|
createMarkdownExtensions,
|
|
14
14
|
createThemeExtensions,
|
|
15
15
|
decorateMarkdown,
|
|
16
|
-
|
|
17
|
-
} from '@dxos/react-ui-editor';
|
|
18
|
-
import { withLayout, withTheme } from '@dxos/storybook-utils';
|
|
16
|
+
} from '@dxos/ui-editor';
|
|
19
17
|
|
|
20
|
-
import { mermaid } from './mermaid';
|
|
18
|
+
import { mermaid } from './mermaid-extension';
|
|
21
19
|
|
|
22
20
|
const str = (...lines: string[]) => lines.join('\n');
|
|
23
21
|
|
|
24
|
-
type
|
|
22
|
+
type DefaultStoryProps = {
|
|
25
23
|
text?: string;
|
|
26
24
|
};
|
|
27
25
|
|
|
28
|
-
const DefaultStory = ({ text }:
|
|
26
|
+
const DefaultStory = ({ text }: DefaultStoryProps) => {
|
|
29
27
|
const { themeMode } = useThemeContext();
|
|
30
28
|
const { parentRef, focusAttributes } = useTextEditor(
|
|
31
29
|
() => ({
|
|
32
30
|
initialValue: text,
|
|
33
31
|
extensions: [
|
|
34
32
|
createBasicExtensions(),
|
|
35
|
-
createMarkdownExtensions(
|
|
33
|
+
createMarkdownExtensions(),
|
|
36
34
|
createThemeExtensions({ themeMode, syntaxHighlighting: true }),
|
|
37
|
-
// TODO(burdon): Bug if mermaid extension is provided after decorateMarkdown.
|
|
38
35
|
mermaid(),
|
|
39
36
|
decorateMarkdown(),
|
|
40
37
|
],
|
|
@@ -45,7 +42,17 @@ const DefaultStory = ({ text }: StoryProps) => {
|
|
|
45
42
|
return <div className='w-[50rem]' ref={parentRef} {...focusAttributes} />;
|
|
46
43
|
};
|
|
47
44
|
|
|
48
|
-
|
|
45
|
+
const meta = {
|
|
46
|
+
title: 'plugins/plugin-mermaid/extensions/mermaid',
|
|
47
|
+
render: DefaultStory,
|
|
48
|
+
decorators: [withTheme(), withLayout({ layout: 'column' })],
|
|
49
|
+
} satisfies Meta<typeof DefaultStory>;
|
|
50
|
+
|
|
51
|
+
export default meta;
|
|
52
|
+
|
|
53
|
+
type Story = StoryObj<typeof meta>;
|
|
54
|
+
|
|
55
|
+
export const Default: Story = {
|
|
49
56
|
args: {
|
|
50
57
|
text: str(
|
|
51
58
|
'# Mermaid',
|
|
@@ -68,7 +75,7 @@ export const Default = {
|
|
|
68
75
|
},
|
|
69
76
|
};
|
|
70
77
|
|
|
71
|
-
export const Error = {
|
|
78
|
+
export const Error: Story = {
|
|
72
79
|
args: {
|
|
73
80
|
text: str(
|
|
74
81
|
'# Mermaid',
|
|
@@ -85,11 +92,3 @@ export const Error = {
|
|
|
85
92
|
),
|
|
86
93
|
},
|
|
87
94
|
};
|
|
88
|
-
|
|
89
|
-
const meta: Meta = {
|
|
90
|
-
title: 'plugins/plugin-mermaid/extensions',
|
|
91
|
-
render: DefaultStory,
|
|
92
|
-
decorators: [withTheme, withLayout({ fullscreen: true, classNames: 'justify-center' })],
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
export default meta;
|
package/src/index.ts
CHANGED
package/src/meta.ts
CHANGED
|
@@ -2,14 +2,34 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Plugin } from '@dxos/app-framework';
|
|
6
|
+
import { DXN } from '@dxos/keys';
|
|
7
|
+
import { trim } from '@dxos/util';
|
|
6
8
|
|
|
7
|
-
export const
|
|
8
|
-
|
|
9
|
-
export const meta: PluginMeta = {
|
|
10
|
-
id: MERMAID_PLUGIN,
|
|
9
|
+
export const meta = Plugin.makeMeta({
|
|
10
|
+
key: DXN.make('org.dxos.plugin.mermaid'),
|
|
11
11
|
name: 'Mermaid',
|
|
12
|
-
|
|
12
|
+
author: 'DXOS',
|
|
13
|
+
description: trim`
|
|
14
|
+
Generate diagrams from simple text-based definitions using Mermaid syntax.
|
|
15
|
+
Create flowcharts, sequence diagrams, class diagrams, state diagrams, entity-relationship
|
|
16
|
+
diagrams, Gantt charts, pie charts, Git graphs, mindmaps, and timelines — all from
|
|
17
|
+
plain-text source embedded directly in your documents.
|
|
18
|
+
|
|
19
|
+
The plugin integrates with the markdown editor as a CodeMirror extension.
|
|
20
|
+
Any fenced code block tagged with the \`mermaid\` language identifier is automatically
|
|
21
|
+
detected and replaced with a live SVG preview rendered by the Mermaid JS library.
|
|
22
|
+
The raw source is revealed for editing whenever the cursor moves inside the block,
|
|
23
|
+
then the rendered diagram is restored when the cursor moves away, providing a seamless
|
|
24
|
+
live-preview experience without leaving the editor.
|
|
25
|
+
|
|
26
|
+
Diagram rendering adapts to the editor's current color scheme: dark-mode documents
|
|
27
|
+
use Mermaid's dark theme while light-mode documents use the neutral theme.
|
|
28
|
+
Parse and render errors are shown inline in place of the diagram so that
|
|
29
|
+
invalid syntax is immediately visible without disrupting the rest of the document.
|
|
30
|
+
`,
|
|
13
31
|
icon: 'ph--anchor-simple--regular',
|
|
32
|
+
iconHue: 'cyan',
|
|
14
33
|
source: 'https://github.com/dxos/dxos/tree/main/packages/plugins/plugin-mermaid',
|
|
15
|
-
|
|
34
|
+
spec: 'PLUGIN.mdl',
|
|
35
|
+
});
|
package/src/plugin.ts
ADDED