2016-03-01 10:47:22 +08:00
|
|
|
export default {
|
2016-02-24 16:19:17 +08:00
|
|
|
applySignature(body, signature) {
|
2016-03-24 06:23:13 +08:00
|
|
|
// https://regex101.com/r/nC0qL2/2
|
|
|
|
const signatureRegex = /(<br\/>){0,2}<signature>[^]*<\/signature>/;
|
2016-02-24 16:19:17 +08:00
|
|
|
|
|
|
|
let newBody = body;
|
2016-03-18 04:11:00 +08:00
|
|
|
let paddingBefore = '';
|
|
|
|
let paddingAfter = '';
|
2016-02-24 16:19:17 +08:00
|
|
|
|
2016-03-18 04:11:00 +08:00
|
|
|
// Remove any existing signature in the body
|
|
|
|
newBody = newBody.replace(signatureRegex, "");
|
2016-02-24 16:19:17 +08:00
|
|
|
|
2016-03-18 04:11:00 +08:00
|
|
|
// http://www.regexpal.com/?fam=94390
|
|
|
|
// prefer to put the signature one <br> before the beginning of the quote,
|
|
|
|
// if possible.
|
|
|
|
let insertionPoint = newBody.search(/<\w+[^>]*gmail_quote/i);
|
|
|
|
if (insertionPoint === -1) {
|
|
|
|
insertionPoint = newBody.length;
|
|
|
|
paddingBefore = '<br/><br/>';
|
|
|
|
} else {
|
|
|
|
paddingAfter = '<br/>';
|
2016-02-24 16:19:17 +08:00
|
|
|
}
|
2016-03-18 04:11:00 +08:00
|
|
|
|
|
|
|
const contentBefore = newBody.slice(0, insertionPoint);
|
|
|
|
const contentAfter = newBody.slice(insertionPoint);
|
2016-03-24 06:23:13 +08:00
|
|
|
return `${contentBefore}${paddingBefore}<signature>${signature}${paddingAfter}</signature>${contentAfter}`;
|
2016-02-24 16:19:17 +08:00
|
|
|
},
|
2016-03-01 10:47:22 +08:00
|
|
|
};
|