If you like to take notes from the books you read and know a little bit of JavaScript, this may help you.
I've been using Apple Notes, Notion, sticky notes, and paper for my notes. They were unorganized and hard to index when needed. I moved all to Markdown, and started publishing on this website.
Taking notes is the hard part. Once they are in place, making them look pretty is a joyful task to deal with. That's what I did while building the book pages, without spending too much effort on details. It took me half-a-day to make it legit enough to share in this post.
Adding front-matter to Markdown
We need some manual labor to do before we get to the acrobatics.
We can use YAML syntax at the beginning of a Markdown file to define the metadata. There's a library called gray-matter
to parse it to JSON so we can use it in the next steps.
It's easy to retrieve a lot of information and generate specific meta images by using the ISBN. That is the only identifier we need for the rest. Amazon usually uses ISBN-10 as a path parameter for the product pages.
Using Google Books API for metadata
If we only need essential information like title, authors, and cover image, we can proceed with the Google Books API. Not sure if it's a bug or feature, but some endpoints don't need authentication. Obtaining an API key is not that hard as well. We just need to create a Google account and register a new app to use the Books API.
Google Books has a pretty simple query endpoint that we can pass the ISBN and API keys as parameters. Unfortunately, thumbnail images are low-resolution. It looks a bit ugly, but doubling the dimensions during the transformation can be a solution.
Creating a meta image for the Book page
Now that we have the least relevant information to render a book page, we can take some time to prettify the meta tags. Here's how related services render the meta images related to book pages.
Alright, that is doable with what we have.
In my experience, the easiest way to generate a custom image in the NodeJS context is to use a canvas library. The easiest to use I found is, hold your breath, called canvas
. I also use image-size
to gather the width
and height
properties.
Here's the flow:
- Fetch the metadata from Google Books API.
- Fetch the thumbnail image as blob.
- Extract the width and height properties.
- Create a canvas with the size of the OpenGraph image.
- Place the thumbnail in the middle, double the size to make it look bigger.
- Save the generated image to somewhere.
- Return the URL.
Since it's a Next.js app, I prefer to save it to the filesystem. An alternative approach could be uploading to a CDN.
After injecting the meta images to the book pages, metatags.io is a great tool to verify how they look.