@prunellejs/mail 0.7.3 → 0.7.4
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/package.json +2 -2
- package/src/render.ts +79 -7
- package/src/transports.ts +2 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/mail",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "Zero-dependency email delivery (JSX/TSX templates, SMTP/SMTPS, Resend, Postmark, MAIL_URL, @prunellejs/jobs retries) and dev outbox/preview plugin for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,6 +21,6 @@
|
|
|
21
21
|
"directory": "packages/plugin-mail"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"prunelle": "^0.7.
|
|
24
|
+
"prunelle": "^0.7.4"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/src/render.ts
CHANGED
|
@@ -27,15 +27,21 @@ const HEAD_BLOCK_RE = /<head\b[^>]*>[\s\S]*?<\/head>/gi
|
|
|
27
27
|
const STYLE_OR_SCRIPT_RE = /<(style|script|title)\b[^>]*>[\s\S]*?<\/\1>/gi
|
|
28
28
|
const PREVIEW_BLOCK_RE = /<div\b[^>]*\bdata-pru-preview\b[^>]*>[\s\S]*?<\/div>/gi
|
|
29
29
|
const HTML_COMMENT_RE = /<!--[\s\S]*?-->/g
|
|
30
|
-
const
|
|
31
|
-
const
|
|
30
|
+
const PRE_RE = /<pre\b(?:[^>"']|"[^"]*"|'[^']*')*>([\s\S]*?)<\/pre>/gi
|
|
31
|
+
const ANCHOR_RE = /<a\b((?:[^>"']|"[^"]*"|'[^']*')*)>([\s\S]*?)<\/a>/gi
|
|
32
|
+
const IMG_RE = /<img\b((?:[^>"']|"[^"]*"|'[^']*')*)\/?>/gi
|
|
33
|
+
const HREF_ATTR_RE = /(?:^|[\s/])href\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i
|
|
34
|
+
const ALT_ATTR_RE = /(?:^|[\s/])alt\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i
|
|
35
|
+
const IMG_TAG_START_RE = /^<img(?:\s|\/|>)/i
|
|
36
|
+
const BR_TAG_RE = /^<br(?:\s|\/|>)/i
|
|
32
37
|
const HR_RE = /<hr\b[^>]*\/?>/gi
|
|
33
38
|
const BR_RE = /<br\b[^>]*\/?>/gi
|
|
34
39
|
const LIST_ITEM_RE = /<li\b[^>]*>/gi
|
|
40
|
+
const CELL_CLOSE_RE = /<\/(?:td|th)>/gi
|
|
35
41
|
const DOUBLE_BREAK_OPEN_RE = /<(?:h[1-6]|p|blockquote|ul|ol)\b[^>]*>/gi
|
|
36
42
|
const DOUBLE_BREAK_CLOSE_RE = /<\/(?:h[1-6]|p|blockquote|ul|ol|table|section|header|footer)>/gi
|
|
37
43
|
const SINGLE_BREAK_CLOSE_RE = /<\/(?:div|tr)>/gi
|
|
38
|
-
const TAG_RE = /<[^>]+>/g
|
|
44
|
+
const TAG_RE = /<(?:[^>"']|"[^"]*"|'[^']*')+>/g
|
|
39
45
|
const ENTITY_RE = /&(#x[0-9a-f]+|#\d+|[a-z]+);/gi
|
|
40
46
|
const MULTI_SPACE_RE = /[ \t\f\v]+/g
|
|
41
47
|
const MULTI_BLANK_LINE_RE = /\n{3,}/g
|
|
@@ -74,11 +80,56 @@ export function decodeHtmlEntities(text: string): string {
|
|
|
74
80
|
})
|
|
75
81
|
}
|
|
76
82
|
|
|
77
|
-
function
|
|
83
|
+
function extractImgAlt(attrs: string): string {
|
|
84
|
+
const match = ALT_ATTR_RE.exec(attrs)
|
|
85
|
+
const rawAlt = match !== null ? (match[1] ?? match[2] ?? match[3] ?? '') : ''
|
|
86
|
+
return decodeHtmlEntities(rawAlt).replace(MULTI_SPACE_RE, ' ').trim()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function extractTextFromFragment(fragment: string, preserveWhitespace: boolean): string {
|
|
90
|
+
let out = ''
|
|
91
|
+
let last = 0
|
|
92
|
+
TAG_RE.lastIndex = 0
|
|
93
|
+
for (let m = TAG_RE.exec(fragment); m !== null; m = TAG_RE.exec(fragment)) {
|
|
94
|
+
if (m.index > last)
|
|
95
|
+
out += decodeHtmlEntities(fragment.slice(last, m.index))
|
|
96
|
+
const tag = m[0]
|
|
97
|
+
if (IMG_TAG_START_RE.test(tag))
|
|
98
|
+
out += extractImgAlt(tag.slice(4, -1))
|
|
99
|
+
else if (BR_TAG_RE.test(tag))
|
|
100
|
+
out += preserveWhitespace ? '\n' : ' '
|
|
101
|
+
last = m.index + tag.length
|
|
102
|
+
}
|
|
103
|
+
if (last < fragment.length)
|
|
104
|
+
out += decodeHtmlEntities(fragment.slice(last))
|
|
105
|
+
return preserveWhitespace ? out : out.replace(MULTI_SPACE_RE, ' ').trim()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function createPlaceholders(source: string) {
|
|
109
|
+
let sentinel = '\uE000'
|
|
110
|
+
while (source.includes(sentinel))
|
|
111
|
+
sentinel += '\uE000'
|
|
112
|
+
const values: string[] = []
|
|
113
|
+
return {
|
|
114
|
+
add(value: string): string {
|
|
115
|
+
const idx = values.length
|
|
116
|
+
values.push(value)
|
|
117
|
+
return `${sentinel}${idx}${sentinel}`
|
|
118
|
+
},
|
|
119
|
+
restore(text: string): string {
|
|
120
|
+
if (values.length === 0)
|
|
121
|
+
return text
|
|
122
|
+
const re = new RegExp(`${sentinel}(\\d+)${sentinel}`, 'g')
|
|
123
|
+
return text.replace(re, (_m, idx: string) => values[Number(idx)]!)
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function formatAnchorText(attrs: string, inner: string): string {
|
|
78
129
|
const hrefMatch = HREF_ATTR_RE.exec(attrs)
|
|
79
130
|
const rawHref = hrefMatch !== null ? (hrefMatch[1] ?? hrefMatch[2] ?? hrefMatch[3] ?? '') : ''
|
|
80
131
|
const href = decodeHtmlEntities(rawHref).trim()
|
|
81
|
-
const label =
|
|
132
|
+
const label = extractTextFromFragment(inner, false)
|
|
82
133
|
if (href.length === 0)
|
|
83
134
|
return label
|
|
84
135
|
if (label.length === 0)
|
|
@@ -89,18 +140,38 @@ function replaceAnchorWithText(_full: string, attrs: string, inner: string): str
|
|
|
89
140
|
return `${label} (${href})`
|
|
90
141
|
}
|
|
91
142
|
|
|
143
|
+
function formatPreText(inner: string): string {
|
|
144
|
+
const decoded = extractTextFromFragment(inner, true).replace(/\r\n?/g, '\n')
|
|
145
|
+
const stripped = decoded.startsWith('\n') ? decoded.slice(1) : decoded
|
|
146
|
+
return stripped.replace(/\n+$/, '')
|
|
147
|
+
}
|
|
148
|
+
|
|
92
149
|
/** Converts email HTML into a clean, readable plain-text alternative part. */
|
|
93
150
|
export function htmlToText(html: string): string {
|
|
151
|
+
const placeholders = createPlaceholders(html)
|
|
152
|
+
|
|
94
153
|
let out = html
|
|
95
154
|
.replace(DOCTYPE_RE, '')
|
|
96
155
|
.replace(HEAD_BLOCK_RE, '')
|
|
97
156
|
.replace(STYLE_OR_SCRIPT_RE, '')
|
|
98
157
|
.replace(PREVIEW_BLOCK_RE, '')
|
|
99
158
|
.replace(HTML_COMMENT_RE, '')
|
|
100
|
-
.replace(
|
|
159
|
+
.replace(PRE_RE, (_full, inner: string) => {
|
|
160
|
+
const text = formatPreText(inner)
|
|
161
|
+
return text.length > 0 ? `\n\n${placeholders.add(text)}\n\n` : ''
|
|
162
|
+
})
|
|
163
|
+
.replace(ANCHOR_RE, (_full, attrs: string, inner: string) => {
|
|
164
|
+
const text = formatAnchorText(attrs, inner)
|
|
165
|
+
return text.length > 0 ? placeholders.add(text) : ''
|
|
166
|
+
})
|
|
167
|
+
.replace(IMG_RE, (_full, attrs: string) => {
|
|
168
|
+
const alt = extractImgAlt(attrs)
|
|
169
|
+
return alt.length > 0 ? placeholders.add(alt) : ''
|
|
170
|
+
})
|
|
101
171
|
.replace(HR_RE, '\n\n---\n\n')
|
|
102
172
|
.replace(BR_RE, '\n')
|
|
103
173
|
.replace(LIST_ITEM_RE, '\n- ')
|
|
174
|
+
.replace(CELL_CLOSE_RE, ' ')
|
|
104
175
|
.replace(DOUBLE_BREAK_OPEN_RE, '\n\n')
|
|
105
176
|
.replace(DOUBLE_BREAK_CLOSE_RE, '\n\n')
|
|
106
177
|
.replace(SINGLE_BREAK_CLOSE_RE, '\n')
|
|
@@ -109,7 +180,8 @@ export function htmlToText(html: string): string {
|
|
|
109
180
|
out = decodeHtmlEntities(out).replace(/\r\n?/g, '\n')
|
|
110
181
|
|
|
111
182
|
const lines = out.split('\n').map(line => line.replace(MULTI_SPACE_RE, ' ').trim())
|
|
112
|
-
|
|
183
|
+
const normalized = lines.join('\n').replace(MULTI_BLANK_LINE_RE, '\n\n').trim()
|
|
184
|
+
return placeholders.restore(normalized)
|
|
113
185
|
}
|
|
114
186
|
|
|
115
187
|
/** Renders a JSX/TSX email component, compiled Html, or lazy factory to both `html` and `text`. */
|
package/src/transports.ts
CHANGED
|
@@ -199,10 +199,9 @@ const TLS_PARAM_MODES: Readonly<Record<string, { requireTls: boolean | undefined
|
|
|
199
199
|
function parseTlsParam(value: string | null): { requireTls: boolean | undefined, disableStartTls: boolean } {
|
|
200
200
|
if (value === null)
|
|
201
201
|
return { requireTls: undefined, disableStartTls: false }
|
|
202
|
-
|
|
203
|
-
if (mode === undefined)
|
|
202
|
+
if (!Object.hasOwn(TLS_PARAM_MODES, value))
|
|
204
203
|
throw new RangeError(`@prunellejs/mail: invalid tls value "${value}" in MAIL_URL: use true, opportunistic or false`)
|
|
205
|
-
return
|
|
204
|
+
return TLS_PARAM_MODES[value]!
|
|
206
205
|
}
|
|
207
206
|
|
|
208
207
|
/** Builds a `MailTransport` from a `MAIL_URL` connection string (`smtp://`, `smtps://`, `resend://`, or `postmark://`). */
|