Skip to content

Commit bc12f5d

Browse files
Merge pull request #62 from contentstack/development
Development
2 parents 556d746 + bb0c5c0 commit bc12f5d

7 files changed

+30
-8
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/json-rte-serializer",
3-
"version": "2.0.10",
3+
"version": "2.0.11",
44
"description": "This Package converts Html Document to Json and vice-versa.",
55
"main": "lib/index.js",
66
"module": "lib/index.mjs",

src/fromRedactor.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ export const ELEMENT_TAGS: IHtmlToJsonElementTags = {
6060
P: () => ({ type: 'p', attrs: {} }),
6161
PRE: () => ({ type: 'code', attrs: {} }),
6262
UL: () => ({ type: 'ul', attrs: {} }),
63-
IFRAME: (el: HTMLElement) => ({ type: 'embed', attrs: { src: el.getAttribute('src') } }),
63+
IFRAME: (el: HTMLElement) => {
64+
if(el.getAttribute('data-type') === "social-embeds") {
65+
const src = el.getAttribute('src')
66+
el.removeAttribute('data-type')
67+
el.removeAttribute('src')
68+
return { type: 'social-embeds', attrs: { src } }
69+
}
70+
return { type: 'embed', attrs: { src: el.getAttribute('src') } }
71+
},
6472
TABLE: (el: HTMLElement) => ({ type: 'table', attrs: {} }),
6573
THEAD: (el: HTMLElement) => ({ type: 'thead', attrs: {} }),
6674
TBODY: (el: HTMLElement) => ({ type: 'tbody', attrs: {} }),

src/toRedactor.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
185185
},
186186
script: (attrs: any, child: any) => {
187187
return `<script ${attrs}>${child}</script>`
188+
},
189+
"social-embeds": (attrs: any, child: any) => {
190+
return `<iframe${attrs} data-type="social-embeds" ></iframe>`
188191
}
189192
}
190193
const TEXT_WRAPPERS: IJsonToHtmlTextTags = {
@@ -222,13 +225,13 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
222225
}
223226
if(jsonValue['classname'] || jsonValue['id']){
224227
if(jsonValue['classname'] && jsonValue['id']){
225-
text = `<span class=${jsonValue['classname']} id=${jsonValue['id']}>${text}</span>`
228+
text = `<span class="${jsonValue['classname']}" id="${jsonValue['id']}">${text}</span>`
226229
}
227230
else if(jsonValue['classname'] && !jsonValue['id']){
228-
text = `<span class=${jsonValue['classname']}>${text}</span>`
231+
text = `<span class="${jsonValue['classname']}">${text}</span>`
229232
}
230233
else if(jsonValue['id'] && !jsonValue['classname']){
231-
text = `<span id=${jsonValue['id']}>${text}</span>`
234+
text = `<span id="${jsonValue['id']}">${text}</span>`
232235
}
233236
}
234237
if (jsonValue.text.includes('\n') && !jsonValue['break']) {

test/expectedJson.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ export default {
17171717
]
17181718
},
17191719
"inline-classname-and-id": {
1720-
"html": '<p><span>This is test for adding inline <span class=class>class</span> and <span id=id>id</span></span></p>',
1720+
"html": '<p><span>This is test for adding inline <span class="class">class</span> and <span id="id">id</span></span></p>',
17211721
"json": [
17221722
{
17231723
"type": "p",

test/expectedMarkdown.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ This is a paragraph with \`inline code\`.`
258258
"display-type": "display",
259259
"asset-uid": "blt3108327519cf8663",
260260
"content-type-uid": "sys_assets",
261-
"asset-link": "https://images.contentstack.io/v3/assets/***REMOVED***/blt3108327519cf8663/65a8be2d14eace6980a5abd6/test-base-64.jpeg",
261+
"asset-link": "./image.jpeg",
262262
"asset-name": "test-base-64.jpeg",
263263
"asset-type": "image/jpeg",
264264
"type": "asset",
@@ -290,7 +290,7 @@ This is a paragraph with \`inline code\`.`
290290
291291
292292
![test-base-64.jpeg]
293-
(https://images.contentstack.io/v3/assets/***REMOVED***/blt3108327519cf8663/65a8be2d14eace6980a5abd6/test-base-64.jpeg)`
293+
(./image.jpeg)`
294294
},
295295
{
296296
"title": "Anchor Link Conversion",

test/fromRedactor.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ describe("Testing html to json conversion", () => {
310310
const json = htmlToJson(html)
311311
expect(json).toEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"p","attrs":{},"uid":"uid","children":[{"text":""}]},{"type":"p","attrs":{},"uid":"uid","children":[{"type":"reference","attrs":{"style":{"text-align":"right"},"redactor-attributes":{"src":"http://localhost:8001/v3/assets/blt77b66f7ca0622ce9/bltc1b32227100685b6/66c81798d5c529eebeabd447/image_(7).png","height":"86","alt":"image (7).png","type":"asset","asset-alt":"image (7).png","max-height":"86","max-width":"168","sys-style-type":"display","position":"right"},"class-name":"embedded-asset","width":168,"type":"asset","asset-alt":"image (7).png","position":"right","asset-link":"http://localhost:8001/v3/assets/blt77b66f7ca0622ce9/bltc1b32227100685b6/66c81798d5c529eebeabd447/image_(7).png","asset-uid":"bltc1b32227100685b6","display-type":"display","asset-name":"image (7).png","asset-type":"image/png","content-type-uid":"sys_assets","inline":true},"uid":"uid","children":[{"text":""}]},{"text":"dasdasdasdasdasdasddaasdasdasdas"},{"text":"\n","break":false,"separaterId":"uid"},{"text":"Hello"},{"text":"\n","break":false,"separaterId":"uid"},{"text":"World"}]}]})
312312
})
313+
test("should convert social embed to proper social embed json", () => {
314+
let html = `<iframe src="https://www.youtube.com/embed/3V-Sq7_uHXQ" data-type="social-embeds"></iframe>`
315+
const json = htmlToJson(html)
316+
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: ""}] }]})
317+
})
313318
})
314319

315320

test/toRedactor.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,11 @@ describe("Testing json to html conversion", () => {
242242
const html = toRedactor(json);
243243
expect(html).toBe(`<figure><a href="link" target="_blank"><img position="none" caption="caption" inline="true" width="217" dirty="true" max-width="243" src="***REMOVED***536/354" alt="alt" anchorLink="link" target="_blank" style="width: 217; height: auto;"/></a><figcaption>caption</figcaption></figure>`)
244244
})
245+
246+
test("should have proper HTML for social-embeds", () => {
247+
const json = {"type":"doc","attrs":{},"uid":"18396bf67f1f4b0a9da57643ac0542ca","children":[{"uid":"45a850acbeb949db86afe415625ad1ce","type":"social-embeds","attrs":{"src":"https://www.youtube.com/embed/3V-Sq7_uHXQ","width":560,"height":320},"children":[{"text":""}]}],"_version":1 }
248+
const html = toRedactor(json);
249+
expect(html).toBe(`<iframe src="https://www.youtube.com/embed/3V-Sq7_uHXQ" width="560" height="320" data-type="social-embeds" ></iframe>`);
250+
})
245251
})
246252

0 commit comments

Comments
 (0)