mirror of
https://github.com/the-djmaze/snappymail.git
synced 2024-09-20 15:45:55 +08:00
Cleanup htmlToPlain() and replace its splitPlainText() for simple wordwrap
This commit is contained in:
parent
e0adf7accd
commit
fea65b7ebf
|
@ -15,82 +15,47 @@ export function isPosNumeric(value) {
|
|||
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
|
||||
* @returns {string}
|
||||
*/
|
||||
export function htmlToPlain(html) {
|
||||
let pos = 0,
|
||||
limit = 0,
|
||||
limit = 800,
|
||||
iP1 = 0,
|
||||
iP2 = 0,
|
||||
iP3 = 0,
|
||||
text = '';
|
||||
|
||||
const convertBlockquote = (blockquoteText) => {
|
||||
blockquoteText = '> ' + blockquoteText.trim().replace(/\n/gm, '\n> ');
|
||||
return blockquoteText.replace(/(^|\n)([> ]+)/gm, (...args) =>
|
||||
args && 2 < args.length ? args[1] + args[2].replace(/[\s]/g, '').trim() + ' ' : ''
|
||||
);
|
||||
};
|
||||
const
|
||||
tpl = createElement('template'),
|
||||
|
||||
const convertDivs = (...args) => {
|
||||
if (args && 1 < args.length) {
|
||||
let divText = args[1].trim();
|
||||
convertBlockquote = (blockquoteText) => {
|
||||
blockquoteText = '> ' + blockquoteText.trim().replace(/\n/gm, '\n> ');
|
||||
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) {
|
||||
divText = divText.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gim, convertDivs);
|
||||
divText = '\n' + divText.trim() + '\n';
|
||||
divText = '\n' + divText.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gim, convertDivs).trim() + '\n';
|
||||
}
|
||||
|
||||
return divText;
|
||||
}
|
||||
},
|
||||
|
||||
return '';
|
||||
};
|
||||
|
||||
const
|
||||
tpl = createElement('template'),
|
||||
convertPre = (...args) =>
|
||||
args && 1 < args.length
|
||||
1 < args.length
|
||||
? args[1]
|
||||
.toString()
|
||||
.replace(/[\n]/gm, '<br/>')
|
||||
.replace(/[\r]/gm, '')
|
||||
: '',
|
||||
fixAttibuteValue = (...args) => (args && 1 < args.length ? '' + args[1] + encodeHtml(args[2]) : ''),
|
||||
convertLinks = (...args) => (args && 1 < args.length ? args[1].trim() : '');
|
||||
fixAttibuteValue = (...args) => (1 < args.length ? args[1] + encodeHtml(args[2]) : ''),
|
||||
|
||||
convertLinks = (...args) => (1 < args.length ? args[1].trim() : '');
|
||||
|
||||
tpl.innerHTML = html
|
||||
.replace(/<p[^>]*><\/p>/gi, '')
|
||||
|
@ -116,35 +81,31 @@ export function htmlToPlain(html) {
|
|||
.replace(/"/gi, '"')
|
||||
.replace(/<[^>]*>/gm, '');
|
||||
|
||||
text = splitPlainText(tpl.content.textContent
|
||||
text = tpl.content.textContent
|
||||
.replace(/\n[ \t]+/gm, '\n')
|
||||
.replace(/[\n]{3,}/gm, '\n\n')
|
||||
.replace(/>/gi, '>')
|
||||
.replace(/</gi, '<')
|
||||
.replace(/&/gi, '&')
|
||||
);
|
||||
// wordwrap max line length 100
|
||||
.match(/.{1,100}(\s|$)|\S+?(\s|$)/g).join('\n');
|
||||
|
||||
pos = 0;
|
||||
limit = 800;
|
||||
|
||||
while (0 < limit) {
|
||||
--limit;
|
||||
while (0 < --limit) {
|
||||
iP1 = text.indexOf('__bq__start__', pos);
|
||||
if (-1 < 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 {
|
||||
if (0 > iP1) {
|
||||
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();
|
||||
|
@ -229,8 +190,6 @@ export function folderListOptionsBuilder(
|
|||
bNoSelectSelectable,
|
||||
aList = FolderUserStore.folderList()
|
||||
) {
|
||||
// FolderUserStore.folderListSystem()
|
||||
|
||||
const
|
||||
aResult = [],
|
||||
sDeepPrefix = '\u00A0\u00A0\u00A0',
|
||||
|
|
Loading…
Reference in a new issue