Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions projects/app/src/components/Markdown/img/EChartsCodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,37 @@ const EChartsCodeBlock = ({ code }: { code: string }) => {
});

useLayoutEffect(() => {
const buildOption = (userOption: any) => {
const userToolbox = userOption.toolbox || {};
const userFeature = userToolbox.feature || {};
return {
...userOption,
toolbox: {
...userToolbox,
// show: true,
feature: {
saveAsImage: {},
...userFeature
}
}
};
};

const option = (() => {
// First try json5 parse (safe, handles JSON5 syntax like comments/trailing commas)
try {
const userOption = json5.parse(code.trim());
const userToolbox = userOption.toolbox || {};
const userFeature = userToolbox.feature || {};

const parse = {
...userOption,
toolbox: {
...userToolbox,
// show: true,
feature: {
saveAsImage: {},
...userFeature
}
}
};
return buildOption(userOption);
} catch (_e) {}

return parse;
} catch (error) {}
// Fallback: evaluate as JS expression to support formatter functions and other JS values
try {
// eslint-disable-next-line no-new-func
const userOption = new Function(`return (${code.trim()})`)();
if (userOption && typeof userOption === 'object') {
return buildOption(userOption);
}
} catch (_e) {}
})();

setOption(option ?? {});
Expand Down
Loading