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.

refactor: switch to lezer highlight (#4347)

Bogdan Chadkinf987b5022093
diff --git a/apps/builder/app/builder/features/navigator/css-preview.tsx b/apps/builder/app/builder/features/navigator/css-preview.tsx
index 65888c4660..6f9f4b0f63 100644
--- a/apps/builder/app/builder/features/navigator/css-preview.tsx
+++ b/apps/builder/app/builder/features/navigator/css-preview.tsx
@@ -17,8 +17,8 @@ import {
import type { StyleMap, StyleProperty } from "@webstudio-is/css-engine";
import { CollapsibleSection } from "~/builder/shared/collapsible-section";
import { useMemo } from "react";
-import Prism from "prismjs";
import { captureError } from "@webstudio-is/error-utils";
+import { highlightCss } from "~/builder/shared/code-highlight";
const preStyle = css(textVariants.mono, {
margin: 0,
@@ -92,7 +92,7 @@ const useHighlightedCss = () => {
return;
}
const cssText = getCssText(currentStyle);
- return Prism.highlight(cssText, Prism.languages.css, "css");
+ return highlightCss(cssText);
}, [currentStyle]);
};
@@ -108,7 +108,6 @@ export const CssPreview = () => {
<ScrollArea css={{ padding: theme.panel.padding }}>
<pre tabIndex={0} className={preStyle()}>
<div
- className="language-css"
style={{ whiteSpace: "break-spaces" }}
dangerouslySetInnerHTML={{ __html: code }}
></div>
diff --git a/apps/builder/app/builder/shared/code-editor-base.tsx b/apps/builder/app/builder/shared/code-editor-base.tsx
index 31c8911bd4..ad648b00d9 100644
--- a/apps/builder/app/builder/shared/code-editor-base.tsx
+++ b/apps/builder/app/builder/shared/code-editor-base.tsx
@@ -26,8 +26,7 @@ import {
historyKeymap,
indentWithTab,
} from "@codemirror/commands";
-import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";
-import { tags } from "@lezer/highlight";
+import { syntaxHighlighting } from "@codemirror/language";
import {
theme,
textVariants,
@@ -44,6 +43,7 @@ import {
rawTheme,
} from "@webstudio-is/design-system";
import { CrossIcon, MaximizeIcon, MinimizeIcon } from "@webstudio-is/icons";
+import { solarizedLight } from "./code-highlight";
// This undocumented flag is required to keep contenteditable fields editable after the first activation of EditorView.
// To reproduce the issue, open any Binding dialog and then try to edit a Navigation Item in the Navigation menu.
@@ -118,39 +118,6 @@ const editorContentStyle = css({
},
});
-// https://thememirror.net/clouds
-const highlightStyle = HighlightStyle.define([
- // darker comment variant from https://github.com/vadimdemedes/thememirror/blob/main/source/themes/ayu-light.ts#L17-L20
- {
- tag: [tags.comment, tags.annotation],
- color: "#787b8099",
- },
- {
- tag: [tags.string, tags.special(tags.brace), tags.regexp, tags.url],
- color: "#5D90CD",
- },
- {
- tag: [tags.number, tags.bool, tags.null],
- color: "#46A609",
- },
- {
- tag: tags.keyword,
- color: "#AF956F",
- },
- {
- tag: [tags.definitionKeyword, tags.modifier],
- color: "#C52727",
- },
- {
- tag: [tags.angleBracket, tags.tagName, tags.attributeName, tags.separator],
- color: "#606060",
- },
- {
- tag: tags.self,
- color: "#000",
- },
-]);
-
const autocompletionTooltipTheme = EditorView.theme({
".cm-tooltip.cm-tooltip-autocomplete": {
...textVariants.mono,
@@ -262,7 +229,7 @@ export const EditorContent = ({
history(),
drawSelection(),
dropCursor(),
- syntaxHighlighting(highlightStyle, { fallback: true }),
+ syntaxHighlighting(solarizedLight, { fallback: true }),
keymap.of([...defaultKeymap, ...historyKeymap, indentWithTab]),
EditorView.lineWrapping,
EditorView.editable.of(readOnly === false),
diff --git a/apps/builder/app/builder/shared/code-highlight.ts b/apps/builder/app/builder/shared/code-highlight.ts
new file mode 100644
index 0000000000..e9722f7c9e
--- /dev/null
+++ b/apps/builder/app/builder/shared/code-highlight.ts
@@ -0,0 +1,85 @@
+import { HighlightStyle } from "@codemirror/language";
+import { highlightCode, tags } from "@lezer/highlight";
+import { parser } from "@lezer/css";
+
+// inspired by https://thememirror.net/solarized-light
+export const solarizedLight = HighlightStyle.define([
+ {
+ tag: tags.comment,
+ color: "#93A1A1",
+ },
+ {
+ tag: tags.string,
+ color: "#2AA198",
+ },
+ {
+ tag: tags.regexp,
+ color: "#D30102",
+ },
+ {
+ tag: tags.number,
+ color: "#D33682",
+ },
+ {
+ tag: tags.variableName,
+ color: "#268BD2",
+ },
+ {
+ tag: [tags.keyword, tags.operator, tags.punctuation],
+ color: "#859900",
+ },
+ {
+ tag: [tags.definitionKeyword, tags.modifier],
+ color: "#073642",
+ },
+ {
+ tag: [tags.self, tags.definition(tags.propertyName)],
+ color: "#268BD2",
+ },
+ {
+ tag: tags.function(tags.variableName),
+ color: "#268BD2",
+ },
+ {
+ tag: [tags.bool, tags.null],
+ color: "#B58900",
+ },
+ {
+ tag: tags.tagName,
+ color: "#268BD2",
+ },
+ {
+ tag: tags.angleBracket,
+ color: "#93A1A1",
+ },
+ {
+ tag: tags.attributeName,
+ color: "#93A1A1",
+ },
+ {
+ tag: tags.typeName,
+ color: "#859900",
+ },
+]);
+
+export const highlightCss = (code: string) => {
+ const styles = solarizedLight.module?.getRules();
+ // generated classes are scoped to parent
+ let highlightedCode = `<style>@scope {${styles}}</style>`;
+ highlightCode(
+ code,
+ parser.parse(code),
+ solarizedLight,
+ (text, classes) => {
+ if (classes) {
+ highlightedCode += `<span class="${classes}">${text}</span>`;
+ } else {
+ highlightedCode += text;
+ }
+ },
+ () => {
+ highlightedCode += "\n";
+ }
+ );
+ return highlightedCode;
+};
diff --git a/apps/builder/app/routes/_ui.(builder).tsx b/apps/builder/app/routes/_ui.(builder).tsx
index d029d409c4..f8882463ba 100644
--- a/apps/builder/app/routes/_ui.(builder).tsx
+++ b/apps/builder/app/routes/_ui.(builder).tsx
@@ -26,7 +26,6 @@ import { dashboardPath, isBuilder, isDashboard } from "~/shared/router-utils";
import env from "~/env/env.server";
import builderStyles from "~/builder/builder.css?url";
-import prismStyles from "prismjs/themes/prism-solarizedlight.min.css?url";
import { ClientOnly } from "~/shared/client-only";
import { parseBuilderUrl } from "@webstudio-is/http-client";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
@@ -40,10 +39,7 @@ import { loader as authWsLoader } from "./auth.ws";
export { ErrorBoundary } from "~/shared/error/error-boundary";
export const links = () => {
- return [
- { rel: "stylesheet", href: builderStyles },
- { rel: "stylesheet", href: prismStyles },
- ];
+ return [{ rel: "stylesheet", href: builderStyles }];
};
export const meta: MetaFunction<typeof loader> = ({ data }) => {
diff --git a/apps/builder/package.json b/apps/builder/package.json
index 3dbec9532c..71f06d1a7b 100644
--- a/apps/builder/package.json
+++ b/apps/builder/package.json
@@ -37,6 +37,7 @@
"@lexical/selection": "^0.16.0",
"@lexical/utils": "^0.16.0",
"@lezer/common": "^1.2.2",
+ "@lezer/css": "^1.1.9",
"@lezer/highlight": "^1.2.1",
"@nanostores/react": "^0.7.1",
"@radix-ui/react-select": "^2.1.2",
@@ -98,7 +99,6 @@
"nanostores": "^0.9.3",
"picocolors": "^1.1.0",
"pretty-bytes": "^6.1.1",
- "prismjs": "^1.29.0",
"react": "18.3.0-canary-14898b6a9-20240318",
"react-colorful": "^5.6.1",
"react-dom": "18.3.0-canary-14898b6a9-20240318",
@@ -129,7 +129,6 @@
"@types/cookie": "^0.6.0",
"@types/debug": "^4.1.12",
"@types/dom-navigation": "^1.0.3",
- "@types/prismjs": "^1.26.0",
"@types/react": "^18.2.70",
"@types/react-dom": "^18.2.25",
"@webstudio-is/jest-config": "workspace:*",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 0536e81d21..3ffc8acd64 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -184,6 +184,9 @@ importers:
'@lezer/common':
specifier: ^1.2.2
version: 1.2.2
+ '@lezer/css':
+ specifier: ^1.1.9
+ version: 1.1.9
'@lezer/highlight':
specifier: ^1.2.1
version: 1.2.1
@@ -367,9 +370,6 @@ importers:
pretty-bytes:
specifier: ^6.1.1
version: 6.1.1
- prismjs:
- specifier: ^1.29.0
- version: 1.29.0
react:
specifier: 18.3.0-canary-14898b6a9-20240318
version: 18.3.0-canary-14898b6a9-20240318
@@ -455,9 +455,6 @@ importers:
'@types/dom-navigation':
specifier: ^1.0.3
version: 1.0.3
- '@types/prismjs':
- specifier: ^1.26.0
- version: 1.26.0
'@types/react':
specifier: ^18.2.70
version: 18.2.79
@@ -5223,9 +5220,6 @@ packages:
resolution: {integrity: sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==}
- '@types/[email protected]':
- resolution: {integrity: sha512-ZTaqn/qSqUuAq1YwvOFQfVW1AR/oQJlLSZVustdjwI+GZ8kr0MSHBj0tsXPW1EqHubx50gtBEjbPGsdZwQwCjQ==}
-
resolution: {integrity: sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==}
@@ -12883,8 +12877,6 @@ snapshots:
'@types/[email protected]': {}
- '@types/[email protected]': {}
-
'@types/[email protected]': {}
'@types/[email protected]': {}