.../articles/
Nuxt.js + Contentful + Prism

Nuxt.js + Contentful + Prism

2019.03.03

As mentioned in this article as well, this site is built with Nuxt.js and Contentful.

Article content is edited in Markdown format via the Contentful editor, then fetched by Nuxt, converted to HTML, and rendered.

However, when the site first launched, the code blocks only had a simple style and felt a bit lacking, so we introduced Prism to enable syntax highlighting.

The usage itself is simple, as shown in Basic Usage, but it took a bit of effort to get it working with Nuxt, so here's a summary of how we did it.

By the way, searching for vue + prism turns up vue-prism-component as well, but it seemed difficult to apply it only to the code blocks within the body text fetched from Contentful, so we didn't use it.


Setting Up Nuxt

Install prismjs with yarn (or npm)

$ yarn add prismjs

Add prismjs to plugins (choose the theme and language to your liking)

// app/plugins/prism.js
import Prism from 'prismjs'
import 'prismjs/themes/prism-tomorrow.css'
export default Prism

// nuxt.config.js
  plugins: [
    '~/plugins/prism',
  ],

Load prism on the pages where you want to enable syntax highlighting

// app/pages/articles/_slug.vue
...
<script>
import Prism from '~/plugins/prism'
...
  mounted() {
    Prism.highlightAll()
  },
...

That's it for the Nuxt setup.

At first the syntax highlighting wasn't kicking in and I got stuck, but calling Prism.highlightAll() inside mounted solved it.


Writing Code Blocks in Contentful

When writing a code block in Contentful, you follow Markdown syntax and wrap it above and below with three backticks, but if you specify a language right after the opening three backticks, the corresponding class gets applied.

In the example below, we specify js, so the code element ends up with the class language-js applied.

Any class starting with language- should generally get syntax highlighting as long as it's listed in Prism's Supported languages.

  • Markdown source
    ```js
    const posts = await client.getEntries({
      content_type: process.env.CTF_BLOG_POST_TYPE_ID,
      'fields.slug': slug,
    })
    ```
  • Converted HTML
<pre><code class="language-js">
const posts = await client.getEntries({
  content_type: process.env.CTF_BLOG_POST_TYPE_ID,
  'fields.slug': slug,
})
</code></pre>

Before and After Introducing Prism

  • Before
      const posts = await client.getEntries({
        content_type: process.env.CTF_BLOG_POST_TYPE_ID,
        'fields.slug': slug,
      })
  • After 🎉
      const posts = await client.getEntries({
        content_type: process.env.CTF_BLOG_POST_TYPE_ID,
        'fields.slug': slug,
      })

Bonus

If you just want to share a bit of code or post it to a blog, a service called Carbon could also work well.

It's as simple as typing your code into the browser and choosing a theme and language — you can then export it as PNG or SVG, or generate embed code.

written by

.../article/

Articles

All articles

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

We moved our corporate site's hosting and CMS, and made it bilingual along the way. The constraints we only found by running against real data were more useful than the migration itself, so this post focuses on where we got stuck.

Can't Read POST Data with Firebase Functions × Remix?

Can't Read POST Data with Firebase Functions × Remix?

How to read POST data from a Remix action when running on Firebase Functions.

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Building a structure where executives and leaders themselves can identify issues and evaluate solutions using generative AI. We introduce how combining this with our hands-on support dramatically improves both the quality and speed of digital transformation.

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Notes on the obstacles we hit while deploying a Next.js app managed in a monorepo to AWS Amplify.

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Many production companies have adopted remote work as a result of the pandemic, and we are one of them.

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

There is no single formula for e-commerce design that sells. Driving revenue requires a solid concept, and getting to that concept requires thorough research.

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

With remote work becoming the norm during the COVID-19 pandemic, our team now works from home most days of the week.

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We hope this helps web designers who work in Figma. Read on for how to use it.

How to Build an E-Commerce Site, and Which Platforms We Recommend

How to Build an E-Commerce Site, and Which Platforms We Recommend

Shopping online for fashion, appliances, and even groceries is now routine. With the pandemic accelerating the shift, we receive a steady stream of questions about which platform to use and how much it costs.

Generating FastAPI Schema Classes from OpenAPI

Generating FastAPI Schema Classes from OpenAPI

We chose FastAPI, a relatively modern framework, for a Python API project. FastAPI can generate an OpenAPI definition from your backend code, but here we do the opposite: generating FastAPI schema classes from an OpenAPI definition prepared in advance.

View all articles

Contact us