-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #11248: Avoiding links with highlighted words
- Loading branch information
Showing
3 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); | ||
}); | ||
}); |