From 2d6a7ed244505acf3941430c110ea504df5ccc57 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 10 Feb 2024 15:58:40 -0500 Subject: [PATCH] fix: Self-hosted grist embeds not matching closes #6451 --- shared/editor/embeds/index.tsx | 12 ++++++++---- shared/utils/urls.test.ts | 7 +++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/shared/editor/embeds/index.tsx b/shared/editor/embeds/index.tsx index cbf2226d9..cf4ab4279 100644 --- a/shared/editor/embeds/index.tsx +++ b/shared/editor/embeds/index.tsx @@ -93,7 +93,9 @@ export class EmbedDescriptor { matcher(url: string): false | RegExpMatchArray { const regexes = this.regexMatch ?? []; - const settingsDomainRegex = urlRegex(this.settings?.url); + const settingsDomainRegex = this.settings?.url + ? urlRegex(this.settings?.url) + : undefined; if (settingsDomainRegex) { regexes.unshift(settingsDomainRegex); @@ -372,11 +374,13 @@ const embeds: EmbedDescriptor[] = [ keywords: "spreadsheet", regexMatch: [new RegExp("^https?://([a-z.-]+\\.)?getgrist\\.com/(.+)$")], transformMatch: (matches: RegExpMatchArray) => { - if (matches[0].includes("style=singlePage")) { - return matches[0]; + const input = matches.input ?? matches[0]; + + if (input.includes("style=singlePage")) { + return input; } - return matches[0].replace(/(\?embed=true)?$/, "?embed=true"); + return input.replace(/(\?embed=true)?$/, "?embed=true"); }, icon: Grist, }), diff --git a/shared/utils/urls.test.ts b/shared/utils/urls.test.ts index 227881b8b..d5eef39fa 100644 --- a/shared/utils/urls.test.ts +++ b/shared/utils/urls.test.ts @@ -138,5 +138,12 @@ describe("#urlRegex", () => { it("should return corresponding regex otherwise", () => { const regex = urlRegex("https://docs.google.com"); expect(regex?.source).toBe(/https:\/\/docs\.google\.com/.source); + expect(regex?.test("https://docs.google.com")).toBe(true); + expect(regex?.test("https://docs.google.com/")).toBe(true); + expect(regex?.test("https://docs.google.com/d/123")).toBe(true); + expect(regex?.test("http://google.com")).toBe(false); + expect(regex?.test("http://docs.google.com")).toBe(false); + expect(regex?.test("http://docs.google.com/")).toBe(false); + expect(regex?.test("http://docs.google.com/d/123")).toBe(false); }); });