UPD: Add Validators & Error Toast for Mermaid & Vega diagrams

### UPD: Feat:  Add Validators & Error Toast for Mermaid & Vega diagrams

Description:
As many time the diagrams generated or entered have syntax errors the diagrams are not rendered due to that errors, but as there isn't any notification is difficult to know what happend.

This PR add validator and toast notification when error on Mermaid and Vega/Vega-Lite diagrams, helping the user to fix its.

Note:
Another possibility of integrating this Graph Visualizer is through its svelte component: https://github.com/vega/svelte-vega/tree/main/packages/svelte-vega
This commit is contained in:
_00_ 2025-10-06 00:09:17 +02:00
parent e6cc7db3c1
commit 8538d1b2cb
2 changed files with 37 additions and 49 deletions

View file

@ -326,12 +326,26 @@
const render = async () => { const render = async () => {
onUpdate(token); onUpdate(token);
if (lang === 'mermaid' && (token?.raw ?? '').slice(-4).includes('```')) { if (lang === 'mermaid' && (token?.raw ?? '').slice(-4).includes('```')) {
mermaidHtml = await renderMermaidDiagram(code, $i18n); try {
mermaidHtml = await renderMermaidDiagram(code);
} catch (error) {
console.error('Failed to render mermaid diagram:', error);
const errorMsg = error instanceof Error ? error.message : String(error);
toast.error($i18n.t('Failed to render diagram') + `: ${errorMsg}`);
mermaidHtml = null;
}
} else if ( } else if (
(lang === 'vega' || lang === 'vega-lite') && (lang === 'vega' || lang === 'vega-lite') &&
(token?.raw ?? '').slice(-4).includes('```') (token?.raw ?? '').slice(-4).includes('```')
) { ) {
vegaHtml = await renderVegaVisualization(code, $i18n); try {
vegaHtml = await renderVegaVisualization(code);
} catch (error) {
console.error('Failed to render Vega visualization:', error);
const errorMsg = error instanceof Error ? error.message : String(error);
toast.error($i18n.t('Failed to render diagram') + `: ${errorMsg}`);
vegaHtml = null;
}
} }
}; };

View file

@ -1581,8 +1581,7 @@ export const decodeString = (str: string) => {
} }
}; };
export const renderMermaidDiagram = async (code: string, i18n?: any) => { export const renderMermaidDiagram = async (code: string) => {
try {
const { default: mermaid } = await import('mermaid'); const { default: mermaid } = await import('mermaid');
mermaid.initialize({ mermaid.initialize({
startOnLoad: false, // Should be false when using render API startOnLoad: false, // Should be false when using render API
@ -1595,42 +1594,17 @@ export const renderMermaidDiagram = async (code: string, i18n?: any) => {
return svg; return svg;
} }
return ''; return '';
} catch (error) {
console.error('Failed to render mermaid diagram:', error);
const errorMsg = error instanceof Error ? error.message : String(error);
toast.error(i18n.t('Failed to render diagram') + `: ${errorMsg}`);
return '';
}
}; };
export const renderVegaVisualization = async (spec: string, i18n?: any) => { export const renderVegaVisualization = async (spec: string, i18n?: any) => {
try {
const vega = await import('vega'); const vega = await import('vega');
const parsedSpec = JSON.parse(spec); const parsedSpec = JSON.parse(spec);
if (!parsedSpec.$schema) { let vegaSpec = parsedSpec;
const errorMsg = 'Specification missing $schema property'; if (parsedSpec.$schema && parsedSpec.$schema.includes('vega-lite')) {
toast.error(i18n.t('Failed to render diagram') + `: ${errorMsg}`);
throw new Error(errorMsg);
}
let vegaSpec;
if (parsedSpec.$schema.includes('vega-lite')) {
const vegaLite = await import('vega-lite'); const vegaLite = await import('vega-lite');
vegaSpec = vegaLite.compile(parsedSpec).spec; vegaSpec = vegaLite.compile(parsedSpec).spec;
} else if (parsedSpec.$schema.includes('vega')) {
vegaSpec = parsedSpec;
} else {
const errorMsg = 'Unknown schema format: ' + parsedSpec.$schema;
toast.error(i18n.t('Failed to render diagram') + `: ${errorMsg}`);
throw new Error(errorMsg);
} }
const runtime = vega.parse(vegaSpec); const view = new vega.View(vega.parse(vegaSpec), { renderer: 'none' });
const view = new vega.View(runtime, { renderer: 'none' });
const svg = await view.toSVG(); const svg = await view.toSVG();
return svg; return svg;
} catch (error) {
console.error('Failed to render Vega visualization:', error);
const errorMsg = error instanceof Error ? error.message : String(error);
toast.error(i18n.t('Failed to render diagram') + `: ${errorMsg}`);
return '';
}
}; };