Created
February 10, 2024 18:17
-
-
Save murtuzaalisurti/1101267d85681db471b69c417387e418 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const markdownIt = require("markdown-it")(); | |
module.exports = (...data) => { | |
const images = data.flatMap((arg, index) => { | |
if (index === data.length - 1) { | |
return []; | |
} | |
return [ | |
{ | |
url: arg.split(" ")[0], | |
type: arg.split(" ")[1], | |
}, | |
]; | |
}); | |
const imgAttributes = JSON.parse(data[data.length - 1]); | |
const imgCaptionHTML = imgAttributes.caption | |
? markdownIt.render(imgAttributes.caption) | |
: null; | |
const srcset = images.flatMap((image, index) => { | |
if (index === images.length - 1) { | |
return []; | |
} | |
return [`<source srcset="${image.url}" type="image/${image.type}">`]; | |
}); | |
return String.raw` | |
<figure> | |
<picture>${srcset.join("")} | |
<img src="${images[images.length - 1].url}" alt="${ | |
imgAttributes.alt | |
}" height="${imgAttributes.height}" width="${imgAttributes.width}"> | |
</picture>${ | |
imgAttributes.caption | |
? `<figcaption>${imgCaptionHTML}</figcaption>` | |
: "" | |
} | |
</figure> | |
`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment