linkify: Add web+ schema support

Co-Authored-By: Reto Brunner <reto@slightlybroken.com>
This commit is contained in:
SoniEx2 2024-01-21 16:42:12 +01:00 committed by Reto Brunner
parent d15998d919
commit ae6bae69ac
2 changed files with 70 additions and 0 deletions

View file

@ -55,6 +55,28 @@ for (const schema of commonSchemes) {
linkify.add(schema + ":", "http:"); 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) { export function findLinks(text: string) {
const matches = linkify.match(text) as NoSchemaMatch[]; const matches = linkify.match(text) as NoSchemaMatch[];

View file

@ -372,4 +372,52 @@ describe("findLinks", () => {
expect(actual).to.deep.equal(expected); 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;
});
}); });