From 65a6c408a721c722f7b8f952b3f1dcdc322669a5 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 12 Sep 2017 20:54:30 +0700 Subject: [PATCH] Add some docs --- .../src/components/CommentBodyHighlighter.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/client/coral-admin/src/components/CommentBodyHighlighter.js b/client/coral-admin/src/components/CommentBodyHighlighter.js index f0c9f9c8d..aa3fa1274 100644 --- a/client/coral-admin/src/components/CommentBodyHighlighter.js +++ b/client/coral-admin/src/components/CommentBodyHighlighter.js @@ -3,7 +3,9 @@ import {linkRegexp} from '../utils/regexp'; const wordSeperator = /([.\s'"?!])/; -function markWords(body, words, index) { +// markWords looks for `words` inside `body` and highlights them by returning +// an array of React Elements. +function markWords(body, words, keyPrefix) { const tokens = body.split(wordSeperator); const content = []; let tmp = []; @@ -11,7 +13,7 @@ function markWords(body, words, index) { if (words.indexOf(token.toLowerCase()) >= 0) { content.push(...tmp); tmp = []; - content.push({token}); + content.push({token}); return; } tmp.push(token); @@ -20,6 +22,8 @@ function markWords(body, words, index) { return content; } +// markWords looks for links inside `body` and highlights them by returning +// an array of React Elements. function markLinks(body) { const tokens = body.split(linkRegexp); const content = []; @@ -41,11 +45,17 @@ function markLinks(body) { export default ({suspectWords, bannedWords, body, ...rest}) => { const words = [...suspectWords, ...bannedWords].map((word) => word.toLowerCase()); + + // First highlight links. const content = markLinks(body) .map((element, index) => { + + // Keep highlighted links. if (typeof element !== 'string') { return element; } + + // Highlight suspect and banned words inside this part of text. return markWords(element, words, index); }); return (