Skip to content

Instantly share code, notes, and snippets.

@joshnuss
Last active July 20, 2025 00:46
Show Gist options
  • Save joshnuss/98697265d37ec01096c2fb003e3da43d to your computer and use it in GitHub Desktop.
Save joshnuss/98697265d37ec01096c2fb003e3da43d to your computer and use it in GitHub Desktop.
Unified with Remark, FrontMatter and Shiki
/*
* usage:
*
* // parse markdown
* const file = await parseMarkdown('posts/foo.md')
*
* // extract front matter
* const { title, summary, date, author, tags } = file.data.matter
*
* // access html
* file.value
*/
import remarkRehype from 'remark-rehype'
import remarkFrontmatter from 'remark-frontmatter'
import remarkParse from 'remark-parse'
import remarkStringify from 'remark-stringify'
import remarkHTML from 'remark-html'
import rehypeShiki from '@shikijs/rehype'
import rehypeStringify from 'rehype-stringify'
import { unified } from 'unified'
import type { VFile } from 'vfile'
import { read } from 'to-vfile'
import { matter } from 'vfile-matter'
export async function parseMarkdown(path: string): Promise<VFile> {
const file = await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter, ['yaml'])
.use(remarkHTML)
.use(remarkRehype)
.use(rehypeShiki, {
langs: ['javascript', 'html', 'css'],
themes: {
light: 'vitesse-light',
dark: 'vitesse-dark'
}
})
.use(rehypeStringify)
.use(() => (tree, file) => {
matter(file)
})
.process(await read(path))
return file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment