Skip to content

Commit 9bf1257

Browse files
committed
fix: embed link issue in images and test cases
1 parent 41bac3e commit 9bf1257

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/fromRedactor.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export 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'), width: el.getAttribute('width') } }
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: {} }),
@@ -631,12 +635,14 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
631635
const { href, target } = newChildren[0].attrs?.["redactor-attributes"]
632636
extraAttrs['anchorLink'] = href;
633637
if (target && target !== '') {
634-
extraAttrs['target'] = target;
638+
extraAttrs['target'] = target === "_blank";
635639
}
636640
const imageAttrs = newChildren[0].children;
637641

638642
if(imageAttrs[0].type === 'img'){
639-
sizeAttrs.width = imageAttrs[0].attrs.width
643+
if(imageAttrs[0].attrs.width){
644+
sizeAttrs.width = imageAttrs[0].attrs.width
645+
}
640646
elementAttrs = getFinalImageAttributes({elementAttrs, newChildren : imageAttrs, extraAttrs, sizeAttrs})
641647

642648
}
@@ -661,6 +667,16 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
661667
elementAttrs = getImageAttributes(imageAttrs, imageAttrs.attrs || {}, extraAttrs)
662668
return jsx('element', elementAttrs, [{ text: '' }])
663669
}
670+
if (newChildren[0]?.type === 'img'){
671+
let extraAttrs: { [key: string]: any } = {}
672+
const imageAttrs = newChildren[0]
673+
elementAttrs = getImageAttributes(imageAttrs, imageAttrs.attrs || {}, extraAttrs)
674+
elementAttrs.attrs['anchorLink'] = el.getAttribute('href')
675+
if(el.getAttribute('target'))
676+
elementAttrs.attrs['target'] = el.getAttribute('target')
677+
return jsx('element', elementAttrs, [{ text: '' }])
678+
679+
}
664680
}
665681
if (nodeName === 'IMG' || nodeName === 'IFRAME' || nodeName === 'VIDEO') {
666682
if (elementAttrs?.attrs?.["redactor-attributes"]?.width) {
@@ -676,7 +692,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
676692
if (elementAttrs?.attrs?.["redactor-attributes"]?.inline) {
677693
elementAttrs.attrs.inline = Boolean(elementAttrs?.attrs?.["redactor-attributes"]?.inline)
678694
}
679-
if(elementAttrs.attrs.width){
695+
if(nodeName === "IMG" && elementAttrs.attrs.width){
680696
elementAttrs.attrs.style.width = `${elementAttrs.attrs.width}px`
681697
elementAttrs.attrs.style['max-width'] = `${elementAttrs.attrs.width}px`
682698
}
@@ -952,11 +968,8 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
952968
delete childAttrs.caption
953969
}
954970

955-
const imageAttrs = getImageAttributes(elementAttrs, {... childAttrs, ...extraAttrs}, extraAttrs);
956-
if(imageAttrs.attrs.link){
957-
imageAttrs.attrs.anchorLink = imageAttrs.attrs.link;
958-
delete imageAttrs.attrs.link;
959-
}
971+
const imageAttrs = getImageAttributes(elementAttrs, childAttrs, extraAttrs);
972+
960973
delete imageAttrs?.attrs?.['redactor-attributes']?.['anchorlink'];
961974
delete imageAttrs?.attrs?.['redactor-attributes']?.['style'];
962975

test/expectedJson.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ export default {
17001700
<figure style="margin: auto; text-align: center;width: 204px;"><a href="https://app.contentstack.com/"><img src="https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg" /></a>
17011701
<figcaption style="text-align: center;" style="text-align: center;">Landscape</figcaption>
17021702
</figure>`,
1703-
"json": {"type":"doc","uid":"d6cd7b938dcc41a8a75fb8bad29aa2e9","attrs":{},"children":[{"type":"p","attrs":{},"uid":"c17f2b982464422aaa58499b9525b437","children":[{"text":""}]},{"type":"img","attrs":{"style":{"text-align":"center"},"redactor-attributes":{"src":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","position":"center","captionAttrs":{"style":"text-align: center;"},"caption":"Landscape","anchorLink":"https://app.contentstack.com/","width":204},"url":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","width":204,"caption":"Landscape","link":"https://app.contentstack.com/"},"children":[{"text":""}]}]}
1703+
"json": {"type":"doc","attrs":{},"children":[{"type":"p","attrs":{},"children":[{"text":""}]},{"type":"img","attrs":{"style":{"text-align":"center"},"redactor-attributes":{"src":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","position":"center","captionAttrs":{"style":"text-align: center;"},"caption":"Landscape","width":204},"url":"https://images.contentstack.io/v3/assets/***REMOVED***/bltfea8157ddfb8e776/6329f1106a7f7364973c028c/landscape-3.jpg","anchorLink":"https://app.contentstack.com/","width":204,"caption":"Landscape"},"children":[{"text":""}]}]}
17041704
},
17051705
"'\n' to <br>": {
17061706
"html": '<p>This is test for break element<br/>This is text on the next line.</p>',

test/toRedactor.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,19 @@ describe("Testing json to html conversion", () => {
228228
test("RT-253 - should convert to proper HTML image code", () => {
229229
const json = {"type":"doc","attrs":{},"children":[{"type":"img","attrs":{"url":"***REMOVED***200","width":100},"children":[{"text":""}]}],"_version":3 }
230230
const html = toRedactor(json);
231-
expect(html).toBe('<img width="100" src="***REMOVED***200"/>');
231+
expect(html).toBe('<img width="100" src="***REMOVED***200" />');
232232
});
233233

234234
test("RT-264 - reference asset should have proper unit in the converted image", () => {
235235
const json = {"type":"doc","attrs":{},"uid":"6a547ebccbd74c0c9a521ee95acfb223","children":[{"uid":"942be31c040145b6a7541ec4f73754c5","type":"reference","attrs":{"display-type":"display","asset-uid":"bltcbce74d3891aaa9d","content-type-uid":"sys_assets","asset-link":"***REMOVED***200","asset-name":"MATHERAN.jpg","asset-type":"image/jpeg","type":"asset","class-name":"embedded-asset","width":"192","style":{"max-height":"144px","height":"144px","text-align":"right","max-width":"192px","width":"auto"},"redactor-attributes":{"height":"144","position":"right"},"max-height":"144","height":"144","position":"right","max-width":"192"},"children":[{"text":""}]}],"_version":1 }
236236
const html = toRedactor(json);
237237
expect(html).toBe(`<figure style="margin: 0; text-align: right"><img src="***REMOVED***200" height="144" class="embedded-asset" content-type-uid="sys_assets" type="asset" width="192" max-height="144" max-width="192" style="max-height: 144px; height: 144px; text-align: right; max-width: 192px; width: auto" data-sys-asset-filelink="***REMOVED***200" data-sys-asset-uid="bltcbce74d3891aaa9d" data-sys-asset-filename="MATHERAN.jpg" data-sys-asset-contenttype="image/jpeg" data-sys-asset-position="right" sys-style-type="display"/></figure>`);
238238
})
239+
240+
test("RT-292", () => {
241+
const json = {"type":"doc","uid":"41870a48806348eb866c1944d37d3a5d","attrs":{},"children":[{"type":"img","attrs":{"url":"***REMOVED***536/354","style":{},"redactor-attributes":{"position":"none","caption":"caption","inline":"true","width":"243","dirty":"true","max-width":"243","src":"***REMOVED***536/354","alt":"alt","anchorLink":"link","target":true},"width":"217","inline":"true","caption":"caption","alt":"alt","anchorLink":"link","target":"_blank"},"uid":"bedc4931f5aa4fd59fd6411665f931e2","children":[{"text":""}]}] }
242+
const html = toRedactor(json);
243+
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>`)
244+
})
239245
})
240246

0 commit comments

Comments
 (0)