markdownnext.jsprismjs

Use PrismJS in Next.js with remark to hightlight code from markdown


I want to include PrismJS in my Next.js Blog to highlight code in .md files. The markdown is processed in /lib/posts.js with remark and remark-html and passed as HTML to react-markdown

// /lib/posts.js

export async function getPostData(id) {
  const fullPath = path.join(postsDirectory, `${id}.md`);
  const fileContents = fs.readFileSync(fullPath, "utf8");

  const matterResult = matter(fileContents);

  const processedContent = await remark()
    .use(html)
    .process(matterResult.content);
  const contentHtml = processedContent.toString();

  return {
    id,
    contentHtml,
    ...matterResult.data,
  };
}

The contentHtml is fetched in [id].js where I use ReactMarkdown to render it. Here I am importing Prism use the CodeBlock function to highlight. I import prism.css in my _app.js next to my global.css

// /pages/_app.js

import Container from "../components/Container";
import "../styles/global.css";
import "../styles/prism.css";

export default ({ Component, pageProps }) => (
  <Container>
    <Component {...pageProps} />
  </Container>
);

// /pages/posts/[id].js

import { getAllPostIds, getPostData } from "../../lib/posts";
import ReactMarkdown from "react-markdown/with-html";
import Prism from "prismjs";

const CodeBlock = (language, values) => {
  return <Prism language={language}>{values}</Prism>;
};

export default function Post({ postData }) {
  return (
    <ReactMarkdown
      escapeHtml={false} // Dangerous if content is user-generated
      source={postData.contentHtml}
      renderers={{ code: CodeBlock }}
    />
  );
}

export async function getStaticPaths() {
  const paths = getAllPostIds();
  return {
    paths,
    fallback: false,
  };
}

export async function getStaticProps({ params }) {
  const postData = await getPostData(params.id);
  return {
    props: {
      postData,
    },
  };
}

It is working when I enter the URL manually and press return. As in https://nextjs-blog-ivory-nine.vercel.app/posts/first-next-js-blog-devdiary But when I navigate from the Main page - https://nextjs-blog-ivory-nine.vercel.app - to the first post, the code won't be hightlighted.

Do you have any idea? Thanks in advance!


Solution

  • I just created an example of this which might help. It uses:

    https://github.com/leerob/nextjs-prism-markdown