You can use the process method provided by unified, which returns a Promise.
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
const parseMarkdown = async (markdown) => {
const result = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeStringify)
.process(markdown);
return result.toString();
};
(async () => {
const markdown = "# Hello, World!";
const html = await parseMarkdown(markdown);
console.log(html);
})();
Define an async function parseMarkdown that takes a markdown string as input. use unified with remarkParse, remarkRehype, and rehypeStringify to process the markdown asynchronously inside the function. The process method returns a Promise, which we can await to get the result. Now convert the result to a string with the toString()
method.
You can then use an async immediately invoked function expression (IIFE) to call the parseMarkdown function.
(async () => {
const markdown = "# Hello, World!";
const html = await parseMarkdown(markdown);
console.log(html);
})();