This post is a continuation of my migration journey from WordPress to Astro. If you have not read the previous posts, you might want to start with:
- Why I Switched from WordPress to Astro: Faster, Cheaper, and Greener
- Migrating from WordPress to Astro: How I Moved My Blog Content
In this post, I explain how I replaced 3 WordPress plugin functionalities in Astro.
Acronyms 2
The Acronyms 2 plugin allowed me to define a list of acronyms, and automatically generated tooltips with their meanings when they appeared in posts.
Astro Implementation
In Astro, I created a rehype plugin that processes text and wraps recognized acronyms with an <abbr>
tag containing the full text of the acronym as the title attribute.
Note
rehype is an ecosystem of plugins that work with HTML as structured data,
specifically ASTs.
This allows for easy manipulation, or extension, of HTML content.
The first time an abbreviation is encountered on a page, the plugin will also wrap the <abbr>
tag with an <dfn>
tag. This semantic tag is used to indicate the defining instance of a term.
A special case is when an acronym is used in a <code>
or <pre>
block. In this case, the plugin will not wrap the acronym with an <abbr>
tag.
See the full code on GitHub: rehypeAbbreviate.js.
Because there is no database to store the acronyms, I defined them in a YAML file and imported them into the plugin.
The YAML file is loaded in the astro.config.mjs
file and passed to the rehypeAbbreviate plugin.
I expect that loading the acronyms from a YAML file could be improved, but for now, it works well enough.
Add Meta Tags
The Add Meta Tags plugin generated metadata for search engines and social media previews.
Astro Implementation
Most of the metadata is defined in the BaseHead.astro
file, which is included in one of the layout files that pass on contextual values.
Some properties are conditionally included based on availability. For example, the article
object is passed to the layout when rendering a blog post.
Sections and tags can even occur multiple times, so I use the Array map
function to generate multiple <meta>
tags.
View the full code on GitHub: BaseHead.astro.
Better Tag Cloud
The Better Tag Cloud plugin displayed a tag cloud where tags were weighted based on post frequency.
Astro Implementation
I used the PHP code from the plugin as a reference and, with some help from ChatGPT, created a function that:
-
Gets a map of tags and their posts.
-
Sorts the tags by post count.
-
Get the top 50 tags.
-
Sort the tags alphabetically.
-
Calculate the weight of each tag based on the post count. Using
8pt
as the smallest font size and22pt
as the largest font size. -
Render the tags in the sidebar.
The full code can be found on GitHub: Sidebar.astro and util.js.
These are the first 3 plugins I replaced with Astro functionality.
In the next post, I will cover more plugins, including syntax highlighting, paging, and more.