Skip to content

Commit db04bfe

Browse files
authored
Merge pull request #57 from contentstack/development
Development
2 parents 5cdf53f + 45b6270 commit db04bfe

File tree

8 files changed

+150
-73
lines changed

8 files changed

+150
-73
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/json-rte-serializer",
3-
"version": "2.0.9",
3+
"version": "2.0.10",
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

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const isInline = ['span', 'a', 'inlineCode', 'reference']
1515
const isVoid = ['img', 'embed']
1616

1717

18-
const ELEMENT_TAGS: IHtmlToJsonElementTags = {
18+
export const ELEMENT_TAGS: IHtmlToJsonElementTags = {
1919
A: (el: HTMLElement) => {
2020
const attrs: Record<string, string> = {}
2121
const target = el.getAttribute('target');
@@ -49,7 +49,11 @@ const ELEMENT_TAGS: IHtmlToJsonElementTags = {
4949
const assetName = splittedUrl[splittedUrl?.length - 1]
5050
return { type: 'reference', attrs: { "asset-name": assetName,"content-type-uid" : "sys_assets", "asset-link": el.getAttribute('src'), "asset-type": `image/${imageType}`, "display-type": "display", "type": "asset", "asset-uid": assetUid } }
5151
}
52-
return { type: 'img', attrs: { url: el.getAttribute('src') } }
52+
const imageAttrs : any = { type: 'img', attrs: { url: el.getAttribute('src') } }
53+
if (el.getAttribute('width')) {
54+
imageAttrs.attrs['width'] = el.getAttribute('width')
55+
}
56+
return imageAttrs
5357
},
5458
LI: () => ({ type: 'li', attrs: {} }),
5559
OL: () => ({ type: 'ol', attrs: {} }),
@@ -98,7 +102,8 @@ const ELEMENT_TAGS: IHtmlToJsonElementTags = {
98102
SCRIPT: (el: HTMLElement) => {
99103
return { type: 'script', attrs: {} }
100104
},
101-
HR: () => ({ type: 'hr', attrs: {} })
105+
HR: () => ({ type: 'hr', attrs: {} }),
106+
FIGCAPTION: () => ({ type: 'figcaption', attrs: {} }),
102107
}
103108

104109
const TEXT_TAGS: IHtmlToJsonTextTags = {
@@ -156,7 +161,7 @@ const traverseChildAndWarpChild = (children: Array<Object>, allowNonStandardTags
156161
if (child.hasOwnProperty('type')) {
157162
if (isInline.includes(child.type)) {
158163
if (child.type === "reference") {
159-
if (child.attrs && (child.attrs['display-type'] === "inline" || child.attrs['display-type'] === "link")) {
164+
if (child.attrs && (child.attrs['display-type'] === "inline" || child.attrs['display-type'] === "link" || child.attrs['inline'] )) {
160165
inlineElementIndex.push(index)
161166
} else {
162167
hasBlockElement = true
@@ -437,7 +442,15 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
437442
}
438443
}
439444
elementAttrs.attrs["redactor-attributes"] = redactor
440-
return jsx('element', { attrs: { ...elementAttrs?.attrs, type, "asset-caption": caption, "link": link, "asset-alt": alt, target, position, "asset-link": fileLink, "asset-uid": uid, "display-type": displayType, "asset-name": fileName, "asset-type": contentType, "content-type-uid": contentTypeUid }, type: "reference", uid: generateId() }, children)
445+
const assetAttrs = { ...elementAttrs?.attrs, type, "asset-caption": caption, "link": link, "asset-alt": alt, target, position, "asset-link": fileLink, "asset-uid": uid, "display-type": displayType, "asset-name": fileName, "asset-type": contentType, "content-type-uid": contentTypeUid }
446+
if(assetAttrs.target === "_self"){
447+
delete assetAttrs.target
448+
}
449+
if(redactor.inline){
450+
assetAttrs.inline = true
451+
delete redactor.inline
452+
}
453+
return jsx('element', { attrs: assetAttrs, type: "reference", uid: generateId() }, children)
441454
}
442455
}
443456
if (nodeName === 'FIGCAPTION') {
@@ -626,11 +639,14 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
626639
const { href, target } = newChildren[0].attrs?.["redactor-attributes"]
627640
extraAttrs['anchorLink'] = href;
628641
if (target && target !== '') {
629-
extraAttrs['target'] = true;
642+
extraAttrs['target'] = target === "_blank";
630643
}
631644
const imageAttrs = newChildren[0].children;
632645

633646
if(imageAttrs[0].type === 'img'){
647+
if(imageAttrs[0].attrs.width){
648+
sizeAttrs.width = imageAttrs[0].attrs.width
649+
}
634650
elementAttrs = getFinalImageAttributes({elementAttrs, newChildren : imageAttrs, extraAttrs, sizeAttrs})
635651

636652
}
@@ -655,6 +671,16 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
655671
elementAttrs = getImageAttributes(imageAttrs, imageAttrs.attrs || {}, extraAttrs)
656672
return jsx('element', elementAttrs, [{ text: '' }])
657673
}
674+
if (newChildren[0]?.type === 'img'){
675+
let extraAttrs: { [key: string]: any } = {}
676+
const imageAttrs = newChildren[0]
677+
elementAttrs = getImageAttributes(imageAttrs, imageAttrs.attrs || {}, extraAttrs)
678+
elementAttrs.attrs['anchorLink'] = el.getAttribute('href')
679+
if(el.getAttribute('target'))
680+
elementAttrs.attrs['target'] = el.getAttribute('target')
681+
return jsx('element', elementAttrs, [{ text: '' }])
682+
683+
}
658684
}
659685
if (nodeName === 'IMG' || nodeName === 'IFRAME' || nodeName === 'VIDEO') {
660686
if (elementAttrs?.attrs?.["redactor-attributes"]?.width) {
@@ -670,6 +696,10 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
670696
if (elementAttrs?.attrs?.["redactor-attributes"]?.inline) {
671697
elementAttrs.attrs.inline = Boolean(elementAttrs?.attrs?.["redactor-attributes"]?.inline)
672698
}
699+
if(nodeName === "IMG" && elementAttrs.attrs.width){
700+
elementAttrs.attrs.style.width = `${elementAttrs.attrs.width}px`
701+
elementAttrs.attrs.style['max-width'] = `${elementAttrs.attrs.width}px`
702+
}
673703
return jsx('element', elementAttrs, [{ text: '' }])
674704
}
675705
if (nodeName === 'BLOCKQUOTE') {
@@ -843,6 +873,12 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
843873
}
844874
}
845875

876+
if(nodeName === "DIV"){
877+
if(el.style?.overflow === 'hidden' && children.find((child: any) => child.type === 'reference')){
878+
elementAttrs = { ...elementAttrs, type: 'p', attrs: {} }
879+
}
880+
}
881+
846882
if (children.length === 0) {
847883
children = [{ text: '' }]
848884
}
@@ -928,14 +964,24 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
928964
sizeAttrs.width = newChildren[0].attrs.width.toString();
929965
}
930966

967+
if(!isNaN(parseInt(sizeAttrs.width))){
968+
sizeAttrs.style = {
969+
width: `${sizeAttrs.width}px`,
970+
"max-width": `${sizeAttrs.width}px`
971+
}
972+
}
973+
931974
const childAttrs = { ...newChildren[0].attrs, ...sizeAttrs, style: { 'text-align': style?.['text-align'] }, caption: extraAttrs['caption'] }
932975
extraAttrs = { ...extraAttrs, ...sizeAttrs }
933976

934977
if (!childAttrs.caption) {
935978
delete childAttrs.caption
936979
}
937980

938-
const imageAttrs = getImageAttributes(elementAttrs, childAttrs, extraAttrs);
981+
const imageAttrs = getImageAttributes(elementAttrs, childAttrs, extraAttrs);
982+
983+
delete imageAttrs?.attrs?.['redactor-attributes']?.['anchorlink'];
984+
delete imageAttrs?.attrs?.['redactor-attributes']?.['style'];
939985

940986
return imageAttrs
941987
}

src/toRedactor.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
482482
}
483483
if(jsonValue['type'] === 'img'){
484484
attrsJson['src'] = allattrs['url']
485+
486+
if(allattrs['caption']) figureStyles.caption = allattrs['caption']
487+
if(allattrs['position']) figureStyles.position = allattrs['position']
488+
if(allattrs['anchorLink']) figureStyles.anchorLink = `href="${allattrs['anchorLink']}"`
489+
if(allattrs['target']){
490+
figureStyles.anchorLink += ` target="${allattrs['target']}"`
491+
}
492+
figureStyles.fieldsEdited.push(figureStyles.caption)
485493
}
486494
if(!(options?.customElementTypes && !isEmpty(options.customElementTypes) && options.customElementTypes[jsonValue['type']])) {
487495
delete attrsJson['url']

0 commit comments

Comments
 (0)