Previously, we talked through the choices behind the tech stack used to build my developer blog. The step towards building a functional
blog is wiring up contentlayer, an SDK for transforming unstructured content into type-safe
json data structures. Contentlayer is a breeze to use, but in case you're stuck, here is how I set it up.
I started by creating a /content folder within my project directory. Within the /content folder, I created two more folders,
/definitions and /posts.
I then put my content schemas within the /definitions folder, starting with Post.
There's two more schemas I need to define: Tag and Series. Tags will be used
to properly divide post topics, while Series will link togther posts that are sequential.
Once I've defined Series and Tag, I can import them and use their definitions to finish
out the fields in the Post model.
Make note of the remarkPlugins and rehypePlugins fields in contentlayer.config.js. Contentlayer is pretty
nifty, and we can use all sorts of plugins during the content generation process.
Github Flavored Markdown is a subset of markdown that Github
uses in its readme files. We can enable it in our content using remark-gfm. Install it using
We can automatically wrap our markdown headers with <a> tags for our table of contents.
Add the following packages using yarn (or another package manager)
then, add it to the list of rehype plugins in your contentlayer.config.js.
Finally, you need to go to the Post model and parse the headings directly from the
content.
We can render our posts now using Next.js Dynamic Routes. In the /pages
folder, create a new folder /post and a file [slug].tsx. Adding brackets to the file name treats it as a parameter.
We can access the json generated by contentlayer by importing from contentlayer/generated.
With that import, you grab all generated posts, and can render how you'd like. If you're using a static generator like Next.js,
you're going to want to put it in the getStaticProps method of a page.
A nice-to-have for any blog is to be able to have visitors "like" your article, and for you to be able to track analytics. Next up, we
will set up a (free) PlanetScale database with Prisma schemas and type-safe client
generation to work with the database.