<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: arvi9</title>
    <description>The latest articles on DEV Community by arvi9 (@arvi9).</description>
    <link>https://dev.to/arvi9</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F330508%2Fc820e68d-600a-46d9-8310-85979668126b.png</url>
      <title>DEV Community: arvi9</title>
      <link>https://dev.to/arvi9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arvi9"/>
    <language>en</language>
    <item>
      <title>Using Prism with Next.js</title>
      <dc:creator>arvi9</dc:creator>
      <pubDate>Wed, 04 Oct 2023 03:20:32 +0000</pubDate>
      <link>https://dev.to/arvi9/using-prism-with-nextjs-570f</link>
      <guid>https://dev.to/arvi9/using-prism-with-nextjs-570f</guid>
      <description>&lt;p&gt;Prism is a popular syntax highlighter commonly used with Markdown. This example shows how to use Prism with Next.js. Use the theme dropdown in the header to switch syntax highlighting themes.&lt;/p&gt;

&lt;p&gt;Next.js uses getStaticPaths/getStaticProps to generate static pages. These functions are not    bundled client-side, so you can write server-side code directly. For example, you can read Markdown files from the filesystem (fs) – including parsing front matter with gray-matter. For example, let's assume you have a Markdown file located at docs/my-post.js.&lt;/p&gt;

&lt;p&gt;We can retrieve that file's contents using getDocBySlug('my-post').&lt;/p&gt;

&lt;p&gt;// lib/docs.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;import fs from 'fs';&lt;br&gt;
import { join } from 'path';&lt;br&gt;
import matter from 'gray-matter';&lt;/p&gt;

&lt;p&gt;const docsDirectory = join(process.cwd(), 'docs');&lt;/p&gt;

&lt;p&gt;export function getDocBySlug(slug) {&lt;br&gt;
  const realSlug = slug.replace(/.md$/, '');&lt;br&gt;
  const fullPath = join(docsDirectory, &lt;code&gt;${realSlug}.md&lt;/code&gt;);&lt;br&gt;
  const fileContents = fs.readFileSync(fullPath, 'utf8');&lt;br&gt;
  const { data, content } = matter(fileContents);&lt;/p&gt;

&lt;p&gt;return { slug: realSlug, meta: data, content };&lt;br&gt;
}&lt;br&gt;
Then, we can transform the raw Markdown into HTML using remark plugins.&lt;/p&gt;

&lt;p&gt;// lib/markdown.js&lt;/p&gt;

&lt;p&gt;import { remark } from 'remark';&lt;br&gt;
import html from 'remark-html';&lt;br&gt;
import prism from 'remark-prism';&lt;/p&gt;

&lt;p&gt;export default async function markdownToHtml(markdown) {&lt;br&gt;
  const result = await remark()&lt;br&gt;
    // &lt;a href="https://github.com/sergioramos/remark-prism/issues/265"&gt;https://github.com/sergioramos/remark-prism/issues/265&lt;/a&gt;&lt;br&gt;
    .use(html, { sanitize: false })&lt;br&gt;
    .use(prism)&lt;br&gt;
    .process(markdown);&lt;br&gt;
  return result.toString();&lt;br&gt;
}&lt;br&gt;
Passing the content returned by getDocBySlug('my-post') into markdownToHtml(content) would convert a Markdown file like this:&lt;/p&gt;



&lt;p&gt;title: 'My First Post'&lt;/p&gt;
&lt;h2&gt;
  
  
  description: 'My very first blog post'
&lt;/h2&gt;
&lt;h1&gt;
  
  
  My First Post
&lt;/h1&gt;

&lt;p&gt;I &lt;strong&gt;love&lt;/strong&gt; using &lt;a href="https://nextjs.org/"&gt;Next.js&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getDocBySlug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;into this HTML, which includes the proper elements and class names.&lt;/p&gt;

&lt;h1&gt;My First Post&lt;/h1&gt;

&lt;p&gt;I &lt;strong&gt;love&lt;/strong&gt; using &lt;a href="https://nextjs.org/"&gt;Next.js&lt;/a&gt;&lt;/p&gt;


&lt;pre&gt;&lt;br&gt;
    &lt;code&gt;&lt;br&gt;
      &lt;span class="token keyword"&gt;const&lt;/span&gt; doc &lt;span class="token operator"&gt;=&lt;/span&gt; &lt;span class="token function"&gt;getDocBySlug&lt;/span&gt;&lt;span class="token punctuation"&gt;(&lt;/span&gt;params&lt;span class="token punctuation"&gt;.&lt;/span&gt;&lt;span class="token property-access"&gt;slug&lt;/span&gt;&lt;span class="token punctuation"&gt;)&lt;/span&gt;&lt;span class="token punctuation"&gt;;&lt;/span&gt;&lt;br&gt;
    &lt;/code&gt;&lt;br&gt;
  &lt;/pre&gt;

&lt;p&gt;Deploy Your Own&lt;br&gt;
View the source code and deploy your own. You can add new Markdown files to docs/ and see them live instantly!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
