Add webplus

This commit is contained in:
SoniEx2 2024-01-18 19:52:08 -03:00
parent d15998d919
commit 767ab499a2
2 changed files with 55 additions and 0 deletions

View file

@ -11,6 +11,8 @@ export type LinkPart = {
link: string;
};
const webplus_scheme_chars = /^[a-z]+:/i;
LinkifyIt.prototype.normalize = function normalize(match: NoSchemaMatch) {
match.noschema = false;
@ -26,6 +28,10 @@ LinkifyIt.prototype.normalize = function normalize(match: NoSchemaMatch) {
match.noschema = true;
}
if (match.schema === "web+") {
match.schema = "web+" + match.url.slice(4).match(webplus_scheme_chars)![0];
}
if (match.schema === "mailto:" && !/^mailto:/i.test(match.url)) {
match.url = "mailto:" + match.url;
}
@ -55,6 +61,25 @@ for (const schema of commonSchemes) {
linkify.add(schema + ":", "http:");
}
linkify.add("web+", {
validate(text: string, pos: number, self: LinkifyIt.LinkifyIt) {
const tail = text.slice(pos);
if (webplus_scheme_chars.test(tail)) {
const offset = tail.match(webplus_scheme_chars)![0].length;
const result = self.testSchemaAt(text, "http:", pos + offset);
if (result === 0) {
return 0;
}
return offset + result;
}
return 0;
},
});
export function findLinks(text: string) {
const matches = linkify.match(text) as NoSchemaMatch[];

View file

@ -372,4 +372,34 @@ describe("findLinks", () => {
expect(actual).to.deep.equal(expected);
});
it("should find web+ap urls", () => {
const input = "web+ap://instance.example/@Example";
const expected = [
{
link: "web+ap://instance.example/@Example",
start: 0,
end: 34,
},
];
const actual = findLinks(input);
expect(actual).to.deep.equal(expected);
});
it("should find web+ap urls if scheme required flag is specified", () => {
const input = "web+ap://instance.example/@Example";
const expected = [
{
link: "web+ap://instance.example/@Example",
start: 0,
end: 34,
},
];
const actual = findLinksWithSchema(input);
expect(actual).to.deep.equal(expected);
});
});