Integrating Comark with Next.js
comarknextjsintegration
This example uses Next.js App Router with Comark as the Markdown renderer.
How it works
Instead of using the typical gray-matter + remark + rehype pipeline, we use Comark's framework-agnostic API:
- Read markdown files — Load
.mdfiles from thecontent/posts/directory - Parse with Comark — Call
parse()to build the AST and extract frontmatter - Static generation — Use
generateStaticParamsfor full SSG - Render with React — Use
ComarkRendererfromcomark/reactwith custom components
import { parse } from 'comark'
import { ComarkRenderer } from '@comark/react'
import highlight from 'comark/plugins/highlight'
import Alert from '@/components/Alert'
const tree = await parse(content, {
plugins: [highlight()],
})
// In your Server Component:
// <ComarkRenderer tree={tree} components={{ Alert }} />Since Next.js Server Components run on the server, Comark's
parse() is called at build time — zero JavaScript is sent to the client.Custom components
You can register any number of custom components. Each one receives props and children from the Comark AST:
export default function Alert({ type = 'info', children }) {
return (
<div className={`alert alert-${type}`} role="alert">
{children}
</div>
)
}This makes it easy to extend your Markdown with reusable, styled components.