Skip to content

Commit 1936865

Browse files
authored
Merge pull request #61 from contentstack/RT-350
feat: add support for social embeds
2 parents 1d8d322 + 9b8ccaf commit 1936865

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

src/fromRedactor.tsx

Lines changed: 9 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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 = {

test/fromRedactor.test.ts

Lines changed: 5 additions & 0 deletions
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/VD6xJq8NguY" 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/VD6xJq8NguY" }, children: [{ text: ""}] }]})
317+
})
313318
})
314319

315320

test/toRedactor.test.ts

Lines changed: 6 additions & 0 deletions
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="https://picsum.photos/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/VD6xJq8NguY","width":560,"height":320},"children":[{"text":""}]}],"_version":1 }
248+
const html = toRedactor(json);
249+
expect(html).toBe(`<iframe src="https://www.youtube.com/embed/VD6xJq8NguY" width="560" height="320"></iframe>`);
250+
})
245251
})
246252

0 commit comments

Comments
 (0)