Skip to content

Commit

Permalink
linkify: Add web+ schema support
Browse files Browse the repository at this point in the history
Co-Authored-By: SoniEx2 <endermoneymod@gmail.com>
  • Loading branch information
brunnre8 and SoniEx2 committed Jan 21, 2024
1 parent d15998d commit 17586e6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
22 changes: 22 additions & 0 deletions shared/linkify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down
48 changes: 48 additions & 0 deletions test/shared/findLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

0 comments on commit 17586e6

Please sign in to comment.