@8btc/mditor 0.0.16 → 0.0.18

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.
@@ -586,6 +586,13 @@ interface IPreviewOptions {
586
586
  speech?: {
587
587
  enable?: boolean;
588
588
  };
589
+ /** 链接点击行为配置(预览模式) */
590
+ link?: {
591
+ /** 是否打开链接地址。默认值: true */
592
+ isOpen?: boolean;
593
+ /** 点击链接事件 */
594
+ click?: (bom: Element) => void;
595
+ };
589
596
  /** 运行 HTML 片段配置(预览模式) */
590
597
  runCode?: {
591
598
  /** 是否启用运行按钮。默认值: false */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@8btc/mditor",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "mditor, Md编辑器",
5
5
  "author": "wujie-vditor",
6
6
  "jsdelivr": "dist/index.min.js",
@@ -0,0 +1,32 @@
1
+
2
+ .mermaid-error {
3
+ border: 1px dashed #E5E5E5;
4
+ border-radius: 0.625rem;
5
+ padding: 3rem 1.5rem;
6
+ text-align: center;
7
+ .mermaid-error-icon {
8
+ display: inline-block;
9
+ color: #0A0A0A;
10
+ padding: 0.5rem;
11
+ border-radius: 0.5rem;
12
+ margin-bottom: 1.5rem;
13
+ border: 1px solid #E5E5E5;
14
+ line-height: 1;
15
+ > svg {
16
+ display: block;
17
+ stroke-width: 2;
18
+ }
19
+ }
20
+ .mermaid-error-title {
21
+ font-size: 1.25rem;
22
+ color: #0A0A0A;
23
+ font-weight: 700;
24
+ line-height: 140%;
25
+ margin-bottom: 0.5rem;
26
+ }
27
+ .mermaid-error-message {
28
+ font-size: 0.875rem;
29
+ line-height: 140%;
30
+ color: #737373;
31
+ }
32
+ }
@@ -15,7 +15,7 @@
15
15
  }
16
16
 
17
17
  .vditor-tooltipped {
18
- position: relative;
18
+ position: relative !important;
19
19
  cursor: pointer;
20
20
 
21
21
  &::after {
@@ -90,6 +90,7 @@
90
90
  @import "_math-menu";
91
91
  @import "_selection-popover";
92
92
  @import "_selection-tag";
93
+ @import "_mermaid";
93
94
 
94
95
  // details 默认样式调整:隐藏展开/折叠指示器
95
96
  details>summary {
@@ -1,19 +1,26 @@
1
- import {Constants} from "../constants";
2
- import {addScript} from "../util/addScript";
3
- import {mermaidRenderAdapter} from "./adapterRender";
4
- import {genUUID} from "../util/function";
1
+ import { Constants } from "../constants";
2
+ import { addScript } from "../util/addScript";
3
+ import { mermaidRenderAdapter } from "./adapterRender";
4
+ import { genUUID } from "../util/function";
5
5
 
6
6
  declare const mermaid: {
7
- initialize(options: any): void,
8
- render(id: string, text: string): { svg: string }
7
+ initialize(options: any): void;
8
+ render(id: string, text: string): { svg: string };
9
9
  };
10
10
 
11
- export const mermaidRender = (element: (HTMLElement | Document) = document, cdn = Constants.CDN, theme: string) => {
11
+ export const mermaidRender = (
12
+ element: HTMLElement | Document = document,
13
+ cdn = Constants.CDN,
14
+ theme: string
15
+ ) => {
12
16
  const mermaidElements = mermaidRenderAdapter.getElements(element);
13
17
  if (mermaidElements.length === 0) {
14
18
  return;
15
19
  }
16
- addScript(`${cdn}/dist/js/mermaid/mermaid.min.js?v=11.11.0`, "vditorMermaidScript").then(() => {
20
+ addScript(
21
+ `${cdn}/dist/js/mermaid/mermaid.min.js?v=11.11.0`,
22
+ "vditorMermaidScript"
23
+ ).then(() => {
17
24
  const config: any = {
18
25
  securityLevel: "loose", // 升级后无 https://github.com/siyuan-note/siyuan/issues/3587,可使用该选项
19
26
  altFontFamily: "sans-serif",
@@ -21,19 +28,19 @@ export const mermaidRender = (element: (HTMLElement | Document) = document, cdn
21
28
  startOnLoad: false,
22
29
  flowchart: {
23
30
  htmlLabels: true,
24
- useMaxWidth: !0
31
+ useMaxWidth: !0,
25
32
  },
26
33
  sequence: {
27
34
  useMaxWidth: true,
28
35
  diagramMarginX: 8,
29
36
  diagramMarginY: 8,
30
37
  boxMargin: 8,
31
- showSequenceNumbers: true // Mermaid 时序图增加序号 https://github.com/siyuan-note/siyuan/pull/6992 https://mermaid.js.org/syntax/sequenceDiagram.html#sequencenumbers
38
+ showSequenceNumbers: true, // Mermaid 时序图增加序号 https://github.com/siyuan-note/siyuan/pull/6992 https://mermaid.js.org/syntax/sequenceDiagram.html#sequencenumbers
32
39
  },
33
40
  gantt: {
34
41
  leftPadding: 75,
35
- rightPadding: 20
36
- }
42
+ rightPadding: 20,
43
+ },
37
44
  };
38
45
  if (theme === "dark") {
39
46
  config.theme = "dark";
@@ -41,17 +48,24 @@ export const mermaidRender = (element: (HTMLElement | Document) = document, cdn
41
48
  mermaid.initialize(config);
42
49
  mermaidElements.forEach(async (item) => {
43
50
  const code = mermaidRenderAdapter.getCode(item);
44
- if (item.getAttribute("data-processed") === "true" || code.trim() === "") {
51
+ if (
52
+ item.getAttribute("data-processed") === "true" ||
53
+ code.trim() === ""
54
+ ) {
45
55
  return;
46
56
  }
47
- const id = "mermaid" + genUUID()
57
+ const id = "mermaid" + genUUID();
48
58
  try {
49
59
  const mermaidData = await mermaid.render(id, item.textContent);
50
60
  item.innerHTML = mermaidData.svg;
51
61
  } catch (e) {
52
62
  const errorElement = document.querySelector("#" + id);
53
- item.innerHTML = `${errorElement.outerHTML}<br>
54
- <div style="text-align: left"><small>${e.message.replace(/\n/, "<br>")}</small></div>`;
63
+ item.innerHTML = `
64
+ <div class="mermaid-error">
65
+ <div class="mermaid-error-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-bug-icon lucide-bug"><path d="M12 20v-9"/><path d="M14 7a4 4 0 0 1 4 4v3a6 6 0 0 1-12 0v-3a4 4 0 0 1 4-4z"/><path d="M14.12 3.88 16 2"/><path d="M21 21a4 4 0 0 0-3.81-4"/><path d="M21 5a4 4 0 0 1-3.55 3.97"/><path d="M22 13h-4"/><path d="M3 21a4 4 0 0 1 3.81-4"/><path d="M3 5a4 4 0 0 0 3.55 3.97"/><path d="M6 13H2"/><path d="m8 2 1.88 1.88"/><path d="M9 7.13V6a3 3 0 1 1 6 0v1.13"/></svg></div>
66
+ <div class="mermaid-error-title">图表加载失败</div>
67
+ <div class="mermaid-error-message">${e.message.replace(/\n/, "<br>")}</div>
68
+ </div>`;
55
69
  errorElement.parentElement.remove();
56
70
  }
57
71
  item.setAttribute("data-processed", "true");
@@ -37,6 +37,9 @@ const mergeOptions = (options?: IPreviewOptions) => {
37
37
  markdown: Constants.MARKDOWN_OPTIONS,
38
38
  math: Constants.MATH_OPTIONS,
39
39
  mode: "light",
40
+ link: {
41
+ isOpen: true,
42
+ },
40
43
  speech: {
41
44
  enable: false,
42
45
  },
@@ -102,6 +105,14 @@ export const md2html = (mdText: string, options?: IPreviewOptions) => {
102
105
  });
103
106
  };
104
107
 
108
+ /**
109
+ * 预览区域渲染入口
110
+ * - 负责将 Markdown 转为 HTML 并对各类增强特性进行初始化
111
+ * - 支持目录点击定位与链接点击行为配置(link.click / link.isOpen)
112
+ * @param previewElement 预览容器元素
113
+ * @param markdown Markdown 文本内容
114
+ * @param options 预览配置项,支持 link 点击行为自定义
115
+ */
105
116
  export const previewRender = async (
106
117
  previewElement: HTMLDivElement,
107
118
  markdown: string,
@@ -196,6 +207,7 @@ export const previewRender = async (
196
207
  if (mergedOptions.lazyLoadImage) {
197
208
  lazyLoadImageRender(previewElement);
198
209
  }
210
+
199
211
  previewElement.addEventListener(
200
212
  "click",
201
213
  (event: MouseEvent & { target: HTMLElement }) => {
@@ -212,6 +224,15 @@ export const previewRender = async (
212
224
  }
213
225
  return;
214
226
  }
227
+ if (event.target.tagName === "A") {
228
+ if (mergedOptions.link?.click) {
229
+ mergedOptions.link.click(event.target);
230
+ } else if (mergedOptions.link?.isOpen) {
231
+ window.open(event.target.getAttribute("href"));
232
+ }
233
+ event.preventDefault();
234
+ return;
235
+ }
215
236
  }
216
237
  );
217
238
  };
@@ -12,7 +12,10 @@ import { SMILESRender } from "../markdown/SMILESRender";
12
12
  import { markmapRender } from "../markdown/markmapRender";
13
13
  import { mindmapRender } from "../markdown/mindmapRender";
14
14
  import { plantumlRender } from "../markdown/plantumlRender";
15
- import { hasClosestByClassName, hasClosestByMatchTag } from "../util/hasClosest";
15
+ import {
16
+ hasClosestByClassName,
17
+ hasClosestByMatchTag,
18
+ } from "../util/hasClosest";
16
19
  import { setSelectionFocus } from "../util/selection";
17
20
  import { selectionRender } from "../markdown/selectionRender";
18
21
  import { previewImage } from "./image";