fix: Token type hr not supported by Markdown parser (#8003)
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
|||||||
MessageMarkdownTransformer,
|
MessageMarkdownTransformer,
|
||||||
MessageMarkdownSerializer,
|
MessageMarkdownSerializer,
|
||||||
} from '@chatwoot/prosemirror-schema';
|
} from '@chatwoot/prosemirror-schema';
|
||||||
|
import * as Sentry from '@sentry/browser';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The delimiter used to separate the signature from the rest of the body.
|
* The delimiter used to separate the signature from the rest of the body.
|
||||||
@@ -14,9 +15,25 @@ export const SIGNATURE_DELIMITER = '--';
|
|||||||
* Parse and Serialize the markdown text to remove any extra spaces or new lines
|
* Parse and Serialize the markdown text to remove any extra spaces or new lines
|
||||||
*/
|
*/
|
||||||
export function cleanSignature(signature) {
|
export function cleanSignature(signature) {
|
||||||
// convert from markdown to common mark format
|
try {
|
||||||
const nodes = new MessageMarkdownTransformer(messageSchema).parse(signature);
|
// remove any horizontal rule tokens
|
||||||
return MessageMarkdownSerializer.serialize(nodes);
|
signature = signature
|
||||||
|
.replace(/^( *\* *){3,} *$/gm, '')
|
||||||
|
.replace(/^( *- *){3,} *$/gm, '')
|
||||||
|
.replace(/^( *_ *){3,} *$/gm, '');
|
||||||
|
|
||||||
|
const nodes = new MessageMarkdownTransformer(messageSchema).parse(
|
||||||
|
signature
|
||||||
|
);
|
||||||
|
return MessageMarkdownSerializer.serialize(nodes);
|
||||||
|
} catch (e) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.warn(e);
|
||||||
|
Sentry.captureException(e);
|
||||||
|
// The parser can break on some cases
|
||||||
|
// for example, Token type `hr` not supported by Markdown parser
|
||||||
|
return signature;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -85,6 +85,37 @@ describe('appendSignature', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('cleanSignature', () => {
|
||||||
|
it('removes any instance of horizontal rule', () => {
|
||||||
|
const options = [
|
||||||
|
'---',
|
||||||
|
'***',
|
||||||
|
'___',
|
||||||
|
'- - -',
|
||||||
|
'* * *',
|
||||||
|
'_ _ _',
|
||||||
|
' ---',
|
||||||
|
'--- ',
|
||||||
|
' --- ',
|
||||||
|
'-----',
|
||||||
|
'*****',
|
||||||
|
'_____',
|
||||||
|
'- - - -',
|
||||||
|
'* * * * *',
|
||||||
|
'_ _ _ _ _ _',
|
||||||
|
' - - - - ',
|
||||||
|
' * * * * * ',
|
||||||
|
' _ _ _ _ _ _',
|
||||||
|
'- - - - -',
|
||||||
|
'* * * * * *',
|
||||||
|
'_ _ _ _ _ _ _',
|
||||||
|
];
|
||||||
|
options.forEach(option => {
|
||||||
|
expect(cleanSignature(option)).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('removeSignature', () => {
|
describe('removeSignature', () => {
|
||||||
it('does not remove signature if not present', () => {
|
it('does not remove signature if not present', () => {
|
||||||
Object.keys(DOES_NOT_HAVE_SIGNATURE).forEach(key => {
|
Object.keys(DOES_NOT_HAVE_SIGNATURE).forEach(key => {
|
||||||
|
|||||||
Reference in New Issue
Block a user