summaryrefslogtreecommitdiff
path: root/src/frontend/render/utils.ts
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2021-04-30 16:33:35 -0500
committerGravatar GitHub <noreply@github.com> 2021-04-30 16:33:35 -0500
commit4df1347156cf2632ea2f3475d3a5f8f08d197cc3 (patch)
tree9d50de89dfe62827c32a8a4046120af4ab61dc0c /src/frontend/render/utils.ts
parent1d498facc8f78a3ffbfecd05cc6ecd45e8a4a1ae (diff)
downloadastro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.tar.gz
astro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.tar.zst
astro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.zip
Migrate to `yarn` monorepo (#157)
* chore: use monorepo * chore: scaffold astro-scripts * chore: move tests inside packages/astro * chore: refactor tests, add scripts * chore: move parser to own module * chore: move runtime to packages/astro * fix: move parser to own package * test: fix prettier-plugin-astro tests * fix: tests * chore: update package-lock * chore: add changesets * fix: cleanup examples * fix: starter example * chore: update changeset config * chore: update changeset config * chore: setup changeset release workflow * chore: bump lockfiles * chore: prism => astro-prism * fix: tsc --emitDeclarationOnly * chore: final cleanup, switch to yarn * chore: add lerna * chore: update workflows to yarn * chore: update workflows * chore: remove lint workflow * chore: add astro-dev script * chore: add symlinked README
Diffstat (limited to 'src/frontend/render/utils.ts')
-rw-r--r--src/frontend/render/utils.ts52
1 files changed, 0 insertions, 52 deletions
diff --git a/src/frontend/render/utils.ts b/src/frontend/render/utils.ts
deleted file mode 100644
index 9d55626fe..000000000
--- a/src/frontend/render/utils.ts
+++ /dev/null
@@ -1,52 +0,0 @@
-import unified from 'unified';
-import parse from 'rehype-parse';
-import toH from 'hast-to-hyperscript';
-import { ComponentRenderer } from '../../@types/renderer';
-import moize from 'moize';
-
-/** @internal */
-function childrenToTree(children: string[]) {
- return children.map((child) => (unified().use(parse, { fragment: true }).parse(child) as any).children.pop());
-}
-
-/**
- * Converts an HTML fragment string into vnodes for rendering via provided framework
- * @param h framework's `createElement` function
- * @param children the HTML string children
- */
-export const childrenToVnodes = moize.deep(function childrenToVnodes(h: any, children: string[]) {
- const tree = childrenToTree(children);
- const vnodes = tree.map((subtree) => {
- if (subtree.type === 'text') return subtree.value;
- return toH(h, subtree);
- });
- return vnodes;
-});
-
-/**
- * Converts an HTML fragment string into h function calls as a string
- * @param h framework's `createElement` function
- * @param children the HTML string children
- */
-export const childrenToH = moize.deep(function childrenToH(renderer: ComponentRenderer<any>, children: string[]): any {
- if (!renderer.jsxPragma) return;
- const tree = childrenToTree(children);
- const innerH = (name: any, attrs: Record<string, any> | null = null, _children: string[] | null = null) => {
- const vnode = renderer.jsxPragma?.(name, attrs, _children);
- const childStr = _children ? `, [${_children.map((child) => serializeChild(child)).join(',')}]` : '';
- /* fix(react): avoid hard-coding keys into the serialized tree */
- if (attrs && attrs.key) attrs.key = undefined;
- const __SERIALIZED = `${renderer.jsxPragmaName}("${name}", ${attrs ? JSON.stringify(attrs) : 'null'}${childStr})` as string;
- return { ...vnode, __SERIALIZED };
- };
- const serializeChild = (child: unknown) => {
- if (['string', 'number', 'boolean'].includes(typeof child)) return JSON.stringify(child);
- if (child === null) return `null`;
- if ((child as any).__SERIALIZED) return (child as any).__SERIALIZED;
- return innerH(child).__SERIALIZED;
- };
- return tree.map((subtree) => {
- if (subtree.type === 'text') return JSON.stringify(subtree.value);
- return toH(innerH, subtree).__SERIALIZED
- });
-});