Disable link prefetching for urls with no schema specified
This commit is contained in:
parent
70937d29e0
commit
8c6460b58a
@ -6,11 +6,13 @@ LinkifyIt.prototype.normalize = function normalize(match) {
|
||||
if (!match.schema) {
|
||||
match.schema = "http:";
|
||||
match.url = "http://" + match.url;
|
||||
match.noschema = true;
|
||||
}
|
||||
|
||||
if (match.schema === "//") {
|
||||
match.schema = "http:";
|
||||
match.url = "http:" + match.url;
|
||||
match.noschema = true;
|
||||
}
|
||||
|
||||
if (match.schema === "mailto:" && !/^mailto:/i.test(match.url)) {
|
||||
@ -47,11 +49,28 @@ function findLinks(text) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return matches.map((url) => ({
|
||||
return matches.map(returnUrl);
|
||||
}
|
||||
|
||||
function findLinksWithSchema(text) {
|
||||
const matches = linkify.match(text);
|
||||
|
||||
if (!matches) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return matches.filter((url) => !url.noschema).map(returnUrl);
|
||||
}
|
||||
|
||||
function returnUrl(url) {
|
||||
return {
|
||||
start: url.index,
|
||||
end: url.lastIndex,
|
||||
link: url.url,
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = findLinks;
|
||||
module.exports = {
|
||||
findLinks,
|
||||
findLinksWithSchema,
|
||||
};
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
import parseStyle from "./ircmessageparser/parseStyle";
|
||||
import findChannels from "./ircmessageparser/findChannels";
|
||||
import findLinks from "./ircmessageparser/findLinks";
|
||||
import {findLinks} from "./ircmessageparser/findLinks";
|
||||
import findEmoji from "./ircmessageparser/findEmoji";
|
||||
import findNames from "./ircmessageparser/findNames";
|
||||
import merge from "./ircmessageparser/merge";
|
||||
|
@ -6,7 +6,7 @@ const URL = require("url").URL;
|
||||
const mime = require("mime-types");
|
||||
const Helper = require("../../helper");
|
||||
const cleanIrcMessage = require("../../../client/js/helpers/ircmessageparser/cleanIrcMessage");
|
||||
const findLinks = require("../../../client/js/helpers/ircmessageparser/findLinks");
|
||||
const {findLinksWithSchema} = require("../../../client/js/helpers/ircmessageparser/findLinks");
|
||||
const storage = require("../storage");
|
||||
const currentFetchPromises = new Map();
|
||||
const imageTypeRegex = /^image\/.+/;
|
||||
@ -20,7 +20,7 @@ module.exports = function (client, chan, msg) {
|
||||
// Remove all IRC formatting characters before searching for links
|
||||
const cleanText = cleanIrcMessage(msg.text);
|
||||
|
||||
msg.previews = findLinks(cleanText).reduce((cleanLinks, link) => {
|
||||
msg.previews = findLinksWithSchema(cleanText).reduce((cleanLinks, link) => {
|
||||
const url = normalizeURL(link.link);
|
||||
|
||||
// If the URL is invalid and cannot be normalized, don't fetch it
|
||||
|
@ -1,7 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
const expect = require("chai").expect;
|
||||
const findLinks = require("../../../../../client/js/helpers/ircmessageparser/findLinks");
|
||||
const {
|
||||
findLinks,
|
||||
findLinksWithSchema,
|
||||
} = require("../../../../../client/js/helpers/ircmessageparser/findLinks");
|
||||
|
||||
describe("findLinks", () => {
|
||||
it("should find url", () => {
|
||||
@ -354,4 +357,24 @@ describe("findLinks", () => {
|
||||
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should not return urls with no schema if flag is specified", () => {
|
||||
const input = "https://example.global //example.com http://example.group example.py";
|
||||
const expected = [
|
||||
{
|
||||
link: "https://example.global",
|
||||
start: 0,
|
||||
end: 22,
|
||||
},
|
||||
{
|
||||
end: 57,
|
||||
link: "http://example.group",
|
||||
start: 37,
|
||||
},
|
||||
];
|
||||
|
||||
const actual = findLinksWithSchema(input);
|
||||
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
@ -623,19 +623,15 @@ Vivamus bibendum vulputate tincidunt. Sed vitae ligula felis.`;
|
||||
});
|
||||
});
|
||||
|
||||
it("should fetch protocol-aware links", function (done) {
|
||||
it("should not fetch links without a schema", function () {
|
||||
const port = this.port;
|
||||
const message = this.irc.createMessage({
|
||||
text: "//localhost:" + port + "",
|
||||
text: `//localhost:${port} localhost:${port} //localhost:${port}/test localhost:${port}/test`,
|
||||
});
|
||||
|
||||
link(this.irc, this.network.channels[0], message);
|
||||
|
||||
this.irc.once("msg:preview", function (data) {
|
||||
expect(data.preview.link).to.equal("http://localhost:" + port + "");
|
||||
expect(data.preview.type).to.equal("error");
|
||||
done();
|
||||
});
|
||||
expect(message.previews).to.be.empty;
|
||||
});
|
||||
|
||||
it("should de-duplicate links", function (done) {
|
||||
|
Loading…
Reference in New Issue
Block a user