2016-04-05 06:05:48 +08:00
|
|
|
import {RegExpUtils} from 'nylas-exports'
|
|
|
|
|
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
|
2016-04-05 06:05:48 +08:00
|
|
|
const signatureRegex = RegExpUtils.signatureRegex();
|
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.
|
2016-04-05 06:05:48 +08:00
|
|
|
let insertionPoint = newBody.search(RegExpUtils.n1QuoteStartRegex());
|
2016-03-18 04:11:00 +08:00
|
|
|
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
|
|
|
};
|