LD
Change your colour scheme

Bringing my omg.lol Now page into Eleventy

Published:

Robb Knight has this great Javascript tool for embedding your omg.lol /now page in another page.

I thought it was pretty cool to use, but because I’m allergic to client-side Javascript, I wanted to port it to Eleventy so that I could generate it once-per-build. It was actually pretty simple to do, because the server-side source for omgnow.js is on Github. So this was basically a port-to-JS job.

I have to files, now.md and now.11tydata.js. My now.md is pretty simple, it just looks like this:


title: My /now page
layout: page.njk
---


And then now.11tydata.js uses eleventyComputed values to retrieve and parse the /now page content from the omg.lol API:

module.exports = {
eleventyComputed: {
content: async () => {
// Retrieve the /now page from the server
const response = await fetch("https://api.omg.lol/address/lewis/now");
const body = await response.json();

// Convert the unix timestamp to a JS datetime timestamp
const updated = new Date(body.response.now.updated * 1000);
let content = body.response.now.content;

// Replace the last-updated tag
content = content.replace("{last-updated}", `<span class="now_updated">Updated ${updated.toLocaleDateString('en-GB', { dateStyle: "medium" })}</span>`);
// Strip out omg.lol-specific tags
content = content.replaceAll(/{[^}]*}/g, "");
// remove comments
content = content.replaceAll(/\/\*.*?\*\//g, "");

// Remove everything before the --- Now --- marker, because I handle page titles and headings in 11ty
if (content.includes("--- Now ---")) {
const [before, after] = content.split("--- Now ---");
content = after;
}

return content;
}
}
}

And there you have it! Robb’s source made this 1000x easier that it would have been. The only thing I need to do is stop stripping out the omg.lol icon sets, and instead replace them with the icons I actually have - my markdown config mostly duplicates the same fontawesome set.

About the author

My face

I'm Lewis Dale, a software engineer and web developer based in the UK. I write about writing software, silly projects, and cycling. A lot of cycling. Too much, maybe.

Responses