Fix data bank text clean-up

This commit is contained in:
Cohee 2024-04-21 02:05:59 +03:00
parent 78ce23750e
commit c2256c2ac7
1 changed files with 11 additions and 4 deletions

View File

@ -1185,16 +1185,23 @@ export function uuidv4() {
}
function postProcessText(text, collapse = true) {
// Remove carriage returns
text = text.replace(/\r/g, '');
// Replace tabs with spaces
text = text.replace(/\t/g, ' ');
// Normalize unicode spaces
text = text.replace(/\u00A0/g, ' ');
// Collapse multiple newlines into one
if (collapse) {
text = collapseNewlines(text);
// Trim leading and trailing whitespace, and remove empty lines
text = text.split('\n').map(l => l.trim()).filter(Boolean).join('\n');
} else {
// Replace more than 4 newlines with 4 newlines
text = text.replace(/\n{4,}/g, '\n\n\n\n');
// Trim lines that contain nothing but whitespace
text = text.split('\n').map(l => /^\s+$/.test(l) ? '' : l).join('\n');
}
// Remove carriage returns
text = text.replace(/\r/g, '');
// Normalize unicode spaces
text = text.replace(/\u00A0/g, ' ');
// Collapse multiple spaces into one (except for newlines)
text = text.replace(/ {2,}/g, ' ');
// Remove leading and trailing spaces