Last active
July 20, 2025 00:46
-
-
Save joshnuss/98697265d37ec01096c2fb003e3da43d to your computer and use it in GitHub Desktop.
Unified with Remark, FrontMatter and Shiki
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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