UNPKG

markdown-code-example-inserter

Version:
30 lines (29 loc) 1.03 kB
import { readFile } from 'node:fs/promises'; import rehypeParse from 'rehype-parse'; import remarkParse from 'remark-parse'; import remarkRehype from 'remark-rehype'; import { unified } from 'unified'; const markdownParser = unified().use(remarkParse).use(remarkRehype); const htmlParser = unified().use(rehypeParse, { fragment: true }); export async function parseMarkdownFile(markdownFilePath) { const fileContents = await readFile(markdownFilePath); return parseMarkdownContents(fileContents); } export function parseMarkdownContents(markdownFileContents) { return markdownParser.parse(markdownFileContents); } export function isCommentNode(input) { return input.type === 'comment'; } export function isCodeBlock(input) { return input.type === 'code'; } export function isHtmlNode(input) { return input.type === 'html'; } export function parseHtmlContents(htmlContents) { return htmlParser.parse(htmlContents); } export function parseHtmlNode(htmlNode) { return parseHtmlContents(htmlNode.value); }