Skip to content

Commit ffa788b

Browse files
committed
fix: proper conversion of HTML inline asset to JSON
1 parent caed8a5 commit ffa788b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/fromRedactor.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const traverseChildAndWarpChild = (children: Array<Object>, allowNonStandardTags
161161
if (child.hasOwnProperty('type')) {
162162
if (isInline.includes(child.type)) {
163163
if (child.type === "reference") {
164-
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'] )) {
165165
inlineElementIndex.push(index)
166166
} else {
167167
hasBlockElement = true
@@ -446,6 +446,10 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
446446
if(assetAttrs.target === "_self"){
447447
delete assetAttrs.target
448448
}
449+
if(redactor.inline){
450+
assetAttrs.inline = true
451+
delete redactor.inline
452+
}
449453
return jsx('element', { attrs: assetAttrs, type: "reference", uid: generateId() }, children)
450454
}
451455
}
@@ -869,6 +873,12 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
869873
}
870874
}
871875

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+
872882
if (children.length === 0) {
873883
children = [{ text: '' }]
874884
}

test/fromRedactor.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ describe("Testing html to json conversion", () => {
305305
const json = htmlToJson(html, { allowNonStandardTags: true })
306306
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"reference","attrs":{"style":{"text-align":"right"},"redactor-attributes":{"src":"***REMOVED***200","height":"141","alt":"image_(9).png","caption":"ss","type":"asset","asset-alt":"image_(9).png","max-height":"141","max-width":"148","sys-style-type":"display","position":"right","captionAttrs":{"style":"text-align:center"},"anchorLink":"ss.com","target":true,"asset-caption":"ss"},"class-name":"embedded-asset","width":148,"type":"asset","asset-caption":"ss","link":"ss.com","asset-alt":"image_(9).png","target":"_blank","position":"right","asset-link":"***REMOVED***200","asset-uid":"blt137d845621ef8168","display-type":"display","asset-name":"image_(9).png","asset-type":"image/png","content-type-uid":"sys_assets"},"uid":"uid","children":[{"text":""}]},{"type":"p","attrs":{},"uid":"uid","children":[{"text":""}]}] })
307307
})
308+
test("should convert inline asset reference HTML to proper JSON", () => {
309+
let html = `<p></p><div style="overflow: hidden"><span><figure style="margin: 0; float: right"><img src="http://localhost:8001/v3/assets/blt77b66f7ca0622ce9/bltc1b32227100685b6/66c81798d5c529eebeabd447/image_(7).png" height="86" alt="image (7).png" inline="true" class="embedded-asset" content-type-uid="sys_assets" type="asset" asset-alt="image (7).png" width="168" max-height="86" max-width="168" style="max-height: 86px; height: 86px; text-align: right; max-width: 168px; width: auto" data-sys-asset-filelink="http://localhost:8001/v3/assets/blt77b66f7ca0622ce9/bltc1b32227100685b6/66c81798d5c529eebeabd447/image_(7).png" data-sys-asset-uid="bltc1b32227100685b6" data-sys-asset-filename="image (7).png" data-sys-asset-contenttype="image/png" data-sys-asset-alt="image (7).png" data-sys-asset-position="right" sys-style-type="display"/></figure>dasdasdasdasdasdasddaasdasdasdas<br/>Hello<br/>World</span></div>`
310+
const json = htmlToJson(html)
311+
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"}]}]})
312+
})
308313
})
309314

310315

@@ -394,4 +399,5 @@ function htmlToJson (html: string, options: IHtmlToJsonOptions) {
394399
let htmlDoc = dom.window.document.querySelector("body");
395400
return fromRedactor(htmlDoc, options);
396401

397-
}
402+
}
403+

0 commit comments

Comments
 (0)