From 1848d044eb1c0eed0a981dcd3d0ffc5485158e70 Mon Sep 17 00:00:00 2001 From: lordelogos Date: Tue, 4 Jul 2023 14:17:10 +0100 Subject: [PATCH] feat: added target attribute to links --- CHANGELOG.md | 8 +++++++- __tests__/parseMarkdownToReactEmailJSX.test.ts | 2 +- package.json | 2 +- src/utils.ts | 17 ++++++++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41d6723..d99a22b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,13 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. -### [3.0.0](https://github.com/codeskills-dev/md-to-react-email/compare/v2.0.2...v3.0.0) (2023-06-20) +### [3.0.1](https://github.com/codeskills-dev/md-to-react-email/compare/v3.0.0...v3.0.1) (2023-07-04) + +### Features + +- Added `target="_blank"` attribute to link tags + +### [3.0.0](https://github.com/codeskills-dev/md-to-react-email/compare/v2.0.2...v3.0.0) (2023-07-04) ### Features diff --git a/__tests__/parseMarkdownToReactEmailJSX.test.ts b/__tests__/parseMarkdownToReactEmailJSX.test.ts index 5c6b37b..0f5a22e 100644 --- a/__tests__/parseMarkdownToReactEmailJSX.test.ts +++ b/__tests__/parseMarkdownToReactEmailJSX.test.ts @@ -44,7 +44,7 @@ describe("Markdown to React Mail JSX Parser", () => { const markdown = "[Codeskills](https://codeskills.dev)"; const expected = `Codeskills`; + )}" target="_blank">Codeskills`; const rendered = parseMarkdownToReactEmailJSX({ markdown }); expect(rendered).toBe(expected); diff --git a/package.json b/package.json index 132dab2..d4afa0c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "md-to-react-email", - "version": "3.0.0", + "version": "3.0.1", "description": "A simple Markdown parser for React-email written in typescript.", "keywords": [ "markdown", diff --git a/src/utils.ts b/src/utils.ts index fa5dfe5..e0aacdc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,10 +1,17 @@ -import { sanitize } from "isomorphic-dompurify"; +import DOMPurify from "isomorphic-dompurify"; import { patterns } from "./patterns"; import { styles } from "./styles"; import { StylesType } from "./types"; import { CSSProperties } from "react"; +// hook to handle target="_blank" in all links +DOMPurify.addHook("afterSanitizeAttributes", (node) => { + if ("target" in node) { + node.setAttribute("target", "_blank"); + } +}); + export function camelToKebabCase(str: string): string { return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase(); } @@ -349,7 +356,9 @@ export function parseMarkdownToReactEmailJSX({ patterns.link, `$1` + } style="${parseCssInJsToInlineCss( + finalStyles.link + )}" href="$2" target="_blank" >$1` ); // Handle code blocks (e.g., ```code```) @@ -390,5 +399,7 @@ export function parseMarkdownToReactEmailJSX({ } style="${parseCssInJsToInlineCss(finalStyles.hr)}" />` ); - return sanitize(reactMailTemplate, { USE_PROFILES: { html: true } }); + return DOMPurify.sanitize(reactMailTemplate, { + USE_PROFILES: { html: true }, + }); }