From 0e52b133af6819092127650aabe41f262b10d00c Mon Sep 17 00:00:00 2001 From: Bonuspunkt Date: Tue, 21 Nov 2017 23:54:20 +0100 Subject: [PATCH 1/2] Sending channel and link together generates duplicate text --- client/js/libs/handlebars/parse.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/client/js/libs/handlebars/parse.js b/client/js/libs/handlebars/parse.js index fdf32bdc..0bccaa1d 100644 --- a/client/js/libs/handlebars/parse.js +++ b/client/js/libs/handlebars/parse.js @@ -2,6 +2,7 @@ const Handlebars = require("handlebars/runtime"); const parseStyle = require("./ircmessageparser/parseStyle"); +const anyIntersection = require("./ircmessageparser/anyIntersection"); const findChannels = require("./ircmessageparser/findChannels"); const findLinks = require("./ircmessageparser/findLinks"); const findEmoji = require("./ircmessageparser/findEmoji"); @@ -66,7 +67,15 @@ module.exports = function parse(text) { const parts = channelParts .concat(linkParts) .concat(emojiParts) - .sort((a, b) => a.start - b.start); + .sort((a, b) => a.start - b.start || b.end - a.end) + .reduce((prev, curr) => { + const intersection = prev.some((p) => anyIntersection(p, curr)); + + if (intersection) { + return prev; + } + return prev.concat([curr]); + }, []); // Merge the styling information with the channels / URLs / text objects and // generate HTML strings with the resulting fragments From ddc7ace78d2512c6f2003995cb2ce290f80feb60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sun, 26 Nov 2017 14:26:59 -0500 Subject: [PATCH 2/2] Bring test from ircmessageparser See https://github.com/Bonuspunkt/ircmessageparser/pull/12/files#diff-e6c9a6bca996bc454cc244d17bfeda5c for reference (same test, linted) --- test/client/js/libs/handlebars/parse.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/client/js/libs/handlebars/parse.js b/test/client/js/libs/handlebars/parse.js index 10d7eac0..2c08f8f2 100644 --- a/test/client/js/libs/handlebars/parse.js +++ b/test/client/js/libs/handlebars/parse.js @@ -312,7 +312,6 @@ describe("parse Handlebars helper", () => { const testCases = [{ input: "http://example.com/#hash", expected: - "" + "" + "http://example.com/#hash" + "", @@ -333,4 +332,15 @@ describe("parse Handlebars helper", () => { "Channel: ##channel" ); }); + + it("should handle overlapping parts by using first starting", () => { + const input = "#test-https://example.com"; + const actual = parse(input); + + expect(actual).to.equal( + "" + + "#test-https://example.com" + + "" + ); + }); });