Created
May 20, 2021 11:38
-
-
Save tnzk/42b66c8849b7273b4351a31eaab2ce4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const { process } = require('babel-jest') | |
const MarkdownIt = require('markdown-it'); | |
const MarkdownItAST = require('markdown-it-ast'); | |
const md = new MarkdownIt({}); | |
function createTransformer(src, filename, config, transformOptions) { | |
const ast = MarkdownItAST.makeAST(md.parse(src)) | |
const specs = [] | |
let currentSpec = { | |
scenarios: [], | |
paragraphs: [], | |
lists: [] | |
} | |
let currentScenario = { | |
paragraphs: [], | |
lists: [] | |
} | |
let inScenario = false | |
ast.forEach((node, i) => { | |
switch (node.nodeType) { | |
case 'heading': | |
if (node.openNode.tag == 'h1') { | |
if (currentSpec.title) specs.push(currentSpec) | |
currentSpec = { | |
title: node.children[0].content, | |
scenarios: [], | |
paragraphs: [], | |
lists: [], | |
} | |
inScenario = false | |
} | |
if (node.openNode.tag == 'h2') { | |
if (currentScenario.title) currentSpec.scenarios.push(currentScenario) | |
currentScenario = { | |
title: node.children[0].content, | |
paragraphs: [], | |
lists: [], | |
} | |
inScenario = true | |
} | |
break | |
case 'paragraph': | |
(inScenario ? currentScenario : currentSpec).paragraphs.push(node.children[0].content) | |
break | |
case 'bullet_list': | |
const inventory = (inScenario ? currentScenario : currentSpec) | |
const listItems = node.children.flatMap(n => n.children.flatMap(p => p.children)).flatMap(c => c.content); | |
inventory.lists.push(...listItems) | |
break | |
} | |
}) | |
currentSpec.scenarios.push(currentScenario) | |
specs.push(currentSpec) | |
const buildScenario = scenario => { | |
const tab = ' ' | |
return `describe('${scenario.title}', () => { | |
${scenario.lists.map(s => `${tab}// TODO: Load ./specs/steps/step_${s}.js`).join("\n")} | |
})` | |
} | |
const buildSpec = spec => { | |
return `describe('${spec.title}', () => { | |
${spec.scenarios.map(buildScenario).join()} | |
})` | |
} | |
console.log( | |
specs.map(buildSpec).join() | |
) | |
// const withFrontMatter = parseFrontMatter(src, options) | |
// const jsx = mdx.sync(withFrontMatter) | |
// const toTransform = `import {mdx} from '@mdx-js/react';${jsx}` | |
// return process(toTransform, filename, config, transformOptions).code | |
return process(src, filename, config, transformOptions).code | |
} | |
module.exports = { | |
process: createTransformer, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment