Skip to content

Commit c0e13b8

Browse files
committed
feat: astro export support
1 parent f6d919e commit c0e13b8

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

packages/gui/src/lib/codegen/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './theme-ui'
66
export * from './emotion'
77
export * from './styled-jsx'
88
export * from './enhance-sfc'
9+
export * from './astro'

0 commit comments

Comments
 (0)