diff --git a/LICENSE b/LICENSE index 60c49c4..b4bd84b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024-2025 Contentstack +Copyright (c) 2025-2026 Contentstack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index 21c4808..0d7711c 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -20,6 +20,7 @@ const trimChildString = (child: any) => { return true } const getDomAttributes = (child: any) => { + console.log(child) return { [child.nodeName]: child.nodeValue } @@ -476,6 +477,13 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject attrs: { ...elementAttrs['attrs'], 'data-sys-asset-uid': redactor['data-sys-asset-uid'] } } } + if (redactor['data-indent-level']) { + elementAttrs = { + ...elementAttrs, + attrs: { ...elementAttrs['attrs'], 'data-indent-level': redactor['data-indent-level'] } + } + delete redactor['data-indent-level'] + } elementAttrs = { ...elementAttrs, attrs: { ...elementAttrs['attrs'], "redactor-attributes": redactor } } } diff --git a/test/fromRedactor.test.ts b/test/fromRedactor.test.ts index 01b3f0e..b5bbe40 100644 --- a/test/fromRedactor.test.ts +++ b/test/fromRedactor.test.ts @@ -421,6 +421,141 @@ describe("ELEMENT_TAGS", () => { }) }) +describe("Indent support", () => { + test("should parse data-indent-level from paragraph", () => { + const html = `
Indented paragraph
` + const json = htmlToJson(html) + expect(json).toStrictEqual({ + "type": "doc", + "uid": "uid", + "attrs": {}, + "children": [{ + "type": "p", + "attrs": { + "style": {}, + "data-indent-level": "1", + "redactor-attributes": {} + }, + "uid": "uid", + "children": [{ "text": "Indented paragraph" }] + }] + }) + }) + + test("should parse data-indent-level from paragraph with other attributes", () => { + const html = `Indented paragraph
` + const json = htmlToJson(html) + expect(json).toStrictEqual({ + "type": "doc", + "uid": "uid", + "attrs": {}, + "children": [{ + "type": "p", + "attrs": { + "style": {}, + "data-indent-level": "1", + "id": "para-1", + "class-name": "custom-class", + "redactor-attributes": { + "class": "custom-class", + "id": "para-1" + } + }, + "uid": "uid", + "children": [{ "text": "Indented paragraph" }] + }] + }) + }) + + test("should parse data-indent-level from paragraph with bold text", () => { + const html = `Bold indented text
` + const json = htmlToJson(html) + expect(json).toStrictEqual({ + "type": "doc", + "uid": "uid", + "attrs": {}, + "children": [{ + "type": "p", + "attrs": { + "style": {}, + "data-indent-level": "1", + "redactor-attributes": {} + }, + "uid": "uid", + "children": [{ "text": "Bold indented text", "attrs": { "style": {} }, "bold": true }] + }] + }) + }) + + test("should parse data-indent-level from heading", () => { + const html = `Indented blockquote` + const json = htmlToJson(html) + expect(json).toStrictEqual({ + "type": "doc", + "uid": "uid", + "attrs": {}, + "children": [{ + "type": "blockquote", + "attrs": { + "style": {}, + "data-indent-level": "1", + "redactor-attributes": {} + }, + "uid": "uid", + "children": [{ "text": "Indented blockquote" }] + }] + }) + }) + + test("should parse data-indent-level from figure with image", () => { + const html = `

Hi\nHello`); }) + +describe("Indent support", () => { + test("should convert paragraph with data-indent-level to HTML", () => { + const json = { + "type": "doc", + "uid": "uid", + "attrs": {}, + "children": [{ + "type": "p", + "attrs": { + "data-indent-level": "1" + }, + "uid": "uid", + "children": [{ "text": "Indented paragraph" }] + }] + } + const html = toRedactor(json) + expect(html).toBe(`
Indented paragraph
`) + }) + + test("should convert paragraph with data-indent-level and other attributes to HTML", () => { + const json = { + "type": "doc", + "uid": "uid", + "attrs": {}, + "children": [{ + "type": "p", + "attrs": { + "data-indent-level": "1", + "id": "para-1", + "class-name": "custom-class" + }, + "uid": "uid", + "children": [{ "text": "Indented paragraph" }] + }] + } + const html = toRedactor(json) + expect(html).toBe(`Indented paragraph
`) + }) + + test("should convert paragraph with data-indent-level and bold text to HTML", () => { + const json = { + "type": "doc", + "uid": "uid", + "attrs": {}, + "children": [{ + "type": "p", + "attrs": { + "data-indent-level": "1" + }, + "uid": "uid", + "children": [{ "text": "Bold indented text", "bold": true }] + }] + } + const html = toRedactor(json) + expect(html).toBe(`Bold indented text
`) + }) + + test("should convert heading with data-indent-level to HTML", () => { + const json = { + "type": "doc", + "uid": "uid", + "attrs": {}, + "children": [{ + "type": "h1", + "attrs": { + "data-indent-level": "1" + }, + "uid": "uid", + "children": [{ "text": "Indented Heading" }] + }] + } + const html = toRedactor(json) + expect(html).toBe(`Indented blockquote`) + }) + + test("should convert image with data-indent-level to HTML", () => { + const json = { + "type": "doc", + "uid": "uid", + "attrs": {}, + "children": [{ + "type": "img", + "attrs": { + "url": "test.png", + "data-indent-level": "1" + }, + "uid": "uid", + "children": [{ "text": "" }] + }] + } + const html = toRedactor(json) + expect(html).toBe(`
`)
+ })
+})