Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: JSX attributes parsing (#2435)

Giuseppea054b4cf6d6b
diff --git a/apps/builder/app/routes/rest.ai.ts b/apps/builder/app/routes/rest.ai.ts
index 351a9e9974..9001b9201a 100644
--- a/apps/builder/app/routes/rest.ai.ts
+++ b/apps/builder/app/routes/rest.ai.ts
@@ -122,10 +122,10 @@ export const action = async ({ request }: ActionArgs) => {
// so that the LLM won't miscategorize the task and for example handle an "edit-style" request as a "write-copy" one.
// During testing the LLM misdetected a stylistic request like "make it bold" as a request to rewrite the text to sound more "bold".
"write-copy",
- "generate-images",
+ "generate-user-interface",
"edit-styles",
- "generate-ui",
"delete-elements",
+ "generate-images-only",
],
},
});
diff --git a/packages/ai/package.json b/packages/ai/package.json
index 1cf44c6c8c..ffe2dad958 100644
--- a/packages/ai/package.json
+++ b/packages/ai/package.json
@@ -48,7 +48,9 @@
"@webstudio-is/sdk": "workspace:*",
"ai": "^2.2.12",
"escape-string-regexp": "^5.0.0",
+ "mdast-util-from-markdown": "^2.0.0",
"openai": "^4.8.0",
+ "unist-util-visit-parents": "^6.0.1",
"web-streams-polyfill": "^3.2.1",
"zod": "^3.21.4",
"zod-to-json-schema": "^3.21.4"
diff --git a/packages/ai/src/chains/template-generator/chain.ts b/packages/ai/src/chains/template-generator/chain.ts
index 077037872e..eb2529b552 100644
--- a/packages/ai/src/chains/template-generator/chain.ts
+++ b/packages/ai/src/chains/template-generator/chain.ts
@@ -9,6 +9,7 @@ import {
postProcessTemplate,
} from "../../utils/jsx-to-template.server";
import { createErrorResponse } from "../../utils/create-error-response";
+import { getCode } from "../../utils/get-code";
/**
* Template Generator Chain.
@@ -69,7 +70,7 @@ export const createChain = <ModelMessageFormat>(): Chain<
let template: WsEmbedTemplate;
try {
- template = await jsxToTemplate(completionText);
+ template = await jsxToTemplate(getCode(completionText, "jsx"));
} catch (error) {
return {
id,
diff --git a/packages/ai/src/utils/get-code.ts b/packages/ai/src/utils/get-code.ts
new file mode 100644
index 0000000000..4262f459fb
--- /dev/null
+++ b/packages/ai/src/utils/get-code.ts
@@ -0,0 +1,22 @@
+import { fromMarkdown as parseMarkdown } from "mdast-util-from-markdown";
+import { visitParents } from "unist-util-visit-parents";
+
+export const getCode = function getCode(text: string, lang: string) {
+ const tree = parseMarkdown(text);
+ let code = text;
+ const codeBlocks: string[] = [];
+
+ visitParents(tree, "code", (node) => {
+ if (node.lang === lang) {
+ codeBlocks.unshift(node.value.trim());
+ } else if (!node.lang) {
+ codeBlocks.push(node.value.trim());
+ }
+ });
+
+ if (codeBlocks.length > 0) {
+ code = codeBlocks[0];
+ }
+
+ return code;
+};
diff --git a/packages/jsx-utils/src/jsx.test.ts b/packages/jsx-utils/src/jsx.test.ts
index a52396c44e..bad5cf736b 100644
--- a/packages/jsx-utils/src/jsx.test.ts
+++ b/packages/jsx-utils/src/jsx.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, test } from "@jest/globals";
-import { WsEmbedTemplate } from "@webstudio-is/react-sdk";
+import { EmbedTemplateProp, WsEmbedTemplate } from "@webstudio-is/react-sdk";
import { jsxToWSEmbedTemplate } from "./jsx";
+import { traverseTemplate } from ".";
describe("jsx", () => {
test("valid template", async () => {
@@ -61,6 +62,64 @@ describe("jsx", () => {
"type": "instance",
},
]
+`);
+ });
+
+ test("parses props values correctly", async () => {
+ const parsed = await jsxToWSEmbedTemplate(`
+
+
+ <Box className="flex flex-col items-center justify-center bg-white text-black p-10">
+ <Image src="" alt="Aromatic coffee in a cup" width="600" height="400" className="rounded-lg shadow-lg mb-6"/>
+</Box>
+
+`);
+ expect(() => WsEmbedTemplate.parse(parsed)).not.toThrow();
+ const props: EmbedTemplateProp[][] = [];
+
+ traverseTemplate(parsed, (node) => {
+ if (node.type === "instance" && node.props) {
+ props.push(node.props);
+ }
+ });
+
+ expect(props).toMatchInlineSnapshot(`
+[
+ [
+ {
+ "name": "className",
+ "type": "string",
+ "value": "flex flex-col items-center justify-center bg-white text-black p-10",
+ },
+ ],
+ [
+ {
+ "name": "src",
+ "type": "string",
+ "value": "",
+ },
+ {
+ "name": "alt",
+ "type": "string",
+ "value": "Aromatic coffee in a cup",
+ },
+ {
+ "name": "width",
+ "type": "number",
+ "value": 600,
+ },
+ {
+ "name": "height",
+ "type": "number",
+ "value": 400,
+ },
+ {
+ "name": "className",
+ "type": "string",
+ "value": "rounded-lg shadow-lg mb-6",
+ },
+ ],
+]
`);
});
});
diff --git a/packages/jsx-utils/src/jsx.ts b/packages/jsx-utils/src/jsx.ts
index 7e4e813546..519299a7ce 100644
--- a/packages/jsx-utils/src/jsx.ts
+++ b/packages/jsx-utils/src/jsx.ts
@@ -14,12 +14,11 @@ import type { JsonObject } from "type-fest";
export const jsxToWSEmbedTemplate = async (
code: string
): Promise<WsEmbedTemplate> => {
- const ast = parser.parseExpression(
- code.replace(/^```(jsx)?/, "").replace(/```$/, ""),
- {
- plugins: ["jsx"],
- }
- );
+ code = code.trim();
+
+ const ast = parser.parseExpression(code, {
+ plugins: ["jsx"],
+ });
if (ast.type === "JSXElement") {
const template = await transform(ast, code);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b7023f1e22..b14220cbd8 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -687,9 +687,15 @@ importers:
escape-string-regexp:
specifier: ^5.0.0
version: 5.0.0
+ mdast-util-from-markdown:
+ specifier: ^2.0.0
+ version: 2.0.0
openai:
specifier: ^4.8.0
version: 4.8.0
+ unist-util-visit-parents:
+ specifier: ^6.0.1
+ version: 6.0.1
web-streams-polyfill:
specifier: ^3.2.1
version: 3.2.1