From 8767ceadb27dcf2f5656050693befb8773ef2ae5 Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Thu, 14 Nov 2024 18:46:02 +0530 Subject: [PATCH 1/4] feat: added conditions to reduce fragment and convert b,i tags to strong and em --- src/fromRedactor.tsx | 24 +++++++++++++++++++++++- src/index.tsx | 10 ++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index 525f27a..68fd671 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -863,7 +863,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 } }) @@ -1084,4 +1084,26 @@ function getTbodyChildren (rows: any[]) { }, []) return newTbodyChildren +} + +export function replaceNonSemanticTags (el: HTMLElement) { + const nonSematicTagMap = { + b: 'strong', + i: 'em' + } as Record + + const nodes = el.querySelectorAll(Object.keys(nonSematicTagMap).join(',')) + + nodes.forEach((node) => { + const nodeName = node.nodeName.toLowerCase() + if(!(nodeName in nonSematicTagMap)) return + + const strong = el.ownerDocument.createElement(nonSematicTagMap[nodeName]) + strong.innerHTML = node.innerHTML + Array.from(node.attributes).forEach((attr) => { + if(attr.nodeValue) + strong.setAttribute(attr.nodeName, attr.nodeValue) + }) + node.replaceWith(strong) + }) } \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index d1e48ff..d86d512 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,6 +1,12 @@ import "array-flat-polyfill" -import { fromRedactor } from "./fromRedactor" +import { fromRedactor, replaceNonSemanticTags } from "./fromRedactor" import { toRedactor } from "./toRedactor" import {jsonToMarkdownSerializer} from './jsonToMarkdown' +import { IHtmlToJsonOptions } from "./types" export * from "./types" -export { fromRedactor as htmlToJson, toRedactor as jsonToHtml, jsonToMarkdownSerializer as jsonToMarkdown } \ No newline at end of file +export { toRedactor as jsonToHtml, jsonToMarkdownSerializer as jsonToMarkdown } + +export const htmlToJson = (el: any, options?:IHtmlToJsonOptions) => { + replaceNonSemanticTags(el) + return fromRedactor(el, options) +} \ No newline at end of file From d0728621a78a0e74c73cd6e26bb8a58194d2838d Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Tue, 19 Nov 2024 00:14:30 +0530 Subject: [PATCH 2/4] refactor: add simpler support for b tag --- src/fromRedactor.tsx | 23 +---------------------- src/index.tsx | 10 ++-------- 2 files changed, 3 insertions(+), 30 deletions(-) diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index 68fd671..da4ef56 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -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 }) @@ -1085,25 +1086,3 @@ function getTbodyChildren (rows: any[]) { }, []) return newTbodyChildren } - -export function replaceNonSemanticTags (el: HTMLElement) { - const nonSematicTagMap = { - b: 'strong', - i: 'em' - } as Record - - const nodes = el.querySelectorAll(Object.keys(nonSematicTagMap).join(',')) - - nodes.forEach((node) => { - const nodeName = node.nodeName.toLowerCase() - if(!(nodeName in nonSematicTagMap)) return - - const strong = el.ownerDocument.createElement(nonSematicTagMap[nodeName]) - strong.innerHTML = node.innerHTML - Array.from(node.attributes).forEach((attr) => { - if(attr.nodeValue) - strong.setAttribute(attr.nodeName, attr.nodeValue) - }) - node.replaceWith(strong) - }) -} \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index d86d512..d1e48ff 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,12 +1,6 @@ import "array-flat-polyfill" -import { fromRedactor, replaceNonSemanticTags } from "./fromRedactor" +import { fromRedactor } from "./fromRedactor" import { toRedactor } from "./toRedactor" import {jsonToMarkdownSerializer} from './jsonToMarkdown' -import { IHtmlToJsonOptions } from "./types" export * from "./types" -export { toRedactor as jsonToHtml, jsonToMarkdownSerializer as jsonToMarkdown } - -export const htmlToJson = (el: any, options?:IHtmlToJsonOptions) => { - replaceNonSemanticTags(el) - return fromRedactor(el, options) -} \ No newline at end of file +export { fromRedactor as htmlToJson, toRedactor as jsonToHtml, jsonToMarkdownSerializer as jsonToMarkdown } \ No newline at end of file From 733220e8fc7e75a1fe04ba799e4cf5d3197e1dfc Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar Date: Tue, 19 Nov 2024 00:27:32 +0530 Subject: [PATCH 3/4] test: add test cases --- test/fromRedactor.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/fromRedactor.test.ts b/test/fromRedactor.test.ts index e27debd..f84adb7 100644 --- a/test/fromRedactor.test.ts +++ b/test/fromRedactor.test.ts @@ -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 and proper json marks", () => { + const html = `

HelloTest2World

` + 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 = `

Hello Hii

` + 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"}]}]}) + + }) }) From 9893e37b13a088e22bcb702958bcdbae06dc5cf8 Mon Sep 17 00:00:00 2001 From: Jayesh Deorukhkar <52153715+Jayesh2812@users.noreply.github.com> Date: Fri, 29 Nov 2024 13:25:14 +0530 Subject: [PATCH 4/4] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9c670d6..48f0f16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/json-rte-serializer", - "version": "2.0.11", + "version": "2.0.12", "description": "This Package converts Html Document to Json and vice-versa.", "main": "lib/index.js", "module": "lib/index.mjs",