vite-plugin-markdown icon indicating copy to clipboard operation
vite-plugin-markdown copied to clipboard

Fix double-escaping issue in code block strings

Open NapoliN opened this issue 1 year ago • 0 comments

#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).

https://github.com/hmsk/vite-plugin-markdown/blob/26e97108120624c8a0f218ea5b15cbde511b108b/src/index.ts#L100-L104

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.

https://github.com/hmsk/vite-plugin-markdown/blob/26e97108120624c8a0f218ea5b15cbde511b108b/src/index.ts#L113

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.

NapoliN avatar Dec 06 '24 12:12 NapoliN