Replace p tags

This commit is contained in:
Wisani Shilumani 2018-07-18 10:41:29 +02:00
parent 0948dc51cb
commit 57602a912e
1 changed files with 31 additions and 17 deletions

View File

@ -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('<p>') !== 0 && strText.indexOf('>') === 0) {
strText = `<p${strText}`;
} else if (strText.indexOf('<p>') !== 0 && strText.indexOf('p>') === 0) {
strText = `<${strText}`;
} else if (strText.indexOf('<p>') !== 0) {
strText = `<p>${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('<p>') !== 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) === '<p') {
strText = strText.substr(0, strText.length - 3);
return 0;
};
const getEndOffset = (text) => {
if (text.substr(text.length - 1, 1) === '<') {
return -1;
} else if (text.substr(text.length - 2, 2) === '<p') {
return -2;
} else if (text.substr(text.length - 3, 3) === '<p>') {
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 = '<p>...</p>';
}
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) :
`<p>${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) => {