Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/markdown-escape-link-quotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-email": patch
---

Escape double quotes in `Markdown` link `href`/`title` and image `title` attributes, matching the escaping already applied to image `src`/`alt`. A markdown title like `'The "Complete" Guide'` no longer breaks out of the attribute in the rendered HTML.
12 changes: 12 additions & 0 deletions packages/react-email/src/components/markdown/markdown.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ console.log(\`Hello, $\{name}!\`);
`);
});

it('escapes double quotes in link/image href and title attributes', async () => {
const actualOutput = await render(
<Markdown>
{`[guide](https://example.com/?q="a" 'The "Complete" Guide') and ![logo](https://cdn.example.com/a.png 'Acme "logo"')`}
</Markdown>,
);
expect(actualOutput).toMatchInlineSnapshot(`
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!--$--><div data-id="react-email-markdown"><p><a href="https://example.com/?q=&quot;a&quot;" target="_blank" title="The &quot;Complete&quot; Guide" style="color:#007bff;text-decoration:underline;background-color:transparent">guide</a> and <img src="https://cdn.example.com/a.png" alt="logo" title="Acme &quot;logo&quot;"></p>
</div><!--/$-->"
`);
});

it('renders lists in the correct format for browsers', async () => {
const actualOutput = await render(
<Markdown>
Expand Down
6 changes: 3 additions & 3 deletions packages/react-email/src/components/markdown/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const Markdown = React.forwardRef<HTMLDivElement, MarkdownProps>(

renderer.image = ({ href, text, title }) => {
return `<img src="${href.replaceAll('"', '&quot;')}" alt="${text.replaceAll('"', '&quot;')}"${
title ? ` title="${title}"` : ''
title ? ` title="${title.replaceAll('"', '&quot;')}"` : ''
}${
parseCssInJsToInlineCss(finalStyles.image) !== ''
? ` style="${parseCssInJsToInlineCss(finalStyles.image)}"`
Expand All @@ -109,8 +109,8 @@ export const Markdown = React.forwardRef<HTMLDivElement, MarkdownProps>(
renderer.link = ({ href, title, tokens }) => {
const text = renderer.parser.parseInline(tokens);

return `<a href="${href}" target="_blank"${
title ? ` title="${title}"` : ''
return `<a href="${href.replaceAll('"', '&quot;')}" target="_blank"${
title ? ` title="${title.replaceAll('"', '&quot;')}"` : ''
}${
parseCssInJsToInlineCss(finalStyles.link) !== ''
? ` style="${parseCssInJsToInlineCss(finalStyles.link)}"`
Expand Down
Loading