From ae6bae69ac2c915c3dcac4262168da46f8eddf39 Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sun, 21 Jan 2024 16:42:12 +0100 Subject: [PATCH] linkify: Add web+ schema support Co-Authored-By: Reto Brunner --- shared/linkify.ts | 22 ++++++++++++++++++ test/shared/findLinks.ts | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/shared/linkify.ts b/shared/linkify.ts index b403b533..af87c5d0 100644 --- a/shared/linkify.ts +++ b/shared/linkify.ts @@ -55,6 +55,28 @@ for (const schema of commonSchemes) { linkify.add(schema + ":", "http:"); } +linkify.add("web+", { + validate(text: string, pos: number, self: LinkifyIt.LinkifyIt) { + const webSchemaRe = /^[a-z]+:/gi; + + if (!webSchemaRe.test(text.slice(pos))) { + return 0; + } + + const linkEnd = self.testSchemaAt(text, "http:", pos + webSchemaRe.lastIndex); + + if (linkEnd === 0) { + return 0; + } + + return webSchemaRe.lastIndex + linkEnd; + }, + normalize(match) { + match.schema = match.text.slice(0, match.text.indexOf(":") + 1); + LinkifyIt.prototype.normalize(match); // hand over to the global override + }, +}); + export function findLinks(text: string) { const matches = linkify.match(text) as NoSchemaMatch[]; diff --git a/test/shared/findLinks.ts b/test/shared/findLinks.ts index c5be4608..d7c77936 100644 --- a/test/shared/findLinks.ts +++ b/test/shared/findLinks.ts @@ -372,4 +372,52 @@ describe("findLinks", () => { expect(actual).to.deep.equal(expected); }); + + it("should find web+ schema urls", () => { + const input = "web+ap://instance.example/@Example web+whatever://example.com?some=value"; + const expected = [ + { + link: "web+ap://instance.example/@Example", + start: 0, + end: 34, + }, + { + link: "web+whatever://example.com?some=value", + start: 35, + end: 72, + }, + ]; + + const actual = findLinks(input); + + expect(actual).to.deep.equal(expected); + }); + + it("should find web+ schema urls if scheme required flag is specified", () => { + const input = + "web+ap://instance.example/@Example web+Whatever://example.com?some=value example.org"; + const expected = [ + { + link: "web+ap://instance.example/@Example", + start: 0, + end: 34, + }, + { + link: "web+Whatever://example.com?some=value", + start: 35, + end: 72, + }, + ]; + + const actual = findLinksWithSchema(input); + + expect(actual).to.deep.equal(expected); + }); + + it("should disregard invalid web+ links", () => { + const input = "web+://whatever.example"; + const actual = findLinksWithSchema(input); + + expect(actual).to.be.empty; + }); });