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.

feat: support GitHub flavored markdown paste (#5225)

Bogdan Chadkin0c9b138bbaae
diff --git a/apps/builder/app/shared/copy-paste/asset-upload.ts b/apps/builder/app/shared/copy-paste/asset-upload.ts
index d5504f9d2f..c8c7039ced 100644
--- a/apps/builder/app/shared/copy-paste/asset-upload.ts
+++ b/apps/builder/app/shared/copy-paste/asset-upload.ts
@@ -4,12 +4,6 @@ import { nanoid } from "nanoid";
import { produce, enablePatches, type Patch, applyPatches } from "immer";
enablePatches();
-
-type StringProp = Extract<
- WebstudioFragment["props"][number],
- { type: "string" }
->;
-
const extractSrcProps = (
data: WebstudioFragment
): [propId: string, href: string][] => {
@@ -22,17 +16,20 @@ const extractSrcProps = (
.map((instance) => instance.id)
);
- const srcProps = data.props
- .filter(
- (prop): prop is StringProp =>
- prop.type === "string" &&
- prop.name === "src" &&
- imageComponentsSet.has(prop.instanceId)
- )
- .map(
- (prop) =>
- [prop.id, new URL(prop.value).href] as [propId: string, href: string]
- );
+ const srcProps: [propId: string, href: string][] = [];
+ for (const prop of data.props) {
+ if (
+ prop.type === "string" &&
+ prop.name === "src" &&
+ imageComponentsSet.has(prop.instanceId)
+ ) {
+ try {
+ srcProps.push([prop.id, new URL(prop.value).href]);
+ } catch {
+ // ignore when invalid url
+ }
+ }
+ }
return srcProps;
};
diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.test.ts b/apps/builder/app/shared/copy-paste/plugin-markdown.test.ts
deleted file mode 100644
index 8ac9b01e8b..0000000000
--- a/apps/builder/app/shared/copy-paste/plugin-markdown.test.ts
+++ /dev/null
@@ -1,832 +0,0 @@
-import { describe, test, expect } from "vitest";
-import { __testing__ } from "./plugin-markdown";
-import { $breakpoints } from "../nano-states";
-
-const options = { generateId: () => "123" };
-
-const { parse } = __testing__;
-
-$breakpoints.set(new Map([["0", { id: "0", label: "base" }]]));
-
-describe("Plugin Markdown", () => {
- test("paragraph", () => {
- expect(parse("xyz", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "xyz",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("h1", () => {
- expect(parse("# heading", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "heading",
- },
- ],
- "component": "Heading",
- "id": "123",
- "tag": "h1",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("h6", () => {
- expect(parse("###### heading", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "heading",
- },
- ],
- "component": "Heading",
- "id": "123",
- "tag": "h6",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("bold 1", () => {
- expect(parse("__bold__", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "bold",
- },
- ],
- "component": "Bold",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("bold 2", () => {
- expect(parse("**bold**", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "bold",
- },
- ],
- "component": "Bold",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("italic 1", () => {
- expect(parse("_italic_", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "italic",
- },
- ],
- "component": "Italic",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("italic 2", () => {
- expect(parse("*italic*", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "italic",
- },
- ],
- "component": "Italic",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("link", () => {
- expect(parse('[link](/uri "Title")', options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "link",
- },
- ],
- "component": "RichTextLink",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [
- {
- "id": "123",
- "instanceId": "123",
- "name": "href",
- "type": "string",
- "value": "/uri",
- },
- {
- "id": "123",
- "instanceId": "123",
- "name": "title",
- "type": "string",
- "value": "Title",
- },
- ],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("image", () => {
- expect(parse('![foo](/url "title")', options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [],
- "component": "Image",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [
- {
- "id": "123",
- "instanceId": "123",
- "name": "src",
- "type": "string",
- "value": "/url",
- },
- {
- "id": "123",
- "instanceId": "123",
- "name": "title",
- "type": "string",
- "value": "title",
- },
- {
- "id": "123",
- "instanceId": "123",
- "name": "alt",
- "type": "string",
- "value": "foo",
- },
- ],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("hard line break", () => {
- expect(
- parse(
- `foo
- baz`,
- options
- )
- ).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "foo",
- },
- {
- "type": "text",
- "value": "baz",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("soft line break", () => {
- expect(
- parse(
- `foo
- baz`,
- options
- )
- ).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "foo
-baz",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("blockquote", () => {
- expect(parse("> bar", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "bar",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "Blockquote",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("inline code", () => {
- expect(parse("`foo`", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [],
- "component": "CodeText",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [
- {
- "id": "123",
- "instanceId": "123",
- "name": "code",
- "type": "string",
- "value": "foo",
- },
- ],
- "resources": [],
- "styleSourceSelections": [
- {
- "instanceId": "123",
- "values": [
- "123",
- ],
- },
- ],
- "styleSources": [
- {
- "id": "123",
- "type": "local",
- },
- ],
- "styles": [
- {
- "breakpointId": "0",
- "property": "display",
- "styleSourceId": "123",
- "value": {
- "type": "keyword",
- "value": "inline-block",
- },
- },
- ],
-}
-`);
- });
-
- test("code", () => {
- expect(parse("```js meta\nfoo\n```", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [],
- "component": "CodeText",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [
- {
- "id": "123",
- "instanceId": "123",
- "name": "code",
- "type": "string",
- "value": "foo",
- },
- {
- "id": "123",
- "instanceId": "123",
- "name": "lang",
- "type": "string",
- "value": "js",
- },
- ],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("list unordered", () => {
- expect(parse("- one", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "one",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "ListItem",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "List",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [
- {
- "id": "123",
- "instanceId": "123",
- "name": "ordered",
- "type": "boolean",
- "value": false,
- },
- ],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("list ordered", () => {
- expect(parse("3. one", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [
- {
- "type": "text",
- "value": "one",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "ListItem",
- "id": "123",
- "type": "instance",
- },
- {
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "component": "List",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [
- {
- "id": "123",
- "instanceId": "123",
- "name": "ordered",
- "type": "boolean",
- "value": true,
- },
- {
- "id": "123",
- "instanceId": "123",
- "name": "start",
- "type": "number",
- "value": 3,
- },
- ],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- test("thematic break | separator", () => {
- expect(parse("---", options)).toMatchInlineSnapshot(`
-{
- "assets": [],
- "breakpoints": [],
- "children": [
- {
- "type": "id",
- "value": "123",
- },
- ],
- "dataSources": [],
- "instances": [
- {
- "children": [],
- "component": "Separator",
- "id": "123",
- "type": "instance",
- },
- ],
- "props": [],
- "resources": [],
- "styleSourceSelections": [],
- "styleSources": [],
- "styles": [],
-}
-`);
- });
-
- // @todo For some reason doesn't work
- test.skip("strikethrough", () => {
- expect(
- parse("~One~ ~~two~~ ~~~three~~~.", options)
- ).toMatchInlineSnapshot();
- });
-});
diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.test.tsx b/apps/builder/app/shared/copy-paste/plugin-markdown.test.tsx
new file mode 100644
index 0000000000..e06730b735
--- /dev/null
+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.test.tsx
@@ -0,0 +1,221 @@
+import { test, expect } from "vitest";
+import { $, renderTemplate, ws } from "@webstudio-is/template";
+import { __testing__ } from "./plugin-markdown";
+
+const { parse } = __testing__;
+
+test("paragraph", () => {
+ expect(parse("xyz")).toEqual(
+ renderTemplate(<ws.element ws:tag="p">xyz</ws.element>)
+ );
+});
+
+test("h1", () => {
+ expect(parse("# heading")).toEqual(
+ renderTemplate(<ws.element ws:tag="h1">heading</ws.element>)
+ );
+});
+
+test("h6", () => {
+ expect(parse("###### heading")).toEqual(
+ renderTemplate(<ws.element ws:tag="h6">heading</ws.element>)
+ );
+});
+
+test("bold 1", () => {
+ expect(parse("__bold__")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <ws.element ws:tag="strong">bold</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("bold 2", () => {
+ expect(parse("**bold**")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <ws.element ws:tag="strong">bold</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("italic 1", () => {
+ expect(parse("_italic_")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <ws.element ws:tag="em">italic</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("italic 2", () => {
+ expect(parse("*italic*")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <ws.element ws:tag="em">italic</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("link", () => {
+ expect(parse('[link](/uri "Title")')).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <ws.element ws:tag="a" href="/uri" title="Title">
+ link
+ </ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("autolink", () => {
+ expect(parse("https://github.com")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <ws.element ws:tag="a" href="https://github.com">
+ https://github.com
+ </ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("image", () => {
+ expect(parse('![foo](/url "title")')).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <$.Image src="/url" alt="foo" title="title" />
+ </ws.element>
+ )
+ );
+});
+
+test("hard line break", () => {
+ expect(
+ parse(`foo
+ baz`)
+ ).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <ws.element ws:tag="span">foo</ws.element>
+ <ws.element ws:tag="br" />
+ <ws.element ws:tag="span"> baz</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("soft line break", () => {
+ expect(
+ parse(`foo
+ baz`)
+ ).toEqual(renderTemplate(<ws.element ws:tag="p">foo baz</ws.element>));
+});
+
+test("blockquote", () => {
+ expect(parse("> bar")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="blockquote">
+ <ws.element ws:tag="p">bar</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("inline code", () => {
+ expect(parse("`foo` and `bar`")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <ws.element ws:tag="code">foo</ws.element>
+ <ws.element ws:tag="span"> and </ws.element>
+ <ws.element ws:tag="code">bar</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("code", () => {
+ expect(parse("```js meta\nfoo\n```")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="pre">
+ <ws.element ws:tag="code" class="language-js">
+ {"foo "}
+ </ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("list unordered", () => {
+ expect(parse("- one")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="ul">
+ <ws.element ws:tag="li">one</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("list ordered", () => {
+ expect(parse("3. one")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="ol" start={3}>
+ <ws.element ws:tag="li">one</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("thematic break | separator", () => {
+ expect(parse("---")).toEqual(renderTemplate(<ws.element ws:tag="hr" />));
+});
+
+test("strikethrough", () => {
+ expect(parse("~One~ ~~two~~ ~~~three~~~.")).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="p">
+ <ws.element ws:tag="del">One</ws.element>
+ <ws.element ws:tag="del">two</ws.element>
+ <ws.element ws:tag="span"> ~~~three~~~.</ws.element>
+ </ws.element>
+ )
+ );
+});
+
+test("table", () => {
+ expect(
+ parse(`
+| Header 1 | Header 2 |
+|------------|------------|
+| Cell 1.1 | Cell 1.2 |
+| Cell 2.1 | Cell 2.2 |
+`)
+ ).toEqual(
+ renderTemplate(
+ <ws.element ws:tag="table">
+ <ws.element ws:tag="thead">
+ <ws.element ws:tag="tr">
+ <ws.element ws:tag="th">Header 1</ws.element>
+ <ws.element ws:tag="th">Header 2</ws.element>
+ </ws.element>
+ </ws.element>
+ <ws.element ws:tag="tbody">
+ <ws.element ws:tag="tr">
+ <ws.element ws:tag="td">Cell 1.1</ws.element>
+ <ws.element ws:tag="td">Cell 1.2</ws.element>
+ </ws.element>
+ <ws.element ws:tag="tr">
+ <ws.element ws:tag="td">Cell 2.1</ws.element>
+ <ws.element ws:tag="td">Cell 2.2</ws.element>
+ </ws.element>
+ </ws.element>
+ </ws.element>
+ )
+ );
+});
diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.ts b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
index b647d5fa28..9098178044 100644
--- a/apps/builder/app/shared/copy-paste/plugin-markdown.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
@@ -1,207 +1,26 @@
-import { nanoid } from "nanoid";
-import { fromMarkdown } from "mdast-util-from-markdown";
-import { gfmFromMarkdown } from "mdast-util-gfm";
-import { gfm } from "micromark-extension-gfm";
-import type {
- Breakpoint,
- Instance,
- WebstudioFragment,
-} from "@webstudio-is/sdk";
+import { gfm, gfmHtml } from "micromark-extension-gfm";
+import { micromark } from "micromark";
import { insertWebstudioFragmentAt } from "../instance-utils";
-import { $breakpoints } from "../nano-states";
-import { isBaseBreakpoint } from "../breakpoints";
import { denormalizeSrcProps } from "./asset-upload";
-
-const micromarkOptions = {
- extensions: [gfm()],
- mdastExtensions: [gfmFromMarkdown()],
-};
+import { generateFragmentFromHtml } from "../html";
export const mimeType = "text/plain";
-// @todo Definition, Strikethrough, Table
-const astTypeComponentMap: Record<string, Instance["component"]> = {
- paragraph: "Paragraph",
- heading: "Heading",
- strong: "Bold",
- emphasis: "Italic",
- link: "RichTextLink",
- // @todo image should not be rendered inside paragraph
- // we need to either have RichTextImage or support Image inside RichText
- image: "Image",
- blockquote: "Blockquote",
- code: "CodeText",
- // @todo same problem as with image
- inlineCode: "CodeText",
- list: "List",
- listItem: "ListItem",
- thematicBreak: "Separator",
-};
-
-type Options = { generateId?: typeof nanoid<string> };
-
-type Root = ReturnType<typeof fromMarkdown>;
-
-const toInstanceData = (
- data: WebstudioFragment,
- breakpointId: Breakpoint["id"],
- ast: { children: Root["children"] },
- options: Options = {}
-): Instance["children"] => {
- const { instances, props, styleSources, styleSourceSelections, styles } =
- data;
- const { generateId = nanoid } = options;
- const children: Instance["children"] = [];
-
- for (const child of ast.children) {
- if (child.type === "text") {
- children.push({ type: "text", value: child.value });
- continue;
- }
-
- const component = astTypeComponentMap[child.type];
- if (component === undefined) {
- continue;
- }
- const instanceId = generateId();
- const instance: Instance = {
- type: "instance",
- id: instanceId,
- component,
- children:
- "children" in child
- ? toInstanceData(data, breakpointId, child, options)
- : [],
- };
- instances.push(instance);
- children.push({ type: "id", value: instanceId });
-
- if (child.type === "heading") {
- instance.tag = `h${child.depth}`;
- }
- if (child.type === "link") {
- props.push({
- id: generateId(),
- type: "string",
- name: "href",
- instanceId,
- value: child.url,
- });
- }
- if (child.type === "image") {
- props.push({
- id: generateId(),
- type: "string",
- name: "src",
- instanceId,
- value: child.url,
- });
- }
- if (child.type === "inlineCode") {
- props.push({
- id: generateId(),
- type: "string",
- name: "code",
- instanceId,
- value: child.value,
- });
- const styleSourceId = generateId();
- styleSources.push({ type: "local", id: styleSourceId });
- styleSourceSelections.push({ instanceId, values: [styleSourceId] });
- styles.push({
- breakpointId,
- styleSourceId,
- property: "display",
- value: { type: "keyword", value: "inline-block" },
- });
- }
- if (child.type === "code") {
- props.push({
- id: generateId(),
- type: "string",
- name: "code",
- instanceId,
- value: child.value,
- });
- if (child.lang) {
- props.push({
- id: generateId(),
- type: "string",
- name: "lang",
- instanceId,
- value: child.lang,
- });
- }
- }
- if (child.type === "list") {
- if (typeof child.ordered === "boolean") {
- props.push({
- id: generateId(),
- type: "boolean",
- name: "ordered",
- instanceId,
- value: child.ordered,
- });
- }
- if (typeof child.start === "number") {
- props.push({
- id: generateId(),
- type: "number",
- name: "start",
- instanceId,
- value: child.start,
- });
- }
- }
-
- if ("title" in child && child.title) {
- props.push({
- id: generateId(),
- type: "string",
- name: "title",
- instanceId,
- value: child.title,
- });
+const parse = (clipboardData: string) => {
+ const html = micromark(clipboardData, "utf-8", {
+ extensions: [gfm()],
+ htmlExtensions: [gfmHtml()],
+ });
+ const fragment = generateFragmentFromHtml(html);
+ const instances = new Map(fragment.instances.map((item) => [item.id, item]));
+ for (const instance of fragment.instances) {
+ if (instance.tag === "img") {
+ delete instance.tag;
+ instance.component = "Image";
}
- if ("alt" in child && child.alt) {
- props.push({
- id: generateId(),
- type: "string",
- name: "alt",
- instanceId,
- value: child.alt,
- });
- }
- }
-
- return children;
-};
-
-const parse = (clipboardData: string, options?: Options) => {
- const ast = fromMarkdown(clipboardData, micromarkOptions);
- if (ast.children.length === 0) {
- return;
- }
- const breakpoints = $breakpoints.get();
- const breakpointValues = Array.from(breakpoints.values());
- const baseBreakpoint = breakpointValues.find(isBaseBreakpoint);
- if (baseBreakpoint === undefined) {
- return;
}
- const data: WebstudioFragment = {
- children: [],
- instances: [],
- props: [],
- breakpoints: [],
- styles: [],
- styleSources: [],
- styleSourceSelections: [],
- dataSources: [],
- resources: [],
- assets: [],
- };
- data.children = toInstanceData(data, baseBreakpoint.id, ast, options);
- return data;
+ fragment.instances = Array.from(instances.values());
+ return fragment;
};
export const onPaste = async (clipboardData: string) => {
diff --git a/apps/builder/package.json b/apps/builder/package.json
index c4ab53d2c1..d7cd588677 100644
--- a/apps/builder/package.json
+++ b/apps/builder/package.json
@@ -93,8 +93,6 @@
"isbot": "^5.1.25",
"lexical": "^0.21.0",
"match-sorter": "^8.0.0",
- "mdast-util-from-markdown": "^2.0.2",
- "mdast-util-gfm": "^3.1.0",
"micromark": "^4.0.2",
"micromark-extension-gfm": "^3.0.0",
"nanoevents": "^9.1.0",
diff --git a/packages/sdk-components-react/src/__generated__/markdown-embed.props.ts b/packages/sdk-components-react/src/__generated__/markdown-embed.props.ts
index 6924b6980d..c1244cb37a 100644
--- a/packages/sdk-components-react/src/__generated__/markdown-embed.props.ts
+++ b/packages/sdk-components-react/src/__generated__/markdown-embed.props.ts
@@ -1,11 +1,5 @@
import type { PropMeta } from "@webstudio-is/sdk";
export const props: Record<string, PropMeta> = {
- className: {
- required: false,
- control: "text",
- type: "string",
- description: "Classes to which the element belongs",
- },
code: { required: true, control: "text", type: "string" },
};
diff --git a/packages/sdk-components-react/src/markdown-embed.tsx b/packages/sdk-components-react/src/markdown-embed.tsx
index 47f94d7956..b48574ca48 100644
--- a/packages/sdk-components-react/src/markdown-embed.tsx
+++ b/packages/sdk-components-react/src/markdown-embed.tsx
@@ -1,10 +1,9 @@
import { micromark } from "micromark";
import { gfmTable, gfmTableHtml } from "micromark-extension-gfm-table";
-import { forwardRef, useMemo } from "react";
+import { forwardRef, useMemo, type ComponentProps } from "react";
-type MarkdownEmbedProps = {
+type MarkdownEmbedProps = ComponentProps<"div"> & {
code: string;
- className?: string;
// avoid builder passing it to dom
children?: never;
};
diff --git a/packages/template/src/jsx.ts b/packages/template/src/jsx.ts
index b0d7f5f49e..ba6d100c3b 100644
--- a/packages/template/src/jsx.ts
+++ b/packages/template/src/jsx.ts
@@ -110,37 +110,14 @@ const isChildValue = (child: unknown) =>
child instanceof PlaceholderValue ||
child instanceof Expression;
-const traverseJsx = (
- element: JSX.Element,
- callback: (
- element: JSX.Element,
- children: JSX.Element[]
- ) => Instance["children"][number]
-) => {
- const children = Array.isArray(element.props?.children)
- ? element.props?.children
- : element.props?.children
- ? [element.props?.children]
- : [];
- const result: Instance["children"] = [];
- if (element.type === Fragment) {
- for (const child of children) {
- if (isChildValue(child)) {
- continue;
- }
- result.push(...traverseJsx(child, callback));
- }
- return result;
+const getElementChildren = (element: JSX.Element): JSX.Element[] => {
+ if (Array.isArray(element.props?.children)) {
+ return element.props?.children;
}
- const child = callback(element, children);
- result.push(child);
- for (const child of children) {
- if (isChildValue(child)) {
- continue;
- }
- traverseJsx(child, callback);
+ if (element.props?.children) {
+ return [element.props?.children];
}
- return result;
+ return [];
};
export const renderTemplate = (
@@ -253,7 +230,9 @@ export const renderTemplate = (
});
return breakpointId;
};
- const children = traverseJsx(root, (element, children) => {
+ const convertElementToInstance = (
+ element: JSX.Element
+ ): Instance["children"][number] => {
const instanceId = element.props?.["ws:id"] ?? getId(element);
let tag: string | undefined;
for (const entry of Object.entries({ ...element.props })) {
@@ -347,7 +326,17 @@ export const renderTemplate = (
type: "instance",
id: instanceId,
component,
- children: children.map((child): Instance["children"][number] => {
+ children: [],
+ };
+ instances.push(instance);
+ if (element.props?.["ws:label"]) {
+ instance.label = element.props?.["ws:label"];
+ }
+ if (tag) {
+ instance.tag = tag;
+ }
+ instance.children = getElementChildren(element).map(
+ (child): Instance["children"][number] => {
if (typeof child === "string") {
return { type: "text", value: child };
}
@@ -358,18 +347,22 @@ export const renderTemplate = (
const expression = compileExpression(instanceId, child);
return { type: "expression", value: expression };
}
- return { type: "id", value: child.props?.["ws:id"] ?? getId(child) };
- }),
- };
- if (element.props?.["ws:label"]) {
- instance.label = element.props?.["ws:label"];
- }
- if (tag) {
- instance.tag = tag;
- }
- instances.push(instance);
+ return convertElementToInstance(child);
+ }
+ );
return { type: "id", value: instance.id };
- });
+ };
+ const children: Instance["children"] = [];
+ if (root.type === Fragment) {
+ for (const child of getElementChildren(root)) {
+ if (isChildValue(child)) {
+ continue;
+ }
+ children.push(convertElementToInstance(child));
+ }
+ } else {
+ children.push(convertElementToInstance(root));
+ }
return {
children,
instances,
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 69996902fa..8a2bcb1280 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -343,12 +343,6 @@ importers:
match-sorter:
specifier: ^8.0.0
version: 8.0.0
- mdast-util-from-markdown:
- specifier: ^2.0.2
- version: 2.0.2
- mdast-util-gfm:
- specifier: ^3.1.0
- version: 3.1.0
micromark:
specifier: ^4.0.2
version: 4.0.2
@@ -7443,9 +7437,6 @@ packages:
resolution: {integrity: sha512-WWC0ZuMzCyDHYCasEGs4IPvLyTGftYwh6wIEOULOF0HXcqZlhwRzrK0w2VUlxWA98xnvb/jszw4ZSkJ6ADpM6Q==}
engines: {node: '>=0.10.0'}
- resolution: {integrity: sha512-Z1NL3Tb1M9wH4XESsCDEksWoKTdlUafKc4pt0GRwjUyXaCFZ+dc3g2erqB6zm3szA2IUSi7VnPI+o/9jnxh9hw==}
-
resolution: {integrity: sha512-bGJ6Zb+OhzXe+ptP5d80OLVx7AkqfRbtGEh30vNSfjNwllu+hHI+tcbMIT/fbkx/FKN1PmKuDb65+Oofg+XUxw==}
@@ -7455,9 +7446,6 @@ packages:
resolution: {integrity: sha512-8SVPMuHqlPME/z3gqVwWY4zVXn8lqKv/pAhC57FuJ40ImXyBpmO5ukh98zB2v7Blql2FiHjHv9LVztSIqjY+MA==}
- resolution: {integrity: sha512-Tmd1Vg/m3Xz43afeNxDIhWRtFZgM2VLyaf4vSTYwudTyeuTneoL3qtWMA5jeLyz/O1vDJmmV4QuScFCA2tBPwg==}
-
resolution: {integrity: sha512-4xTO/M8c82qBcnQc1tgpNtubGUW/Y1tBQ1B0i5CtSoelOLKFYlElIr3bvgREYYO5iRqbMY1YuqZng0GVOI8Qww==}
@@ -7467,24 +7455,6 @@ packages:
resolution: {integrity: sha512-JjA2OjxRqAa8wEG8hloD0uTU0kdn8kbtOWpPP94NBkfAlbxn4S8gCGf/9DwFtEeGPXrDcNXdiDjVaRdUFqYokw==}
- resolution: {integrity: sha512-5HVP2MKaP6L+G6YaxPNjuL0BPrq9orG3TsrZ9YXbA3vDw/ACI4MEsnoDpn6ZNm7GnZgtAcONJyPhOP8tNJQavQ==}
-
- resolution: {integrity: sha512-sqpDWlsHn7Ac9GNZQMeUzPQSMzR6Wv0WKRNvQRg0KqHh02fpTz69Qc1QSseNX29bhz1ROIyNyxExfawVKTm1GQ==}
-
- resolution: {integrity: sha512-mKKb915TF+OC5ptj5bJ7WFRPdYtuHv0yTRxK2tJvi+BDqbkiG7h7u/9SI89nRAYcmap2xHQL9D+QG/6wSrTtXg==}
-
- resolution: {integrity: sha512-78UEvebzz/rJIxLvE7ZtDd/vIQ0RHv+3Mh5DR96p7cS7HsBhYIICDBCu8csTNWNO6tBWfqXPWekRuj2FNOGOZg==}
-
- resolution: {integrity: sha512-IrtvNvjxC1o06taBAVJznEnkiHxLFTzgonUdy8hzFVeDun0uTjxxrRGVaNFqkU1wJR3RBPEfsxmU6jDWPofrTQ==}
-
- resolution: {integrity: sha512-0ulfdQOM3ysHhCJ1p06l0b0VKlhU0wuQs3thxZQagjcjPrlFRqY215uZGHHJan9GEAXd9MbfPjFJz+qMkVR6zQ==}
-
resolution: {integrity: sha512-xIPmR5ReJDu/DHH1OoIT1HkuybIfRGYRywC+gJtI7qHjCJp/M9jrmBEJW22O8lskDWm562BX2W8TiAwRTb0rKA==}
@@ -7500,18 +7470,12 @@ packages:
resolution: {integrity: sha512-WmI1gTXUBJo4/ZmSk79Wcb2HcjPJBzM1nlI/OUWA8yk2X9ik3ffNbBGsU+09BFmXaL1IBb9fiuvq6/KMiNycSg==}
- resolution: {integrity: sha512-TqICwyvJJpBwvGAMZjj4J2n0X8QWp21b9l0o7eXyVJ25YNWYbJDVIyD1bZXE6WtV6RmKJVYmQAKWa0zWOABz2w==}
-
resolution: {integrity: sha512-pits93r8PhnIoU4Vy9bjW39M2jJ6/tdHyja9rrot9uujkN7UTU9SDnE6WNJz/IGyQk3XHX6yNNtrBH6cQzm8Hw==}
resolution: {integrity: sha512-bbv7TPv/WC49thZPg3jXuqzuvI45IL2EVAr/KxF0BSdHsU0ceFHOmwQn6evxAh1GaoK/6GQ1wp4R4oW2+LFL/A==}
- resolution: {integrity: sha512-xj68wMTvGXVOKonmog6LwyJKrYXZPvlwabaryTjLh9LuvovB/KAH+kvi8Gjj+7rJjsFi23nkUxRQv1KqSroMqA==}
-
resolution: {integrity: sha512-V4Zn/ncyN1QNSqSBxTrMOLpjr+IKdHl2v3KVLoWmDPscP4r9GcCi71gjgvUV1SFSKh92AjAG4peFuBl2/YgCJg==}
@@ -9257,9 +9221,6 @@ packages:
resolution: {integrity: sha512-MSd8OUGISqHdVvfY9TPhyK2VdUrPgxkUtWSuMHF6XAAFuL4LokseigBnZtPnJMu+FbynTkFNnFlyjxpVKujMRg==}
- resolution: {integrity: sha512-MR04uvD+07cwl/yhVuVWAtw+3GOR/knlL55Nd/wAdblk27GCVt3lqpTivy/tkJcZoNPzTwS1Y+KMojlLDhoTzg==}
-
resolution: {integrity: sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==}
engines: {node: '>= 4.0.0'}
@@ -15328,8 +15289,6 @@ snapshots:
-
dependencies:
'@babel/runtime': 7.25.0
@@ -15347,13 +15306,6 @@ snapshots:
'@types/unist': 2.0.6
unist-util-visit: 4.1.2
- dependencies:
- '@types/mdast': 4.0.4
- escape-string-regexp: 5.0.0
- unist-util-is: 6.0.0
- unist-util-visit-parents: 6.0.1
-
dependencies:
'@types/mdast': 3.0.15
@@ -15394,63 +15346,6 @@ snapshots:
mdast-util-to-markdown: 1.5.0
micromark-extension-frontmatter: 1.1.1
- dependencies:
- '@types/mdast': 4.0.4
- ccount: 2.0.1
- devlop: 1.1.0
- mdast-util-find-and-replace: 3.0.2
- micromark-util-character: 2.1.1
-
- dependencies:
- '@types/mdast': 4.0.4
- devlop: 1.1.0
- mdast-util-from-markdown: 2.0.2
- mdast-util-to-markdown: 2.1.2
- micromark-util-normalize-identifier: 2.0.1
- transitivePeerDependencies:
- - supports-color
-
- dependencies:
- '@types/mdast': 4.0.4
- mdast-util-from-markdown: 2.0.2
- mdast-util-to-markdown: 2.1.2
- transitivePeerDependencies:
- - supports-color
-
- dependencies:
- '@types/mdast': 4.0.4
- devlop: 1.1.0
- markdown-table: 3.0.3
- mdast-util-from-markdown: 2.0.2
- mdast-util-to-markdown: 2.1.2
- transitivePeerDependencies:
- - supports-color
-
- dependencies:
- '@types/mdast': 4.0.4
- devlop: 1.1.0
- mdast-util-from-markdown: 2.0.2
- mdast-util-to-markdown: 2.1.2
- transitivePeerDependencies:
- - supports-color
-
- dependencies:
- mdast-util-from-markdown: 2.0.2
- mdast-util-gfm-autolink-literal: 2.0.1
- mdast-util-gfm-footnote: 2.1.0
- mdast-util-gfm-strikethrough: 2.0.0
- mdast-util-gfm-table: 2.0.0
- mdast-util-gfm-task-list-item: 2.0.0
- mdast-util-to-markdown: 2.1.2
- transitivePeerDependencies:
- - supports-color
-
dependencies:
'@types/estree-jsx': 1.0.0
@@ -15503,11 +15398,6 @@ snapshots:
'@types/mdast': 3.0.15
unist-util-is: 5.2.0
- dependencies:
- '@types/mdast': 4.0.4
- unist-util-is: 6.0.0
-
dependencies:
'@types/hast': 2.3.4
@@ -15530,18 +15420,6 @@ snapshots:
unist-util-visit: 4.1.2
zwitch: 2.0.4
- dependencies:
- '@types/mdast': 4.0.4
- '@types/unist': 3.0.0
- longest-streak: 3.1.0
- mdast-util-phrasing: 4.1.0
- mdast-util-to-string: 4.0.0
- micromark-util-classify-character: 2.0.1
- micromark-util-decode-string: 2.0.1
- unist-util-visit: 5.0.0
- zwitch: 2.0.4
-
dependencies:
'@types/mdast': 3.0.15
@@ -17586,12 +17464,6 @@ snapshots:
unist-util-is: 5.2.0
unist-util-visit-parents: 5.1.3
- dependencies:
- '@types/unist': 3.0.0
- unist-util-is: 6.0.0
- unist-util-visit-parents: 6.0.1
-
optional: true