vditor 3.10.1 → 3.10.3

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/src/index.ts CHANGED
@@ -60,6 +60,10 @@ class Vditor extends VditorMethod {
60
60
  } else if (!options.cache.id) {
61
61
  options.cache.id = `vditor${id}`;
62
62
  }
63
+ if (!document.getElementById(id)) {
64
+ this.showErrorTip(`Failed to get element by id: ${id}`);
65
+ return;
66
+ }
63
67
  id = document.getElementById(id);
64
68
  }
65
69
 
@@ -83,9 +87,7 @@ class Vditor extends VditorMethod {
83
87
  addScript(`${mergedOptions.cdn}/dist/js/i18n/${mergedOptions.lang}.js`, i18nScriptID).then(() => {
84
88
  this.init(id as HTMLElement, mergedOptions);
85
89
  }).catch(error => {
86
- const tip = new Tip();
87
- document.body.appendChild(tip.element);
88
- tip.show(`GET ${mergedOptions.cdn}/dist/js/i18n/${mergedOptions.lang}.js net::ERR_ABORTED 404 (Not Found)`, 0)
90
+ this.showErrorTip(`GET ${mergedOptions.cdn}/dist/js/i18n/${mergedOptions.lang}.js net::ERR_ABORTED 404 (Not Found)`);
89
91
  });
90
92
  }
91
93
  } else {
@@ -94,6 +96,12 @@ class Vditor extends VditorMethod {
94
96
  }
95
97
  }
96
98
 
99
+ private showErrorTip(error: string) {
100
+ const tip = new Tip();
101
+ document.body.appendChild(tip.element);
102
+ tip.show(error, 0)
103
+ }
104
+
97
105
  /** 设置主题 */
