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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
feat: Google Search Preview block (#2526)
Ivan Starkovd17a899b008d
diff --git a/apps/builder/app/builder/features/sidebar-left/panels/pages/search-preview.stories.tsx b/apps/builder/app/builder/features/sidebar-left/panels/pages/search-preview.stories.tsxnew file mode 100644index 0000000000..24145b8707--- /dev/null+++ b/apps/builder/app/builder/features/sidebar-left/panels/pages/search-preview.stories.tsx@@ -0,0 +1,16 @@+import { Box } from "@webstudio-is/design-system";+import { SearchPreview } from "./search-preview";++export default { component: SearchPreview };++export const Basic = () => (+ <Box css={{ width: 600, margin: 20 }}>+ <SearchPreview+ pageUrl="https://webstudio.is/blog-webstudios-architecture-an-overview"+ title="Blog: Webstudio's Architecture - an overview"+ siteName="Webstudio"+ faviconUrl="https://webstudio.is/favicon.ico"+ description="This is an introduction for developers who want to contribute to Webstudio Core. However, it may also be an interesting read for ambitious designers who want to better understand the future of Webstudio and Visual Development."+ />+ </Box>+);diff --git a/apps/builder/app/builder/features/sidebar-left/panels/pages/search-preview.test.ts b/apps/builder/app/builder/features/sidebar-left/panels/pages/search-preview.test.tsnew file mode 100644index 0000000000..fdd5dde1d4--- /dev/null+++ b/apps/builder/app/builder/features/sidebar-left/panels/pages/search-preview.test.ts@@ -0,0 +1,61 @@+/* eslint-disable no-irregular-whitespace */+import { expect, test } from "@jest/globals";+import { truncateMetaText, truncateUrl } from "./search-preview";++test("Truncates description", () => {+ expect(+ truncateMetaText(+ "Accept everyone around you. Part of being a good person is not being judgmental. You accept everyone, no matter what race, age, sexual orientation, gender identity, or culture they are. Realize that everybody has feelings, every person is valid, and everyone should always be treated with respect.\n\nBe respectful of elderly people. "+ )+ ).toMatchInlineSnapshot(+ `"Accept everyone around you. Part of being a good person is not being judgmental. You accept everyone, no matter what race, age, sexual orientation, gender ..."`+ );+});++test("Truncates description", () => {+ expect(+ truncateMetaText(+ "The best way to determine the best balance for you is by learning to check in with your inner compass — and your results. With intentionality and a little creativity, you can recalibrate your expectations and reset your work-home balance. "+ )+ ).toMatchInlineSnapshot(+ `"The best way to determine the best balance for you is by learning to check in with your inner compass — and your results. With intentionality and a little ..."`+ );+});++test("Truncates description", () => {+ expect(+ truncateMetaText(+ "The meta description summarizes a page’s content and presents that to users in the search results. It’s one of the first things people will likely see when searching for something, so optimizing it is crucial for SEO. It’s your chance to persuade users to click on "+ )+ ).toMatchInlineSnapshot(+ `"The meta description summarizes a page’s content and presents that to users in the search results. It’s one of the first things people will ..."`+ );+});++test("Truncates description", () => {+ expect(+ truncateMetaText(+ "A meta description (also known as a “description tag”) is an HTML attribute designed to describe the content of a webpage. Here’s what a meta description looks like in HTML form:"+ )+ ).toMatchInlineSnapshot(+ `"A meta description (also known as a “description tag”) is an HTML attribute designed to describe the content of a webpage. Here’s what a meta description ..."`+ );+});++test("Truncates url", () => {+ expect(+ truncateUrl("https://ahrefs.com/writing-tools/meta-description-generator")+ ).toMatchInlineSnapshot(+ `"https://ahrefs.com/writing-tools/meta-description-gen..."`+ );+});++test("Truncates url", () => {+ expect(+ truncateUrl(+ "https://university.webflow.com›lesson›seo-title-meta-description"+ )+ ).toMatchInlineSnapshot(+ `"https://university.webflow.com›lesson›seo-title-meta-..."`+ );+});diff --git a/apps/builder/app/builder/features/sidebar-left/panels/pages/search-preview.tsx b/apps/builder/app/builder/features/sidebar-left/panels/pages/search-preview.tsxnew file mode 100644index 0000000000..79dc266528--- /dev/null+++ b/apps/builder/app/builder/features/sidebar-left/panels/pages/search-preview.tsx@@ -0,0 +1,155 @@+import { Flex, Grid } from "@webstudio-is/design-system";+import { Image, createImageLoader } from "@webstudio-is/image";++/**+ * Exact google truncation logic is not known, but this is a close approximation+ */+export const truncateMetaText = (+ description: string,+ maxLength: number = 155+) => {+ if (description.length <= maxLength) {+ return description;+ }++ const ellipsis = "\u00A0...";+ let truncated = description.substring(0, maxLength);+ const lastSpaceIndex = truncated.lastIndexOf(" ");++ // If there's a space to truncate at, use it; otherwise, use the max length+ truncated =+ lastSpaceIndex > 0 ? truncated.substring(0, lastSpaceIndex) : truncated;++ return `${truncated}${ellipsis}`;+};++export const truncateUrl = (pageUrl: string, maxLength = 53) => {+ if (pageUrl.length <= maxLength) {+ return pageUrl;+ }++ const ellipsis = "...";+ const truncated = pageUrl.substring(0, maxLength);++ return `${truncated}${ellipsis}`;+};++const formatUrl = (urlString: string) => {+ const url = new URL(urlString);++ return `${url.origin}${url.pathname.split("/").join(" › ")}`;+};++type SearchPreviewProps = {+ /**+ * The URL of the page to preview in search results+ */+ pageUrl: string;+ title: string;+ description: string;+ siteName: string;+ faviconUrl: string;+};++/**+ * ... rotated 90 degrees+ */+const VerticalThreePointIcon = () => (+ <svg+ height="18"+ focusable="false"+ xmlns="http://www.w3.org/2000/svg"+ viewBox="0 0 24 24"+ >+ <path d="M12 8c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"></path>+ </svg>+);++const loader = createImageLoader({ imageBaseUrl: "" });++export const SearchPreview = (props: SearchPreviewProps) => {+ return (+ <Grid+ gap={1}+ css={{+ fontFamily: "arial,sans-serif",+ "& *": {+ boxSizing: "border-box",+ },+ }}+ >+ <Flex+ align={"center"}+ css={{+ gap: 12,+ }}+ >+ <Flex+ css={{+ width: 28,+ height: 28,+ borderRadius: "50%",+ backgroundColor: "#f1f3f4",+ border: "1px solid #ecedef",+ }}+ justify={"center"}+ align={"center"}+ >+ <Image+ width={18}+ height={18}+ loader={loader}+ src={props.faviconUrl}+ />+ </Flex>+ <Grid>+ <Flex+ css={{+ color: "#202124",+ fontSize: "14px",+ lineHeight: "20px",+ whiteSpace: "nowrap",+ }}+ >+ {truncateMetaText(props.siteName)}+ </Flex>+ <Flex+ css={{+ fontSize: "12px",+ lineHeight: "18px",+ color: "#4d5156",+ gap: 8,+ }}+ align={"center"}+ >+ {/*todo add > instead of / */ formatUrl(truncateUrl(props.pageUrl))}+ <VerticalThreePointIcon />+ </Flex>+ </Grid>+ </Flex>+ <div />+ <Flex+ css={{+ fontSize: "20px",+ fontWeight: 400,+ color: "#1a0dab",+ }}+ >+ {truncateMetaText(props.title, 60)}+ </Flex>+ <Flex+ css={{+ lineHeight: 1.58,+ fontSize: 14,+ color: "#4d5156",+ "-webkit-line-clamp": 2,+ display: "-webkit-box",+ "-webkit-box-orient": "vertical",+ overflow: "hidden",+ }}+ >+ {truncateMetaText(props.description)}+ </Flex>+ </Grid>+ );+};