Skip to content

Instantly share code, notes, and snippets.

@JTRNS
Created August 3, 2023 19:25
Show Gist options
  • Save JTRNS/fc6b96807dedc4d799fff6db33dfefed to your computer and use it in GitHub Desktop.
Save JTRNS/fc6b96807dedc4d799fff6db33dfefed to your computer and use it in GitHub Desktop.
GitHub Flavoured Markdown Parsing in Deno
import { unified } from "https://esm.sh/unified@10.1.2";
import remarkParse from "https://esm.sh/remark-parse@10.0.2";
import remarkFrontmatter from "https://esm.sh/remark-frontmatter@4.0.1";
import remarkGfm from "https://esm.sh/remark-gfm@3.0.1";
import remarkRehype from "https://esm.sh/remark-rehype@10.1.0";
import rehypeStringify from "https://esm.sh/rehype-stringify@9.0.3";
import rehypeSlug from "https://esm.sh/rehype-slug@5.1.0";
import rehypeAutolinkHeadings from "https://esm.sh/rehype-autolink-headings@6.1.1";
import remarkGemoji from "https://esm.sh/remark-gemoji@7.0.1";
import rehypeStarryNight from "https://esm.sh/@microflash/rehype-starry-night@2.1.1";
export async function parseMarkdown(content: string) {
const file = await unified()
.use(remarkParse)
.use(remarkFrontmatter)
.use(remarkGemoji)
.use(remarkGfm)
.use(remarkRehype, {
allowDangerousHtml: true,
})
.use(rehypeSlug)
.use(rehypeAutolinkHeadings, {
properties: { ariaHidden: true, tabIndex: -1, class: "anchor" },
content: {
type: "element",
tagName: "span",
properties: { class: ["octicon", "octicon-link"] },
children: [],
},
})
.use(rehypeStarryNight)
/// @ts-ignore rehype-stringify types are wrong
.use(rehypeStringify, { allowDangerousHtml: true })
.process(content);
return file.toString("utf8");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment