Skip to content

Commit

Permalink
Fix template string escaping (#431)
Browse files Browse the repository at this point in the history
We need to escape template strings after back slash
escaping so that unescaping template strings doesn't
happen by accident.

---

Fixes #430
  • Loading branch information
johno authored Mar 2, 2019
1 parent 83fa08f commit c0f2a21
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/mdx/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ it('Should output parseable JSX', async () => {
})

it('Should be able to render JSX with React', async () => {
const result = await renderWithReact('# Hello, world!')
const result = await renderWithReact(`# Hello, world!
const code = () => \`template string\`
`)

expect(result).toContain('<h1 style="color:tomato">Hello, world!</h1>')
})
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const EMPTY_NEWLINE = '\n\n'

const toTemplateLiteral = text => {
const escaped = text
.replace(/`/g, '\\`') // Escape "`"" since
.replace(/\\/g, '\\\\') // Escape all "\" to avoid unwanted escaping in text nodes
.replace(/`/g, '\\`') // Escape "`"" since

return '{`' + escaped + '`}'
}
Expand Down

0 comments on commit c0f2a21

Please sign in to comment.