Skip to content

Commit

Permalink
chore: add target attribute to a tags
Browse files Browse the repository at this point in the history
  • Loading branch information
sairajchouhan committed Dec 19, 2023
1 parent ec4585d commit c0fd0bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/fromRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {}
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: {} }),
Expand Down
2 changes: 2 additions & 0 deletions test/expectedJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ export default {
"type": "a",
"attrs": {
"url": "link.com",
"target": "_self",
"style": {},
"redactor-attributes": {
"href": "link.com",
Expand Down Expand Up @@ -870,6 +871,7 @@ export default {
"type": "a",
"attrs": {
"url": "#",
"target": "_blank",
"style": {},
"redactor-attributes": {
"target": "_blank"
Expand Down

0 comments on commit c0fd0bd

Please sign in to comment.