Skip to content

feat: added conditions to reduce fragment and convert b,i tags to strong and em #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/fromRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const TEXT_TAGS: IHtmlToJsonTextTags = {
I: () => ({ italic: true }),
S: () => ({ strikethrough: true }),
STRONG: () => ({ bold: true }),
B: () => ({ bold: true }),
U: () => ({ underline: true }),
SUP: () => ({ superscript: true }),
SUB: () => ({ subscript: true })
Expand Down Expand Up @@ -863,7 +864,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
}
let noOfInlineElement = 0
Array.from(el.parentNode?.childNodes || []).forEach((child: any) => {
if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline'))) {
if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline')) || child.nodeName in TEXT_TAGS) {
noOfInlineElement += 1
}
})
Expand Down Expand Up @@ -1084,4 +1085,4 @@ function getTbodyChildren (rows: any[]) {

}, [])
return newTbodyChildren
}
}
13 changes: 13 additions & 0 deletions test/fromRedactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,19 @@ describe("Testing html to json conversion", () => {
const json = htmlToJson(html)
expect(json).toEqual({ type: "doc", attrs: {}, uid: "uid", children:[{ type: "social-embeds", uid: 'uid', attrs: { src: "https://www.youtube.com/embed/3V-Sq7_uHXQ" }, children: [{ text: ""}] }]})
})

test("should replace all instances of <b> and <i> proper json marks", () => {
const html = `<p><b>Hello</b><i>Test2</i><b>World</b></p>`
const json = htmlToJson(html)
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"text":"Hello","attrs":{"style":{}},"bold":true},{"text":"Test2","attrs":{"style":{}},"italic":true},{"text":"World","attrs":{"style":{}},"bold":true}]}]})
})

test("should not add fragment for html containing a text tag and span tag", () => {
const html = `<p><strong>Hello</strong><span> Hii</span></p>`
const json = htmlToJson(html)
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"text":"Hello","attrs":{"style":{}},"bold":true},{"text":" Hii"}]}]})

})
})


Expand Down
Loading