Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double-escaping issue in code block strings #525

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

NapoliN
Copy link

@NapoliN NapoliN commented Dec 6, 2024

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

if (node.tagName === 'code') {
const codeContent = DomUtils.getInnerHTML(node, { decodeEntities: true })
node.attribs.dangerouslySetInnerHTML = `vfm{{ __html: \`${codeContent.replace(/([\\`])/g, '\\$1')}\`}}vfm`
node.childNodes = []
}

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.

const h = DomUtils.getOuterHTML(root, { selfClosingTags: true }).replace(/"vfm{{/g, '{{').replace(/}}vfm"/g, '}}')

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant