diff --git a/src/helpers.js b/src/helpers.js index 1f90346..b2d7e72 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,3 +1,7 @@ +export function cloneObject(object) { + return JSON.parse(JSON.stringify(object)); +} + export function removeTags(html) { var tagBody = '(?:[^"\'>]|"[^"]*"|\'[^\']*\')*'; @@ -19,4 +23,4 @@ var tagOrComment = new RegExp( html = html.replace(tagOrComment, ''); } while (html !== oldHtml); return html.replace(/ { - let detailsMarkdown = removeTags(word.longDefinition); + words.forEach(originalWord => { + let detailsMarkdown = removeTags(originalWord.longDefinition); const references = detailsMarkdown.match(/\{\{.+?\}\}/g); if (references && Array.isArray(references)) { new Set(references).forEach(reference => { - console.log(reference); const wordToFind = reference.replace(/\{\{|\}\}/g, ''); const existingWordId = wordExists(wordToFind, true); if (existingWordId !== false) { const wordMarkdownLink = `[${wordToFind}](#${existingWordId})`; - console.log(wordMarkdownLink); detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink); } }); } - console.log(detailsMarkdown); + const word = highlightSearchTerm({ + name: removeTags(originalWord.name), + pronunciation: removeTags(originalWord.pronunciation), + partOfSpeech: removeTags(originalWord.partOfSpeech), + simpleDefinition: removeTags(originalWord.simpleDefinition), + longDefinition: detailsMarkdown, + wordId: originalWord.wordId, + }); wordsHTML += `
-

${removeTags(word.name)}

- ${removeTags(word.pronunciation)} - ${removeTags(word.partOfSpeech)} +

${word.name}

+ ${word.pronunciation} + ${word.partOfSpeech}
-
${removeTags(word.simpleDefinition)}
+
${word.simpleDefinition}
- ${md(detailsMarkdown)} + ${md(word.longDefinition)}
`; diff --git a/src/js/utilities.js b/src/js/utilities.js index ed6ae61..e4c8961 100644 --- a/src/js/utilities.js +++ b/src/js/utilities.js @@ -1,8 +1,8 @@ -const { currentDictionary } = window; +import { cloneObject } from '../helpers'; export function getWordsStats() { - const {words, partsOfSpeech} = currentDictionary; - const {caseSensitive} = currentDictionary.settings; + const {words, partsOfSpeech} = window.currentDictionary; + const {caseSensitive} = window.currentDictionary.settings; const wordStats = { numberOfWords: [ @@ -99,13 +99,29 @@ export function wordExists(word, returnId = false) { return foundWord ? (returnId ? foundWord.wordId : true) : false; } +export function getSearchTerm() { + return document.getElementById('searchButton').value; +} + export function getMatchingSearchWords() { - const searchTerm = document.getElementById('searchButton').value.trim(); - const matchingWords = window.currentDictionary.words.filter(word => { + const searchTerm = getSearchTerm(); + const matchingWords = window.currentDictionary.words.slice().filter(word => { const isInName = new RegExp(searchTerm, 'g').test(word.name); const isInDefinition = new RegExp(searchTerm, 'g').test(word.simpleDefinition); const isInDetails = new RegExp(searchTerm, 'g').test(word.longDefinition); return isInName || isInDefinition || isInDetails; }); return matchingWords; +} + +export function highlightSearchTerm(word) { + const searchTerm = getSearchTerm(); + const markedUpWord = cloneObject(word); + if (searchTerm) { + markedUpWord.name = markedUpWord.name.replace(new RegExp(searchTerm, 'g'), `${searchTerm}`); + markedUpWord.simpleDefinition = markedUpWord.simpleDefinition.replace(new RegExp(searchTerm, 'g'), `${searchTerm}`); + markedUpWord.longDefinition = markedUpWord.longDefinition.replace(new RegExp(searchTerm, 'g'), `${searchTerm}`); + } + console.log('markedUpWord', markedUpWord); + return markedUpWord; } \ No newline at end of file