UNPKG

@power-doctest/markdown

Version:
72 lines 2.71 kB
import { remark } from "remark"; import findAllBetween from "unist-util-find-all-between"; import { findBefore } from "unist-util-find-before"; import { parents as attachParents } from "unist-util-parents"; import { selectAll } from "unist-util-select"; import { DocTestController } from "./DocTestController.js"; const processor = remark(); const getComments = (parentNode, codeNode) => { const nonHtmlNode = findBefore(parentNode, codeNode, (node) => { return node.type !== "html"; }); const startNode = nonHtmlNode ? nonHtmlNode : parentNode.children[0]; const htmlNodes = findAllBetween(parentNode, startNode, codeNode, "html"); return htmlNodes.map((htmlNode) => { return htmlNode.value.replace(/^<!--/, "").replace(/-->$/, ""); }); }; /** * Parse Markdown code and return ParseResult object. */ export const parse = ({ content, filePath }) => { const markdownAST = attachParents(processor.parse(content)); const codeBlocks = [ ...selectAll(`code[lang="js"]`, markdownAST), ...selectAll(`code[lang="javascript"]`, markdownAST), ]; return codeBlocks.map((codeBlock) => { const codeValue = codeBlock.value || ""; const comments = getComments(codeBlock.parent, codeBlock); const docTestController = new DocTestController(comments); const state = docTestController.state; const doctestOptions = docTestController.doctestOptions; const expectedError = docTestController.expectedErrorName; const metadata = docTestController.doctestMetadata; return { code: codeValue, location: codeBlock.position ? { start: { line: codeBlock.position.start.line, column: codeBlock.position.start.column, }, end: { line: codeBlock.position.end.line, column: codeBlock.position.end.column, }, } : { start: { line: 1, column: 0, }, end: { line: 1, column: 0, }, }, state: state, expectedError: expectedError, metadata: metadata, doctestOptions: doctestOptions ? { filePath: filePath, ...doctestOptions, } : { filePath, }, }; }); }; //# sourceMappingURL=index.js.map