Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions laya-ts/src/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ const QUOTE_HEADERS: RegExp[] = [
/^\s*-{2,}\s*(Original|Forwarded) Message\s*-{2,}/i,
/^\s*-{2,}\s*(Mensagem (original|encaminhada)|Mensaje (original|reenviado))\s*-{2,}/i,
/^\s*_{8,}\s*$/,
/^\s*From:\s.+$/i,
// `From:` opens ordinary prose too ("From: my side the integration works, but please
// refund..."), and a reply header always carries the sender, so the header is only recognised
// when an address follows -- the same rule as `De:` below. A bare `From: Name` header is
// caught by HEADER_FROM_NAME/HEADER_NEXT instead, which need the header's own `Sent:`/`Date:`
// line to tell it apart from a sentence.
/^\s*From:\s.*[@<]/i,
// `De:` also opens ordinary Portuguese/Spanish lines ("De: 10/09 a 15/09"), so the Outlook
// header is only recognised when it carries an address
/^\s*De:\s.*[@<]/i,
];
const ATTRIBUTION_TAIL = /^.{0,120}\S@\S+\s+(wrote|escreveu|escribi[óo]):\s*$/i;
const ATTRIBUTION_HEAD = /^\s*(On|Em|El) (?=.*\d)/i;
const HEADER_FROM_NAME = /^\s*De:\s+\S/i;
const HEADER_NEXT = /^\s*(Enviad[oa]( em| el)?:\s|(Data|Fecha):\s.*\d{4})/i;
// Exchange often leaves the address out of Outlook's reply header ("De: Maria Souza"), so a bare
// `De:` only cuts when the header's own `Enviado:` line, or a dated `Data:`/`Fecha:` line, follows
// it. `Para:` is not enough: "De: 10/09 / Para: 15/09" is how a leave request reads.
//
// The same is true of a bare English `From: Maria Souza`, which is why the marker above needs
// this rule: the English client lines are the translations of the two `De:` neighbours. A line
// that only looks like prose still has to be told apart from a header by its neighbours, so the
// English pair is "From: <name>" followed by "Sent:"/"Date:".
const HEADER_FROM_NAME = /^\s*(De|From):\s+\S/i;
const HEADER_NEXT = /^\s*(Enviad[oa]( em| el)?:\s|Sent:\s|(Data|Fecha|Date):\s.*\d{4})/i;
// A closing line is the closing word plus punctuation and at most a name. Anything else on
// the line is a sentence, and the case of the next word is what separates the two: a name is
// capitalised, "for" in "Thanks for the quick reply." is not. JS regexes have no scoped
Expand Down
29 changes: 29 additions & 0 deletions laya-ts/tests/email-presets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,35 @@ describe("email+presets", () => {
const out = cleanEmailBody("Please review the draft when you can.\nIt is two pages.\nWarmest regards,\nŁukasz");
expect(out).toBe("Please review the draft when you can.\nIt is two pages.");
});
// `From:` opens ordinary prose as well as a reply header, and a reply header always carries
// the sender, so the marker only cuts when an address follows -- Python's `From:\s.*[@<]`.
// The looser `From:\s.+$` this port shipped with deleted the rest of every request whose body
// happened to contain a line starting "From: ", which is issue #338 on the Python side.
it("keeps a request whose prose line starts with From: (Python parity)", () => {
const body = "Hi team, the export failed again this morning.\n"
+ "From: my side the integration works, but the downstream job still times out.\n"
+ "Could you take a look before Friday?";
expect(cleanEmailBody(body)).toBe(body);
});
it("still cuts a From: line that carries an address", () => {
const out = cleanEmailBody("Please refund my order\n\nFrom: Bob <bob@example.com>\nold text");
expect(out).toContain("Please refund my order");
expect(out).not.toContain("old text");
});
// A bare `From: Name` header has no address, so it is told apart from a sentence by its
// neighbours: the English client lines are the translations of the `De:` pair below.
it("cuts a bare From: name header followed by Sent:", () => {
const out = cleanEmailBody("Please refund order 123.\nFrom: Maria Souza\nSent: Monday\nold");
expect(out).toBe("Please refund order 123.");
});
it("does not cut a bare From: name with no header after it", () => {
const body = "Please refund order 123.\nFrom: Maria Souza\nPlease help with my refund.";
expect(cleanEmailBody(body)).toBe(body);
});
it("cuts a De: name header followed by a dated Date: (Python parity)", () => {
const out = cleanEmailBody("Please refund order 123.\nDe: Maria Souza\nDate: 12/09/2026\nold");
expect(out).toBe("Please refund order 123.");
});
it("cuts thanks in advance plus a two-word name", () => {
const out = cleanEmailBody("Please review the draft when you can.\nIt is two pages.\nThanks in advance,\nPriya Nair");
expect(out).toBe("Please review the draft when you can.\nIt is two pages.");
Expand Down
Loading