Skip to content

Commit

Permalink
feat: support for image type asset
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayesh2812 committed Oct 30, 2023
1 parent 06da814 commit 6fe7b84
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/toRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,46 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
} else if (extraAttrs?.displayType === 'asset') {
return `<figure${attrs}>${child}</figure>`
}

else if (extraAttrs?.displayType === "display") {
const anchor = jsonBlock?.["attrs"]?.["link"];

const caption = jsonBlock?.["attrs"]?.["asset-caption"];
const position = jsonBlock?.["attrs"]?.["position"];
const inline = jsonBlock?.["attrs"]?.["inline"]

attrs = ` src="${jsonBlock?.["attrs"]?.["asset-link"]}"` + attrs;
let img = `<img${attrs}/>`;

if (anchor) {
const target = jsonBlock?.["attrs"]?.["target"];
let anchorAttrs = `href="${anchor}"`;
if (target) {
anchorAttrs = `${anchorAttrs} target="${target}"`;
}
img = `<a ${anchorAttrs}>${img}</a>`;
}

if (caption || (position && position !== "none")) {
const figcaption = `<figcaption style="text-align:center">${caption}</figcaption>`;
let figureAttrs = ``;

if (position && position !== "none") {
const style = Object.entries(jsonBlock["attrs"]["style"])
.map((entry) => entry.join(":"))
.join(";");

if (style) figureAttrs = ` style="${style}"`;
}
img = `<figure${figureAttrs ? figureAttrs : ""}>${img}${
caption ? figcaption : ""
}</figure>`;
}
if(inline){
img = `<span>${img}</span>`
}
return `${img}`;
}
return `<span${attrs}>${child}</span>`
},
inlineCode: (attrs: any, child: any) => {
Expand Down

0 comments on commit 6fe7b84

Please sign in to comment.