Fix trimming message before sending
This commit is contained in:
parent
ebdf950858
commit
46901ed65f
@ -244,7 +244,7 @@ namespace RichTextProcessor {
|
|||||||
})
|
})
|
||||||
} */
|
} */
|
||||||
|
|
||||||
export function parseMarkdown(text: string, currentEntities: MessageEntity[], noTrim?: boolean): string {
|
export function parseMarkdown(raw: string, currentEntities: MessageEntity[], noTrim?: boolean): string {
|
||||||
/* if(!markdownTestRegExp.test(text)) {
|
/* if(!markdownTestRegExp.test(text)) {
|
||||||
return noTrim ? text : text.trim();
|
return noTrim ? text : text.trim();
|
||||||
} */
|
} */
|
||||||
@ -253,14 +253,12 @@ namespace RichTextProcessor {
|
|||||||
let pushedEntity = false;
|
let pushedEntity = false;
|
||||||
const pushEntity = (entity: MessageEntity) => !findConflictingEntity(currentEntities, entity) ? (entities.push(entity), pushedEntity = true) : pushedEntity = false;
|
const pushEntity = (entity: MessageEntity) => !findConflictingEntity(currentEntities, entity) ? (entities.push(entity), pushedEntity = true) : pushedEntity = false;
|
||||||
|
|
||||||
let raw = text;
|
const newTextParts: string[] = [];
|
||||||
let match;
|
let rawOffset = 0, match;
|
||||||
let newText: any = [];
|
|
||||||
let rawOffset = 0;
|
|
||||||
while(match = raw.match(markdownRegExp)) {
|
while(match = raw.match(markdownRegExp)) {
|
||||||
const matchIndex = rawOffset + match.index;
|
const matchIndex = rawOffset + match.index;
|
||||||
newText.push(raw.substr(0, match.index));
|
newTextParts.push(raw.substr(0, match.index));
|
||||||
let text = (match[3] || match[8] || match[11] || match[13]);
|
const text = (match[3] || match[8] || match[11] || match[13]);
|
||||||
rawOffset -= text.length;
|
rawOffset -= text.length;
|
||||||
//text = text.replace(/^\s+|\s+$/g, '');
|
//text = text.replace(/^\s+|\s+$/g, '');
|
||||||
rawOffset += text.length;
|
rawOffset += text.length;
|
||||||
@ -268,7 +266,7 @@ namespace RichTextProcessor {
|
|||||||
let entity: MessageEntity;
|
let entity: MessageEntity;
|
||||||
pushedEntity = false;
|
pushedEntity = false;
|
||||||
if(text.match(/^`*$/)) {
|
if(text.match(/^`*$/)) {
|
||||||
newText.push(match[0]);
|
newTextParts.push(match[0]);
|
||||||
} else if(match[3]) { // pre
|
} else if(match[3]) { // pre
|
||||||
entity = {
|
entity = {
|
||||||
_: 'messageEntityPre',
|
_: 'messageEntityPre',
|
||||||
@ -283,7 +281,7 @@ namespace RichTextProcessor {
|
|||||||
rawOffset -= 1;
|
rawOffset -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
newText.push(match[1] + text + match[5]);
|
newTextParts.push(match[1] + text + match[5]);
|
||||||
|
|
||||||
rawOffset -= match[2].length + match[4].length;
|
rawOffset -= match[2].length + match[4].length;
|
||||||
}
|
}
|
||||||
@ -299,9 +297,9 @@ namespace RichTextProcessor {
|
|||||||
|
|
||||||
if(pushEntity(entity)) {
|
if(pushEntity(entity)) {
|
||||||
if(!isSOH) {
|
if(!isSOH) {
|
||||||
newText.push(match[6] + text + match[9]);
|
newTextParts.push(match[6] + text + match[9]);
|
||||||
} else {
|
} else {
|
||||||
newText.push(text);
|
newTextParts.push(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
rawOffset -= match[7].length * 2 + (isSOH ? 2 : 0);
|
rawOffset -= match[7].length * 2 + (isSOH ? 2 : 0);
|
||||||
@ -315,7 +313,7 @@ namespace RichTextProcessor {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if(pushEntity(entity)) {
|
if(pushEntity(entity)) {
|
||||||
newText.push(text);
|
newTextParts.push(text);
|
||||||
|
|
||||||
rawOffset -= match[0].length - text.length;
|
rawOffset -= match[0].length - text.length;
|
||||||
}
|
}
|
||||||
@ -328,24 +326,24 @@ namespace RichTextProcessor {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if(pushEntity(entity)) {
|
if(pushEntity(entity)) {
|
||||||
newText.push(text);
|
newTextParts.push(text);
|
||||||
|
|
||||||
rawOffset -= match[12].length - text.length;
|
rawOffset -= match[12].length - text.length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!pushedEntity) {
|
if(!pushedEntity) {
|
||||||
newText.push(match[0]);
|
newTextParts.push(match[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
raw = raw.substr(match.index + match[0].length);
|
raw = raw.substr(match.index + match[0].length);
|
||||||
rawOffset += match.index + match[0].length;
|
rawOffset += match.index + match[0].length;
|
||||||
}
|
}
|
||||||
|
|
||||||
newText.push(raw);
|
newTextParts.push(raw);
|
||||||
newText = newText.join('');
|
let newText = newTextParts.join('');
|
||||||
if(!newText.replace(/\s+/g, '').length) {
|
if(!newText.replace(/\s+/g, '').length) {
|
||||||
newText = text;
|
newText = raw;
|
||||||
entities.splice(0, entities.length);
|
entities.splice(0, entities.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -356,6 +354,31 @@ namespace RichTextProcessor {
|
|||||||
mergeEntities(currentEntities, entities);
|
mergeEntities(currentEntities, entities);
|
||||||
combineSameEntities(currentEntities);
|
combineSameEntities(currentEntities);
|
||||||
|
|
||||||
|
let length = newText.length;
|
||||||
|
if(!noTrim) {
|
||||||
|
// trim left
|
||||||
|
newText = newText.replace(/^\s*/, '');
|
||||||
|
|
||||||
|
let diff = length - newText.length;
|
||||||
|
if(diff) {
|
||||||
|
currentEntities.forEach(entity => {
|
||||||
|
entity.offset = Math.max(0, entity.offset - diff);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// trim right
|
||||||
|
newText = newText.replace(/\s*$/, '');
|
||||||
|
diff = length - newText.length;
|
||||||
|
length = newText.length;
|
||||||
|
if(diff) {
|
||||||
|
currentEntities.forEach(entity => {
|
||||||
|
if((entity.offset + entity.length) > length) {
|
||||||
|
entity.length = length - entity.offset;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return newText;
|
return newText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user