@dxos/plugin-mermaid 0.8.4-main.f5c0578 → 0.8.4-main.fcfe5033a5

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/package.json CHANGED
@@ -1,13 +1,20 @@
1
1
  {
2
2
  "name": "@dxos/plugin-mermaid",
3
- "version": "0.8.4-main.f5c0578",
3
+ "version": "0.8.4-main.fcfe5033a5",
4
4
  "description": "Mermaid markdown plugin",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "DXOS.org",
9
13
  "sideEffects": true,
10
14
  "type": "module",
15
+ "imports": {
16
+ "#meta": "./src/meta.ts"
17
+ },
11
18
  "exports": {
12
19
  ".": {
13
20
  "source": "./src/index.ts",
@@ -24,34 +31,36 @@
24
31
  "src"
25
32
  ],
26
33
  "dependencies": {
27
- "@codemirror/language": "^6.10.3",
28
- "@codemirror/state": "^6.4.1",
29
- "@codemirror/view": "^6.34.1",
30
- "@preact-signals/safe-react": "^0.9.0",
31
- "effect": "3.17.7",
32
- "mermaid": "^10.6.1",
33
- "@dxos/app-framework": "0.8.4-main.f5c0578",
34
- "@dxos/plugin-space": "0.8.4-main.f5c0578",
35
- "@dxos/react-ui-editor": "0.8.4-main.f5c0578",
36
- "@dxos/plugin-markdown": "0.8.4-main.f5c0578"
34
+ "@codemirror/language": "^6.11.3",
35
+ "@codemirror/state": "^6.5.2",
36
+ "@codemirror/view": "^6.38.4",
37
+ "mermaid": "^10.9.4",
38
+ "@dxos/operation": "0.8.4-main.fcfe5033a5",
39
+ "@dxos/app-framework": "0.8.4-main.fcfe5033a5",
40
+ "@dxos/plugin-markdown": "0.8.4-main.fcfe5033a5",
41
+ "@dxos/react-ui-editor": "0.8.4-main.fcfe5033a5",
42
+ "@dxos/ui-editor": "0.8.4-main.fcfe5033a5",
43
+ "@dxos/util": "0.8.4-main.fcfe5033a5",
44
+ "@dxos/plugin-space": "0.8.4-main.fcfe5033a5"
37
45
  },
38
46
  "devDependencies": {
39
- "@types/react": "~18.2.0",
40
- "@types/react-dom": "~18.2.0",
41
- "react": "~18.2.0",
42
- "react-dom": "~18.2.0",
43
- "vite": "5.4.7",
44
- "@dxos/echo-schema": "0.8.4-main.f5c0578",
45
- "@dxos/react-ui": "0.8.4-main.f5c0578",
46
- "@dxos/react-ui-theme": "0.8.4-main.f5c0578",
47
- "@dxos/storybook-utils": "0.8.4-main.f5c0578"
47
+ "@types/react": "~19.2.7",
48
+ "@types/react-dom": "~19.2.3",
49
+ "effect": "3.20.0",
50
+ "react": "~19.2.3",
51
+ "react-dom": "~19.2.3",
52
+ "vite": "^7.1.11",
53
+ "@dxos/storybook-utils": "0.8.4-main.fcfe5033a5",
54
+ "@dxos/echo": "0.8.4-main.fcfe5033a5",
55
+ "@dxos/ui-theme": "0.8.4-main.fcfe5033a5",
56
+ "@dxos/react-ui": "0.8.4-main.fcfe5033a5"
48
57
  },
49
58
  "peerDependencies": {
50
- "effect": "^3.13.3",
51
- "react": "~18.2.0",
52
- "react-dom": "~18.2.0",
53
- "@dxos/react-ui": "0.8.4-main.f5c0578",
54
- "@dxos/react-ui-theme": "0.8.4-main.f5c0578"
59
+ "effect": "3.20.0",
60
+ "react": "~19.2.3",
61
+ "react-dom": "~19.2.3",
62
+ "@dxos/react-ui": "0.8.4-main.fcfe5033a5",
63
+ "@dxos/ui-theme": "0.8.4-main.fcfe5033a5"
55
64
  },
56
65
  "publishConfig": {
57
66
  "access": "public"
@@ -2,17 +2,20 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { contributes, defineModule, definePlugin } from '@dxos/app-framework';
5
+ import * as Effect from 'effect/Effect';
6
+
7
+ import { Capability, Plugin } from '@dxos/app-framework';
6
8
  import { MarkdownCapabilities, MarkdownEvents } from '@dxos/plugin-markdown';
7
9
 
10
+ import { meta } from '#meta';
11
+
8
12
  import { mermaid } from './extensions';
9
- import { meta } from './meta';
10
13
 
11
- export const MermaidPlugin = () =>
12
- definePlugin(meta, [
13
- defineModule({
14
- id: `${meta.id}/module/markdown`,
15
- activatesOn: MarkdownEvents.SetupExtensions,
16
- activate: () => contributes(MarkdownCapabilities.Extensions, [mermaid]),
17
- }),
18
- ]);
14
+ export const MermaidPlugin = Plugin.define(meta).pipe(
15
+ Plugin.addModule({
16
+ id: 'markdown',
17
+ activatesOn: MarkdownEvents.SetupExtensions,
18
+ activate: () => Effect.succeed(Capability.contributes(MarkdownCapabilities.Extensions, [mermaid])),
19
+ }),
20
+ Plugin.make,
21
+ );
@@ -2,4 +2,4 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './mermaid';
5
+ export * from './mermaid-extension';
@@ -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 '@dxos-theme';
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
- useTextEditor,
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 StoryProps = {
22
+ type DefaultStoryProps = {
25
23
  text?: string;
26
24
  };
27
25
 
28
- const DefaultStory = ({ text }: StoryProps) => {
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({ themeMode }),
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
- export const Default = {
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/meta.ts CHANGED
@@ -2,14 +2,16 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type PluginMeta } from '@dxos/app-framework';
5
+ import { type Plugin } from '@dxos/app-framework';
6
+ import { trim } from '@dxos/util';
6
7
 
7
- export const MERMAID_PLUGIN = 'dxos.org/plugin/mermaid';
8
-
9
- export const meta: PluginMeta = {
10
- id: MERMAID_PLUGIN,
8
+ export const meta: Plugin.Meta = {
9
+ id: 'org.dxos.plugin.mermaid',
11
10
  name: 'Mermaid',
12
- description: 'Tool that renders text definitions to create diagrams.',
11
+ description: trim`
12
+ Generate diagrams from simple text-based definitions using Mermaid syntax.
13
+ Create flowcharts, sequence diagrams, and other visualizations that stay in sync with your documentation.
14
+ `,
13
15
  icon: 'ph--anchor-simple--regular',
14
16
  source: 'https://github.com/dxos/dxos/tree/main/packages/plugins/plugin-mermaid',
15
17
  };
@@ -1,4 +0,0 @@
1
- import { type Extension } from '@codemirror/state';
2
- export type MermaidOptions = {};
3
- export declare const mermaid: (options?: MermaidOptions) => Extension;
4
- //# sourceMappingURL=mermaid.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mermaid.d.ts","sourceRoot":"","sources":["../../../../src/extensions/mermaid.ts"],"names":[],"mappings":"AAKA,OAAO,EAEL,KAAK,SAAS,EAKf,MAAM,mBAAmB,CAAC;AAI3B,MAAM,MAAM,cAAc,GAAG,EAAE,CAAC;AAEhC,eAAO,MAAM,OAAO,GAAI,UAAS,cAAmB,KAAG,SAStD,CAAC"}
@@ -1,155 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { syntaxTree } from '@codemirror/language';
6
- import {
7
- type EditorState,
8
- type Extension,
9
- type RangeSet,
10
- RangeSetBuilder,
11
- StateField,
12
- type Transaction,
13
- } from '@codemirror/state';
14
- import { Decoration, EditorView, WidgetType } from '@codemirror/view';
15
- import _mermaid from 'mermaid';
16
-
17
- export type MermaidOptions = {};
18
-
19
- export const mermaid = (options: MermaidOptions = {}): Extension => {
20
- return [
21
- StateField.define<RangeSet<Decoration>>({
22
- create: (state) => update(state, options),
23
- update: (_: RangeSet<Decoration>, tr: Transaction) => update(tr.state, options),
24
- provide: (field) => EditorView.decorations.from(field),
25
- }),
26
- styles,
27
- ];
28
- };
29
-
30
- const update = (state: EditorState, options: MermaidOptions) => {
31
- const builder = new RangeSetBuilder<Decoration>();
32
- syntaxTree(state).iterate({
33
- enter: (node) => {
34
- if (node.name === 'FencedCode') {
35
- const cursor = state.selection.main.head;
36
- if (state.readOnly || cursor < node.from || cursor > node.to) {
37
- const info = node.node.getChild('CodeInfo');
38
- if (info) {
39
- const type = state.sliceDoc(info.from, info.to);
40
- const text = node.node.getChild('CodeText');
41
- if (type === 'mermaid' && text) {
42
- const content = state.sliceDoc(text.from, text.to);
43
- // TODO(burdon): Error if extension defined AFTER decorateMarkdown.
44
- // Decorations that replace line breaks may not be specified via plugins
45
- builder.add(
46
- node.from,
47
- node.to,
48
- Decoration.replace({
49
- widget: new MermaidWidget(`mermaid-${node.from}`, content),
50
- }),
51
- );
52
- }
53
- }
54
- }
55
- }
56
- },
57
- });
58
-
59
- return builder.finish();
60
- };
61
-
62
- class MermaidWidget extends WidgetType {
63
- _svg: string | undefined;
64
- _error: string | undefined;
65
-
66
- // TODO(burdon): Mermaid API requires unique id.
67
- constructor(
68
- private readonly _id: string,
69
- private readonly _source: string,
70
- ) {
71
- super();
72
- }
73
-
74
- override eq(other: this): boolean {
75
- return this._source === other._source;
76
- }
77
-
78
- override toDOM(view: EditorView): HTMLDivElement {
79
- const div = document.createElement('div');
80
- setTimeout(async () => {
81
- // https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts
82
- _mermaid.initialize({
83
- darkMode: view.state.facet(EditorView.darkTheme),
84
- theme: 'neutral',
85
- // TODO(burdon): Styles.
86
- // NOTE: Must specify 'base' in order to override.
87
- // theme: 'base',
88
- // themeVariables: {
89
- // primaryColor: getToken('extend.colors.red.100'),
90
- // primaryBorderColor: getToken('extend.colors.neutral.200'),
91
- // },
92
- // https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/flowchart/styles.ts
93
- // https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/sequence/styles.js
94
- // https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/diagrams/state/styles.js
95
- // themeCSS: '.node rect { fill: red; }',
96
- });
97
-
98
- // TODO(burdon): Cache?
99
- const svg = await this.render(div);
100
- if (this._error) {
101
- div.className = 'cm-mermaid-error';
102
- div.innerText = this._error;
103
- } else {
104
- div.className = 'cm-mermaid';
105
- div.innerHTML = svg!;
106
-
107
- // const label = document.createElement('span');
108
- // label.innerText = 'Mermaid';
109
- // label.className = 'cm-mermaid-label';
110
- // wrapper.appendChild(label);
111
- view.requestMeasure();
112
- }
113
- });
114
-
115
- return div;
116
- }
117
-
118
- async render(container: Element): Promise<string | undefined> {
119
- try {
120
- // https://github.com/mermaid-js/mermaid
121
- const valid = await _mermaid.parse(this._source);
122
- if (valid) {
123
- const result = await _mermaid.render(this._id, this._source);
124
- this._error = undefined;
125
- this._svg = result.svg;
126
- return result.svg;
127
- }
128
- } catch (err: any) {
129
- this._error = String(err);
130
- this._svg = undefined;
131
- }
132
- }
133
-
134
- override ignoreEvent(e: Event): boolean {
135
- return !/^mouse/.test(e.type);
136
- }
137
- }
138
-
139
- const styles = EditorView.theme({
140
- '& .cm-mermaid': {
141
- position: 'relative',
142
- display: 'inline-flex',
143
- width: '100%',
144
- justifyContent: 'center',
145
- },
146
- // '& .cm-mermaid-label': {
147
- // position: 'absolute',
148
- // right: 0,
149
- // textSize: 10,
150
- // },
151
- '& .cm-mermaid-error': {
152
- display: 'inline-block',
153
- color: 'var(--dx-errorText)',
154
- },
155
- });