Skip to content

Commit

Permalink
Merge pull request #21 from contentstack/development
Browse files Browse the repository at this point in the history
feat: to add support for inline class and id while converting json data to html
  • Loading branch information
Ashwini-Rathod committed Mar 20, 2023
2 parents 5473a1b + 1e16b59 commit 1e8a550
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
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.1",
"version": "2.0.2",
"description": "This Package converts Html Document to Json and vice-versa.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
13 changes: 12 additions & 1 deletion src/toRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const TEXT_WRAPPERS: IJsonToHtmlTextTags = {
},
'inlineCode': (child: any, value:any) => {
return `<span data-type='inlineCode'>${child}</span>`
}
},
}
export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string => {
//TODO: optimize assign once per function call
Expand All @@ -165,6 +165,17 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
if (jsonValue['break']) {
text += `<br/>`
}
if(jsonValue['classname'] || jsonValue['id']){
if(jsonValue['classname'] && jsonValue['id']){
text = `<span class=${jsonValue['classname']} id=${jsonValue['id']}>${text}</span>`
}
else if(jsonValue['classname'] && !jsonValue['id']){
text = `<span class=${jsonValue['classname']}>${text}</span>`
}
else if(jsonValue['id'] && !jsonValue['classname']){
text = `<span id=${jsonValue['id']}>${text}</span>`
}
}
if (jsonValue.text.includes('\n')) {
text = text.replace(/\n/g, '<br/>')
}
Expand Down
31 changes: 31 additions & 0 deletions test/expectedJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1714,5 +1714,36 @@ export default {
]
}
]
},
"inline-classname-and-id": {
"html": '<p><span>This is test for adding inline <span class=class>class</span> and <span id=id>id</span></span></p>',
"json": [
{
"type": "p",
"attrs": {},
"children": [
{
"type": "span",
"attrs": {},
"children": [
{
"text": "This is test for adding inline "
},
{
"text": "class",
"classname": "class"
},
{
"text": " and "
},
{
"text": "id",
"id": "id"
}
]
}
]
}
]
}
}
7 changes: 7 additions & 0 deletions test/toRedactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,11 @@ describe("Testing json to html conversion", () => {
let testResult = isEqual(htmlValue, expectedValue["'\n' to <br>"].html)
expect(testResult).toBe(true)
})

it("Inline classname and id", () => {
let jsonValue = expectedValue["inline-classname-and-id"].json
let htmlValue = toRedactor({ type: "doc", attrs: {}, children: jsonValue })
let testResult = isEqual(htmlValue, expectedValue["inline-classname-and-id"].html)
expect(testResult).toBe(true)
})
})

0 comments on commit 1e8a550

Please sign in to comment.