Skip to content

Commit

Permalink
Issue #11248: Avoiding links with highlighted words
Browse files Browse the repository at this point in the history
  • Loading branch information
rssilva committed Sep 21, 2018
1 parent 4e058e5 commit 0fdfaf2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
9 changes: 2 additions & 7 deletions packages/rocketchat-highlight-words/client/client.js
Expand Up @@ -4,6 +4,7 @@
*/
import _ from 'underscore';
import s from 'underscore.string';
import { highlightWords } from './helper';

function HighlightWordsClient(message) {
let msg = message;
Expand All @@ -16,13 +17,7 @@ function HighlightWordsClient(message) {
}

const to_highlight = RocketChat.getUserPreference(Meteor.user(), 'highlights');
if (Array.isArray(to_highlight)) {
to_highlight.forEach((highlight) => {
if (!s.isBlank(highlight)) {
return msg = msg.replace(new RegExp(`(^|\\b|[\\s\\n\\r\\t.,،'\\\"\\+!?:-])(${ s.escapeRegExp(highlight) })($|\\b|[\\s\\n\\r\\t.,،'\\\"\\+!?:-])(?![^<]*>|[^<>]*<\\/)`, 'gmi'), '$1<span class="highlight-text">$2</span>$3');
}
});
}
msg = highlightWords(msg, to_highlight);

message.html = msg;
return message;
Expand Down
42 changes: 42 additions & 0 deletions packages/rocketchat-highlight-words/client/helper.js
@@ -0,0 +1,42 @@
import s from 'underscore.string';

export const checkHighlightedWordsInUrls = (msg, highlight) => {
const urlRegex = new RegExp(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)(${ s.escapeRegExp(highlight) })\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)`, 'gmi');
const urlMatches = msg.match(urlRegex);

return urlMatches;
};

export const removeHighlightedUrls = (msg, highlight, urlMatches) => {

urlMatches.forEach((match) => {
const highlightRegex = new RegExp(highlight, 'gmi');
const withTemplate = match.replace(highlightRegex, `<span class="highlight-text">${ highlight }</span>`);
const regexWithTemplate = new RegExp(withTemplate, 'i');

msg = msg.replace(regexWithTemplate, match);
});

return msg;
};

export const highlightWords = (msg, to_highlight) => {
const highlightTemplate = '$1<span class="highlight-text">$2</span>$3';

if (Array.isArray(to_highlight)) {
to_highlight.forEach((highlight) => {
if (!s.isBlank(highlight)) {
const regex = new RegExp(`(^|\\b|[\\s\\n\\r\\t.,،'\\\"\\+!?:-])(${ s.escapeRegExp(highlight) })($|\\b|[\\s\\n\\r\\t.,،'\\\"\\+!?:-])(?![^<]*>|[^<>]*<\\/)`, 'gmi');
const urlMatches = checkHighlightedWordsInUrls(msg, highlight);

msg = msg.replace(regex, highlightTemplate);

if (urlMatches) {
msg = removeHighlightedUrls(msg, highlight, urlMatches);
}
}
});
}

return msg;
};
36 changes: 36 additions & 0 deletions packages/rocketchat-highlight-words/tests/helper.test.js
@@ -0,0 +1,36 @@
/* eslint-env mocha */

import 'babel-polyfill';
import assert from 'assert';
import { highlightWords } from '../client/helper';

describe('helper', () => {
describe('highlightWords', () => {
it('highlights the correct words', () => {
const res = highlightWords('here is some word', ['word']);

assert.equal(res, 'here is some <span class="highlight-text">word</span>');
});

describe('handles links', () => {
it('not highlighting one link', () => {
const res = highlightWords('here we go https://somedomain.com/here-some.word/pulls more words after', ['word']);

assert.equal(res, 'here we go https://somedomain.com/here-some.word/pulls more words after');
});

it('not highlighting two links', () => {
const msg = 'here https://somedomain.com/here-some-foo/pulls more words after http://www.domain.com/some.foo/bar words after';
const res = highlightWords(msg, ['foo']);

assert.equal(res, msg);
});

it('not highlighting link but keep words on message highlighted', () => {
const res = highlightWords('here we go https://somedomain.com/here-some.foo/pulls more foo after', ['foo']);

assert.equal(res, 'here we go https://somedomain.com/here-some.foo/pulls more <span class="highlight-text">foo</span> after');
});
});
});
});

0 comments on commit 0fdfaf2

Please sign in to comment.