Cleanup htmlToPlain() and replace its splitPlainText() for simple wordwrap

This commit is contained in:
djmaze 2021-08-20 12:10:15 +02:00
parent e0adf7accd
commit fea65b7ebf

View file

@ -15,82 +15,47 @@ export function isPosNumeric(value) {
return null != value && /^[0-9]*$/.test(value.toString()); return null != value && /^[0-9]*$/.test(value.toString());
} }
/**
* @param {string} text
* @param {number=} len = 100
* @returns {string}
*/
function splitPlainText(text, len = 100) {
let prefix = '',
subText = '',
result = text,
spacePos = 0,
newLinePos = 0;
while (result.length > len) {
subText = result.substr(0, len);
spacePos = subText.lastIndexOf(' ');
newLinePos = subText.lastIndexOf('\n');
if (-1 !== newLinePos) {
spacePos = newLinePos;
}
if (-1 === spacePos) {
spacePos = len;
}
prefix += subText.substr(0, spacePos) + '\n';
result = result.substr(spacePos + 1);
}
return prefix + result;
}
/** /**
* @param {string} html * @param {string} html
* @returns {string} * @returns {string}
*/ */
export function htmlToPlain(html) { export function htmlToPlain(html) {
let pos = 0, let pos = 0,
limit = 0, limit = 800,
iP1 = 0, iP1 = 0,
iP2 = 0, iP2 = 0,
iP3 = 0, iP3 = 0,
text = ''; text = '';
const convertBlockquote = (blockquoteText) => { const
blockquoteText = '> ' + blockquoteText.trim().replace(/\n/gm, '\n> '); tpl = createElement('template'),
return blockquoteText.replace(/(^|\n)([> ]+)/gm, (...args) =>
args && 2 < args.length ? args[1] + args[2].replace(/[\s]/g, '').trim() + ' ' : ''
);
};
const convertDivs = (...args) => { convertBlockquote = (blockquoteText) => {
if (args && 1 < args.length) { blockquoteText = '> ' + blockquoteText.trim().replace(/\n/gm, '\n> ');
let divText = args[1].trim(); return blockquoteText.replace(/(^|\n)([> ]+)/gm, (...args) =>
args && 2 < args.length ? args[1] + args[2].replace(/[\s]/g, '').trim() + ' ' : ''
);
},
convertDivs = (...args) => {
let divText = 1 < args.length ? args[1].trim() : '';
if (divText.length) { if (divText.length) {
divText = divText.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gim, convertDivs); divText = '\n' + divText.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gim, convertDivs).trim() + '\n';
divText = '\n' + divText.trim() + '\n';
} }
return divText; return divText;
} },
return '';
};
const
tpl = createElement('template'),
convertPre = (...args) => convertPre = (...args) =>
args && 1 < args.length 1 < args.length
? args[1] ? args[1]
.toString() .toString()
.replace(/[\n]/gm, '<br/>') .replace(/[\n]/gm, '<br/>')
.replace(/[\r]/gm, '') .replace(/[\r]/gm, '')
: '', : '',
fixAttibuteValue = (...args) => (args && 1 < args.length ? '' + args[1] + encodeHtml(args[2]) : ''), fixAttibuteValue = (...args) => (1 < args.length ? args[1] + encodeHtml(args[2]) : ''),
convertLinks = (...args) => (args && 1 < args.length ? args[1].trim() : '');
convertLinks = (...args) => (1 < args.length ? args[1].trim() : '');
tpl.innerHTML = html tpl.innerHTML = html
.replace(/<p[^>]*><\/p>/gi, '') .replace(/<p[^>]*><\/p>/gi, '')
@ -116,35 +81,31 @@ export function htmlToPlain(html) {
.replace(/&quot;/gi, '"') .replace(/&quot;/gi, '"')
.replace(/<[^>]*>/gm, ''); .replace(/<[^>]*>/gm, '');
text = splitPlainText(tpl.content.textContent text = tpl.content.textContent
.replace(/\n[ \t]+/gm, '\n') .replace(/\n[ \t]+/gm, '\n')
.replace(/[\n]{3,}/gm, '\n\n') .replace(/[\n]{3,}/gm, '\n\n')
.replace(/&gt;/gi, '>') .replace(/&gt;/gi, '>')
.replace(/&lt;/gi, '<') .replace(/&lt;/gi, '<')
.replace(/&amp;/gi, '&') .replace(/&amp;/gi, '&')
); // wordwrap max line length 100
.match(/.{1,100}(\s|$)|\S+?(\s|$)/g).join('\n');
pos = 0; while (0 < --limit) {
limit = 800;
while (0 < limit) {
--limit;
iP1 = text.indexOf('__bq__start__', pos); iP1 = text.indexOf('__bq__start__', pos);
if (-1 < iP1) { if (0 > iP1) {
iP2 = text.indexOf('__bq__start__', iP1 + 5);
iP3 = text.indexOf('__bq__end__', iP1 + 5);
if ((-1 === iP2 || iP3 < iP2) && iP1 < iP3) {
text = text.substr(0, iP1) + convertBlockquote(text.substring(iP1 + 13, iP3)) + text.substr(iP3 + 11);
pos = 0;
} else if (-1 < iP2 && iP2 < iP3) {
pos = iP2 - 1;
} else {
pos = 0;
}
} else {
break; break;
} }
iP2 = text.indexOf('__bq__start__', iP1 + 5);
iP3 = text.indexOf('__bq__end__', iP1 + 5);
if ((-1 === iP2 || iP3 < iP2) && iP1 < iP3) {
text = text.substr(0, iP1) + convertBlockquote(text.substring(iP1 + 13, iP3)) + text.substr(iP3 + 11);
pos = 0;
} else if (-1 < iP2 && iP2 < iP3) {
pos = iP2 - 1;
} else {
pos = 0;
}
} }
return text.replace(/__bq__start__|__bq__end__/gm, '').trim(); return text.replace(/__bq__start__|__bq__end__/gm, '').trim();
@ -229,8 +190,6 @@ export function folderListOptionsBuilder(
bNoSelectSelectable, bNoSelectSelectable,
aList = FolderUserStore.folderList() aList = FolderUserStore.folderList()
) { ) {
// FolderUserStore.folderListSystem()
const const
aResult = [], aResult = [],
sDeepPrefix = '\u00A0\u00A0\u00A0', sDeepPrefix = '\u00A0\u00A0\u00A0',