-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutlook-export.js
More file actions
223 lines (208 loc) · 8.4 KB
/
Copy pathoutlook-export.js
File metadata and controls
223 lines (208 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Outlook-friendly HTML export builder.
*
* Why a separate module?
* When a user pastes an HTML file into a *new* Outlook email, Outlook's
* compose surface (the Word HTML renderer on Windows; a sanitized
* renderer on Mac/Web/Mobile) aggressively strips `<style>` blocks,
* `[data-ogsc]` selectors, `@media (prefers-color-scheme)` rules, CSS
* variables, and most class-based selectors. The only things that
* reliably survive are:
* - Inline `style="..."` on every element
* - Legacy HTML4 attributes (`bgcolor`, `align`, `valign`, `width`)
* - `mso-*` properties inside inline styles
*
* So this module emits a table where EVERY cell carries inline colors
* and a redundant `bgcolor` attribute. It commits to a single high-
* contrast light palette (Outlook's own dark-mode rendering handles the
* conversion more reliably than our own dark CSS would).
*
* Usage:
* const html = window.OutlookExport.buildHtml({
* title: 'Power Platform Release Plan',
* subtitle: 'Exported … • 35 features',
* columns: ['Product', 'Feature Name', ...],
* rows: [
* ['Power Apps', 'Improve bulk delete …', ...],
* ...
* ],
* });
* window.OutlookExport.download({ ..., filename: 'rpe-2026-06-15.html' });
*/
(function () {
'use strict';
// ── Palette (must work both standalone in a browser AND when pasted into Outlook) ──
const C = {
bg: '#ffffff',
text: '#1f2937',
muted: '#6b7280',
headerBg: '#0078d4',
headerTxt: '#ffffff',
headerBd: '#005a9e',
rowAlt: '#f3f6fb',
border: '#d1d5db',
cellBd: '#e5e7eb',
link: '#0067b8',
};
function escapeHtml(s) {
return String(s == null ? '' : s)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function escapeAttr(s) { return escapeHtml(s); }
// Wrap raw HTML/text so it survives both the browser AND Outlook compose.
// - `escape: true` (default) HTML-escapes; pass false if `value` is already
// trusted HTML (e.g. a sanitized `<a>` link).
// - `nowrap: true` keeps the cell on one line (use for short IDs that have
// no break opportunities, e.g. "MC1383792").
function cell(value, opts) {
opts = opts || {};
const isEven = !!opts.even;
const bg = isEven ? C.rowAlt : C.bg;
const align = opts.align || 'left';
const valign = opts.valign || 'top';
const nowrap = !!opts.nowrap;
const inner = opts.escape === false ? String(value || '') : escapeHtml(value || '—');
const style = [
'background-color:' + bg,
'color:' + C.text,
'border:1px solid ' + C.cellBd,
'padding:8px 12px',
'vertical-align:' + valign,
'text-align:' + align,
'mso-line-height-rule:exactly',
'line-height:1.4',
'font-size:13px',
"font-family:'Segoe UI',Calibri,Arial,sans-serif",
nowrap ? 'white-space:nowrap' : 'word-break:break-word',
].join(';') + ';';
const nowrapAttr = nowrap ? ' nowrap="nowrap"' : '';
return '<td bgcolor="' + bg + '" align="' + align + '" valign="' + valign
+ '"' + nowrapAttr + ' style="' + style + '">' + (inner || '—') + '</td>';
}
function headerCell(label) {
const style = [
'background-color:' + C.headerBg,
'color:' + C.headerTxt,
'border:1px solid ' + C.headerBd,
'padding:9px 12px',
'vertical-align:top',
'text-align:left',
'font-weight:600',
'font-size:13px',
"font-family:'Segoe UI',Calibri,Arial,sans-serif",
'mso-line-height-rule:exactly',
'line-height:1.35',
'white-space:nowrap',
].join(';') + ';';
return '<th bgcolor="' + C.headerBg + '" align="left" valign="top" style="'
+ style + '">' + escapeHtml(label) + '</th>';
}
// Build a single anchor that survives Outlook compose.
function link(href, label) {
if (!href) return '—';
const h = escapeAttr(href);
const l = escapeHtml(label || href);
return '<a href="' + h + '" target="_blank" rel="noopener noreferrer" '
+ 'style="color:' + C.link + ';text-decoration:underline;">' + l + '</a>';
}
function buildHtml(spec) {
spec = spec || {};
const title = spec.title || 'Export';
const subtitle = spec.subtitle || '';
const columns = spec.columns || [];
const rows = spec.rows || [];
const thead = '<tr>' + columns.map(headerCell).join('') + '</tr>';
const tbody = rows.map((cells, i) => {
const even = i % 2 === 1;
const cellsHtml = cells.map(c => {
// Accept either a plain string (escape it) or { html, align, valign, nowrap }
// where `html` is pre-escaped trusted markup (e.g. an <a> link).
if (c && typeof c === 'object' && 'html' in c) {
return cell(c.html, { even, escape: false, align: c.align, valign: c.valign, nowrap: c.nowrap });
}
if (c && typeof c === 'object' && 'value' in c) {
return cell(c.value, { even, align: c.align, valign: c.valign, nowrap: c.nowrap });
}
return cell(c, { even });
}).join('');
return '<tr>' + cellsHtml + '</tr>';
}).join('\n');
const tableStyle = [
'border-collapse:collapse',
'border-spacing:0',
'width:100%',
'background-color:' + C.bg,
'color:' + C.text,
'border:1px solid ' + C.border,
'mso-table-lspace:0pt',
'mso-table-rspace:0pt',
"font-family:'Segoe UI',Calibri,Arial,sans-serif",
'font-size:13px',
].join(';') + ';';
const bodyStyle = [
'margin:0',
'padding:24px',
'background-color:' + C.bg,
'color:' + C.text,
"font-family:'Segoe UI',Calibri,Arial,sans-serif",
'font-size:13px',
'mso-line-height-rule:exactly',
'line-height:1.45',
].join(';') + ';';
const h2Style = 'font-size:18px;font-weight:600;margin:0 0 4px;color:' + C.text
+ ";font-family:'Segoe UI',Calibri,Arial,sans-serif;";
const pStyle = 'font-size:12px;color:' + C.muted + ';margin:0 0 14px;'
+ "font-family:'Segoe UI',Calibri,Arial,sans-serif;";
return '<!DOCTYPE html>\n'
+ '<html lang="en">\n'
+ '<head>\n'
+ ' <meta charset="UTF-8">\n'
+ ' <meta name="viewport" content="width=device-width, initial-scale=1.0">\n'
+ ' <meta name="color-scheme" content="light only">\n'
+ ' <meta name="supported-color-schemes" content="light">\n'
+ ' <title>' + escapeHtml(title) + '</title>\n'
+ ' <!--[if mso]>\n'
+ ' <style type="text/css">\n'
+ ' table { border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt; }\n'
+ ' td,th { mso-line-height-rule:exactly; }\n'
+ ' </style>\n'
+ ' <![endif]-->\n'
+ ' <style>\n'
+ ' /* Standalone browser fallback only — Outlook strips this. */\n'
+ ' body { background:' + C.bg + '; color:' + C.text + "; font-family:'Segoe UI',Calibri,Arial,sans-serif; }\n"
+ ' </style>\n'
+ '</head>\n'
+ '<body bgcolor="' + C.bg + '" style="' + bodyStyle + '">\n'
+ ' <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%" '
+ 'bgcolor="' + C.bg + '" style="background-color:' + C.bg + ';">\n'
+ ' <tr><td bgcolor="' + C.bg + '" style="background-color:' + C.bg + ';padding:0;">\n'
+ ' <h2 style="' + h2Style + '">' + escapeHtml(title) + '</h2>\n'
+ (subtitle ? ' <p style="' + pStyle + '">' + escapeHtml(subtitle) + '</p>\n' : '')
+ ' <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%" '
+ 'bgcolor="' + C.bg + '" style="' + tableStyle + '">\n'
+ ' <thead>' + thead + '</thead>\n'
+ ' <tbody>\n' + tbody + '\n </tbody>\n'
+ ' </table>\n'
+ ' </td></tr>\n'
+ ' </table>\n'
+ '</body>\n'
+ '</html>\n';
}
function download(spec) {
const html = buildHtml(spec);
const blob = new Blob([html], { type: 'text/html;charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = spec.filename || ('export-' + new Date().toISOString().slice(0, 10) + '.html');
document.body.appendChild(a); a.click();
document.body.removeChild(a);
setTimeout(() => URL.revokeObjectURL(url), 5000);
return html;
}
window.OutlookExport = { buildHtml, download, link, escapeHtml };
})();