AlertThis post is over a year old, some of this information may be out of date.
Generate open graph preview image in Code with Front Matter
Since I moved my blog to Hugo, I created a Front Matter - VSCode extension which makes content/article management easier within Visual Studio Code.
A couple of versions ago, I added the ability to integrate your custom scripts. These will show up as actions in the Front Matter side panel. This functionality allows total flexibility to your content management workflow. As everyone has different needs, it will not limit you.
One of the scripts I use for new articles is generating a preview image for open graph and Twitter.
The script
The script uses node-html-to-image dependency, which, as the name suggests, converts HTML to an image. Under the hood, it uses puppeteer to generate the image.
To make the script available as an action in the Front Matter side panel, you will first have to add a file with the script’s code to your project.
Info
I use a scripts folder in my project, where I created the social-img.mjs file.
Once you added the file, make sure to install the dependencies: npm i node-html-to-image uuid date-fns @frontmatter/extensibility -D.
After that, it is time for the final step, registering the command. You can register the script in your .vscode/settings.json file with the following code:
1
2
3
4
5
"frontMatter.custom.scripts":[{"title":"Generate social image","script":"./scripts/social-img.mjs","nodeBin":"~/.nvm/versions/node/v18.17.1/bin/node"}]
Info
The node location needs to be provided as I want to make sure it uses the correct node.js version to run the script. When using nvm this is a requirement to set the nodeBin property.
Open the Front Matter side panel, and the new button should show up. When you click on the new button, it will start generating the image. When the script completes, it will show a notification with the path of the image.
Running this in WSL
I encountered an issue when trying to run the script in WSL. It had to do with some missing dependencies.