diff --git a/public/og-images/roadmaps/datastructures-and-algorithms.png b/public/og-images/roadmaps/datastructures-and-algorithms.png index 22f3e4646..8a349c525 100644 Binary files a/public/og-images/roadmaps/datastructures-and-algorithms.png and b/public/og-images/roadmaps/datastructures-and-algorithms.png differ diff --git a/scripts/generate-og-images.mjs b/scripts/generate-og-images.mjs index be7ae451c..1c1357f9a 100644 --- a/scripts/generate-og-images.mjs +++ b/scripts/generate-og-images.mjs @@ -184,7 +184,16 @@ async function generateResourceOpenGraph() { for (const resource of resources) { if (!resource.image) { - const template = getRoadmapDefaultTemplate(resource); + let template = getRoadmapDefaultTemplate(resource); + if ( + hasSpecialCharacters(resource.title) || + hasSpecialCharacters(resource.description) + ) { + // For some reason special characters are not being rendered properly + // https://github.com/natemoo-re/satori-html/issues/20 + // So we need to unescape the html + template = JSON.parse(unescapeHtml(JSON.stringify(template))); + } await generateOpenGraph( template, resource.type, @@ -193,19 +202,29 @@ async function generateResourceOpenGraph() { ); } else { const image = await fs.readFile(resource.image); - const dimensions = imageSize(image); const widthRatio = 1200 / dimensions.width; let width = dimensions.width * widthRatio * 0.85; let height = dimensions.height * widthRatio * 0.85; - const template = getRoadmapImageTemplate({ + let template = getRoadmapImageTemplate({ ...resource, image: `data:image/${dimensions.type};base64,${image.toString('base64')}`, width, height, }); + + if ( + hasSpecialCharacters(resource.title) || + hasSpecialCharacters(resource.description) + ) { + // For some reason special characters are not being rendered properly + // https://github.com/natemoo-re/satori-html/issues/20 + // So we need to unescape the html + template = JSON.parse(unescapeHtml(JSON.stringify(template))); + } + await generateOpenGraph(template, resource.type, resource.id + '.png'); } } @@ -236,6 +255,15 @@ async function generateGuideOpenGraph() { ? image : `data:image/${authorImageExtention};base64,${authorAvatar.toString('base64')}`, }); + if ( + hasSpecialCharacters(guide.title) || + hasSpecialCharacters(guide.description) + ) { + // For some reason special characters are not being rendered properly + // https://github.com/natemoo-re/satori-html/issues/20 + // So we need to unescape the html + template = JSON.parse(unescapeHtml(JSON.stringify(template))); + } await generateOpenGraph(template, 'guides', guide.id + '.png'); } } @@ -451,7 +479,9 @@ function getRoadmapImageTemplate({ title, description, image, height, width }) {
-
${title}
+
+ ${title?.replace('&', `{"&"}`)} +
@@ -509,3 +539,16 @@ function getGuideTemplate({ title, description, authorName, authorAvatar }) {
`; } + +function unescapeHtml(html) { + return html + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, "'"); +} + +function hasSpecialCharacters(str) { + return /[&<>"]/.test(str); +}