Skip to content

Instantly share code, notes, and snippets.

@murtuzaalisurti
Created February 10, 2024 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murtuzaalisurti/1101267d85681db471b69c417387e418 to your computer and use it in GitHub Desktop.
Save murtuzaalisurti/1101267d85681db471b69c417387e418 to your computer and use it in GitHub Desktop.
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