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:
parent
f999777a2d
commit
27fc24375d
@ -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.
|
* The delimiter used to separate the signature from the rest of the body.
|
||||||
* @type {string}
|
* @type {string}
|
||||||
@ -5,25 +11,12 @@
|
|||||||
export const SIGNATURE_DELIMITER = '--';
|
export const SIGNATURE_DELIMITER = '--';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove trailing spaces from each line in a markdown text
|
* Parse and Serialize the markdown text to remove any extra spaces or new lines
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
export function cleanSignature(signature) {
|
export function cleanSignature(signature) {
|
||||||
const cleaned = signature.trim().replace(/\r\n?/g, '\n');
|
// convert from markdown to common mark format
|
||||||
return removeTrailingSpaces(cleaned);
|
const nodes = new MessageMarkdownTransformer(messageSchema).parse(signature);
|
||||||
|
return MessageMarkdownSerializer.serialize(nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import {
|
|||||||
appendSignature,
|
appendSignature,
|
||||||
removeSignature,
|
removeSignature,
|
||||||
replaceSignature,
|
replaceSignature,
|
||||||
|
cleanSignature,
|
||||||
extractTextFromMarkdown,
|
extractTextFromMarkdown,
|
||||||
} from '../editorHelper';
|
} from '../editorHelper';
|
||||||
|
|
||||||
@ -17,11 +18,19 @@ const DOES_NOT_HAVE_SIGNATURE = {
|
|||||||
body: 'This is a test\n\n--\n\nThis is a signature\n\nThis is more text',
|
body: 'This is a test\n\n--\n\nThis is a signature\n\nThis is more text',
|
||||||
signature: 'This is a signature',
|
signature: 'This is a signature',
|
||||||
},
|
},
|
||||||
signature_has_images: {
|
'signature has images': {
|
||||||
body: 'This is a test',
|
body: 'This is a test',
|
||||||
signature:
|
signature:
|
||||||
'Testing\n',
|
'Testing\n',
|
||||||
},
|
},
|
||||||
|
'signature has non commonmark syntax': {
|
||||||
|
body: 'This is a test',
|
||||||
|
signature: '- Signature',
|
||||||
|
},
|
||||||
|
'signature has trailing spaces': {
|
||||||
|
body: 'This is a test',
|
||||||
|
signature: '**hello** \n**world**',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const HAS_SIGNATURE = {
|
const HAS_SIGNATURE = {
|
||||||
@ -37,6 +46,10 @@ const HAS_SIGNATURE = {
|
|||||||
body: '\n\n--\n\nThis is a signature',
|
body: '\n\n--\n\nThis is a signature',
|
||||||
signature: 'This is a signature',
|
signature: 'This is a signature',
|
||||||
},
|
},
|
||||||
|
'signature has non-commonmark syntax': {
|
||||||
|
body: '\n\n--\n\n* Signature',
|
||||||
|
signature: '- Signature',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('findSignatureInBody', () => {
|
describe('findSignatureInBody', () => {
|
||||||
@ -58,9 +71,10 @@ describe('appendSignature', () => {
|
|||||||
it('appends the signature if it is not present', () => {
|
it('appends the signature if it is not present', () => {
|
||||||
Object.keys(DOES_NOT_HAVE_SIGNATURE).forEach(key => {
|
Object.keys(DOES_NOT_HAVE_SIGNATURE).forEach(key => {
|
||||||
const { body, signature } = DOES_NOT_HAVE_SIGNATURE[key];
|
const { body, signature } = DOES_NOT_HAVE_SIGNATURE[key];
|
||||||
expect(appendSignature(body, signature)).toBe(
|
const cleanedSignature = cleanSignature(signature);
|
||||||
`${body}\n\n--\n\n${signature}`
|
expect(
|
||||||
);
|
appendSignature(body, signature).includes(cleanedSignature)
|
||||||
|
).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('does not append signature if already present', () => {
|
it('does not append signature if already present', () => {
|
||||||
|
|||||||
@ -113,7 +113,6 @@ export default {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
setCursor() {
|
setCursor() {
|
||||||
const textarea = this.$refs.textarea;
|
|
||||||
const bodyWithoutSignature = removeSignature(
|
const bodyWithoutSignature = removeSignature(
|
||||||
this.value,
|
this.value,
|
||||||
this.cleanedSignature
|
this.cleanedSignature
|
||||||
@ -121,9 +120,12 @@ export default {
|
|||||||
|
|
||||||
// only trim at end, so if there are spaces at the start, those are not removed
|
// only trim at end, so if there are spaces at the start, those are not removed
|
||||||
const bodyEndsAt = bodyWithoutSignature.trimEnd().length;
|
const bodyEndsAt = bodyWithoutSignature.trimEnd().length;
|
||||||
|
const textarea = this.$refs.textarea;
|
||||||
|
|
||||||
textarea.focus();
|
if (textarea) {
|
||||||
textarea.setSelectionRange(bodyEndsAt, bodyEndsAt);
|
textarea.focus();
|
||||||
|
textarea.setSelectionRange(bodyEndsAt, bodyEndsAt);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onInput(event) {
|
onInput(event) {
|
||||||
this.$emit('input', event.target.value);
|
this.$emit('input', event.target.value);
|
||||||
@ -157,7 +159,7 @@ export default {
|
|||||||
this.$emit('focus');
|
this.$emit('focus');
|
||||||
},
|
},
|
||||||
focus() {
|
focus() {
|
||||||
this.$refs.textarea.focus();
|
if (this.$refs.textarea) this.$refs.textarea.focus();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -30,7 +30,7 @@
|
|||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@braid/vue-formulate": "^2.5.2",
|
"@braid/vue-formulate": "^2.5.2",
|
||||||
"@chatwoot/prosemirror-schema": "https://github.com/chatwoot/prosemirror-schema.git#825755b53f1ef4ae14fac2393b53a02e14ce21e0",
|
"@chatwoot/prosemirror-schema": "^1.0.1",
|
||||||
"@chatwoot/utils": "^0.0.16",
|
"@chatwoot/utils": "^0.0.16",
|
||||||
"@hcaptcha/vue-hcaptcha": "^0.3.2",
|
"@hcaptcha/vue-hcaptcha": "^0.3.2",
|
||||||
"@june-so/analytics-next": "^1.36.5",
|
"@june-so/analytics-next": "^1.36.5",
|
||||||
|
|||||||
@ -2994,9 +2994,10 @@
|
|||||||
is-url "^1.2.4"
|
is-url "^1.2.4"
|
||||||
nanoid "^2.1.11"
|
nanoid "^2.1.11"
|
||||||
|
|
||||||
"@chatwoot/prosemirror-schema@https://github.com/chatwoot/prosemirror-schema.git#825755b53f1ef4ae14fac2393b53a02e14ce21e0":
|
"@chatwoot/prosemirror-schema@^1.0.1":
|
||||||
version "1.0.0"
|
version "1.0.1"
|
||||||
resolved "https://github.com/chatwoot/prosemirror-schema.git#825755b53f1ef4ae14fac2393b53a02e14ce21e0"
|
resolved "https://registry.yarnpkg.com/@chatwoot/prosemirror-schema/-/prosemirror-schema-1.0.1.tgz#7beb7a9303fbf5281d3a7e6c470ac78cce21be04"
|
||||||
|
integrity sha512-fnT2zSzAmiAh1ElEWqBhISavGZF73DLUzQyml0iiXDy6IU7HS3TtQPI/AGoCK3CkCxvoqBtzhRLGZrHsftoQ9w==
|
||||||
dependencies:
|
dependencies:
|
||||||
markdown-it-sup "^1.0.0"
|
markdown-it-sup "^1.0.0"
|
||||||
prosemirror-commands "^1.1.4"
|
prosemirror-commands "^1.1.4"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user