feat: programmatically generate open graph cards

This commit is contained in:
Cory Dransfeldt 2023-12-13 14:41:46 -08:00
parent e34ad2cb0e
commit 0b47dc7747
No known key found for this signature in database
15 changed files with 144 additions and 35 deletions

29
config/events/index.js Normal file
View file

@ -0,0 +1,29 @@
const fs = require('fs')
const Image = require('@11ty/eleventy-img')
const svgToJpeg = function () {
const socialPreviewImagesDir = '_site/assets/img/social-preview/'
fs.readdir(socialPreviewImagesDir, (err, files) => {
if (!!files && files.length > 0) {
files.forEach((fileName) => {
if (fileName.endsWith('.svg')) {
let imageUrl = socialPreviewImagesDir + fileName
Image(imageUrl, {
formats: ['jpeg'],
outputDir: './' + socialPreviewImagesDir,
filenameFormat: function (id, src, width, format) {
let outputFileName = fileName.substring(0, fileName.length - 4)
return `${outputFileName}.${format}`
},
})
}
})
} else {
console.log('⚠ No social images found')
}
})
}
module.exports = {
svgToJpeg,
}

View file

@ -20,6 +20,17 @@ module.exports = {
const replacement = '&'
return string.replace(pattern, replacement)
},
splitlines: (input, maxCharLength) => {
const parts = input.split(' ')
const lines = parts.reduce(function (acc, cur) {
if (!acc.length) return [cur]
let lastOne = acc[acc.length - 1]
if (lastOne.length + cur.length > maxCharLength) return [...acc, cur]
acc[acc.length - 1] = lastOne + ' ' + cur
return acc
}, [])
return lines
},
stripUtm: (string) => {
if (!string) return
return string.replace(utmPattern, '')
@ -99,9 +110,15 @@ module.exports = {
readableDate: (date) => {
return DateTime.fromISO(date).toFormat('LLLL d, yyyy')
},
dateToReadableDate: (date) => {
return new Date(date)
.toLocaleString('en-US', {
timeZone: 'America/Los_Angeles',
})
.split(',')[0]
},
toDateTime: (date) => {
const formatted = DateTime.fromISO(date)
const trail = (number) => {
return parseInt(number, 10) < 10 ? `0${number}` : number
}

13
config/utils/index.js Normal file
View file

@ -0,0 +1,13 @@
const slugify = require('slugify')
const slugifyString = (str) => {
return slugify(str, {
replacement: '-',
remove: /[#,&,+()$~%.'":*?<>{}]/g,
lower: true,
})
}
module.exports = {
slugifyString,
}