mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-20 22:03:48 +00:00
refactor(domain): own sender display name on EmailAddress
Push the "Name <addr>" display-name parsing onto the EmailAddress VO (displayName field + label() = displayName ?? normalized) and delete the ad-hoc parseFromAddress helper in feed-generator. The feed builder now parses the from-address once via EmailAddress and reuses it for the site base URL, the [Sender] title prefix, and the entry author — one parser, on the type that owns the data. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -30,4 +30,20 @@ describe("EmailAddress", () => {
|
|||||||
"https://example.com/",
|
"https://example.com/",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("captures the display name verbatim from a display form", () => {
|
||||||
|
const email = EmailAddress.parse("Alice B <Alice@Example.com>")!;
|
||||||
|
expect(email.displayName).toBe("Alice B");
|
||||||
|
expect(email.label()).toBe("Alice B");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has no display name for a bare address and labels by the address", () => {
|
||||||
|
const email = EmailAddress.parse("Bob@Example.com")!;
|
||||||
|
expect(email.displayName).toBeUndefined();
|
||||||
|
expect(email.label()).toBe("bob@example.com");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to the address as the label when the display name is empty", () => {
|
||||||
|
expect(EmailAddress.parse("<a@b.com>")?.label()).toBe("a@b.com");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,12 +3,16 @@ import { Domain } from "./domain";
|
|||||||
/**
|
/**
|
||||||
* A normalised email address. `parse` accepts a bare address (`a@b.com`) or a
|
* A normalised email address. `parse` accepts a bare address (`a@b.com`) or a
|
||||||
* display form (`Name <a@b.com>`), lowercasing the local part and normalising
|
* display form (`Name <a@b.com>`), lowercasing the local part and normalising
|
||||||
* the domain. Returns null when no plausible address can be found.
|
* the domain. When the input carries a display name it is captured (verbatim,
|
||||||
|
* not normalised — names are case-sensitive). Returns null when no plausible
|
||||||
|
* address can be found.
|
||||||
*/
|
*/
|
||||||
export class EmailAddress {
|
export class EmailAddress {
|
||||||
private constructor(
|
private constructor(
|
||||||
readonly normalized: string,
|
readonly normalized: string,
|
||||||
readonly domain: Domain,
|
readonly domain: Domain,
|
||||||
|
/** The sender's display name from a `Name <addr>` input, if any. */
|
||||||
|
readonly displayName?: string,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
static parse(raw: string): EmailAddress | null {
|
static parse(raw: string): EmailAddress | null {
|
||||||
@@ -17,7 +21,17 @@ export class EmailAddress {
|
|||||||
const domain = Domain.parse(match[2]);
|
const domain = Domain.parse(match[2]);
|
||||||
if (!domain) return null;
|
if (!domain) return null;
|
||||||
const local = match[1].trim().toLowerCase();
|
const local = match[1].trim().toLowerCase();
|
||||||
return new EmailAddress(`${local}@${domain.value}`, domain);
|
const displayName =
|
||||||
|
raw.match(/^\s*(.+?)\s*<[^>]+>\s*$/)?.[1].trim() || undefined;
|
||||||
|
return new EmailAddress(`${local}@${domain.value}`, domain, displayName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The best human-readable label for this sender: the display name when the
|
||||||
|
* address came in `Name <addr>` form, else the normalised address.
|
||||||
|
*/
|
||||||
|
label(): string {
|
||||||
|
return this.displayName ?? this.normalized;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -14,18 +14,6 @@ function stripInvalidXmlChars(xml: string): string {
|
|||||||
return xml.replace(/[^\x09\x0A\x0D\x20--�\u{10000}-\u{10FFFF}]/gu, "");
|
return xml.replace(/[^\x09\x0A\x0D\x20--�\u{10000}-\u{10FFFF}]/gu, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseFromAddress(from: string): { name: string; email?: string } {
|
|
||||||
const match = from.match(/^(.*?)\s*<([^>]+)>\s*$/);
|
|
||||||
if (match) {
|
|
||||||
return { name: match[1].trim() || match[2], email: match[2].trim() };
|
|
||||||
}
|
|
||||||
const emailOnly = from.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);
|
|
||||||
if (emailOnly) {
|
|
||||||
return { email: from.trim(), name: from.trim() };
|
|
||||||
}
|
|
||||||
return { name: from.trim() };
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildFeed(
|
function buildFeed(
|
||||||
feedConfig: FeedConfig,
|
feedConfig: FeedConfig,
|
||||||
emails: EmailData[],
|
emails: EmailData[],
|
||||||
@@ -68,23 +56,28 @@ function buildFeed(
|
|||||||
const entryUrl = `${baseUrl}${entryPath(feedId, email.receivedAt)}`;
|
const entryUrl = `${baseUrl}${entryPath(feedId, email.receivedAt)}`;
|
||||||
// Inline images are rendered in the body, not surfaced as an enclosure.
|
// Inline images are rendered in the body, not surfaced as an enclosure.
|
||||||
const firstAttachment = email.attachments?.find((a) => !a.inline);
|
const firstAttachment = email.attachments?.find((a) => !a.inline);
|
||||||
|
const sender = EmailAddress.parse(email.from);
|
||||||
|
const senderLabel = sender?.label() ?? email.from.trim();
|
||||||
const bodyContent = processEmailContent(
|
const bodyContent = processEmailContent(
|
||||||
email.content,
|
email.content,
|
||||||
email.attachments,
|
email.attachments,
|
||||||
baseUrl,
|
baseUrl,
|
||||||
EmailAddress.parse(email.from)?.siteBaseUrl() ?? "",
|
sender?.siteBaseUrl() ?? "",
|
||||||
);
|
);
|
||||||
const sender = parseFromAddress(email.from);
|
|
||||||
const subject = htmlToText(email.subject);
|
const subject = htmlToText(email.subject);
|
||||||
feed.addItem({
|
feed.addItem({
|
||||||
title: feedConfig.sender_in_title
|
title: feedConfig.sender_in_title
|
||||||
? `[${sender.name}] ${subject}`
|
? `[${senderLabel}] ${subject}`
|
||||||
: subject,
|
: subject,
|
||||||
id: entryUrl,
|
id: entryUrl,
|
||||||
link: entryUrl,
|
link: entryUrl,
|
||||||
description: bodyContent,
|
description: bodyContent,
|
||||||
content: bodyContent,
|
content: bodyContent,
|
||||||
author: [sender],
|
author: [
|
||||||
|
sender
|
||||||
|
? { name: senderLabel, email: sender.normalized }
|
||||||
|
: { name: senderLabel },
|
||||||
|
],
|
||||||
date: new Date(email.receivedAt),
|
date: new Date(email.receivedAt),
|
||||||
enclosure: firstAttachment
|
enclosure: firstAttachment
|
||||||
? {
|
? {
|
||||||
|
|||||||
Reference in New Issue
Block a user