Skip to content

Commit 65713b9

Browse files
committed
feat: support for image type asset
1 parent 3e52f34 commit 65713b9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/toRedactor.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,46 @@ const ELEMENT_TYPES: IJsonToHtmlElementTags = {
118118
} else if (extraAttrs?.displayType === 'asset') {
119119
return `<figure${attrs}>${child}</figure>`
120120
}
121+
122+
else if (extraAttrs?.displayType === "display") {
123+
const anchor = jsonBlock?.["attrs"]?.["link"];
124+
125+
const caption = jsonBlock?.["attrs"]?.["asset-caption"];
126+
const position = jsonBlock?.["attrs"]?.["position"];
127+
const inline = jsonBlock?.["attrs"]?.["inline"]
128+
129+
attrs = ` src="${jsonBlock?.["attrs"]?.["asset-link"]}"` + attrs;
130+
let img = `<img${attrs}/>`;
131+
132+
if (anchor) {
133+
const target = jsonBlock?.["attrs"]?.["target"];
134+
let anchorAttrs = `href="${anchor}"`;
135+
if (target) {
136+
anchorAttrs = `${anchorAttrs} target="${target}"`;
137+
}
138+
img = `<a ${anchorAttrs}>${img}</a>`;
139+
}
140+
141+
if (caption || (position && position !== "none")) {
142+
const figcaption = `<figcaption style="text-align:center">${caption}</figcaption>`;
143+
let figureAttrs = ``;
144+
145+
if (position && position !== "none") {
146+
const style = Object.entries(jsonBlock["attrs"]["style"])
147+
.map((entry) => entry.join(":"))
148+
.join(";");
149+
150+
if (style) figureAttrs = ` style="${style}"`;
151+
}
152+
img = `<figure${figureAttrs ? figureAttrs : ""}>${img}${
153+
caption ? figcaption : ""
154+
}</figure>`;
155+
}
156+
if(inline){
157+
img = `<span>${img}</span>`
158+
}
159+
return `${img}`;
160+
}
121161
return `<span${attrs}>${child}</span>`
122162
},
123163
inlineCode: (attrs: any, child: any) => {

0 commit comments

Comments
 (0)