Skip to content

Commit

Permalink
Merge pull request #28 from contentstack/development
Browse files Browse the repository at this point in the history
Support for rendering image type assets while json to html conversion and fix empty fragment added between table
  • Loading branch information
Jayesh2812 authored Nov 15, 2023
2 parents 06da814 + a5d6df4 commit 8c0a7b1
Show file tree
Hide file tree
Showing 7 changed files with 1,078 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/json-rte-serializer",
"version": "2.0.3",
"version": "2.0.4",
"description": "This Package converts Html Document to Json and vice-versa.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions src/fromRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ const traverseChildAndWarpChild = (children: Array<Object>) => {

const whiteCharPattern = /^[\s ]{2,}$/
export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject | null => {
// If node is text node
if (el.nodeType === 3) {
if (whiteCharPattern.test(el.textContent)) return null
if (["TABLE", "THEAD", "TBODY", "TR"].includes(el?.parentElement?.nodeName ?? "") && el?.textContent?.trim?.()?.length === 0) return null
if (el.textContent === '\n') {
return null
}
Expand Down
71 changes: 69 additions & 2 deletions src/toRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
return `<iframe${attrs}></iframe>`
},
p: (attrs: any, child: any) => {
if(child.includes("<figure"))
return `<div${attrs} style="overflow: hidden"><span>${child}</span></div>`
return `<p${attrs}>${child}</p>`
},
ol: (attrs: any, child: any) => {
Expand Down Expand Up @@ -118,6 +120,53 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
} else if (extraAttrs?.displayType === 'asset') {
return `<figure${attrs}>${child}</figure>`
}

else if (extraAttrs?.displayType === "display") {
const anchor = jsonBlock?.["attrs"]?.["link"];

const caption = jsonBlock?.["attrs"]?.["asset-caption"];
const position = jsonBlock?.["attrs"]?.["position"];
const inline = jsonBlock?.["attrs"]?.["inline"]
let figureAttrs = ""
const figureStyles = {
margin: "0",
};
attrs = ` src="${jsonBlock?.["attrs"]?.["asset-link"]}"` + attrs;
let img = `<img${attrs}/>`;

if (anchor) {
const target = jsonBlock?.["attrs"]?.["target"];
let anchorAttrs = `href="${anchor}"`;
if (target) {
anchorAttrs = `${anchorAttrs} target="${target}"`;
}
img = `<a ${anchorAttrs}>${img}</a>`;
}

if (caption || (position && position !== "none")) {
const figcaption = caption
? `<figcaption style="text-align:center">${caption}</figcaption>`
: "";

if (inline && position !== "right" && position !== "left") {
figureStyles["display"] = "inline-block";
}
if (position && position !== "none") {
figureStyles[inline ? "float" : "text-align"] = position;
}

if(figcaption){
img = `<div style="display: inline-block">${img}${figcaption}</div>`;
}
}
if(!isEmpty(figureStyles)){
figureAttrs = ` style="${getStyleStringFromObject(figureStyles)}"`
}
if(inline && !caption && (!position ||position==='none')){
return img
}
return `<figure${figureAttrs ? figureAttrs : ""}>${img}</figure>`;
}
return `<span${attrs}>${child}</span>`
},
inlineCode: (attrs: any, child: any) => {
Expand Down Expand Up @@ -362,7 +411,9 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
delete attrsJson['content-type-uid']
attrsJson['sys-style-type'] = allattrs['display-type']
delete attrsJson['display-type']
} else if (attrsJson['type'] === "asset") {
}

else if (attrsJson['type'] === "asset") {
attrsJson['data-sys-asset-filelink'] = allattrs['asset-link']
delete attrsJson['asset-link']
attrsJson['data-sys-asset-uid'] = allattrs['asset-uid']
Expand Down Expand Up @@ -399,7 +450,16 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
if (!attrsJson['sys-style-type']) {
attrsJson['sys-style-type'] = String(allattrs['asset-type']).indexOf('image') > -1 ? 'display' : 'download'
}

if (attrsJson?.["display-type"] === "display") {
const styleObj = jsonValue?.["attrs"]?.["style"] ?? {};
if (!styleObj["width"]) {
styleObj["width"] = "auto";
}
delete styleObj["float"];
// (attrsJson["style"] && typeof attrsJson["style"] === 'string')
// ? (attrsJson["style"] += getStyleStringFromObject(styleObj)) :
(attrsJson["style"] = getStyleStringFromObject(styleObj));
}
delete attrsJson['display-type']
}
}
Expand Down Expand Up @@ -468,3 +528,10 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string

return children
}


function getStyleStringFromObject(styleObj: { [key: string]: string }) {
return Object.keys(styleObj)
.map((key) => `${key}: ${styleObj[key]}`)
.join("; ");
}
35 changes: 35 additions & 0 deletions test/fromRedactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,38 @@ describe('getNestedValueIfAvailable', () => {
});

});
describe("CS-41001", () =>{
test("should not add fragment for text nodes having white spaces", () => {
const dom = new JSDOM();
const document = dom.window.document;
const body = document.createElement("body");
const td1 = document.createElement("td");
const td2 = document.createElement("td");
const tr = document.createElement("tr");
const text = document.createTextNode(` `)
td1.textContent = "Hello";
td2.textContent = "World";
tr.appendChild(td1);
tr.append(text)
tr.appendChild(td2);
body.append(tr)
const jsonValue = fromRedactor(body);
expect(jsonValue).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"tr","attrs":{},"uid":"uid","children":[{"type":"td","attrs":{},"uid":"uid","children":[{"text":"Hello"}]},{"type":"td","attrs":{},"uid":"uid","children":[{"text":"World"}]}]}]})
})
test("should add fragment for text nodes between block nodes", () => {
const dom = new JSDOM();
const document = dom.window.document;
const body = document.createElement("body");
const p1 = document.createElement("p");
const p2 = document.createElement("p");
const text = document.createTextNode(` beautiful `)
p1.textContent = "Hello";
p2.textContent = "World";
body.appendChild(p1);
body.append(text)
body.appendChild(p2);
const jsonValue = fromRedactor(body);
expect(jsonValue).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"text":"Hello"}]},{"type":"fragment","attrs":{},"uid":"uid","children":[{"text":" beautiful "}]},{"type":"p","attrs":{},"uid":"uid","children":[{"text":"World"}]}]})
})
})

Loading

0 comments on commit 8c0a7b1

Please sign in to comment.