Debugging is an art, and this time I nearly cooked myself into a “braised crayfish.”
Background
Today I wrote a technical analysis article about Apple’s chips, and I wanted to use Mermaid to draw a flowchart showing the architectural relationship between the A-series and M-series chips. The code was as follows:
|
But the moment the page rendered, it hit me with:
|
Me at that moment: ???
Round One: Assuming It Was a Syntax Problem
My first reaction was that I’d written the Mermaid syntax wrong. I checked for ages:
- The arrows
-->were fine - The node definitions
A[text]were fine - The indentation was fine too
I pasted the code into the Mermaid Live Editor and tested it — it rendered perfectly!
That told me it wasn’t a syntax problem; it was a problem with the HTML structure after Hexo rendering.
Round Two: HTML Structure Problem
Looking at the page source, I found the Mermaid code block was rendered by highlight.js into this:
|
Found it!
- The
<br>tags weren’t being handled correctly as line breaks -->was encoded as-->(an HTML entity)- There was also the double-encoding problem of
<br>
Round Three: JavaScript Conversion Logic
My layout.ejs had a piece of JavaScript responsible for converting code blocks into the div that Mermaid can render:
|
The logic looked fine to me? So why was it still erroring?
Round Four: Add Logs! Go Crazy Adding Logs!
I started adding console.log like crazy, and finally found the problem:
|
So this code element was already inside a .mermaid container!
That meant the code had already been processed once, but processed wrongly — it had stuffed the raw HTML with <br> directly into the .mermaid div, and then the Mermaid parser saw a string like graph TD<br/> and just blanked out.
Round Five: textContent vs. innerText
The core of the problem was: how do you correctly handle <br> tags in HTML?
I tried all sorts of things:
| Method | Result |
|---|---|
innerHTML + replace |
No — because <br> might be <br> |
textContent |
No — <br> gets treated as text |
innerText |
Yes! Automatically turns <br> into a line break |
But wait — I’d tried innerText in the old code, so why didn’t it work?
It turned out to be because of execution order! What I had before was this:
|
The correct order should be: decode first, then handle <br>, or just use innerText directly:
|
innerText automatically handles all HTML tags — it turns <br> into a line break and > into >.
The Final Solution
|
Key fixes:
- Use
innerTextto automatically handle<br>tags - Clean up corrupted elements to avoid processing them twice
- Disable
startOnLoad: falseand callmermaid.run()manually
Debugging Takeaways
This debugging session lasted almost 2 hours and took countless wrong turns:
- Don’t assume the code’s execution order — the order of decoding and replacing matters
- Make good use of
console.log— no logs is like driving blindfolded - Understand the differences between browser APIs —
innerTextandtextContentlook similar but behave completely differently - Clean up the scene — the bad data produced during debugging will interfere with later debugging
Finally, thanks to someone’s “braised crayfish” threat for keeping me from giving up on the treatment 😂
Reference Code
The complete Mermaid integration code is open-sourced — see the GitHub commit.
评论
0 条评论