fix: Handling markdown before replacing or appending signature [CW-2532] (#7944)

Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
This commit is contained in:
Shivam Mishra
2023-09-21 11:28:29 +05:30
committed by GitHub
parent f999777a2d
commit 27fc24375d
5 changed files with 39 additions and 29 deletions

View File

@@ -1,3 +1,9 @@
import {
messageSchema,
MessageMarkdownTransformer,
MessageMarkdownSerializer,
} from '@chatwoot/prosemirror-schema';
/**
* The delimiter used to separate the signature from the rest of the body.
* @type {string}
@@ -5,25 +11,12 @@
export const SIGNATURE_DELIMITER = '--';
/**
* Remove trailing spaces from each line in a markdown text
* @param {string} markdownText
* @returns
*/
function removeTrailingSpaces(markdownText) {
return markdownText
.split('\n')
.map(line => line.replace(/\s+$/, ''))
.join('\n');
}
/**
* Trim the signature and remove all " \r" from the signature
* 1. Trim any extra lines or spaces at the start or end of the string
* 2. Converts all \r or \r\n to \f
* Parse and Serialize the markdown text to remove any extra spaces or new lines
*/
export function cleanSignature(signature) {
const cleaned = signature.trim().replace(/\r\n?/g, '\n');
return removeTrailingSpaces(cleaned);
// convert from markdown to common mark format
const nodes = new MessageMarkdownTransformer(messageSchema).parse(signature);
return MessageMarkdownSerializer.serialize(nodes);
}
/**