Fix double-escaping issue in code block strings #525
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#524
Analyzed the code to identify the root cause and implemented a fix.
Initially, each code block is wrapped using vfm{{___html: ....}}, where an HTML-escaped string is processed and assigned to the dangerouslySetInnerHTML attribute (as a node attribute, which is a critical detail).
vite-plugin-markdown/src/index.ts
Lines 100 to 104 in 26e9710
After all nodes are processed, getOuterHTML is used to retrieve another HTML-escaped string. Since the default value of the decodeEntities option is true, the dangerouslySetInnerHTML attribute of the nodes representing each code block undergoes an additional round of escaping.
vite-plugin-markdown/src/index.ts
Line 113 in 26e9710
I believe this double escaping is the root cause of the issue.
A similar change was made in this commit, but the approach taken here is the exact opposite. By setting decodeEntities to false, we can prevent special characters from being escaped again.
This approach worked successfully in my environment. Please review and confirm.