98
106
  public setTheme(
99
107
  theme: "dark" | "classic",
@@ -75,7 +75,7 @@ export const exportHTML = (vditor: IVditor) => {
75
75
  Vditor.chartRender(previewElement, '${vditor.options.cdn}', '${vditor.options.theme}');
76
76
  Vditor.mindmapRender(previewElement, '${vditor.options.cdn}', '${vditor.options.theme}');
77
77
  Vditor.abcRender(previewElement, '${vditor.options.cdn}');
78
- Vditor.mediaRender(previewElement);
78
+ ${vditor.options.preview.render.media.enable ? 'Vditor.mediaRender(previewElement);' : ""}
79
79
  Vditor.speechRender(previewElement);
80
80
  </script>
81
81
  <script src="${vditor.options.cdn}/dist/js/icons/${vditor.options.icon}.js"></script></body></html>`;
@@ -1,3 +1,5 @@
1
+ import {getSearch} from "../util/function";
2
+
1
3
  const videoRender = (element: HTMLElement, url: string) => {
2
4
  element.insertAdjacentHTML("afterend", `<video controls="controls" src="${url}"></video>`);
3
5
  element.remove();
@@ -46,10 +48,35 @@ const iframeRender = (element: HTMLElement, url: string) => {
46
48
  `<iframe class="iframe__video"
47
49
  src="https://www.dailymotion.com/embed/video/${dailymotionMatch[2]}"></iframe>`);
48
50
  element.remove();
49
- } else if (bilibiliMatch && bilibiliMatch[1]) {
51
+ } else if (url.indexOf("bilibili.com") > -1 && (url.indexOf("bvid=") > -1 || (bilibiliMatch && bilibiliMatch[1]))) {
52
+ const params: IObject = {
53
+ bvid: getSearch("bvid", url) || (bilibiliMatch && bilibiliMatch[1]),
54
+ page: "1",
55
+ high_quality: "1",
56
+ as_wide: "1",
57
+ allowfullscreen: "true",
58
+ autoplay: "0"
59
+ };
60
+ new URL(url.startsWith("http") ? url : "https:" + url).search.split("&").forEach((item, index) => {
61
+ if (!item) {
62
+ return;
63
+ }
64
+ if (index === 0) {
65
+ item = item.substr(1);
66
+ }
67
+ const keyValue = item.split("=");
68
+ params[keyValue[0]] = keyValue[1];
69
+ });
70
+ let src = "https://player.bilibili.com/player.html?";
71
+ const keys = Object.keys(params);
72
+ keys.forEach((key, index) => {
73
+ src += `${key}=${params[key]}`;
74
+ if (index < keys.length - 1) {
75
+ src += "&";
76
+ }
77
+ });
50
78
  element.insertAdjacentHTML("afterend",
51
- `<iframe class="iframe__video"
52
- src="//player.bilibili.com/player.html?bvid=${bilibiliMatch[1]}"></iframe>`);
79
+ `<iframe class="iframe__video" src="${src}"></iframe>`);
53
80
  element.remove();
54
81
  } else if (tedMatch && tedMatch[1]) {
55
82
  element.insertAdjacentHTML("afterend",
@@ -1,10 +1,11 @@
1
1
  import {Constants} from "../constants";
2
2
  import {addScript} from "../util/addScript";
3
3
  import {mermaidRenderAdapter} from "./adapterRender";
4
+ import {genUUID} from "../util/function";
4
5
 
5
6
  declare const mermaid: {
6
7
  initialize(options: any): void,
7
- init(options: any, element: Element): void,
8
+ render(id: string, text: string): { svg: string }
8
9
  };
9
10
 
10
11
  export const mermaidRender = (element: HTMLElement, cdn = Constants.CDN, theme: string) => {
@@ -12,7 +13,7 @@ export const mermaidRender = (element: HTMLElement, cdn = Constants.CDN, theme:
12
13
  if (mermaidElements.length === 0) {
13
14
  return;
14
15
  }
15
- addScript(`${cdn}/dist/js/mermaid/mermaid.min.js?v=10.8.0`, "vditorMermaidScript").then(() => {
16
+ addScript(`${cdn}/dist/js/mermaid/mermaid.min.js`, "vditorMermaidScript").then(() => {
16
17
  const config: any = {
17
18
  securityLevel: "loose", // 升级后无 https://github.com/siyuan-note/siyuan/issues/3587,可使用该选项
18
19
  altFontFamily: "sans-serif",
@@ -26,7 +27,8 @@ export const mermaidRender = (element: HTMLElement, cdn = Constants.CDN, theme:
26
27
  useMaxWidth: true,
27
28
  diagramMarginX: 8,
28
29
  diagramMarginY: 8,
29
- boxMargin: 8
30
+ boxMargin: 8,
31
+ showSequenceNumbers: true // Mermaid 时序图增加序号 https://github.com/siyuan-note/siyuan/pull/6992 https://mermaid.js.org/syntax/sequenceDiagram.html#sequencenumbers
30
32
  },
31
33
  gantt: {
32
34
  leftPadding: 75,
@@ -37,12 +39,21 @@ export const mermaidRender = (element: HTMLElement, cdn = Constants.CDN, theme:
37
39
  config.theme = "dark";
38
40
  }
39
41
  mermaid.initialize(config);
40
- mermaidElements.forEach((item) => {
42
+ mermaidElements.forEach(async (item) => {
41
43
  const code = mermaidRenderAdapter.getCode(item);
42
44
  if (item.getAttribute("data-processed") === "true" || code.trim() === "") {
43
45
  return;
44
46
  }
45
- mermaid.init(undefined, item);
47
+ const id = "mermaid"+genUUID()
48
+ try {
49
+ const mermaidData = await mermaid.render(id, item.textContent);
50
+ item.innerHTML = mermaidData.svg;
51
+ } catch (e) {
52
+ 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>`;
55
+ errorElement.parentElement.remove();
56
+ }
46
57
  item.setAttribute("data-processed", "true");
47
58
  });
48
59
  });
