Nakodo 網站的 50 個電郵範本以陣列項目儲存,單元測試逐一校對,全部不含驚嘆號
Fifty email templates on our site are rows in an array, a unit test proofreads them, and not one contains an exclamation mark
Nakodo 將網站上的 50 個電郵範本存為陣列項目,並以單元測試校對,確保範本沒有驚嘆號。範本正文使用純文字,讓讀者複製及貼到電郵程式時保留原樣;有主旨的範本會在複製內容中加上主旨行。作者亦指出,頁面顯示和 Markdown 版本各自組合主旨與正文,計劃改為共用一個函式。
Nakodo's public pages are typed objects rather than MDX, which means a page is an array of blocks and a block is a member of a union:
export type Block =
| { type: "p"; text: Rich }
| { type: "list"; items: Rich[]; ordered?: boolean }
| { type: "steps"; items: { title: string; text: Rich }[] }
| { type: "table"; caption?: string; head: string[]; rows: Rich[][] }
/** A message the reader can copy. `subject` empty for direct messages. Use [Brackets] for the parts to fill in. */
| { type: "template"; label: string; subject?: string; body: string }
| { type: "callout"; title?: string; text: Rich }
| { type: "terms"; items: { term: string; text: Rich }[] }
| { type: "tags"; items: string[] };
Seven of those eight hold Rich, which is our two-token inline markup: [label](/path) and **bold**. One holds a plain string, and that one is the whole subject of this post. There are 50 of them on the site now, across 16 pages, and last week six of those pages were new.
First, the thing these are not. The messages Nakodo itself writes and sends on a customer's behalf are a different system entirely and stay out of these posts. These 50 are templates for a reader to copy, fill in and send themselves, which is why they are content rather than code.
Why body is a plain string
Rich exists so that one string is both renderable as HTML and valid markdown for our llms-full.txt. Every paragraph, list item and table cell on the site goes through it.
A template cannot. The thing a reader does with a template is select it, or press a button, and paste it into a mail client. Whatever is in that string is what lands in the email. If the body were Rich, then either the markup would survive into somebody's outbox, or the copy path would have to render it first and I would be maintaining two definitions of what "the template" is.
So the body is a string, verbatim, and the content files confirm it: across 50 templates there are zero occurrences of ]( and zero of **. The field being plain is not a limitation anybody has run into, because an email is plain.
There is a second, quieter reason, which is that the placeholder convention and the link syntax share a character. A template says [Brand] and [Your name]. Our markup token is:
export const RICH_TOKEN = /\[([^\]]+)\]\(([^)\s]+)\)|\*\*([^*]+)\*\*/g;
A bracket needs an immediately adjacent ( to become a link, so [Brand] is safe even inside a Rich field. But "safe because of a lookahead" is not the footing I want for 688 placeholders, and a plain field removes the question.
46 lines is the longest one
templates 50
with a subject line 41
without 9
lines of body 688
longest template 46 lines
median 6 paragraphs
characters 28,961
The nine without a subject are the interesting nine, because subject? being optional is doing semantic work rather than saving a field. They are four direct messages, two briefs, a contract outline, a contract clause and the note you put in the parcel when you send a creator a product. No subject means not an email, and the renderer branches on exactly that.
Three renderings of one template, and two of them agree by coincidence
On the page, the block is a figure:
case "template": {
const copy = block.subject ? `Subject: ${block.subject}\n\n${block.body}` : block.body;
return (
<figure className="rounded-lg border bg-card">
<figcaption className="flex items-center justify-between gap-4 border-b px-5 py-3">
<span className="font-semibold">{block.label}</span>
<CopyButton text={copy} />
</figcaption>
<div className="space-y-3 px-5 py-4 text-[15px] leading-7">
{block.subject && <p><span className="text-muted-foreground">Subject: </span>{block.subject}</p>}
<p className="whitespace-pre-line">{block.body}</p>
</div>
</figure>
);
}
A figure with a figcaption because that is what this is: a block of content with a label, referred to by the prose around it. whitespace-pre-line means the newlines in the content file are the layout, so the paragraph breaks a writer typed in a template literal are the paragraph breaks in the email.
Note that what you see and what you copy are not the same string. On screen the subject is its own line with a muted label; in the clipboard it is re-prefixed as text, because a pasted template with no "Subject:" in it loses the subject entirely.
In the clipboard, through a function whose only interesting part is the failure:
export async function copyText(text: string, done?: string): Promise<boolean> {
try {
await navigator.clipboard.writeText(text);
if (done) notify({ message: done });
return true;
} catch {
notify({ error: "Couldn't copy. Select the text and copy it yourself." });
return false;
}
}
navigator.clipboard rejects on an insecure origin, without permission, and in older browsers, and the failure is silent unless you catch it. A copy button that does nothing is worse than no copy button, because the reader walks away believing they have the template. The toast tells them to do it by hand, which they can, because the text is right there as selectable text rather than in a canvas or an input.
In markdown, for the plain-text version of the whole site:
case "template":
return `**${b.label}**\n\n\`\`\`\n${b.subject ? `Subject: ${b.subject}\n\n` : ""}${b.body}\n\`\`\``;
A fenced code block, so an assistant quoting the site quotes the template without reflowing it.
And there is the finding. Subject: ${subject}\n\n${body} is written twice, once in the component and once in the markdown generator, by two different authors-of-the-day who happened to choose the same prefix and the same blank line. Nothing connects them. If one changes, a reader who copies the button gets a different string from a reader who copies from the text file, and no test compares the two. It wants to be one exported function next to the type, and that is a five-line change I am making.
The test reads the templates too
Our content test splits every page into search-facing fields and body copy and holds each half to different rules. Templates are body copy, and the function that extracts their text treats them as three strings:
case "template":
return [b.label, b.subject ?? "", b.body];
Which means all 50 templates go through the banned-words list, and the measurement is my favourite number in this post:
- 0 exclamation marks in 28,961 characters of template text.
- 0 uses of the word "influencer" in any of them, because body copy says "creator".
The exclamation mark is banned site-wide, and if you have ever read a pack of outreach templates you will know that is not the default register for the genre. The rule is one regex in an array, and the reason it holds across 50 messages that are explicitly trying to be friendly is that it is not a preference, it is a failing test.
They also count toward the 600-word floor every content page has to clear, which is correct and slightly uncomfortable: a page of templates clears a content floor using text that is mostly template. The honest version of that floor is that a page of templates needs the surrounding explanation to be worth the page, and the count does not distinguish. No fix yet, just a note.
267 placeholder names and no vocabulary
Every fill-in part is in square brackets. Across the 50 templates:
placeholder uses 688
distinct names 267
used exactly once 204
The top ten:
[Brand] 76 [Your name] 65 [product] 39 [Creator first name] 28
[Product] 17 [topic] 17 [date] 16 [website] 12
[commission] 11 [Your business] 11
Two things jump out of that list. [Product] and [product] are the same concept with two capitalisations, 56 uses between them, differing only by where in a sentence they first appeared. And [Brand] and [Your business] are the same thing said two ways, because the business templates were written weeks after the creator ones.
The 204 names used once are mostly fine: [specific moment or point] and [length, e.g. 60 to 90 seconds] are instructions rather than variables, and that is a good use of the bracket. But the handful that are genuinely shared and spelled differently are the kind of inconsistency a reader notices and cannot explain, and nothing in the build has an opinion about any of it.
Three checks the content test could have and does not:
- Every
[has a matching]. Currently true in all 50 by hand, and an unbalanced bracket renders as literal text in somebody's email, which is exactly the failure that gets screenshotted. - A shared vocabulary: a small set of canonical placeholder names, with anything outside it needing to be lowercase prose. That turns
[Product]into a test failure rather than a thing I notice while writing a blog post. - No tabs and no leading or trailing whitespace in a body. All 50 are clean today, and
whitespace-pre-linemeans the day one is not, the email has a stray indent in it.
One divergence worth knowing about
The test reads body text through plain(), which strips the rich markup. Template bodies are not rich, so plain() is a no-op on them, right up until somebody writes [label](url) in a template. On that day the test sees "label" and the page renders the raw [label](url), because the component prints the string as it is. The text being checked and the text being shown would differ, in the one field on the site where they are supposed to be identical.
It is a one-line fix (do not run plain() on a template body) and I would not have found it by reading either file on its own. It only shows up when you line up the three renderings and ask which of them the test is actually looking at.
Have a look at the output rather than the shape: the templates index lists the six new pages, the contract outline holds the longest single one at 46 lines, the brief has the next two at 42 and 34, and the outreach emails are seven templates on one page with a copy button each. The brackets you see are the 267.
來源:DEV Community · dev.to