|
| 1 | +import { unified } from 'unified'; |
| 2 | +import remarkParse from 'remark-parse'; |
| 3 | +import { getComponentType } from './get-component-type.js'; |
| 4 | +import { jsx2js } from './jsx2js.js'; |
| 5 | +import { parse as parseYaml } from 'yaml'; |
| 6 | + |
| 7 | +const parseMDZ = (markdown) => unified().use(remarkParse).parse(markdown); |
| 8 | + |
| 9 | +const processMDZ = (markdown) => { |
| 10 | + // Frontmatter regex (between '---' or '+++') |
| 11 | + const frontmatterRegex = /^---\n([\s\S]+?)\n---/m; |
| 12 | + const importRegex = /^import\s+.+from\s+.+;$/gm; |
| 13 | + |
| 14 | + let imports = ['import {tag, Flex, HTMLWrapper} from "ziko"']; |
| 15 | + let frontmatter = {}; |
| 16 | + let cleanedMarkdown = markdown; |
| 17 | + |
| 18 | + const frontmatterMatch = markdown.match(frontmatterRegex); |
| 19 | + if (frontmatterMatch) { |
| 20 | + const frontmatterContent = frontmatterMatch[1]; |
| 21 | + frontmatter = parseYaml(frontmatterContent); |
| 22 | + cleanedMarkdown = cleanedMarkdown.replace(frontmatterRegex, ''); |
| 23 | + } |
| 24 | + |
| 25 | + cleanedMarkdown = cleanedMarkdown.replace(importRegex, (match) => { |
| 26 | + imports.push(match.trim()); |
| 27 | + return ''; |
| 28 | + }); |
| 29 | + return { imports, frontmatter, cleanedMarkdown }; |
| 30 | +}; |
| 31 | + |
| 32 | +const transformMDZ = (markdownAST) => { |
| 33 | + const transformNode = (node) => { |
| 34 | + // console.log(node.type) |
| 35 | + switch(node.type){ |
| 36 | + case 'text' : return JSON.stringify(node.value); |
| 37 | + case 'paragraph' : { |
| 38 | + const childNodes = node.children.map(transformNode).join(', '); |
| 39 | + return ` tag('p', {}, ${childNodes})`; |
| 40 | + } |
| 41 | + case 'heading' : { |
| 42 | + const childNodes = node.children.map(transformNode).join(', '); |
| 43 | + return ` tag('h${node.depth}', {}, ${childNodes})`; |
| 44 | + } |
| 45 | + case 'strong': { |
| 46 | + const childNodes = node.children.map(transformNode).join(', '); |
| 47 | + return ` tag('strong', {}, ${childNodes})`; |
| 48 | + } |
| 49 | + |
| 50 | + case 'emphasis': { |
| 51 | + const childNodes = node.children.map(transformNode).join(', '); |
| 52 | + return ` tag('em', {}, ${childNodes})`; |
| 53 | + } |
| 54 | + |
| 55 | + case 'link': { |
| 56 | + const childNodes = node.children.map(transformNode).join(', '); |
| 57 | + return ` tag('a', { href: "${node.url}" }, ${childNodes})`; |
| 58 | + } |
| 59 | + |
| 60 | + case 'image': { |
| 61 | + return ` tag('img', { src: "${node.url}", alt: "${node.alt || ''}" })`; |
| 62 | + } |
| 63 | + |
| 64 | + case 'list': { |
| 65 | + const listTag = node.ordered ? 'ol' : 'ul'; |
| 66 | + const childNodes = node.children.map(transformNode).join(', '); |
| 67 | + return ` tag('${listTag}', {}, ${childNodes})`; |
| 68 | + } |
| 69 | + |
| 70 | + case 'listItem': { |
| 71 | + const childNodes = node.children.map(transformNode).join(', '); |
| 72 | + return ` tag('li', {}, ${childNodes})`; |
| 73 | + } |
| 74 | + |
| 75 | + case 'code': { |
| 76 | + const language = node.lang ? `, { 'data-lang': '${node.lang}' }` : ''; |
| 77 | + return ` tag('pre', {}, tag('code'${language}, {}, ${JSON.stringify(node.value)}))`; |
| 78 | + } |
| 79 | + |
| 80 | + case 'blockquote': { |
| 81 | + const childNodes = node.children.map(transformNode).join(', '); |
| 82 | + return ` tag('blockquote', {}, ${childNodes})`; |
| 83 | + } |
| 84 | + case 'thematicBreak': { |
| 85 | + return ` tag('hr', {}, '')`; |
| 86 | + } |
| 87 | + case 'html' : { |
| 88 | + const component = node.value.trim(); |
| 89 | + const type = getComponentType(component); |
| 90 | + switch (type) { |
| 91 | + case 'jsx': |
| 92 | + return ` ${jsx2js(component)}`; |
| 93 | + case 'html': |
| 94 | + return ` HTMLWrapper("${component}")`; |
| 95 | + default: |
| 96 | + return null; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + } |
| 102 | + return 'null'; |
| 103 | + }; |
| 104 | + |
| 105 | + const body = markdownAST.children.map(transformNode).join(',\n'); |
| 106 | + return `()=>Flex( |
| 107 | +${body} |
| 108 | +).vertical(0,0)` |
| 109 | +}; |
| 110 | + |
| 111 | +const transpileMDZ= markdown =>{ |
| 112 | + const { imports, frontmatter, cleanedMarkdown } = processMDZ(markdown); |
| 113 | + const ast = parseMDZ(cleanedMarkdown); |
| 114 | + const jsFunction = transformMDZ(ast); |
| 115 | + let exports = ` |
| 116 | +const attributes = ${JSON.stringify(frontmatter)}; |
| 117 | +export {${Object.keys(frontmatter)}} = attributes; |
| 118 | + ` |
| 119 | + const Output = [ |
| 120 | + ...imports, |
| 121 | + jsFunction, |
| 122 | + exports, |
| 123 | + ].join('\n'); |
| 124 | + |
| 125 | + return Output |
| 126 | + |
| 127 | +} |
| 128 | +export {transpileMDZ} |
0 commit comments