|
| 1 | +import { toH } from 'hast-to-hyperscript' |
| 2 | +import { HtmlNode } from '../../components/html/types' |
| 3 | +import { editorSchemaToHast } from '../transformers/editor-schema-to-hast' |
| 4 | +import { toCSSObject } from './to-css-object' |
| 5 | +import { stringifyHastNode } from './stringify-hast-node-as-html' |
| 6 | +import { toReactProps } from './to-react-props' |
| 7 | +import { format } from './format' |
| 8 | +import { CodegenOptions } from './types' |
| 9 | +import { Theme } from '../../types/theme' |
| 10 | +import { inlineStylesToStyleElement } from '../transformers/inline-styles-to-style-element' |
| 11 | + |
| 12 | +const h = (theme: Theme) => (tagName: string, props: any, children?: any[]) => { |
| 13 | + const newProps = toReactProps(props) |
| 14 | + |
| 15 | + if (newProps.style) { |
| 16 | + const style = newProps.style |
| 17 | + delete newProps.style |
| 18 | + newProps.style = toCSSObject(style, theme) |
| 19 | + } |
| 20 | + |
| 21 | + return { tagName, props: newProps, children } |
| 22 | +} |
| 23 | + |
| 24 | +export const astro = async (node: HtmlNode, options: CodegenOptions) => { |
| 25 | + const root = editorSchemaToHast(node, { addSlotTagSyntax: true }) |
| 26 | + const { node: htmlNode, styles } = inlineStylesToStyleElement(root, options) |
| 27 | + // @ts-ignore |
| 28 | + const markup = stringifyHastNode(toH(h(options?.theme), htmlNode)) |
| 29 | + |
| 30 | + const htmlString = `${markup} |
| 31 | +${styles.trim() ? `\n<style>${styles}</style>\n` : ''}` |
| 32 | + const formattedHtmlString = await format('html', htmlString) |
| 33 | + |
| 34 | + const output = `--- |
| 35 | +--- |
| 36 | +
|
| 37 | +${formattedHtmlString}` |
| 38 | + |
| 39 | + return format('html', output) |
| 40 | +} |
0 commit comments