From 2e7812dbc99fe2970db0ee156a33ab0a6eb5b190 Mon Sep 17 00:00:00 2001 From: Guilherme Ermel Date: Thu, 25 Aug 2022 14:43:27 -0300 Subject: [PATCH] Correction proposal for emphasis tags () problem. --- src/utils.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/utils.js b/src/utils.js index 517dc02..817ee19 100644 --- a/src/utils.js +++ b/src/utils.js @@ -20,8 +20,13 @@ module.exports.getPageText = (page) => { return text; }; -function escapeRegExp(string) { - return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +function normalizeString(string) { + return string.replace(/<|>/g, '').replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} + +function generateRegExp(string) { + const stringNormalized = normalizeString(string); + return `(?]*|&[^;]*)${stringNormalized}`; } /** @@ -34,10 +39,10 @@ module.exports.highlightText = (fullText, highlightTarget) => { highlightWords = highlightTarget.split(" ").filter((word) => word.length > 0); if (highlightWords.length > 0) { for (const word of highlightWords) { - result = result.replace(new RegExp(escapeRegExp(word), "ig"), "$&"); + result = result.replace(new RegExp(generateRegExp(word), "ig"), "$&"); } } else { - result = fullText.replace(new RegExp(escapeRegExp(highlightTarget), "ig"), "$&"); + result = fullText.replace(new RegExp(generateRegExp(highlightTarget), "ig"), "$&"); } return result;