Skip to content

Commit ad7522a

Browse files
committed
🎨 fix #1672
1 parent f06b1a3 commit ad7522a

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
### v3.10.6 / 2024-09
2121

22+
* [支持 echarts 中执行函数](https://github.com/Vanessa219/vditor/issues/1672) `改进功能`
2223
* [井号后输入空格就渲染标题](https://github.com/Vanessa219/vditor/issues/729) `改进功能`
2324
* [ir 模式下标题删除报错](https://github.com/Vanessa219/vditor/issues/1666) `修复缺陷`
2425
* [升级 echarts 至 5.5.1](https://github.com/Vanessa219/vditor/issues/1664) `开发重构`

src/ts/markdown/chartRender.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Constants} from "../constants";
22
import {addScript} from "../util/addScript";
33
import {chartRenderAdapter} from "./adapterRender";
4+
import {looseJsonParse} from "../util/function";
45

56
declare const echarts: {
67
init(element: HTMLElement, theme?: string): IEChart;
@@ -10,7 +11,7 @@ export const chartRender = (element: (HTMLElement | Document) = document, cdn =
1011
const echartsElements = chartRenderAdapter.getElements(element);
1112
if (echartsElements.length > 0) {
1213
addScript(`${cdn}/dist/js/echarts/echarts.min.js?v=5.5.1`, "vditorEchartsScript").then(() => {
13-
echartsElements.forEach((e: HTMLDivElement) => {
14+
echartsElements.forEach(async (e: HTMLDivElement) => {
1415
if (e.parentElement.classList.contains("vditor-wysiwyg__pre") ||
1516
e.parentElement.classList.contains("vditor-ir__marker--pre")) {
1617
return;
@@ -24,7 +25,7 @@ export const chartRender = (element: (HTMLElement | Document) = document, cdn =
2425
if (e.getAttribute("data-processed") === "true") {
2526
return;
2627
}
27-
const option = JSON.parse(text);
28+
const option = await looseJsonParse(text);
2829
echarts.init(e, theme === "dark" ? "dark" : undefined).setOption(option);
2930
e.setAttribute("data-processed", "true");
3031
} catch (error) {

src/ts/util/function.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ export const getSearch = (key: string, link = window.location.search) => {
99
const urlSearchParams = new URLSearchParams(params.substring(0, hashIndex >= 0 ? hashIndex : undefined));
1010
return urlSearchParams.get(key);
1111
};
12+
13+
export const looseJsonParse = (text: string) => {
14+
return Function(`"use strict";return (${text})`)();
15+
};

0 commit comments

Comments
 (0)