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:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user