I wasn't able to go into as much detail as I would have liked to during the talk, so this blog post compliments the talk.
Here is my flow for writing blog posts. I create and publish them on dev.to (DEV) via the DEV editor. Every night at 8 pm Eastern, a GitHub action runs and updates my blog post markdown and associated assets. If there are changes, the main branch is updated and starts a deployment on Netlify.
Let's break down the whole flow.
Caching
Blog Post Markdown
I call the DEV API, which pulls in all my blog posts. At the time of writing, the function to do that looks like this. Feel free to peek at the complete source code.
/**
* Retrieves the latest blog posts from dev.to.
*
* @returns {Promise<object[]>} A promise that resolves to an array of blog posts.
*/asyncfunctiongetDevPosts(){constresponse=awaitfetch(DEV_TO_API_URL+'/articles/me/published?per_page=1000',{headers:{'api-key':DEV_API_KEY,},});constposts=awaitresponse.json();returnposts.filter(isValidPost);}
I filter out certain posts via the isValidPost(post) function. I filter out discussion posts, water cooler posts etc., as I enjoy having them on DEV, but not my blog.
The API does allow you to exclude tags instead of doing it once you’ve received all posts, but for some reason it doesn’t work and I have haven’t had time to investigate why.
I'm using short codes in Eleventy which are the same syntax as liquid tags. In the past DEV had specific liquid tags for different embeds. For example, to embed a GitHub repository, you'd use the {% github url %} liquid tag. The liquid tag is still supported, but they now have a generic embed liquid tag, {% embed url %} which determines what type of embed based on the URL.
In my project, I have shortcodes for specific embeds, e.g. {% github url %}, {% twitter url %}, etc. I have older posts that use the old liquid tags of DEV, but newer posts use the {% embed url %} liquid tag. On my end I manipulate the markdown to convert e.g. {% embed https://twitter.com/nickytonline/status/1521650477674471424 %} to {% twitter https://twitter.com/nickytonline/status/1521650477674471424 %}
I don't support all embeds at the moment. For example, comment and tag embeds. I had DEV comment embeds at one point, but it proved troublesome for comment embeds with Tweets or any embed. I used so few of them in blog posts that I made it a rule to create a hyperlink to the comment instead. For the tag embed, I barely used it, so I made another rule to not reference a tag on DEV or, if I did, to create a hyperlink instead.
There are some other manipulations I do to the markdown that I'm probably forgetting. The markdown of a blog post from DEV is now in a state that Eleventy can consume.
Boost links
On all my blog posts, you'll notice that they have a Boost on DEV link, and some also have a Boost on Hashnode link. I got this idea from Stephanie Eckles, giving credit where credit is due.
These links are generated in the markdown by the boostLink shortcode
/**
* Generates markup for a boost on DEV button.
*
* @param {string} fileSlug A pages file slug.
* @param {string} url A pages URL.
*
* @returns {string} Markup for a boost links on DEV and Hashnode.
*/functionboostLink(title,fileSlug,url){if (!url.startsWith('/posts/')){return'';}lethashnodeBoosterLink='';consthashnodeUrl=hashnodeData[fileSlug];if (hashnodeUrl){hashnodeBoosterLink=`<a href="${hashnodeUrl}" class="boost-link">Boost on Hashnode</a>`+hashnodeBoosterLink;}constintentToTweet=`<a class="boost-link" href="https://twitter.com/intent/tweet?text=${encodeURIComponent(`${title} by ${site.authorHandle}${site.url}${url}`)}">Share on Twitter</a>`;return`<a href="https://dev.to/nickytonline/${fileSlug}" class="boost-link">Boost on DEV</a>${hashnodeBoosterLink}${intentToTweet}`;}
One of the parameters is the blog post slug. When I pull in post from DEV, the same slug will be used for my blog post on my blog, so it's trivial generating a link back to DEV. For Hashnode, I currently import DEV posts using their DEV importer, so I need to alter some things like the slug, so that it's uniform with DEV and my blog.
I persist a list of blog post URLs from Hashnode by pulling in my Hashnode RSS feed because not all blog posts from my blog are on Hashnode. This is why only some posts have a Hashnode boost link.
Images
Any images in blog posts not on my omission list are pulled down and committed to the repository. Currently, the only images I omit are from giphy.com. Everything else is my images or Unsplash images which I have attributed to the author as per the Unsplash guidelines.
Before downloading any images, I check if they already exist in the repository. If they don't, I download and save them.
/**
* Saves a markdown image URL to a local file and returns the new image URL.
* TODO: Fix mixing two concerns.
* @param {string} markdownImageUrl
*
* @returns {string} Returns the new image URL.
*/asyncfunctionsaveMarkdownImageUrl(markdownImageUrl=null){letnewMarkdownImageUrl=null;if (markdownImageUrl){constimageUrl=newURL(markdownImageUrl);constimagefilename=imageUrl.pathname.replaceAll('/','_');constlocalCoverImagePath=path.join(POSTS_IMAGES_DIRECTORY,imagefilename);newMarkdownImageUrl=generateNewImageUrl(imageUrl);if (!(awaitfileExists(localCoverImagePath))){console.log(`Saving image ${imageUrl} to ${localCoverImagePath}`);awaitsaveImageUrl(markdownImageUrl,localCoverImagePath);}}returnnewMarkdownImageUrl;}
Embedded articles
I link to DEV posts withing some of my DEV blog posts. These are persisted as well to my repostitory. They are stored in the embeddedPostsMarkup.json file I generate via the updateBlogPostEmbeds(embeds, filepaths) function.
asyncfunctionupdateBlogPostEmbeds(embeds,filePaths){letblogPostEmbedsMarkup={};for (const[url]ofembeds){// You can't use the dev.to API to grab an article by slug, so we need to use the URL instead// to fetch the markup of the article page to extract the article ID.// This is only an issue for article embeds.constresponse=awaitfetch(url);consthtml=awaitresponse.text();constmatch=html.match(/data-article-id="(?<blogPostId>.+?)"/);if (match){const{blogPostId}=match.groups;const{body_html,body_markdown,comments_count,public_reactions_count,positive_reactions_count,...data}=awaitgetDevPost(blogPostId);blogPostEmbedsMarkup[url]=data;}else{thrownewError(`Could not find blog post at ${url}`);}}constdata=JSON.stringify(blogPostEmbedsMarkup,null,2);awaitfs.writeFile(filePaths,data,()=>console.log(`Saved image ${imageUrl} to ${imageFilePath}!`));}
This is the source code for the web site of Nick Taylor built using the Eleventy static site generator. It uses Andy Bell's Hylia template. I've made tweaks to it and will probably make more over time, but just wanted to give Andy a shout out for his awesome template.
As soon as anything is updated in the repository's main branch, a deployment to Netlify begins. This is where Eleventy gets to work.
Thanks to our caching efforts, all the markdown is in a state that Eleventy can now consume. I'm currently using the latest Canary version of Eleventy along with the backported experimental version of Node 16 in case your wondering why I run Eleventy using node directlty.
For images in the repository associated with blog posts, it'd be good to clean up images that are no longer used once a month.
I haven't dug into this yet, but sometimes the GitHub action errors out. I'm not overly concerned at the moment as it generally passes. However, still, it'd be nice for it to recover or handle this error more gracefully.
Currently, I manually post blog posts to Hashnode via their import from DEV functionality, add the associated tags, set the canonical URL and save the post. It would be nice to post to them when a new post is created on my blog. They don't use liquid tags. They use another format like this %[some url], so I would need to handle that.
It'd be nice to support more embeds from DEV in my blog
Automatically posting my latest posts to social media would be nice, but I'm on the fence about this one. I enjoy writing these Tweets or short posts manually instead of having some generic Tweet or post on LinkedIn.