Skip to content
Open
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/fromRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const trimChildString = (child: any) => {
return true
}
const getDomAttributes = (child: any) => {
console.log(child)
return {
[child.nodeName]: child.nodeValue
}
Expand Down Expand Up @@ -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 } }
}
Expand Down
135 changes: 135 additions & 0 deletions test/fromRedactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,141 @@ describe("ELEMENT_TAGS", () => {
})
})

describe("Indent support", () => {
test("should parse data-indent-level from paragraph", () => {
const html = `<p data-indent-level="1">Indented paragraph</p>`
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 = `<p class="custom-class" data-indent-level="1" id="para-1">Indented paragraph</p>`
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 = `<p data-indent-level="1"><strong>Bold indented text</strong></p>`
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 = `<h1 data-indent-level="1">Indented Heading</h1>`
const json = htmlToJson(html)
expect(json).toStrictEqual({
"type": "doc",
"uid": "uid",
"attrs": {},
"children": [{
"type": "h1",
"attrs": {
"style": {},
"data-indent-level": "1",
"redactor-attributes": {}
},
"uid": "uid",
"children": [{ "text": "Indented Heading" }]
}]
})
})

test("should parse data-indent-level from blockquote", () => {
const html = `<blockquote data-indent-level="1">Indented blockquote</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 = `<figure data-indent-level="1"><img src="test.png" /></figure>`
const json = htmlToJson(html)
expect(json).toStrictEqual({
"type": "doc",
"uid": "uid",
"attrs": {},
"children": [{
"type": "img",
"attrs": {
"style": {
"text-align": undefined
},
"url": "test.png",
"width": "auto",
"data-indent-level": "1",
"redactor-attributes": {
"src": "test.png",
"position": null,
"width": "auto"
}
},
"uid": "uid",
"children": [{ "text": "" }]
}]
})
})
})

function htmlToJson (html: string, options: IHtmlToJsonOptions) {
const dom = new JSDOM(html);
let htmlDoc = dom.window.document.querySelector("body");
Expand Down
113 changes: 113 additions & 0 deletions test/toRedactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,116 @@ test("should convert codeblock to proper html, where \n should not be replaced w
const html = toRedactor(json);
expect(html).toBe(`<pre>Hi\nHello</pre>`);
})

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(`<p data-indent-level="1">Indented paragraph</p>`)
})

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(`<p class="custom-class" data-indent-level="1" id="para-1">Indented paragraph</p>`)
})

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(`<p data-indent-level="1"><strong>Bold indented text</strong></p>`)
})

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(`<h1 data-indent-level="1">Indented Heading</h1>`)
})

test("should convert blockquote with data-indent-level to HTML", () => {
const json = {
"type": "doc",
"uid": "uid",
"attrs": {},
"children": [{
"type": "blockquote",
"attrs": {
"data-indent-level": "1"
},
"uid": "uid",
"children": [{ "text": "Indented blockquote" }]
}]
}
const html = toRedactor(json)
expect(html).toBe(`<blockquote data-indent-level="1">Indented blockquote</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(`<img data-indent-level="1" src="test.png" />`)
})
})
Loading