|
| 1 | +import remark from 'remark' |
| 2 | +import parents from 'unist-util-parents' |
| 3 | +import select from 'unist-util-select' |
| 4 | +import findBefore from 'unist-util-find-before' |
| 5 | +import htmlToReact from 'html-to-react' |
| 6 | +import parsePairs from 'parse-pairs' |
| 7 | + |
| 8 | +const htmlParser = new htmlToReact.Parser() |
| 9 | + |
| 10 | +const nodeToStory = (node, file) => { |
| 11 | + const html = node.value |
| 12 | + const element = htmlParser.parse(html) |
| 13 | + const pairs = node.lang.replace(/^html\s*/, '') |
| 14 | + const attrs = pairs.length ? parsePairs(pairs) : {} |
| 15 | + const title = attrs.title || getPreviousHeading(node) || |
| 16 | + `story @ ${file}:${node.position.start.line}` |
| 17 | + return { |
| 18 | + title, |
| 19 | + story: () => element, |
| 20 | + attrs, |
| 21 | + html, |
| 22 | + file, |
| 23 | + node, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const getPreviousHeading = node => { |
| 28 | + const heading = findBefore(node.parent, node, 'heading') |
| 29 | + return (heading && !heading.used) |
| 30 | + ? (heading.used = true, heading.children.map(c => c.value).join('')) |
| 31 | + : undefined |
| 32 | +} |
| 33 | + |
| 34 | +export default req => { |
| 35 | + return req.keys().reduce((stories, file) => { |
| 36 | + const content = req(file) |
| 37 | + const ast = parents(remark.parse(content)) |
| 38 | + const path = file.replace(/^\.\//, '') |
| 39 | + return stories.concat( |
| 40 | + select(ast, 'code[lang^=html]') |
| 41 | + .map(node => nodeToStory(node, path)) |
| 42 | + .filter(({attrs}) => attrs.story !== "false") |
| 43 | + ) |
| 44 | + }, []) |
| 45 | +} |
0 commit comments