feat: support Mermaid charts in rich text parsing#21
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7078e7910b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (mermaidInitialized) return; | ||
| mermaid.initialize({ | ||
| startOnLoad: false, | ||
| securityLevel: "loose", |
There was a problem hiding this comment.
Keep Mermaid in strict mode for untrusted rich-text input
This parser renders user-provided rich text and then injects Mermaid SVG via innerHTML, so switching Mermaid to securityLevel: "loose" removes the default safety posture (HTML encoding and disabled click behavior) for untrusted diagram content. In contexts like message/comment rendering, this materially increases XSS/scriptable-content risk compared with Mermaid’s default strict mode and should be treated as a security regression unless the input is fully trusted and separately sanitized.
Useful? React with 👍 / 👎.
| if (!pre) return; | ||
|
|
||
| try { | ||
| const renderId = `mermaid-${Date.now()}-${index}`; |
There was a problem hiding this comment.
Generate collision-free Mermaid render IDs
The render ID uses Date.now() plus per-call index, which is not globally unique across separate parse() executions; multiple components rendering Mermaid diagrams in the same millisecond can produce identical IDs. Mermaid requires a unique ID per diagram, so collisions can cause intermittent broken rendering or duplicated SVG ID references when several rich-text blocks mount together.
Useful? React with 👍 / 👎.
Motivation
highlight.jsprocessing.Description
mermaidruntime dependency inpackage.jsonand updatepackage-lock.jsonto include Mermaid and its transitive dependencies.src/services/pltxt2htm/advancedParser.tsto importmermaid, initialize it once viaensureMermaidInitialized, and addrenderMermaidDiagramswhich findspre code.language-mermaidblocks and replaces them with rendered SVGs when possible.renderMermaidDiagramsbefore syntax highlighting and change the highlight selector topre code:not(.language-mermaid)to prevent double-processing of Mermaid blocks.Testing
npm install mermaidwhich completed successfully and updatedpackage-lock.json.npm run buildwhich completed successfully with a production build created.npm run eslintwhich finished with existing warnings and no new errors introduced by the change.Codex Task