markdown-code-example-inserter
Version:
Syncs code examples with markdown documentation.
32 lines (31 loc) • 1.04 kB
JavaScript
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);
}