Skip to content

Commit d8ec7a0

Browse files
committed
chore: rebase
2 parents b62233f + 3a19496 commit d8ec7a0

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/fromRedactor.tsx

Lines changed: 3 additions & 5 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');
@@ -98,7 +98,8 @@ const ELEMENT_TAGS: IHtmlToJsonElementTags = {
9898
SCRIPT: (el: HTMLElement) => {
9999
return { type: 'script', attrs: {} }
100100
},
101-
HR: () => ({ type: 'hr', attrs: {} })
101+
HR: () => ({ type: 'hr', attrs: {} }),
102+
FIGCAPTION: () => ({ type: 'figcaption', attrs: {} }),
102103
}
103104

104105
const TEXT_TAGS: IHtmlToJsonTextTags = {
@@ -437,13 +438,10 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject
437438
}
438439
}
439440
elementAttrs.attrs["redactor-attributes"] = redactor
440-
441441
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 }
442-
443442
if(assetAttrs.target === "_self"){
444443
delete assetAttrs.target
445444
}
446-
447445
return jsx('element', { attrs: assetAttrs, type: "reference", uid: generateId() }, children)
448446
}
449447
}

test/fromRedactor.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-nocheck
2-
import { fromRedactor, getNestedValueIfAvailable } from "../src/fromRedactor"
2+
import { ELEMENT_TAGS, fromRedactor, getNestedValueIfAvailable } from "../src/fromRedactor"
33
import { JSDOM } from "jsdom"
44
import isEqual from "lodash.isequal"
55
import omitdeep from "omit-deep-lodash"
@@ -278,6 +278,16 @@ describe("Testing html to json conversion", () => {
278278
const json = htmlToJson(html)
279279
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"reference","attrs":{"style":{"text-align":"right"},"redactor-attributes":{"src":"https://picsum.photos/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":"https://picsum.photos/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":""}]}] })
280280
})
281+
test("should convert asset to reference with non standard tags", () => {
282+
const html = `<figure style="margin: 0; text-align: right">
283+
<div style="display: inline-block"><a href="ss.com" target="_blank"><img src="https://picsum.photos/200" height="141" alt="image_(9).png" caption="ss" anchorLink="ss.com" class="embedded-asset" content-type-uid="sys_assets" type="asset" asset-alt="image_(9).png" width="148" max-height="141" max-width="148" style="max-height: 141px; height: 141px; text-align: right; max-width: 148px; width: auto" data-sys-asset-filelink="https://picsum.photos/200" data-sys-asset-uid="blt137d845621ef8168" data-sys-asset-filename="image_(9).png" data-sys-asset-contenttype="image/png" data-sys-asset-caption="ss" data-sys-asset-alt="image_(9).png" data-sys-asset-link="ss.com" data-sys-asset-position="right" data-sys-asset-isnewtab="true" sys-style-type="display" /></a>
284+
<figcaption style="text-align:center">ss</figcaption>
285+
</div>
286+
</figure>
287+
<p></p>`
288+
const json = htmlToJson(html, { allowNonStandardTags: true })
289+
expect(json).toStrictEqual({"type":"doc","uid":"uid","attrs":{},"children":[{"type":"reference","attrs":{"style":{"text-align":"right"},"redactor-attributes":{"src":"https://picsum.photos/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":"https://picsum.photos/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":""}]}] })
290+
})
281291
})
282292

283293

@@ -355,9 +365,15 @@ describe("CS-41001", () =>{
355365
})
356366
})
357367

368+
describe("ELEMENT_TAGS", () => {
369+
test("should have FIGCAPTION as a standard element tag", () => {
370+
const standardElementTags = Object.keys(ELEMENT_TAGS);
371+
expect(standardElementTags).toContain("FIGCAPTION");
372+
})
373+
})
374+
358375
function htmlToJson (html: string, options: IHtmlToJsonOptions) {
359376
const dom = new JSDOM(html);
360377
let htmlDoc = dom.window.document.querySelector("body");
361378
return fromRedactor(htmlDoc, options);
362-
}
363-
379+
}

0 commit comments

Comments
 (0)