@meith/mail 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@meith/mail",
3
+ "version": "0.16.0",
4
+ "license": "LGPL-3.0-or-later",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/meith-dev/meith.git",
8
+ "directory": "packages/mail"
9
+ },
10
+ "type": "module",
11
+ "main": "./src/index.ts",
12
+ "types": "./src/index.ts",
13
+ "files": [
14
+ "src",
15
+ "!src/**/*.test.*",
16
+ "!src/**/*.type-test.*"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "dependencies": {
22
+ "@meith/i18n": "0.16.0",
23
+ "@meith/markdown": "0.16.0",
24
+ "@meith/theme-kit": "0.16.0",
25
+ "@meith/settings": "0.16.0"
26
+ }
27
+ }
package/src/brand.ts ADDED
@@ -0,0 +1,82 @@
1
+ import { isLogoKey, type LogoScheme, logoFormat, logoPath } from '@meith/settings'
2
+
3
+ import { FALLBACK_PALETTE, type MailPalette, mailPalette, type ThemeTokenSet } from './palette'
4
+
5
+ export interface MailLogo {
6
+ readonly src: string
7
+ readonly darkSrc: string | null
8
+ readonly alt: string
9
+ }
10
+
11
+ export interface MailBrand {
12
+ readonly boardName: string
13
+ readonly fromName?: string
14
+ readonly boardUrl: string
15
+ readonly logo: MailLogo | null
16
+ readonly palette: MailPalette
17
+ }
18
+
19
+ const UNRENDERABLE_IN_MAIL: readonly string[] = ['svg']
20
+
21
+ export function normaliseBoardUrl(value: string): string {
22
+ const trimmed = value.trim().replace(/\/+$/, '')
23
+ return /^https?:\/\/[^\s/]+$/i.test(trimmed) ? trimmed : ''
24
+ }
25
+
26
+ export function absoluteUrl(boardUrl: string, path: string | null): string | null {
27
+ if (boardUrl === '' || path === null || !path.startsWith('/')) return null
28
+ return `${boardUrl}${path}`
29
+ }
30
+
31
+ function renderable(key: string): boolean {
32
+ const format = logoFormat(key)
33
+ return format !== null && !UNRENDERABLE_IN_MAIL.includes(format)
34
+ }
35
+
36
+ function usableKey(value: string): string | null {
37
+ return isLogoKey(value) && renderable(value) ? value : null
38
+ }
39
+
40
+ export function mailLogo(input: {
41
+ readonly boardUrl: string
42
+ readonly lightKey: string
43
+ readonly darkKey: string
44
+ readonly alt: string
45
+ }): MailLogo | null {
46
+ const boardUrl = normaliseBoardUrl(input.boardUrl)
47
+ if (boardUrl === '' || input.alt.trim() === '') return null
48
+
49
+ const light = usableKey(input.lightKey)
50
+ const dark = usableKey(input.darkKey)
51
+ if (light === null && dark === null) return null
52
+
53
+ const source = (scheme: LogoScheme, key: string): string => `${boardUrl}${logoPath(scheme, key)}`
54
+
55
+ const lightSrc = light === null ? source('dark', dark!) : source('light', light)
56
+ const darkSrc = dark === null ? source('light', light!) : source('dark', dark)
57
+
58
+ return { src: lightSrc, darkSrc: darkSrc === lightSrc ? null : darkSrc, alt: input.alt.trim() }
59
+ }
60
+
61
+ export function buildMailBrand(input: {
62
+ readonly boardName: string
63
+ readonly fromName?: string
64
+ readonly boardUrl: string
65
+ readonly tokens?: ThemeTokenSet | null
66
+ readonly logo?: {
67
+ readonly lightKey: string
68
+ readonly darkKey: string
69
+ readonly alt: string
70
+ }
71
+ }): MailBrand {
72
+ const boardUrl = normaliseBoardUrl(input.boardUrl)
73
+ const fromName = (input.fromName ?? '').trim()
74
+
75
+ return {
76
+ boardName: input.boardName.trim(),
77
+ ...(fromName === '' ? {} : { fromName }),
78
+ boardUrl,
79
+ logo: input.logo === undefined ? null : mailLogo({ boardUrl, ...input.logo }),
80
+ palette: input.tokens === undefined ? FALLBACK_PALETTE : mailPalette(input.tokens),
81
+ }
82
+ }
@@ -0,0 +1,290 @@
1
+ import type { Translator } from '@meith/i18n'
2
+ import { escapeAttribute, escapeHtml } from '@meith/markdown'
3
+
4
+ import type { MailBrand } from './brand'
5
+ import type { MailScheme } from './palette'
6
+
7
+ export interface MailLink {
8
+ readonly label: string
9
+ readonly href: string
10
+ }
11
+
12
+ export interface MailFooterLine {
13
+ readonly text: string
14
+ readonly href?: string | null
15
+ }
16
+
17
+ export interface MailBody {
18
+ readonly title: string
19
+ readonly greeting?: string | null
20
+ readonly paragraphs: readonly string[]
21
+ readonly action?: MailLink | null
22
+ readonly note?: string | null
23
+ readonly footer?: readonly MailFooterLine[]
24
+ }
25
+
26
+ export interface RenderedMail {
27
+ readonly subject: string
28
+ readonly text: string
29
+ readonly html: string
30
+ }
31
+
32
+ const CARD_WIDTH = 600
33
+
34
+ const LOGO_HEIGHT = 36
35
+
36
+ export function safeHref(value: string | null | undefined): string | null {
37
+ if (typeof value !== 'string') return null
38
+
39
+ const trimmed = value.trim()
40
+ if (!/^https?:\/\/[^\s<>\x22\x27]+$/i.test(trimmed)) return null
41
+
42
+ return trimmed
43
+ }
44
+
45
+ function paragraphs(body: MailBody): readonly string[] {
46
+ return body.paragraphs.filter((paragraph) => paragraph.trim() !== '')
47
+ }
48
+
49
+ function footerLines(body: MailBody): readonly MailFooterLine[] {
50
+ return (body.footer ?? []).filter((line) => line.text.trim() !== '')
51
+ }
52
+
53
+ function preheader(body: MailBody): string {
54
+ return (paragraphs(body)[0] ?? body.title).replace(/\s+/g, ' ').slice(0, 140)
55
+ }
56
+
57
+ export function renderMailText(input: {
58
+ readonly brand: MailBrand
59
+ readonly body: MailBody
60
+ readonly boardName: string
61
+ }): string {
62
+ const { body } = input
63
+ const lines: string[] = []
64
+
65
+ if (typeof body.greeting === 'string' && body.greeting.trim() !== '') {
66
+ lines.push(body.greeting, '')
67
+ }
68
+ lines.push(body.title)
69
+
70
+ for (const paragraph of paragraphs(body)) lines.push('', paragraph)
71
+
72
+ const action = body.action === null || body.action === undefined ? null : body.action
73
+ const actionHref = action === null ? null : safeHref(action.href)
74
+ if (action !== null && actionHref !== null) lines.push('', `${action.label}: ${actionHref}`)
75
+
76
+ if (typeof body.note === 'string' && body.note.trim() !== '') lines.push('', body.note)
77
+
78
+ const footer = footerLines(body)
79
+ if (footer.length > 0) {
80
+ lines.push('', '--')
81
+ for (const line of footer) {
82
+ const href = safeHref(line.href)
83
+ lines.push(href === null ? line.text : `${line.text}: ${href}`)
84
+ }
85
+ }
86
+
87
+ return lines.join('\n')
88
+ }
89
+
90
+ function styleSheet(dark: MailScheme): string {
91
+ return [
92
+ `.m-page{margin:0;padding:0;width:100%!important}`,
93
+ `img{border:0;outline:none;text-decoration:none;-ms-interpolation-mode:bicubic}`,
94
+ `a{text-decoration:none}`,
95
+ `.m-logo-dark{display:none}`,
96
+ `@media (max-width:620px){`,
97
+ `.m-pad{padding-left:20px!important;padding-right:20px!important}`,
98
+ `}`,
99
+ `@media (prefers-color-scheme:dark){`,
100
+ `.m-page,.m-page-cell{background-color:${dark.page}!important}`,
101
+ `.m-card{background-color:${dark.card}!important;border-color:${dark.border}!important}`,
102
+ `.m-band{background-color:${dark.band}!important;border-color:${dark.border}!important}`,
103
+ `.m-title,.m-text,.m-wordmark{color:${dark.text}!important}`,
104
+ `.m-muted,.m-foot,.m-foot a{color:${dark.muted}!important}`,
105
+ `.m-rule{border-color:${dark.border}!important}`,
106
+ `.m-action{background-color:${dark.primary}!important}`,
107
+ `.m-action a,.m-action-label{color:${dark.primaryText}!important}`,
108
+ `.m-link{color:${dark.text}!important;text-decoration-color:${dark.primary}!important}`,
109
+ `.m-accent{background-color:${dark.primary}!important}`,
110
+ `.m-logo-light{display:none!important}`,
111
+ `.m-logo-dark{display:inline-block!important}`,
112
+ `}`,
113
+ ].join('')
114
+ }
115
+
116
+ function masthead(brand: MailBrand, boardName: string, light: MailScheme, font: string): string {
117
+ const home = safeHref(brand.boardUrl)
118
+ const wordmark =
119
+ `<span class="m-wordmark" style="font-family:${font};font-size:19px;` +
120
+ `font-weight:700;letter-spacing:-0.01em;color:${light.text}">${escapeHtml(boardName)}</span>`
121
+
122
+ const inner =
123
+ brand.logo === null
124
+ ? wordmark
125
+ : logoImage(brand.logo.src, brand.logo.alt, `m-logo-light`) +
126
+ (brand.logo.darkSrc === null
127
+ ? ''
128
+ : logoImage(brand.logo.darkSrc, brand.logo.alt, `m-logo-dark`))
129
+
130
+ return home === null
131
+ ? inner
132
+ : `<a href="${escapeAttribute(home)}" style="text-decoration:none">${inner}</a>`
133
+ }
134
+
135
+ function logoImage(src: string, alt: string, className: string): string {
136
+ const href = safeHref(src)
137
+ if (href === null) return ''
138
+
139
+ return (
140
+ `<img src="${escapeAttribute(href)}" alt="${escapeAttribute(alt)}" height="${LOGO_HEIGHT}" ` +
141
+ `class="${className}" style="display:inline-block;height:${LOGO_HEIGHT}px;` +
142
+ `max-height:${LOGO_HEIGHT}px;width:auto;border:0">`
143
+ )
144
+ }
145
+
146
+ function actionButton(
147
+ action: MailLink,
148
+ href: string,
149
+ light: MailScheme,
150
+ font: string,
151
+ radius: number,
152
+ ): string {
153
+ return (
154
+ `<table role="presentation" border="0" cellpadding="0" cellspacing="0">` +
155
+ `<tr><td class="m-action" align="center" ` +
156
+ `style="background-color:${light.primary};border-radius:${radius}px">` +
157
+ `<a href="${escapeAttribute(href)}" class="m-action-label" ` +
158
+ `style="display:inline-block;padding:13px 26px;font-family:${font};font-size:15px;` +
159
+ `font-weight:600;line-height:1;color:${light.primaryText};text-decoration:none">` +
160
+ `${escapeHtml(action.label)}</a></td></tr></table>`
161
+ )
162
+ }
163
+
164
+ export function renderMailHtml(input: {
165
+ readonly brand: MailBrand
166
+ readonly body: MailBody
167
+ readonly boardName: string
168
+ readonly subject: string
169
+ readonly t: Translator
170
+ }): string {
171
+ const { brand, body, boardName, t } = input
172
+ const { light, bodyFont, headingFont, radius } = {
173
+ light: brand.palette.light,
174
+ bodyFont: brand.palette.bodyFont,
175
+ headingFont: brand.palette.headingFont,
176
+ radius: brand.palette.radius,
177
+ }
178
+
179
+ const text = `font-family:${bodyFont};font-size:15px;line-height:1.6;color:${light.text}`
180
+ const parts: string[] = []
181
+
182
+ parts.push(
183
+ `<!doctype html><html lang="${escapeAttribute(t.locale)}">`,
184
+ `<head><meta charset="utf-8">`,
185
+ `<meta name="viewport" content="width=device-width,initial-scale=1">`,
186
+ `<meta name="color-scheme" content="light dark">`,
187
+ `<meta name="supported-color-schemes" content="light dark">`,
188
+ `<title>${escapeHtml(input.subject)}</title>`,
189
+ `<style>${styleSheet(brand.palette.dark)}</style></head>`,
190
+ `<body class="m-page" style="margin:0;padding:0;background-color:${light.page};${text}">`,
191
+ `<div style="display:none;max-height:0;overflow:hidden;opacity:0">`,
192
+ `${escapeHtml(preheader(body))}</div>`,
193
+ `<table role="presentation" width="100%" border="0" cellpadding="0" cellspacing="0" `,
194
+ `style="background-color:${light.page}"><tr>`,
195
+ `<td class="m-page-cell" align="center" style="padding:32px 12px;background-color:${light.page}">`,
196
+ `<table role="presentation" class="m-card" width="${CARD_WIDTH}" border="0" cellpadding="0" `,
197
+ `cellspacing="0" style="width:100%;max-width:${CARD_WIDTH}px;background-color:${light.card};`,
198
+ `border:1px solid ${light.border};border-radius:${radius + 4}px;overflow:hidden">`,
199
+ `<tr><td class="m-accent" style="height:4px;line-height:4px;font-size:0;`,
200
+ `background-color:${light.primary}">&nbsp;</td></tr>`,
201
+ `<tr><td class="m-band m-pad" align="left" style="padding:20px 32px;`,
202
+ `background-color:${light.band};border-bottom:1px solid ${light.border}">`,
203
+ `${masthead(brand, boardName, light, headingFont)}</td></tr>`,
204
+ `<tr><td class="m-pad" style="padding:32px">`,
205
+ `<h1 class="m-title" style="margin:0 0 20px;font-family:${headingFont};font-size:21px;`,
206
+ `line-height:1.3;font-weight:700;color:${light.text}">${escapeHtml(body.title)}</h1>`,
207
+ )
208
+
209
+ if (typeof body.greeting === 'string' && body.greeting.trim() !== '') {
210
+ parts.push(`<p class="m-text" style="margin:0 0 16px;${text}">`)
211
+ parts.push(`${escapeHtml(body.greeting)}</p>`)
212
+ }
213
+
214
+ for (const paragraph of paragraphs(body)) {
215
+ parts.push(
216
+ `<p class="m-text" style="margin:0 0 16px;${text}">`,
217
+ `${escapeHtml(paragraph).replace(/\n/g, `<br>`)}</p>`,
218
+ )
219
+ }
220
+
221
+ const action = body.action ?? null
222
+ const actionHref = action === null ? null : safeHref(action.href)
223
+
224
+ if (action !== null && actionHref !== null) {
225
+ parts.push(
226
+ `<div style="margin:24px 0 20px">`,
227
+ actionButton(action, actionHref, light, bodyFont, radius),
228
+ `</div>`,
229
+ `<p class="m-muted" style="margin:0;font-size:13px;line-height:1.6;color:${light.muted};`,
230
+ `word-break:break-all">${escapeHtml(t.t('mail.action.fallback'))}<br>`,
231
+ `<a href="${escapeAttribute(actionHref)}" class="m-link" `,
232
+ `style="color:${light.text};text-decoration:underline;`,
233
+ `text-decoration-color:${light.primary}">${escapeHtml(actionHref)}</a></p>`,
234
+ )
235
+ }
236
+
237
+ if (typeof body.note === 'string' && body.note.trim() !== '') {
238
+ parts.push(
239
+ `<p class="m-muted" style="margin:20px 0 0;font-size:13px;line-height:1.6;`,
240
+ `color:${light.muted}">${escapeHtml(body.note)}</p>`,
241
+ )
242
+ }
243
+
244
+ parts.push(`</td></tr></table>`)
245
+
246
+ const footer = footerLines(body)
247
+ if (footer.length > 0) {
248
+ parts.push(
249
+ `<table role="presentation" width="${CARD_WIDTH}" border="0" cellpadding="0" `,
250
+ `cellspacing="0" style="width:100%;max-width:${CARD_WIDTH}px"><tr>`,
251
+ `<td class="m-foot m-pad" align="center" style="padding:20px 32px 0;font-family:${bodyFont};`,
252
+ `font-size:12px;line-height:1.7;color:${light.muted}">`,
253
+ )
254
+
255
+ for (const line of footer) {
256
+ const href = safeHref(line.href)
257
+ parts.push(
258
+ `<p style="margin:0">`,
259
+ href === null
260
+ ? escapeHtml(line.text)
261
+ : `<a href="${escapeAttribute(href)}" style="color:${light.muted};` +
262
+ `text-decoration:underline">${escapeHtml(line.text)}</a>`,
263
+ `</p>`,
264
+ )
265
+ }
266
+
267
+ parts.push(`</td></tr></table>`)
268
+ }
269
+
270
+ parts.push(`</td></tr></table></body></html>`)
271
+
272
+ return parts.join('')
273
+ }
274
+
275
+ export function renderMail(input: {
276
+ readonly brand: MailBrand
277
+ readonly body: MailBody
278
+ readonly t: Translator
279
+ }): RenderedMail {
280
+ const boardName =
281
+ input.brand.boardName === '' ? input.t.t('mail.boardFallback') : input.brand.boardName
282
+
283
+ const subject = `[${boardName}] ${input.body.title}`
284
+
285
+ return {
286
+ subject,
287
+ text: renderMailText({ brand: input.brand, body: input.body, boardName }),
288
+ html: renderMailHtml({ ...input, boardName, subject }),
289
+ }
290
+ }
package/src/index.ts ADDED
@@ -0,0 +1,27 @@
1
+ export {
2
+ absoluteUrl,
3
+ buildMailBrand,
4
+ type MailBrand,
5
+ type MailLogo,
6
+ mailLogo,
7
+ normaliseBoardUrl,
8
+ } from './brand'
9
+ export {
10
+ type MailBody,
11
+ type MailFooterLine,
12
+ type MailLink,
13
+ type RenderedMail,
14
+ renderMail,
15
+ renderMailHtml,
16
+ renderMailText,
17
+ safeHref,
18
+ } from './document'
19
+ export {
20
+ FALLBACK_PALETTE,
21
+ type MailPalette,
22
+ type MailScheme,
23
+ mailPalette,
24
+ mergeTokenOverrides,
25
+ type ThemeTokenSet,
26
+ type TokenRecord,
27
+ } from './palette'
package/src/palette.ts ADDED
@@ -0,0 +1,166 @@
1
+ import { colourToHex } from '@meith/theme-kit'
2
+
3
+ export type TokenRecord = Readonly<Record<string, string>>
4
+
5
+ export interface ThemeTokenSet {
6
+ readonly light: TokenRecord
7
+ readonly dark: TokenRecord
8
+ }
9
+
10
+ export interface MailScheme {
11
+ readonly page: string
12
+ readonly card: string
13
+ readonly band: string
14
+ readonly border: string
15
+ readonly text: string
16
+ readonly muted: string
17
+ readonly primary: string
18
+ readonly primaryText: string
19
+ }
20
+
21
+ export interface MailPalette {
22
+ readonly light: MailScheme
23
+ readonly dark: MailScheme
24
+ readonly bodyFont: string
25
+ readonly headingFont: string
26
+ readonly radius: number
27
+ }
28
+
29
+ const FALLBACK: { readonly light: MailScheme; readonly dark: MailScheme } = {
30
+ light: {
31
+ page: '#f4f4f4',
32
+ card: '#ffffff',
33
+ band: '#ebebeb',
34
+ border: '#dfdfdf',
35
+ text: '#171717',
36
+ muted: '#616161',
37
+ primary: '#047857',
38
+ primaryText: '#fafafa',
39
+ },
40
+ dark: {
41
+ page: '#0b0b0b',
42
+ card: '#151515',
43
+ band: '#0f0f0f',
44
+ border: '#303030',
45
+ text: '#f4f4f4',
46
+ muted: '#9f9f9f',
47
+ primary: '#35d399',
48
+ primaryText: '#04160f',
49
+ },
50
+ }
51
+
52
+ const SANS_FALLBACK = "ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Arial, sans-serif"
53
+
54
+ const MAX_RADIUS = 20
55
+
56
+ const ROOT_FONT_PX = 16
57
+
58
+ const VAR_REFERENCE = /^var\(\s*--([\w-]+)\s*\)$/
59
+
60
+ function dereference(tokens: TokenRecord, name: string, depth = 0): string {
61
+ const value = tokens[name]
62
+ if (value === undefined || depth > 4) return ''
63
+
64
+ const reference = VAR_REFERENCE.exec(value.trim())
65
+ return reference === null ? value.trim() : dereference(tokens, reference[1]!, depth + 1)
66
+ }
67
+
68
+ function colour(tokens: TokenRecord, names: readonly string[], fallback: string): string {
69
+ for (const name of names) {
70
+ const hex = colourToHex(dereference(tokens, name))
71
+ if (hex !== null) return hex
72
+ }
73
+ return fallback
74
+ }
75
+
76
+ function fontStack(tokens: TokenRecord, name: string): string {
77
+ const declared = dereference(tokens, name)
78
+ .split(',')
79
+ .map((segment) => segment.trim())
80
+ .map((segment) => segment.replace(/\x22/g, '\x27'))
81
+ .filter((segment) => segment !== '' && !segment.includes('var(') && !/[;{}<>]/.test(segment))
82
+
83
+ const fallbacks = SANS_FALLBACK.split(',').map((segment) => segment.trim())
84
+ const seen = new Set<string>()
85
+
86
+ return [...declared, ...fallbacks]
87
+ .filter((segment) => {
88
+ const key = segment.toLowerCase()
89
+ if (seen.has(key)) return false
90
+ seen.add(key)
91
+ return true
92
+ })
93
+ .join(', ')
94
+ }
95
+
96
+ function radiusPx(tokens: TokenRecord): number {
97
+ const declared = dereference(tokens, 'radius')
98
+ const match = /^([\d.]+)(rem|px)$/.exec(declared)
99
+ if (match === null) return 8
100
+
101
+ const value = Number(match[1]) * (match[2] === 'rem' ? ROOT_FONT_PX : 1)
102
+ return Number.isFinite(value) ? Math.round(Math.min(MAX_RADIUS, Math.max(0, value))) : 8
103
+ }
104
+
105
+ function scheme(tokens: TokenRecord, fallback: MailScheme): MailScheme {
106
+ return {
107
+ page: colour(tokens, ['background'], fallback.page),
108
+ card: colour(tokens, ['card', 'background'], fallback.card),
109
+ band: colour(tokens, ['surface', 'muted', 'card'], fallback.band),
110
+ border: colour(tokens, ['border'], fallback.border),
111
+ text: colour(tokens, ['card-foreground', 'foreground'], fallback.text),
112
+ muted: colour(tokens, ['muted-foreground', 'foreground'], fallback.muted),
113
+ primary: colour(tokens, ['primary'], fallback.primary),
114
+ primaryText: colour(tokens, ['primary-foreground'], fallback.primaryText),
115
+ }
116
+ }
117
+
118
+ export function mergeTokenOverrides(baseline: ThemeTokenSet, overrides: unknown): ThemeTokenSet {
119
+ if (overrides === null || typeof overrides !== 'object' || Array.isArray(overrides)) {
120
+ return baseline
121
+ }
122
+
123
+ const record = overrides as Record<string, unknown>
124
+ const shaped =
125
+ Object.keys(record).length > 0 &&
126
+ Object.keys(record).every((key) => key === 'light' || key === 'dark')
127
+
128
+ const flat = shaped ? {} : strings(record)
129
+
130
+ return {
131
+ light: { ...baseline.light, ...flat, ...(shaped ? strings(record.light) : {}) },
132
+ dark: { ...baseline.dark, ...flat, ...(shaped ? strings(record.dark) : {}) },
133
+ }
134
+ }
135
+
136
+ function strings(value: unknown): TokenRecord {
137
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return {}
138
+
139
+ return Object.fromEntries(
140
+ Object.entries(value as Record<string, unknown>).filter(
141
+ (entry): entry is [string, string] => typeof entry[1] === 'string' && entry[1].trim() !== '',
142
+ ),
143
+ )
144
+ }
145
+
146
+ export function mailPalette(tokens: ThemeTokenSet | null): MailPalette {
147
+ if (tokens === null) {
148
+ return {
149
+ light: FALLBACK.light,
150
+ dark: FALLBACK.dark,
151
+ bodyFont: SANS_FALLBACK,
152
+ headingFont: SANS_FALLBACK,
153
+ radius: 8,
154
+ }
155
+ }
156
+
157
+ return {
158
+ light: scheme(tokens.light, FALLBACK.light),
159
+ dark: scheme(tokens.dark, FALLBACK.dark),
160
+ bodyFont: fontStack(tokens.light, 'font-sans-stack'),
161
+ headingFont: fontStack(tokens.light, 'font-heading-stack'),
162
+ radius: radiusPx(tokens.light),
163
+ }
164
+ }
165
+
166
+ export const FALLBACK_PALETTE: MailPalette = mailPalette(null)