Skip to content

Commit 1848d04

Browse files
committed
feat: added target attribute to links
1 parent f88c17a commit 1848d04

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6-
### [3.0.0](https://github.com/codeskills-dev/md-to-react-email/compare/v2.0.2...v3.0.0) (2023-06-20)
6+
### [3.0.1](https://github.com/codeskills-dev/md-to-react-email/compare/v3.0.0...v3.0.1) (2023-07-04)
7+
8+
### Features
9+
10+
- Added `target="_blank"` attribute to link tags
11+
12+
### [3.0.0](https://github.com/codeskills-dev/md-to-react-email/compare/v2.0.2...v3.0.0) (2023-07-04)
713

814
### Features
915

__tests__/parseMarkdownToReactEmailJSX.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe("Markdown to React Mail JSX Parser", () => {
4444
const markdown = "[Codeskills](https://codeskills.dev)";
4545
const expected = `<a href="https://codeskills.dev" style="${parseCssInJsToInlineCss(
4646
styles.link
47-
)}">Codeskills</a>`;
47+
)}" target="_blank">Codeskills</a>`;
4848

4949
const rendered = parseMarkdownToReactEmailJSX({ markdown });
5050
expect(rendered).toBe(expected);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "md-to-react-email",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "A simple Markdown parser for React-email written in typescript.",
55
"keywords": [
66
"markdown",

src/utils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { sanitize } from "isomorphic-dompurify";
1+
import DOMPurify from "isomorphic-dompurify";
22
import { patterns } from "./patterns";
33
import { styles } from "./styles";
44
import { StylesType } from "./types";
55

66
import { CSSProperties } from "react";
77

8+
// hook to handle target="_blank" in all links
9+
DOMPurify.addHook("afterSanitizeAttributes", (node) => {
10+
if ("target" in node) {
11+
node.setAttribute("target", "_blank");
12+
}
13+
});
14+
815
export function camelToKebabCase(str: string): string {
916
return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
1017
}
@@ -349,7 +356,9 @@ export function parseMarkdownToReactEmailJSX({
349356
patterns.link,
350357
`<a${
351358
withDataAttr ? ' data-id="react-email-link"' : ""
352-
} style="${parseCssInJsToInlineCss(finalStyles.link)}" href="$2" >$1</a>`
359+
} style="${parseCssInJsToInlineCss(
360+
finalStyles.link
361+
)}" href="$2" target="_blank" >$1</a>`
353362
);
354363

355364
// Handle code blocks (e.g., ```code```)
@@ -390,5 +399,7 @@ export function parseMarkdownToReactEmailJSX({
390399
} style="${parseCssInJsToInlineCss(finalStyles.hr)}" />`
391400
);
392401

393-
return sanitize(reactMailTemplate, { USE_PROFILES: { html: true } });
402+
return DOMPurify.sanitize(reactMailTemplate, {
403+
USE_PROFILES: { html: true },
404+
});
394405
}

0 commit comments

Comments
 (0)