From c0fd0bda18739f91f4cfc8237f08ab6f61e75dcc Mon Sep 17 00:00:00 2001 From: Sairaj Chouhan Date: Tue, 19 Dec 2023 17:30:36 +0530 Subject: [PATCH] chore: add target attribute to a tags --- src/fromRedactor.tsx | 21 +++++++++++++++------ test/expectedJson.ts | 2 ++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index 27242cd..963473a 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -15,13 +15,22 @@ const isVoid = ['img', 'embed'] const ELEMENT_TAGS: IHtmlToJsonElementTags = { - A: (el: HTMLElement) => ({ - type: 'a', - attrs: { - url: - el.getAttribute('href') ? el.getAttribute('href') : '#' + A: (el: HTMLElement) => { + const attrs: Record = {} + const target = el.getAttribute('target'); + const href = el.getAttribute('href'); + + attrs.url = href ? href : '#'; + + if(target && target !== '') { + attrs.target = target; } - }), + + return { + type: "a", + attrs: attrs, + }; + }, BLOCKQUOTE: () => ({ type: 'blockquote', attrs: {} }), H1: () => ({ type: 'h1', attrs: {} }), H2: () => ({ type: 'h2', attrs: {} }), diff --git a/test/expectedJson.ts b/test/expectedJson.ts index f2584d0..0540dc5 100644 --- a/test/expectedJson.ts +++ b/test/expectedJson.ts @@ -632,6 +632,7 @@ export default { "type": "a", "attrs": { "url": "link.com", + "target": "_self", "style": {}, "redactor-attributes": { "href": "link.com", @@ -870,6 +871,7 @@ export default { "type": "a", "attrs": { "url": "#", + "target": "_blank", "style": {}, "redactor-attributes": { "target": "_blank"