@@ -22,7 +22,7 @@ export const plantumlRender = (element: (HTMLElement | Document) = document, cdn
22
22
  return;
23
23
  }
24
24
  try {
25
- e.innerHTML = `<img src="http://www.plantuml.com/plantuml/svg/~1${plantumlEncoder.encode(text)}">`;
25
+ e.innerHTML = `<object type="image/svg+xml" data="https://www.plantuml.com/plantuml/svg/~1${plantumlEncoder.encode(text)}"/>`;
26
26
  } catch (error) {
27
27
  e.className = "vditor-reset--error";
28
28
  e.innerHTML = `plantuml render error: <br>${error}`;
@@ -35,6 +35,11 @@ const mergeOptions = (options?: IPreviewOptions) => {
35
35
  speech: {
36
36
  enable: false,
37
37
  },
38
+ render: {
39
+ media: {
40
+ enable: true,
41
+ }
42
+ },
38
43
  theme: Constants.THEME_OPTIONS,
39
44
  };
40
45
  if (options.cdn) {
@@ -133,7 +138,9 @@ export const previewRender = async (previewElement: HTMLDivElement, markdown: st
133
138
  mindmapRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
134
139
  plantumlRender(previewElement, mergedOptions.cdn);
135
140
  abcRender(previewElement, mergedOptions.cdn);
136
- mediaRender(previewElement);
141
+ if (mergedOptions.render.media.enable) {
142
+ mediaRender(previewElement);
143
+ }
137
144
  if (mergedOptions.speech.enable) {
138
145
  speechRender(previewElement);
139
146
  }
@@ -231,7 +231,9 @@ export class Preview {
231
231
  mindmapRender(vditor.preview.previewElement, vditor.options.cdn, vditor.options.theme);
232
232
  plantumlRender(vditor.preview.previewElement, vditor.options.cdn);
233
233
  abcRender(vditor.preview.previewElement, vditor.options.cdn);
234
- mediaRender(vditor.preview.previewElement);
234
+ if (vditor.options.preview.render.media.enable) {
235
+ mediaRender(vditor.preview.previewElement);
236
+ }
235
237
  vditor.options.customRenders.forEach((item) => {
236
238
  item.render(vditor.preview.previewElement, vditor);
237
239
  })
@@ -59,6 +59,11 @@ export class Options {
59
59
  maxWidth: 800,
60
60
  mode: "both",
61
61
  theme: Constants.THEME_OPTIONS,
62
+ render: {
63
+ media: {
64
+ enable: true,
65
+ }
66
+ }
62
67
  },
63
68
  link: {
64
69
  isOpen: true,
@@ -150,6 +155,9 @@ export class Options {
150
155
  if (this.options.preview?.theme?.list) {
151
156
  this.defaultOptions.preview.theme.list = this.options.preview.theme.list;
152
157
  }
158
+ if (this.options.preview?.render?.media?.enable) {
159
+ this.defaultOptions.preview.render.media.enable = this.options.preview.render.media.enable;
160
+ }
153
161
  if (this.options.hint?.emoji) {
154
162
  this.defaultOptions.hint.emoji = this.options.hint.emoji;
155
163
  }
@@ -1,3 +1,11 @@
1
1
  export const genUUID = () => ([1e7].toString() + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
2
2
  (parseInt(c, 10) ^ (window.crypto.getRandomValues(new Uint32Array(1))[0] & (15 >> (parseInt(c, 10) / 4)))).toString(16)
3
3
  );
4
+
5
+ export const getSearch = (key: string, link = window.location.search) => {
6
+ const params = link.substring(link.indexOf("?"));
7
+ const hashIndex = params.indexOf("#");
8
+ // REF https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams
9
+ const urlSearchParams = new URLSearchParams(params.substring(0, hashIndex >= 0 ? hashIndex : undefined));
10
+ return urlSearchParams.get(key);
11
+ };