Skip to content

Commit fce436a

Browse files
committed
chore: add target attribute to a tags
1 parent aed8ad7 commit fce436a

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/fromRedactor.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@ const isVoid = ['img', 'embed']
1515

1616

1717
const ELEMENT_TAGS: IHtmlToJsonElementTags = {
18-
A: (el: HTMLElement) => ({
19-
type: 'a',
20-
attrs: {
21-
url:
22-
el.getAttribute('href') ? el.getAttribute('href') : '#'
18+
A: (el: HTMLElement) => {
19+
const attrs: Record<string, string> = {}
20+
const target = el.getAttribute('target');
21+
const href = el.getAttribute('href');
22+
23+
attrs.url = href ? href : '#';
24+
25+
if(target && target !== '') {
26+
attrs.target = target;
2327
}
24-
}),
28+
29+
return {
30+
type: "a",
31+
attrs: attrs,
32+
};
33+
},
2534
BLOCKQUOTE: () => ({ type: 'blockquote', attrs: {} }),
2635
H1: () => ({ type: 'h1', attrs: {} }),
2736
H2: () => ({ type: 'h2', attrs: {} }),

test/expectedJson.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ export default {
632632
"type": "a",
633633
"attrs": {
634634
"url": "link.com",
635+
"target": "_self",
635636
"style": {},
636637
"redactor-attributes": {
637638
"href": "link.com",
@@ -870,6 +871,7 @@ export default {
870871
"type": "a",
871872
"attrs": {
872873
"url": "#",
874+
"target": "_blank",
873875
"style": {},
874876
"redactor-attributes": {
875877
"target": "_blank"

0 commit comments

Comments
 (0)