From 57602a912ebb69e5063b9610f6091662317496e7 Mon Sep 17 00:00:00 2001 From: Wisani Shilumani Date: Wed, 18 Jul 2018 10:41:29 +0200 Subject: [PATCH] Replace p tags --- components/searchResults/index.js | 48 ++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/components/searchResults/index.js b/components/searchResults/index.js index a372a80..48c48b2 100644 --- a/components/searchResults/index.js +++ b/components/searchResults/index.js @@ -16,30 +16,38 @@ const SearchResults = (props) => { // sort array alphabetically const sortedInterviews = props.data.sort((a, b) => a.name.localeCompare(b.name)); - const cleanTextForParse = (text) => { - let strText = text; - - if (strText.indexOf('

') !== 0 && strText.indexOf('>') === 0) { - strText = `') !== 0 && strText.indexOf('p>') === 0) { - strText = `<${strText}`; - } else if (strText.indexOf('

') !== 0) { - strText = `

${strText}`; + const getStartOffset = (text) => { + if (text.indexOf('>') === 0) { + return -2; + } else if (text.indexOf('/p>') === 0) { + return 3; + } else if (text.indexOf('p>') === 0) { + return -1; + } else if (text.indexOf('

') !== 0) { + return false; } - if (strText.substr(strText.length - 1, 1) === '<') { - strText = strText.substr(0, strText.length - 2); - } else if (strText.substr(strText.length - 1, 1) === ' { + if (text.substr(text.length - 1, 1) === '<') { + return -1; + } else if (text.substr(text.length - 2, 2) === '') { + return -3; } - return strText; + return 0; }; const trimText = (text, strpos, length) => { let offset = 0; let firstEllipses = ''; let lastEllipses = ''; + let startOffset = 0; + let endOffset = 0; if (text === null) { return ''; @@ -50,13 +58,19 @@ const SearchResults = (props) => { firstEllipses = '

...

'; } - const replacedText = cleanTextForParse(text.substr(offset, length + offset)); + const offsetText = text.substr(offset, length + offset); + startOffset = getStartOffset(offsetText); + endOffset = getEndOffset(offsetText); - if (replacedText.substr(replacedText.length - 1, 1) !== '.' && replacedText.substr(replacedText.length - 1, 1) !== '>') { + const newOffsetText = startOffset ? + text.substr(offset + startOffset, length + offset + endOffset) : + `

${text.substr(offset + 0, length + offset + endOffset)}`; + + if (newOffsetText.substr(newOffsetText.length - 1, 1) !== '.' && newOffsetText.substr(newOffsetText.length - 1, 1) !== '>') { lastEllipses = '...'; } - return text.length <= length ? text : `${firstEllipses}${replacedText}${lastEllipses}`; + return text.length <= length ? text : `${firstEllipses}${newOffsetText}${lastEllipses}`; }; const highlightTerm = (text) => {