Posts

Parsing Metadata Inside of Markdown Using JavaScript Without Any Dependencies

In a recent project, I worked with Markdown files that contained metadata at the top for blog posts. I needed to parse the Markdown with JavaScript and make a set of key/value pairs. As you can see, the metadata is encapsulated in – – – with the metadata contained within. // Function to parse metadata from a markdown file const parseMarkdownMetadata = markdown => { // Regular expression to match metadata at the beginning of the file const metadataRegex = /^---([\\s\\S]\*?)---/; const metadataMatch = markdown.match(metadataRegex); // If there is no metadata, return an empty object if (!metadataMatch) { return {}; } // Split the metadata into lines const metadataLines = metadataMatch[1].split("\\n"); // Use reduce to accumulate the metadata as an object const metadata = metadataLines.reduce((acc, line) => { // Split the line into key-value pairs const [key, value] = line.split(":").map(part => part.trim()); // If the line is not empty add the key-value pair to the metadata object if(key) acc[key] = value; return acc; }, {}); // Return the metadata object return metadata; }; You can call this function and pass in the markdown file content. It will return an object with the metadata.

Programmatically Update the Status of a Post Using Gutenberg JS

In a WordPress project I am building, I needed a way to programmatically update the post status of a post on the edit screen, specifically in the Gutenberg editor. Before arriving at the solution below, I first struggled to figure this out. I was trying to call the updatePost method with a post object and modified status, then calling savePost but what kept happening was the post would revert to draft status.

How to Hide Meta Boxes in WordPress Gutenberg

When WordPress introduced the Gutenberg editor, it was a mess, to say the least. Everything was turned upside for developers and things that worked in previous versions were completely broken when Gutenberg was released. One of the things that were broken in WordPress was the ability to hide a meta box on the editor screen. Instead of setting meta_box_cb to false and expecting the box the hidden, nothing happens. Well, there is a way you can hide meta boxes by using the following snippet of code inside of functions.php

How to programmatically populate a TinyMCE Advanced Custom Fields field in WordPress

The Advanced Custom Fields plugin for WordPress is invaluable. It has filled a gap in WordPress for the better part of a decade and is one of the first plugins that I install in a new WordPress installation (I have a lifetime developer licence). The ACF plugin provides a Javascript API available using acf it has numerous methods for interacting with ACF fields in your WordPress installation. You can programmatically get and set field values, observe for changes and react accordingly.

Programmatically Clicking the “Convert to Blocks” Button in WordPress Gutenberg on the Edit Screen

In the beginning, I was one of the loud developers protesting WordPress Gutenberg. But, now that Gutenberg has been around for a while, I’ve grown to like it. I can’t tell if it’s because of Stockholm syndrome or if it has actually improved. Anyway. One of the looming problems users of WordPress will still face is the fact if you programmatically insert content into WordPress using wp_insert_post or however else you insert content (say from a CSV or API) you’ll find that WordPress will put it into a Classic Editor block. You will then be prompted to “Convert to blocks” in the editor.

Waiting for an Element to Exist With JavaScript

There are many different ways to solve this use case. You want to wait for an element to exist on the page and run a callback function when it does. It seems simple enough, but what’s the easiest way? By using an async function and a while loop, we can wrap setTimeout in a promise, then await it, and then resolve the promise when the timeout completes. As you can see, it will continue the loop until we reach the timeout passed into the function (or the element is found).

Stop Forcing People Back Into the Office

Remote work (also known as WFH) is a hot topic in 2022. As pandemic-era mandates and restrictions come to an end, there has been a new battle forming. In the left corner, we have companies that want their employees to come back into the office, and in the right-hand corner, we have employees who have been allowed to work remotely for the last two years being asked to come back into the office.

Passing Environment Variables Into Your Code With Webpack

In some use cases, it can be beneficial to pass environment variables into your code. In my case, at build time I pass an environment variable to Webpack in the form of --env production and so forth. I wanted to get this in my code so I could load different configuration files depending on the built environment. I have three environments I build for: Production Staging Development While there are code solutions you can implement such as checking the URL, I wanted something handled in the build itself.

Rate Rise Calculator

As a homeowner with a mortgage, interest rate rises have become an area of interest for my wife and me. As the Reserve Bank of Australia (and the rest of the world) sees continued rate hikes, it’s important to know how much extra money you’ll need to cough up. I wanted a simple tool that wasn’t trying to get me to provide my contact details and sell my information to banks. A simple calculator that gives me an approximation of what my monthly repayments will be with rate increases.

Fixing WiFi issues with the Neural DSP Quad Cortex

For some Quad Cortex users, you will encounter an issue with the WiFi not working. While your first thought might be yoy have a defective unit, the issue might be more simple than you think. The Quad Cortex only operates on the 2.4GHz band of WiFi. This band offers slower speeds than 5GHz WiFi, but is more reliable and can penetrate obstacles better. Chances are your WiFi isn’t setup to work with the 2.4GHz band and you could be transmitting on 5 instead.