{"record":{"id":"c4569d9a3d890842","repo":"tldraw/tldraw","slug":"mermaid-diagram-error-not-a-mermaid-diagram","errorCode":null,"errorMessage":"mermaid diagram error: not a mermaid diagram","messagePattern":"mermaid diagram error: not a mermaid diagram","errorType":"exception","errorClass":"MermaidDiagramError","httpStatus":null,"severity":"error","filePath":"packages/mermaid/src/createMermaidDiagram.ts","lineNumber":78,"sourceCode":"\t// a static import, which compiles to require(<esm>) and throws\n\t// ERR_REQUIRE_ESM on Node <20.19, Jest, and ts-node) and avoids pulling\n\t// mermaid in when @tldraw/mermaid is merely imported.\n\tconst mermaid = (await import('mermaid')).default\n\n\tmermaid.initialize({\n\t\t...MERMAID_CONFIG,\n\t\t...(options.mermaidConfig ?? {}),\n\t\tflowchart: { ...MERMAID_CONFIG.flowchart, ...options.mermaidConfig?.flowchart },\n\t\tstate: { ...MERMAID_CONFIG.state, ...options.mermaidConfig?.state },\n\t\tmindmap: { ...MERMAID_CONFIG.mindmap, ...options.mermaidConfig?.mindmap },\n\t\tsequence: { ...MERMAID_CONFIG.sequence, ...options.mermaidConfig?.sequence },\n\t\tthemeVariables: { ...MERMAID_CONFIG.themeVariables, ...options.mermaidConfig?.themeVariables },\n\t})\n\n\tconst parsedResult = await mermaid.parse(text, { suppressErrors: true })\n\n\tif (!parsedResult) {\n\t\tthrow new MermaidDiagramError('not a mermaid diagram', 'parse')\n\t}\n\n\tconst offscreen = document.createElement('div')\n\toffscreen.style.position = 'absolute'\n\toffscreen.style.left = '-9999px'\n\toffscreen.style.top = '-9999px'\n\toffscreen.style.overflow = 'hidden'\n\tdocument.body.appendChild(offscreen)\n\n\ttry {\n\t\tconst parsedSvg = (await mermaid.render(`mermaid-${nextMermaidId++}`, text, offscreen)).svg\n\n\t\t// Reuse the live SVG that mermaid.render() already mounted into the\n\t\t// offscreen container.  This avoids a second DOM mount and ensures\n\t\t// getBBox() works for every diagram type (state diagrams in particular\n\t\t// lack explicit dimension attributes and rely on live layout).\n\t\tlet liveSvg = offscreen.querySelector('svg')\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/tldraw/tldraw/blob/b31086b44731a7d1d9d46be4163ac8ef7417321d/packages/mermaid/src/createMermaidDiagram.ts#L60-L96","documentation":"createMermaidDiagram calls mermaid.parse(text, { suppressErrors: true }). When suppressErrors is true, mermaid.parse returns falsy instead of throwing on invalid syntax. If it returns null/falsy, the function throws MermaidDiagramError('not a mermaid diagram', 'parse'), meaning the input text could not be parsed as any mermaid diagram.","triggerScenarios":"Passing plain text or arbitrary strings that are not valid mermaid syntax; passing mermaid syntax with a typo in the diagram type keyword; passing an empty string; passing text with a diagram type mermaid does not recognize at parse time.","commonSituations":"User-generated diagram text in a text box that has not been validated; pasting content that lost its mermaid code fence; incomplete diagram definitions (e.g. 'graph' with no edges or nodes).","solutions":["Validate the mermaid text with mermaid.parse before calling createMermaidDiagram","Show the user a syntax error and let them fix the diagram text","Wrap the call in try/catch for MermaidDiagramError and present a user-facing error message"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const mermaid = await import('mermaid')\nconst parsed = await mermaid.parse(text, { suppressErrors: true })\nif (!parsed) {\n  // show user a syntax error before calling createMermaidDiagram\n}","typeGuard":null,"tryCatchPattern":"try {\n  await createMermaidDiagram(editor, text)\n} catch (e) {\n  if (e instanceof MermaidDiagramError) {\n    // show user-friendly diagram error\n  }\n}","preventionTips":["Validate mermaid syntax in the UI before submitting to createMermaidDiagram","Provide a live-preview editor so users see parse errors before committing"],"tags":["mermaid","parsing","diagram","user-input"],"backgroundTag":null,"analyzedSha":"b31086b44731a7d1d9d46be4163ac8ef7417321d","analyzedAt":"2026-08-12T17:05:39.947Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}