markdown-to-word 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +145 -2
- package/build/buffer-global.d.ts +1 -0
- package/build/buffer-global.js +9 -0
- package/build/buffer-global.js.map +1 -0
- package/build/converter.d.ts +20 -0
- package/build/converter.js +111 -0
- package/build/converter.js.map +1 -0
- package/build/docx-code-blocks.d.ts +24 -0
- package/build/docx-code-blocks.js +142 -0
- package/build/docx-code-blocks.js.map +1 -0
- package/build/docx-font-preset.d.ts +45 -0
- package/build/docx-font-preset.js +268 -0
- package/build/docx-font-preset.js.map +1 -0
- package/build/docx-html.d.ts +29 -0
- package/build/docx-html.js +75 -0
- package/build/docx-html.js.map +1 -0
- package/build/docx-structure.d.ts +22 -0
- package/build/docx-structure.js +99 -0
- package/build/docx-structure.js.map +1 -0
- package/build/font-presets.d.ts +94 -0
- package/build/font-presets.js +72 -0
- package/build/font-presets.js.map +1 -0
- package/build/main.d.ts +3 -0
- package/build/main.js +7 -0
- package/build/main.js.map +1 -0
- package/build/markdown-parser.d.ts +25 -0
- package/build/markdown-parser.js +95 -0
- package/build/markdown-parser.js.map +1 -0
- package/build/node.d.ts +1 -0
- package/build/node.js +15 -0
- package/build/node.js.map +1 -0
- package/build/xml-part.d.ts +14 -0
- package/build/xml-part.js +76 -0
- package/build/xml-part.js.map +1 -0
- package/package.json +92 -4
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import JSZip from 'jszip';
|
|
2
|
+
import { keepXmlDeclaration, paragraphProperty, WORDPROCESSING_NS as W_NS } from './xml-part.js';
|
|
3
|
+
/** Theme slots WinWord prefers over explicitly named fonts. */
|
|
4
|
+
const THEME_FONT_ATTRIBUTES = ['asciiTheme', 'eastAsiaTheme', 'hAnsiTheme', 'cstheme'];
|
|
5
|
+
/**
|
|
6
|
+
* `<w:line>` is ignored while the rule is auto, but WinWord still writes 240
|
|
7
|
+
* twips, so the preset writes the pair Word itself produces.
|
|
8
|
+
*/
|
|
9
|
+
const SINGLE_SPACING = { line: '240', rule: 'auto' };
|
|
10
|
+
/**
|
|
11
|
+
* Rewrites the fonts, sizes, paragraph spacing and page grid of an already
|
|
12
|
+
* generated .docx.
|
|
13
|
+
*
|
|
14
|
+
* `html-to-docx` only ever writes `w:ascii`, which WinWord applies to Latin
|
|
15
|
+
* characters and digits; Chinese characters fall back to the theme font. The
|
|
16
|
+
* East Asian slots are set here instead, so this stays out of the way of the
|
|
17
|
+
* default conversion output.
|
|
18
|
+
*
|
|
19
|
+
* The grid is measured from the page the document actually has, so it still
|
|
20
|
+
* fits when `pageMargins` overrode the preset's margins.
|
|
21
|
+
*/
|
|
22
|
+
export async function applyFontPreset(wordDocument, preset) {
|
|
23
|
+
const zip = await JSZip.loadAsync(await wordDocument.arrayBuffer());
|
|
24
|
+
const stylesEntry = zip.file('word/styles.xml');
|
|
25
|
+
if (!stylesEntry) {
|
|
26
|
+
throw new Error('Cannot apply font preset: document contains no word/styles.xml');
|
|
27
|
+
}
|
|
28
|
+
let grid = null;
|
|
29
|
+
const pageGrid = preset.page?.grid;
|
|
30
|
+
if (pageGrid ||
|
|
31
|
+
preset.paragraph.lineSpacing === 'grid' ||
|
|
32
|
+
preset.paragraph.firstLineIndentChars) {
|
|
33
|
+
const documentEntry = zip.file('word/document.xml');
|
|
34
|
+
if (!documentEntry) {
|
|
35
|
+
throw new Error('Cannot apply font preset: document contains no word/document.xml');
|
|
36
|
+
}
|
|
37
|
+
const documentXml = await documentEntry.async('string');
|
|
38
|
+
const patched = patchDocumentXml(documentXml, preset);
|
|
39
|
+
grid = patched.grid;
|
|
40
|
+
zip.file('word/document.xml', patched.xml);
|
|
41
|
+
}
|
|
42
|
+
const stylesXml = await stylesEntry.async('string');
|
|
43
|
+
zip.file('word/styles.xml', patchStylesXml(stylesXml, preset, grid));
|
|
44
|
+
const patched = await zip.generateAsync({ type: 'arraybuffer' });
|
|
45
|
+
return new Blob([patched], { type: wordDocument.type });
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Applies `preset` to the document default run properties and to the six
|
|
49
|
+
* heading styles of a styles.xml part. `grid` is the page grid that
|
|
50
|
+
* `patchDocumentXml` set, which `lineSpacing: 'grid'` takes its pitch from.
|
|
51
|
+
*/
|
|
52
|
+
export function patchStylesXml(stylesXml, preset, grid = null) {
|
|
53
|
+
const dom = new DOMParser().parseFromString(stylesXml, 'application/xml');
|
|
54
|
+
const layout = resolveLayout(preset, grid);
|
|
55
|
+
const docDefaults = requireElement(dom, 'docDefaults');
|
|
56
|
+
applyLevelStyle(docDefaults, preset.body, preset.paragraph, layout);
|
|
57
|
+
if (layout.spacing.rule === 'exact') {
|
|
58
|
+
// Widow control pulls a line onto the next page whenever a paragraph would
|
|
59
|
+
// leave a single line behind, so pages would often hold one line fewer
|
|
60
|
+
// than the grid. Word turns it on unless the document says otherwise.
|
|
61
|
+
paragraphProperty(paragraphPropertiesOf(docDefaults), 'widowControl').setAttributeNS(W_NS, 'w:val', '0');
|
|
62
|
+
}
|
|
63
|
+
const styles = Array.from(dom.getElementsByTagNameNS(W_NS, 'style'));
|
|
64
|
+
for (const style of styles) {
|
|
65
|
+
const heading = /^Heading([1-6])$/.exec(style.getAttributeNS(W_NS, 'styleId') ?? '');
|
|
66
|
+
if (heading) {
|
|
67
|
+
applyLevelStyle(style, preset.headings[Number(heading[1]) - 1], preset.paragraph, layout);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return keepXmlDeclaration(stylesXml, new XMLSerializer().serializeToString(dom));
|
|
71
|
+
}
|
|
72
|
+
function resolveLayout(preset, grid) {
|
|
73
|
+
const { lineSpacing, firstLineIndentChars = 0 } = preset.paragraph;
|
|
74
|
+
if (lineSpacing === 'grid' && !grid) {
|
|
75
|
+
throw new Error("Cannot apply font preset: lineSpacing 'grid' needs page.grid");
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
spacing: lineSpacing === 'grid' && grid
|
|
79
|
+
? { line: String(grid.linePitch), rule: 'exact' }
|
|
80
|
+
: SINGLE_SPACING,
|
|
81
|
+
firstLineIndent: Math.round(firstLineIndentChars * preset.body.sizePt * 20),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Sets the page grid in the section properties of a document.xml part, and
|
|
86
|
+
* fixes up the paragraphs that the style-level layout would break:
|
|
87
|
+
*
|
|
88
|
+
* - The generator gives every paragraph `<w:spacing w:lineRule="auto"/>`.
|
|
89
|
+
* Spacing attributes inherit one at a time, so that lone `auto` combined
|
|
90
|
+
* with the style's exact line height would read as a multiple of 240 twips
|
|
91
|
+
* (about 2.4 lines). It is dropped when the preset uses grid spacing.
|
|
92
|
+
* - Exact line spacing would crop an inline image to one line, so paragraphs
|
|
93
|
+
* with a drawing keep single spacing; the grid then rounds them up to whole
|
|
94
|
+
* lines.
|
|
95
|
+
* - Table cells drop the first-line indent; it only crowds narrow columns.
|
|
96
|
+
*/
|
|
97
|
+
export function patchDocumentXml(documentXml, preset) {
|
|
98
|
+
const dom = new DOMParser().parseFromString(documentXml, 'application/xml');
|
|
99
|
+
let grid = null;
|
|
100
|
+
const pageGrid = preset.page?.grid;
|
|
101
|
+
if (pageGrid) {
|
|
102
|
+
for (const sectPr of Array.from(dom.getElementsByTagNameNS(W_NS, 'sectPr'))) {
|
|
103
|
+
grid = measureGrid(sectPr, pageGrid, preset.body.sizePt);
|
|
104
|
+
setDocGrid(sectPr, grid);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const gridSpacing = preset.paragraph.lineSpacing === 'grid';
|
|
108
|
+
for (const paragraph of Array.from(dom.getElementsByTagNameNS(W_NS, 'p'))) {
|
|
109
|
+
const pPr = children(paragraph).find((child) => child.localName === 'pPr');
|
|
110
|
+
if (gridSpacing) {
|
|
111
|
+
if (paragraph.getElementsByTagNameNS(W_NS, 'drawing').length > 0) {
|
|
112
|
+
const spacing = paragraphProperty(pPrOf(paragraph), 'spacing');
|
|
113
|
+
spacing.setAttributeNS(W_NS, 'w:line', SINGLE_SPACING.line);
|
|
114
|
+
spacing.setAttributeNS(W_NS, 'w:lineRule', SINGLE_SPACING.rule);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
const spacing = pPr && children(pPr).find((child) => child.localName === 'spacing');
|
|
118
|
+
if (spacing && !spacing.hasAttributeNS(W_NS, 'line')) {
|
|
119
|
+
spacing.removeAttributeNS(W_NS, 'lineRule');
|
|
120
|
+
if (spacing.attributes.length === 0) {
|
|
121
|
+
spacing.remove();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (preset.paragraph.firstLineIndentChars && isInTableCell(paragraph)) {
|
|
127
|
+
removeFirstLineIndent(pPrOf(paragraph));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
xml: keepXmlDeclaration(documentXml, new XMLSerializer().serializeToString(dom)),
|
|
132
|
+
grid,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The grid for the text block of `sectPr`'s page. Word stores the character
|
|
137
|
+
* pitch as an offset from the body font size. Both pitches are rounded down,
|
|
138
|
+
* as Word does, so the lines and characters always fit: for the page Word
|
|
139
|
+
* itself sets up with GB/T 9704 margins, this reproduces its linePitch="579"
|
|
140
|
+
* and charSpace="-834".
|
|
141
|
+
*/
|
|
142
|
+
function measureGrid(sectPr, { linesPerPage, charsPerLine }, bodySizePt) {
|
|
143
|
+
const pgSz = children(sectPr).find((child) => child.localName === 'pgSz');
|
|
144
|
+
const pgMar = children(sectPr).find((child) => child.localName === 'pgMar');
|
|
145
|
+
const twips = (element, attribute) => {
|
|
146
|
+
const value = Number(element?.getAttributeNS(W_NS, attribute));
|
|
147
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
148
|
+
throw new Error(`Cannot apply font preset: section has no page ${attribute}`);
|
|
149
|
+
}
|
|
150
|
+
return value;
|
|
151
|
+
};
|
|
152
|
+
const textHeight = twips(pgSz, 'h') - twips(pgMar, 'top') - twips(pgMar, 'bottom');
|
|
153
|
+
const textWidth = twips(pgSz, 'w') - twips(pgMar, 'left') - twips(pgMar, 'right');
|
|
154
|
+
const charPitchPt = textWidth / 20 / charsPerLine;
|
|
155
|
+
return {
|
|
156
|
+
linePitch: Math.floor(textHeight / linesPerPage),
|
|
157
|
+
charSpace: Math.floor((charPitchPt - bodySizePt) * 4096),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function setDocGrid(sectPr, grid) {
|
|
161
|
+
let docGrid = children(sectPr).find((child) => child.localName === 'docGrid');
|
|
162
|
+
if (!docGrid) {
|
|
163
|
+
docGrid = sectPr.ownerDocument.createElementNS(W_NS, 'w:docGrid');
|
|
164
|
+
// <w:docGrid> is the last section property but <w:printerSettings>.
|
|
165
|
+
const printerSettings = children(sectPr).find((child) => child.localName === 'printerSettings');
|
|
166
|
+
sectPr.insertBefore(docGrid, printerSettings ?? null);
|
|
167
|
+
}
|
|
168
|
+
docGrid.setAttributeNS(W_NS, 'w:type', 'linesAndChars');
|
|
169
|
+
docGrid.setAttributeNS(W_NS, 'w:linePitch', String(grid.linePitch));
|
|
170
|
+
docGrid.setAttributeNS(W_NS, 'w:charSpace', String(grid.charSpace));
|
|
171
|
+
}
|
|
172
|
+
function isInTableCell(paragraph) {
|
|
173
|
+
for (let node = paragraph.parentElement; node; node = node.parentElement) {
|
|
174
|
+
if (node.namespaceURI === W_NS && node.localName === 'tc') {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
function removeFirstLineIndent(pPr) {
|
|
181
|
+
const ind = paragraphProperty(pPr, 'ind');
|
|
182
|
+
if (!ind.hasAttributeNS(W_NS, 'hanging')) {
|
|
183
|
+
ind.setAttributeNS(W_NS, 'w:firstLine', '0');
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/** A paragraph's `<w:pPr>`, created as its first child if missing. */
|
|
187
|
+
function pPrOf(paragraph) {
|
|
188
|
+
return childOrCreate(paragraph, 'pPr', 'first');
|
|
189
|
+
}
|
|
190
|
+
function applyLevelStyle(scope, style, paragraph, layout) {
|
|
191
|
+
const rPr = runPropertiesOf(scope);
|
|
192
|
+
const rFonts = childOrCreate(rPr, 'rFonts', 'first');
|
|
193
|
+
for (const attribute of THEME_FONT_ATTRIBUTES) {
|
|
194
|
+
rFonts.removeAttributeNS(W_NS, attribute);
|
|
195
|
+
}
|
|
196
|
+
rFonts.setAttributeNS(W_NS, 'w:ascii', style.latin);
|
|
197
|
+
rFonts.setAttributeNS(W_NS, 'w:hAnsi', style.latin);
|
|
198
|
+
rFonts.setAttributeNS(W_NS, 'w:eastAsia', style.eastAsia);
|
|
199
|
+
for (const child of children(rPr)) {
|
|
200
|
+
if (child.localName === 'b' || child.localName === 'bCs') {
|
|
201
|
+
child.remove();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (style.bold) {
|
|
205
|
+
rPr.insertBefore(rPr.ownerDocument.createElementNS(W_NS, 'w:b'), rFonts.nextSibling);
|
|
206
|
+
}
|
|
207
|
+
// <w:sz> is measured in half points.
|
|
208
|
+
const size = String(style.sizePt * 2);
|
|
209
|
+
childOrCreate(rPr, 'sz', 'last').setAttributeNS(W_NS, 'w:val', size);
|
|
210
|
+
childOrCreate(rPr, 'szCs', 'last').setAttributeNS(W_NS, 'w:val', size);
|
|
211
|
+
const pPr = paragraphPropertiesOf(scope);
|
|
212
|
+
const spacing = paragraphProperty(pPr, 'spacing');
|
|
213
|
+
spacing.setAttributeNS(W_NS, 'w:before', String(paragraph.beforePt * 20));
|
|
214
|
+
spacing.setAttributeNS(W_NS, 'w:after', String(paragraph.afterPt * 20));
|
|
215
|
+
spacing.setAttributeNS(W_NS, 'w:line', layout.spacing.line);
|
|
216
|
+
spacing.setAttributeNS(W_NS, 'w:lineRule', layout.spacing.rule);
|
|
217
|
+
if (style.centered) {
|
|
218
|
+
// An indented first line would push a centered title off center.
|
|
219
|
+
if (layout.firstLineIndent) {
|
|
220
|
+
paragraphProperty(pPr, 'ind').setAttributeNS(W_NS, 'w:firstLine', '0');
|
|
221
|
+
}
|
|
222
|
+
paragraphProperty(pPr, 'jc').setAttributeNS(W_NS, 'w:val', 'center');
|
|
223
|
+
}
|
|
224
|
+
else if (layout.firstLineIndent) {
|
|
225
|
+
paragraphProperty(pPr, 'ind').setAttributeNS(W_NS, 'w:firstLine', String(layout.firstLineIndent));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function runPropertiesOf(parent) {
|
|
229
|
+
const container = parent.localName === 'docDefaults'
|
|
230
|
+
? childOrCreate(parent, 'rPrDefault', 'first')
|
|
231
|
+
: parent;
|
|
232
|
+
// In a <w:style> the run properties go last, after any <w:pPr>.
|
|
233
|
+
return childOrCreate(container, 'rPr', 'last');
|
|
234
|
+
}
|
|
235
|
+
function paragraphPropertiesOf(parent) {
|
|
236
|
+
if (parent.localName === 'docDefaults') {
|
|
237
|
+
const pPrDefault = childOrCreate(parent, 'pPrDefault', 'last');
|
|
238
|
+
return childOrCreate(pPrDefault, 'pPr', 'first');
|
|
239
|
+
}
|
|
240
|
+
const existing = children(parent).find((child) => child.localName === 'pPr');
|
|
241
|
+
if (existing) {
|
|
242
|
+
return existing;
|
|
243
|
+
}
|
|
244
|
+
// In a <w:style> the paragraph properties go before the run properties.
|
|
245
|
+
const created = parent.ownerDocument.createElementNS(W_NS, 'w:pPr');
|
|
246
|
+
return parent.insertBefore(created, runPropertiesOf(parent));
|
|
247
|
+
}
|
|
248
|
+
function children(element) {
|
|
249
|
+
return Array.from(element.children).filter((child) => child.namespaceURI === W_NS);
|
|
250
|
+
}
|
|
251
|
+
function childOrCreate(parent, localName, position) {
|
|
252
|
+
const existing = children(parent).find((child) => child.localName === localName);
|
|
253
|
+
if (existing) {
|
|
254
|
+
return existing;
|
|
255
|
+
}
|
|
256
|
+
const created = parent.ownerDocument.createElementNS(W_NS, `w:${localName}`);
|
|
257
|
+
return position === 'first'
|
|
258
|
+
? parent.insertBefore(created, parent.firstChild)
|
|
259
|
+
: parent.appendChild(created);
|
|
260
|
+
}
|
|
261
|
+
function requireElement(dom, localName) {
|
|
262
|
+
const element = dom.getElementsByTagNameNS(W_NS, localName)[0];
|
|
263
|
+
if (!element) {
|
|
264
|
+
throw new Error(`Cannot apply font preset: styles.xml has no <w:${localName}> element`);
|
|
265
|
+
}
|
|
266
|
+
return element;
|
|
267
|
+
}
|
|
268
|
+
//# sourceMappingURL=docx-font-preset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"docx-font-preset.js","sourceRoot":"","sources":["../src/docx-font-preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,iBAAiB,IAAI,IAAI,EAAE,MAAM,eAAe,CAAC;AAEjG,+DAA+D;AAC/D,MAAM,qBAAqB,GAAG,CAAC,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;AAEvF;;;GAGG;AACH,MAAM,cAAc,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AAiBrD;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,YAAkB,EAAE,MAAkB;IAC1E,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,MAAM,YAAY,CAAC,WAAW,EAAE,CAAC,CAAC;IACpE,MAAM,WAAW,GAAG,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAChD,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;IACpF,CAAC;IAED,IAAI,IAAI,GAAgB,IAAI,CAAC;IAC7B,MAAM,QAAQ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AAC;AAED,SAAS,QAAQ,CAAC,OAAgB;IAChC,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC;AACrF,CAAC;AAED,SAAS,aAAa,CAAC,MAAe,EAAE,SAAiB,EAAE,QAA0B;IACnF,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC;IACjF,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,QAAQ,CAAC;IAClB,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,CAAC,aAAa,CAAC,eAAe,CAAC,IAAI,EAAE,KAAK,SAAS,EAAE,CAAC,CAAC;IAC7E,OAAO,QAAQ,KAAK,OAAO;QACzB,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC;QACjD,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;AAClC,CAAC;AAED,SAAS,cAAc,CAAC,GAAa,EAAE,SAAiB;IACtD,MAAM,OAAO,GAAG,GAAG,CAAC,sBAAsB,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/D,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,kDAAkD,SAAS,WAAW,CAAC,CAAC;IAC1F,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import JSZip from 'jszip';\nimport type { FontPreset, LevelStyle, ParagraphStyle } from './font-presets.js';\nimport { keepXmlDeclaration, paragraphProperty, WORDPROCESSING_NS as W_NS } from './xml-part.js';\n\n/** Theme slots WinWord prefers over explicitly named fonts. */\nconst THEME_FONT_ATTRIBUTES = ['asciiTheme', 'eastAsiaTheme', 'hAnsiTheme', 'cstheme'];\n\n/**\n * `<w:line>` is ignored while the rule is auto, but WinWord still writes 240\n * twips, so the preset writes the pair Word itself produces.\n */\nconst SINGLE_SPACING = { line: '240', rule: 'auto' };\n\n/** Word's document grid, in the units of `<w:docGrid>`. */\ninterface Grid {\n /** Line pitch in twips. */\n linePitch: number;\n /** Character pitch relative to the body font size, in 1/4096 point. */\n charSpace: number;\n}\n\n/** What the styles need to know about the paragraphs, resolved once. */\ninterface Layout {\n spacing: { line: string; rule: string };\n /** First-line indent of body paragraphs in twips, or 0 for none. */\n firstLineIndent: number;\n}\n\n/**\n * Rewrites the fonts, sizes, paragraph spacing and page grid of an already\n * generated .docx.\n *\n * `html-to-docx` only ever writes `w:ascii`, which WinWord applies to Latin\n * characters and digits; Chinese characters fall back to the theme font. The\n * East Asian slots are set here instead, so this stays out of the way of the\n * default conversion output.\n *\n * The grid is measured from the page the document actually has, so it still\n * fits when `pageMargins` overrode the preset's margins.\n */\nexport async function applyFontPreset(wordDocument: Blob, preset: FontPreset): Promise<Blob> {\n const zip = await JSZip.loadAsync(await wordDocument.arrayBuffer());\n const stylesEntry = zip.file('word/styles.xml');\n if (!stylesEntry) {\n throw new Error('Cannot apply font preset: document contains no word/styles.xml');\n }\n\n let grid: Grid | null = null;\n const pageGrid = preset.page?.grid;\n if (\n pageGrid ||\n preset.paragraph.lineSpacing === 'grid' ||\n preset.paragraph.firstLineIndentChars\n ) {\n const documentEntry = zip.file('word/document.xml');\n if (!documentEntry) {\n throw new Error('Cannot apply font preset: document contains no word/document.xml');\n }\n const documentXml = await documentEntry.async('string');\n const patched = patchDocumentXml(documentXml, preset);\n grid = patched.grid;\n zip.file('word/document.xml', patched.xml);\n }\n\n const stylesXml = await stylesEntry.async('string');\n zip.file('word/styles.xml', patchStylesXml(stylesXml, preset, grid));\n\n const patched = await zip.generateAsync({ type: 'arraybuffer' });\n return new Blob([patched], { type: wordDocument.type });\n}\n\n/**\n * Applies `preset` to the document default run properties and to the six\n * heading styles of a styles.xml part. `grid` is the page grid that\n * `patchDocumentXml` set, which `lineSpacing: 'grid'` takes its pitch from.\n */\nexport function patchStylesXml(\n stylesXml: string,\n preset: FontPreset,\n grid: Grid | null = null\n): string {\n const dom = new DOMParser().parseFromString(stylesXml, 'application/xml');\n const layout = resolveLayout(preset, grid);\n\n const docDefaults = requireElement(dom, 'docDefaults');\n applyLevelStyle(docDefaults, preset.body, preset.paragraph, layout);\n if (layout.spacing.rule === 'exact') {\n // Widow control pulls a line onto the next page whenever a paragraph would\n // leave a single line behind, so pages would often hold one line fewer\n // than the grid. Word turns it on unless the document says otherwise.\n paragraphProperty(paragraphPropertiesOf(docDefaults), 'widowControl').setAttributeNS(\n W_NS,\n 'w:val',\n '0'\n );\n }\n\n const styles = Array.from(dom.getElementsByTagNameNS(W_NS, 'style'));\n for (const style of styles) {\n const heading = /^Heading([1-6])$/.exec(style.getAttributeNS(W_NS, 'styleId') ?? '');\n if (heading) {\n applyLevelStyle(style, preset.headings[Number(heading[1]) - 1], preset.paragraph, layout);\n }\n }\n\n return keepXmlDeclaration(stylesXml, new XMLSerializer().serializeToString(dom));\n}\n\nfunction resolveLayout(preset: FontPreset, grid: Grid | null): Layout {\n const { lineSpacing, firstLineIndentChars = 0 } = preset.paragraph;\n if (lineSpacing === 'grid' && !grid) {\n throw new Error(\"Cannot apply font preset: lineSpacing 'grid' needs page.grid\");\n }\n return {\n spacing:\n lineSpacing === 'grid' && grid\n ? { line: String(grid.linePitch), rule: 'exact' }\n : SINGLE_SPACING,\n firstLineIndent: Math.round(firstLineIndentChars * preset.body.sizePt * 20),\n };\n}\n\n/**\n * Sets the page grid in the section properties of a document.xml part, and\n * fixes up the paragraphs that the style-level layout would break:\n *\n * - The generator gives every paragraph `<w:spacing w:lineRule=\"auto\"/>`.\n * Spacing attributes inherit one at a time, so that lone `auto` combined\n * with the style's exact line height would read as a multiple of 240 twips\n * (about 2.4 lines). It is dropped when the preset uses grid spacing.\n * - Exact line spacing would crop an inline image to one line, so paragraphs\n * with a drawing keep single spacing; the grid then rounds them up to whole\n * lines.\n * - Table cells drop the first-line indent; it only crowds narrow columns.\n */\nexport function patchDocumentXml(\n documentXml: string,\n preset: FontPreset\n): { xml: string; grid: Grid | null } {\n const dom = new DOMParser().parseFromString(documentXml, 'application/xml');\n\n let grid: Grid | null = null;\n const pageGrid = preset.page?.grid;\n if (pageGrid) {\n for (const sectPr of Array.from(dom.getElementsByTagNameNS(W_NS, 'sectPr'))) {\n grid = measureGrid(sectPr, pageGrid, preset.body.sizePt);\n setDocGrid(sectPr, grid);\n }\n }\n\n const gridSpacing = preset.paragraph.lineSpacing === 'grid';\n for (const paragraph of Array.from(dom.getElementsByTagNameNS(W_NS, 'p'))) {\n const pPr = children(paragraph).find((child) => child.localName === 'pPr');\n\n if (gridSpacing) {\n if (paragraph.getElementsByTagNameNS(W_NS, 'drawing').length > 0) {\n const spacing = paragraphProperty(pPrOf(paragraph), 'spacing');\n spacing.setAttributeNS(W_NS, 'w:line', SINGLE_SPACING.line);\n spacing.setAttributeNS(W_NS, 'w:lineRule', SINGLE_SPACING.rule);\n } else {\n const spacing = pPr && children(pPr).find((child) => child.localName === 'spacing');\n if (spacing && !spacing.hasAttributeNS(W_NS, 'line')) {\n spacing.removeAttributeNS(W_NS, 'lineRule');\n if (spacing.attributes.length === 0) {\n spacing.remove();\n }\n }\n }\n }\n\n if (preset.paragraph.firstLineIndentChars && isInTableCell(paragraph)) {\n removeFirstLineIndent(pPrOf(paragraph));\n }\n }\n\n return {\n xml: keepXmlDeclaration(documentXml, new XMLSerializer().serializeToString(dom)),\n grid,\n };\n}\n\n/**\n * The grid for the text block of `sectPr`'s page. Word stores the character\n * pitch as an offset from the body font size. Both pitches are rounded down,\n * as Word does, so the lines and characters always fit: for the page Word\n * itself sets up with GB/T 9704 margins, this reproduces its linePitch=\"579\"\n * and charSpace=\"-834\".\n */\nfunction measureGrid(\n sectPr: Element,\n { linesPerPage, charsPerLine }: { linesPerPage: number; charsPerLine: number },\n bodySizePt: number\n): Grid {\n const pgSz = children(sectPr).find((child) => child.localName === 'pgSz');\n const pgMar = children(sectPr).find((child) => child.localName === 'pgMar');\n const twips = (element: Element | undefined, attribute: string): number => {\n const value = Number(element?.getAttributeNS(W_NS, attribute));\n if (!Number.isFinite(value) || value <= 0) {\n throw new Error(`Cannot apply font preset: section has no page ${attribute}`);\n }\n return value;\n };\n\n const textHeight = twips(pgSz, 'h') - twips(pgMar, 'top') - twips(pgMar, 'bottom');\n const textWidth = twips(pgSz, 'w') - twips(pgMar, 'left') - twips(pgMar, 'right');\n const charPitchPt = textWidth / 20 / charsPerLine;\n\n return {\n linePitch: Math.floor(textHeight / linesPerPage),\n charSpace: Math.floor((charPitchPt - bodySizePt) * 4096),\n };\n}\n\nfunction setDocGrid(sectPr: Element, grid: Grid): void {\n let docGrid = children(sectPr).find((child) => child.localName === 'docGrid');\n if (!docGrid) {\n docGrid = sectPr.ownerDocument.createElementNS(W_NS, 'w:docGrid');\n // <w:docGrid> is the last section property but <w:printerSettings>.\n const printerSettings = children(sectPr).find((child) => child.localName === 'printerSettings');\n sectPr.insertBefore(docGrid, printerSettings ?? null);\n }\n docGrid.setAttributeNS(W_NS, 'w:type', 'linesAndChars');\n docGrid.setAttributeNS(W_NS, 'w:linePitch', String(grid.linePitch));\n docGrid.setAttributeNS(W_NS, 'w:charSpace', String(grid.charSpace));\n}\n\nfunction isInTableCell(paragraph: Element): boolean {\n for (let node = paragraph.parentElement; node; node = node.parentElement) {\n if (node.namespaceURI === W_NS && node.localName === 'tc') {\n return true;\n }\n }\n return false;\n}\n\nfunction removeFirstLineIndent(pPr: Element): void {\n const ind = paragraphProperty(pPr, 'ind');\n if (!ind.hasAttributeNS(W_NS, 'hanging')) {\n ind.setAttributeNS(W_NS, 'w:firstLine', '0');\n }\n}\n\n/** A paragraph's `<w:pPr>`, created as its first child if missing. */\nfunction pPrOf(paragraph: Element): Element {\n return childOrCreate(paragraph, 'pPr', 'first');\n}\n\nfunction applyLevelStyle(\n scope: Element,\n style: LevelStyle,\n paragraph: ParagraphStyle,\n layout: Layout\n): void {\n const rPr = runPropertiesOf(scope);\n\n const rFonts = childOrCreate(rPr, 'rFonts', 'first');\n for (const attribute of THEME_FONT_ATTRIBUTES) {\n rFonts.removeAttributeNS(W_NS, attribute);\n }\n rFonts.setAttributeNS(W_NS, 'w:ascii', style.latin);\n rFonts.setAttributeNS(W_NS, 'w:hAnsi', style.latin);\n rFonts.setAttributeNS(W_NS, 'w:eastAsia', style.eastAsia);\n\n for (const child of children(rPr)) {\n if (child.localName === 'b' || child.localName === 'bCs') {\n child.remove();\n }\n }\n if (style.bold) {\n rPr.insertBefore(rPr.ownerDocument.createElementNS(W_NS, 'w:b'), rFonts.nextSibling);\n }\n\n // <w:sz> is measured in half points.\n const size = String(style.sizePt * 2);\n childOrCreate(rPr, 'sz', 'last').setAttributeNS(W_NS, 'w:val', size);\n childOrCreate(rPr, 'szCs', 'last').setAttributeNS(W_NS, 'w:val', size);\n\n const pPr = paragraphPropertiesOf(scope);\n const spacing = paragraphProperty(pPr, 'spacing');\n spacing.setAttributeNS(W_NS, 'w:before', String(paragraph.beforePt * 20));\n spacing.setAttributeNS(W_NS, 'w:after', String(paragraph.afterPt * 20));\n spacing.setAttributeNS(W_NS, 'w:line', layout.spacing.line);\n spacing.setAttributeNS(W_NS, 'w:lineRule', layout.spacing.rule);\n\n if (style.centered) {\n // An indented first line would push a centered title off center.\n if (layout.firstLineIndent) {\n paragraphProperty(pPr, 'ind').setAttributeNS(W_NS, 'w:firstLine', '0');\n }\n paragraphProperty(pPr, 'jc').setAttributeNS(W_NS, 'w:val', 'center');\n } else if (layout.firstLineIndent) {\n paragraphProperty(pPr, 'ind').setAttributeNS(\n W_NS,\n 'w:firstLine',\n String(layout.firstLineIndent)\n );\n }\n}\n\nfunction runPropertiesOf(parent: Element): Element {\n const container =\n parent.localName === 'docDefaults'\n ? childOrCreate(parent, 'rPrDefault', 'first')\n : parent;\n // In a <w:style> the run properties go last, after any <w:pPr>.\n return childOrCreate(container, 'rPr', 'last');\n}\n\nfunction paragraphPropertiesOf(parent: Element): Element {\n if (parent.localName === 'docDefaults') {\n const pPrDefault = childOrCreate(parent, 'pPrDefault', 'last');\n return childOrCreate(pPrDefault, 'pPr', 'first');\n }\n\n const existing = children(parent).find((child) => child.localName === 'pPr');\n if (existing) {\n return existing;\n }\n // In a <w:style> the paragraph properties go before the run properties.\n const created = parent.ownerDocument.createElementNS(W_NS, 'w:pPr');\n return parent.insertBefore(created, runPropertiesOf(parent));\n}\n\nfunction children(element: Element): Element[] {\n return Array.from(element.children).filter((child) => child.namespaceURI === W_NS);\n}\n\nfunction childOrCreate(parent: Element, localName: string, position: 'first' | 'last'): Element {\n const existing = children(parent).find((child) => child.localName === localName);\n if (existing) {\n return existing;\n }\n const created = parent.ownerDocument.createElementNS(W_NS, `w:${localName}`);\n return position === 'first'\n ? parent.insertBefore(created, parent.firstChild)\n : parent.appendChild(created);\n}\n\nfunction requireElement(dom: Document, localName: string): Element {\n const element = dom.getElementsByTagNameNS(W_NS, localName)[0];\n if (!element) {\n throw new Error(`Cannot apply font preset: styles.xml has no <w:${localName}> element`);\n }\n return element;\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A monospace face that ships with Windows, so Chinese comments stay aligned.
|
|
3
|
+
* Defined here rather than in the code block patch so the main bundle can use
|
|
4
|
+
* it without loading that patch and JSZip.
|
|
5
|
+
*/
|
|
6
|
+
export declare const CODE_FONT = "Courier New";
|
|
7
|
+
/**
|
|
8
|
+
* Stands in for `<hr>`, which the generator drops. The structure patch turns
|
|
9
|
+
* each one into a bordered paragraph; if that patch fails, a centered `* * *`
|
|
10
|
+
* still reads as a section break.
|
|
11
|
+
*/
|
|
12
|
+
export declare const RULE_TEXT = "* * *";
|
|
13
|
+
/**
|
|
14
|
+
* Adjusts the parser's HTML for html-to-docx, which drops or flattens some of
|
|
15
|
+
* what the browser preview renders:
|
|
16
|
+
*
|
|
17
|
+
* - Task list checkboxes: `<input>` is dropped, so done and pending items read
|
|
18
|
+
* the same. Each becomes a ☒ or ☐ character.
|
|
19
|
+
* - Inline code: `<code>` keeps the body font. Its text is wrapped in a span
|
|
20
|
+
* naming the code block font, which the generator does carry over.
|
|
21
|
+
* - Table header cells: `<th>` isn't bold, so its content is wrapped in
|
|
22
|
+
* `<strong>`. Column alignment from GFM's `align` attribute is ignored, but
|
|
23
|
+
* the same value as a `text-align` style is honored.
|
|
24
|
+
* - Horizontal rules: `<hr>` is dropped, so it becomes a centered RULE_TEXT
|
|
25
|
+
* paragraph for the structure patch to draw as a rule.
|
|
26
|
+
*
|
|
27
|
+
* Only the HTML bound for the .docx changes; the preview keeps the original.
|
|
28
|
+
*/
|
|
29
|
+
export declare function prepareHtmlForDocx(html: string): string;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A monospace face that ships with Windows, so Chinese comments stay aligned.
|
|
3
|
+
* Defined here rather than in the code block patch so the main bundle can use
|
|
4
|
+
* it without loading that patch and JSZip.
|
|
5
|
+
*/
|
|
6
|
+
export const CODE_FONT = 'Courier New';
|
|
7
|
+
/** What Word shows in place of a task list checkbox, which it can't render. */
|
|
8
|
+
const CHECKBOX = { checked: '☒', unchecked: '☐' };
|
|
9
|
+
/**
|
|
10
|
+
* Stands in for `<hr>`, which the generator drops. The structure patch turns
|
|
11
|
+
* each one into a bordered paragraph; if that patch fails, a centered `* * *`
|
|
12
|
+
* still reads as a section break.
|
|
13
|
+
*/
|
|
14
|
+
export const RULE_TEXT = '* * *';
|
|
15
|
+
/**
|
|
16
|
+
* Stands in for `style` while the HTML is a live DOM. The page's CSP applies
|
|
17
|
+
* to DOMParser documents too, and blocks (and reports) `style` attributes set
|
|
18
|
+
* there. (CSSOM is allowed, but it quotes a font name with a space, which the
|
|
19
|
+
* generator would read as part of the name.) Renamed back to `style` in the
|
|
20
|
+
* serialized tags, where the CSP doesn't reach.
|
|
21
|
+
*/
|
|
22
|
+
const STYLE_PLACEHOLDER = 'data-docx-style';
|
|
23
|
+
/**
|
|
24
|
+
* Adjusts the parser's HTML for html-to-docx, which drops or flattens some of
|
|
25
|
+
* what the browser preview renders:
|
|
26
|
+
*
|
|
27
|
+
* - Task list checkboxes: `<input>` is dropped, so done and pending items read
|
|
28
|
+
* the same. Each becomes a ☒ or ☐ character.
|
|
29
|
+
* - Inline code: `<code>` keeps the body font. Its text is wrapped in a span
|
|
30
|
+
* naming the code block font, which the generator does carry over.
|
|
31
|
+
* - Table header cells: `<th>` isn't bold, so its content is wrapped in
|
|
32
|
+
* `<strong>`. Column alignment from GFM's `align` attribute is ignored, but
|
|
33
|
+
* the same value as a `text-align` style is honored.
|
|
34
|
+
* - Horizontal rules: `<hr>` is dropped, so it becomes a centered RULE_TEXT
|
|
35
|
+
* paragraph for the structure patch to draw as a rule.
|
|
36
|
+
*
|
|
37
|
+
* Only the HTML bound for the .docx changes; the preview keeps the original.
|
|
38
|
+
*/
|
|
39
|
+
export function prepareHtmlForDocx(html) {
|
|
40
|
+
const dom = new DOMParser().parseFromString(html, 'text/html');
|
|
41
|
+
for (const input of Array.from(dom.querySelectorAll('input[type="checkbox"]'))) {
|
|
42
|
+
const box = input.hasAttribute('checked') ? CHECKBOX.checked : CHECKBOX.unchecked;
|
|
43
|
+
input.replaceWith(dom.createTextNode(box));
|
|
44
|
+
}
|
|
45
|
+
for (const code of Array.from(dom.querySelectorAll('code'))) {
|
|
46
|
+
if (code.closest('pre'))
|
|
47
|
+
continue;
|
|
48
|
+
const span = dom.createElement('span');
|
|
49
|
+
// A raw attribute, not `style.fontFamily`: see STYLE_PLACEHOLDER.
|
|
50
|
+
span.setAttribute(STYLE_PLACEHOLDER, `font-family: ${CODE_FONT}`);
|
|
51
|
+
span.append(...Array.from(code.childNodes));
|
|
52
|
+
code.append(span);
|
|
53
|
+
}
|
|
54
|
+
for (const cell of Array.from(dom.querySelectorAll('th, td'))) {
|
|
55
|
+
const align = cell.getAttribute('align');
|
|
56
|
+
if (align) {
|
|
57
|
+
cell.style.textAlign = align;
|
|
58
|
+
cell.removeAttribute('align');
|
|
59
|
+
}
|
|
60
|
+
if (cell.localName === 'th' && cell.textContent?.trim()) {
|
|
61
|
+
const strong = dom.createElement('strong');
|
|
62
|
+
strong.append(...Array.from(cell.childNodes));
|
|
63
|
+
cell.append(strong);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (const rule of Array.from(dom.querySelectorAll('hr'))) {
|
|
67
|
+
const paragraph = dom.createElement('p');
|
|
68
|
+
paragraph.setAttribute(STYLE_PLACEHOLDER, 'text-align: center');
|
|
69
|
+
paragraph.textContent = RULE_TEXT;
|
|
70
|
+
rule.replaceWith(paragraph);
|
|
71
|
+
}
|
|
72
|
+
// Only inside tags: text can't contain a raw `<`, so it's never touched.
|
|
73
|
+
return dom.body.innerHTML.replace(/<[^>]+>/g, (tag) => tag.replace(` ${STYLE_PLACEHOLDER}=`, ' style='));
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=docx-html.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"docx-html.js","sourceRoot":"","sources":["../src/docx-html.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,aAAa,CAAC;AAEvC,+EAA+E;AAC/E,MAAM,QAAQ,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC;AAElD;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,OAAO,CAAC;AAEjC;;;;;;GAMG;AACH,MAAM,iBAAiB,GAAG,iBAAiB,CAAC;AAE5C;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,MAAM,GAAG,GAAG,IAAI,SAAS,EAAE,CAAC,eAAe,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IAE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AAE,EAAE,CAAC;YACxD,MAAM,MAAM,GAAG,GAAG,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;YAC3C,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;YAC9C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;QAC1D,MAAM,SAAS,GAAG,GAAG,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;QACzC,SAAS,CAAC,YAAY,CAAC,iBAAiB,EAAE,oBAAoB,CAAC,CAAC;QAChE,SAAS,CAAC,WAAW,GAAG,SAAS,CAAC;QAClC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IAC9B,CAAC;IAED,yEAAyE;IACzE,OAAO,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,GAAG,EAAE,EAAE,CACpD,GAAG,CAAC,OAAO,CAAC,IAAI,iBAAiB,GAAG,EAAE,SAAS,CAAC,CACjD,CAAC;AACJ,CAAC","sourcesContent":["/**\n * A monospace face that ships with Windows, so Chinese comments stay aligned.\n * Defined here rather than in the code block patch so the main bundle can use\n * it without loading that patch and JSZip.\n */\nexport const CODE_FONT = 'Courier New';\n\n/** What Word shows in place of a task list checkbox, which it can't render. */\nconst CHECKBOX = { checked: '☒', unchecked: '☐' };\n\n/**\n * Stands in for `<hr>`, which the generator drops. The structure patch turns\n * each one into a bordered paragraph; if that patch fails, a centered `* * *`\n * still reads as a section break.\n */\nexport const RULE_TEXT = '* * *';\n\n/**\n * Stands in for `style` while the HTML is a live DOM. The page's CSP applies\n * to DOMParser documents too, and blocks (and reports) `style` attributes set\n * there. (CSSOM is allowed, but it quotes a font name with a space, which the\n * generator would read as part of the name.) Renamed back to `style` in the\n * serialized tags, where the CSP doesn't reach.\n */\nconst STYLE_PLACEHOLDER = 'data-docx-style';\n\n/**\n * Adjusts the parser's HTML for html-to-docx, which drops or flattens some of\n * what the browser preview renders:\n *\n * - Task list checkboxes: `<input>` is dropped, so done and pending items read\n * the same. Each becomes a ☒ or ☐ character.\n * - Inline code: `<code>` keeps the body font. Its text is wrapped in a span\n * naming the code block font, which the generator does carry over.\n * - Table header cells: `<th>` isn't bold, so its content is wrapped in\n * `<strong>`. Column alignment from GFM's `align` attribute is ignored, but\n * the same value as a `text-align` style is honored.\n * - Horizontal rules: `<hr>` is dropped, so it becomes a centered RULE_TEXT\n * paragraph for the structure patch to draw as a rule.\n *\n * Only the HTML bound for the .docx changes; the preview keeps the original.\n */\nexport function prepareHtmlForDocx(html: string): string {\n const dom = new DOMParser().parseFromString(html, 'text/html');\n\n for (const input of Array.from(dom.querySelectorAll('input[type=\"checkbox\"]'))) {\n const box = input.hasAttribute('checked') ? CHECKBOX.checked : CHECKBOX.unchecked;\n input.replaceWith(dom.createTextNode(box));\n }\n\n for (const code of Array.from(dom.querySelectorAll('code'))) {\n if (code.closest('pre')) continue;\n const span = dom.createElement('span');\n // A raw attribute, not `style.fontFamily`: see STYLE_PLACEHOLDER.\n span.setAttribute(STYLE_PLACEHOLDER, `font-family: ${CODE_FONT}`);\n span.append(...Array.from(code.childNodes));\n code.append(span);\n }\n\n for (const cell of Array.from(dom.querySelectorAll('th, td'))) {\n const align = cell.getAttribute('align');\n if (align) {\n (cell as HTMLElement).style.textAlign = align;\n cell.removeAttribute('align');\n }\n if (cell.localName === 'th' && cell.textContent?.trim()) {\n const strong = dom.createElement('strong');\n strong.append(...Array.from(cell.childNodes));\n cell.append(strong);\n }\n }\n\n for (const rule of Array.from(dom.querySelectorAll('hr'))) {\n const paragraph = dom.createElement('p');\n paragraph.setAttribute(STYLE_PLACEHOLDER, 'text-align: center');\n paragraph.textContent = RULE_TEXT;\n rule.replaceWith(paragraph);\n }\n\n // Only inside tags: text can't contain a raw `<`, so it's never touched.\n return dom.body.innerHTML.replace(/<[^>]+>/g, (tag) =>\n tag.replace(` ${STYLE_PLACEHOLDER}=`, ' style='),\n );\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** What the source HTML says about the document's rules and tables. */
|
|
2
|
+
export interface DocumentStructure {
|
|
3
|
+
/** How many `<hr>` the HTML has. */
|
|
4
|
+
rules: number;
|
|
5
|
+
/** For each `<table>` in document order, whether its first row is a header. */
|
|
6
|
+
headerRows: boolean[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Restores structure the generator drops or gets wrong:
|
|
10
|
+
*
|
|
11
|
+
* - Horizontal rules: each centered RULE_TEXT paragraph that stands in for an
|
|
12
|
+
* `<hr>` (see prepareHtmlForDocx) becomes an empty paragraph with a bottom
|
|
13
|
+
* border. They're matched in order against the number of `<hr>`, so a
|
|
14
|
+
* paragraph that only happens to say `* * *` is left alone once they run out.
|
|
15
|
+
* - Header rows: a table's header row gets `<w:tblHeader/>`, so Word repeats
|
|
16
|
+
* it at the top of each page the table spans.
|
|
17
|
+
* - Table grids: the generator writes a second `<w:tblGrid>` between rows,
|
|
18
|
+
* which the schema doesn't allow; only the first is kept.
|
|
19
|
+
*/
|
|
20
|
+
export declare function patchStructure(wordDocument: Blob, structure: DocumentStructure): Promise<Blob>;
|
|
21
|
+
/** Exported for testing. */
|
|
22
|
+
export declare function patchDocumentXml(documentXml: string, structure: DocumentStructure): string;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import JSZip from 'jszip';
|
|
2
|
+
import { RULE_TEXT } from './docx-html.js';
|
|
3
|
+
import { keepXmlDeclaration, WORDPROCESSING_NS as W_NS } from './xml-part.js';
|
|
4
|
+
/**
|
|
5
|
+
* Restores structure the generator drops or gets wrong:
|
|
6
|
+
*
|
|
7
|
+
* - Horizontal rules: each centered RULE_TEXT paragraph that stands in for an
|
|
8
|
+
* `<hr>` (see prepareHtmlForDocx) becomes an empty paragraph with a bottom
|
|
9
|
+
* border. They're matched in order against the number of `<hr>`, so a
|
|
10
|
+
* paragraph that only happens to say `* * *` is left alone once they run out.
|
|
11
|
+
* - Header rows: a table's header row gets `<w:tblHeader/>`, so Word repeats
|
|
12
|
+
* it at the top of each page the table spans.
|
|
13
|
+
* - Table grids: the generator writes a second `<w:tblGrid>` between rows,
|
|
14
|
+
* which the schema doesn't allow; only the first is kept.
|
|
15
|
+
*/
|
|
16
|
+
export async function patchStructure(wordDocument, structure) {
|
|
17
|
+
const zip = await JSZip.loadAsync(await wordDocument.arrayBuffer());
|
|
18
|
+
const documentEntry = zip.file('word/document.xml');
|
|
19
|
+
if (!documentEntry) {
|
|
20
|
+
throw new Error('Cannot patch structure: document contains no word/document.xml');
|
|
21
|
+
}
|
|
22
|
+
const documentXml = await documentEntry.async('string');
|
|
23
|
+
const patched = patchDocumentXml(documentXml, structure);
|
|
24
|
+
if (patched === documentXml) {
|
|
25
|
+
return wordDocument;
|
|
26
|
+
}
|
|
27
|
+
zip.file('word/document.xml', patched);
|
|
28
|
+
return new Blob([await zip.generateAsync({ type: 'arraybuffer' })], {
|
|
29
|
+
type: wordDocument.type,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/** Exported for testing. */
|
|
33
|
+
export function patchDocumentXml(documentXml, structure) {
|
|
34
|
+
const dom = new DOMParser().parseFromString(documentXml, 'application/xml');
|
|
35
|
+
let changed = false;
|
|
36
|
+
let rulesLeft = structure.rules;
|
|
37
|
+
for (const paragraph of Array.from(dom.getElementsByTagNameNS(W_NS, 'p'))) {
|
|
38
|
+
if (rulesLeft === 0)
|
|
39
|
+
break;
|
|
40
|
+
if (isRulePlaceholder(paragraph)) {
|
|
41
|
+
drawRule(paragraph);
|
|
42
|
+
rulesLeft--;
|
|
43
|
+
changed = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const tables = Array.from(dom.getElementsByTagNameNS(W_NS, 'tbl'));
|
|
47
|
+
for (const [index, table] of tables.entries()) {
|
|
48
|
+
const grids = children(table, 'tblGrid');
|
|
49
|
+
for (const extra of grids.slice(1)) {
|
|
50
|
+
extra.remove();
|
|
51
|
+
changed = true;
|
|
52
|
+
}
|
|
53
|
+
const firstRow = children(table, 'tr')[0];
|
|
54
|
+
if (structure.headerRows[index] && firstRow) {
|
|
55
|
+
markHeaderRow(firstRow);
|
|
56
|
+
changed = true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (!changed) {
|
|
60
|
+
return documentXml;
|
|
61
|
+
}
|
|
62
|
+
return keepXmlDeclaration(documentXml, new XMLSerializer().serializeToString(dom));
|
|
63
|
+
}
|
|
64
|
+
function isRulePlaceholder(paragraph) {
|
|
65
|
+
const text = Array.from(paragraph.getElementsByTagNameNS(W_NS, 't'))
|
|
66
|
+
.map((t) => t.textContent ?? '')
|
|
67
|
+
.join('');
|
|
68
|
+
const jc = children(children(paragraph, 'pPr')[0] ?? paragraph, 'jc')[0];
|
|
69
|
+
return text.trim() === RULE_TEXT && jc?.getAttributeNS(W_NS, 'val') === 'center';
|
|
70
|
+
}
|
|
71
|
+
/** Replaces the paragraph's content with nothing but a bottom border. */
|
|
72
|
+
function drawRule(paragraph) {
|
|
73
|
+
const document = paragraph.ownerDocument;
|
|
74
|
+
while (paragraph.firstChild) {
|
|
75
|
+
paragraph.removeChild(paragraph.firstChild);
|
|
76
|
+
}
|
|
77
|
+
const pPr = paragraph.appendChild(document.createElementNS(W_NS, 'w:pPr'));
|
|
78
|
+
const pBdr = pPr.appendChild(document.createElementNS(W_NS, 'w:pBdr'));
|
|
79
|
+
const bottom = pBdr.appendChild(document.createElementNS(W_NS, 'w:bottom'));
|
|
80
|
+
bottom.setAttributeNS(W_NS, 'w:val', 'single');
|
|
81
|
+
bottom.setAttributeNS(W_NS, 'w:sz', '6');
|
|
82
|
+
bottom.setAttributeNS(W_NS, 'w:space', '1');
|
|
83
|
+
bottom.setAttributeNS(W_NS, 'w:color', 'auto');
|
|
84
|
+
}
|
|
85
|
+
function markHeaderRow(row) {
|
|
86
|
+
const document = row.ownerDocument;
|
|
87
|
+
let trPr = children(row, 'trPr')[0];
|
|
88
|
+
if (!trPr) {
|
|
89
|
+
trPr = row.insertBefore(document.createElementNS(W_NS, 'w:trPr'), row.firstChild);
|
|
90
|
+
}
|
|
91
|
+
if (children(trPr, 'tblHeader').length === 0) {
|
|
92
|
+
// The generator writes at most cantSplit here, which comes before tblHeader.
|
|
93
|
+
trPr.appendChild(document.createElementNS(W_NS, 'w:tblHeader'));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function children(parent, localName) {
|
|
97
|
+
return Array.from(parent.children).filter((child) => child.namespaceURI === W_NS && child.localName === localName);
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=docx-structure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"docx-structure.js","sourceRoot":"","sources":["../src/docx-structure.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,IAAI,IAAI,EAAE,MAAM,eAAe,CAAC;AAU9E;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,YAAkB,EAClB,SAA4B;IAE5B,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,MAAM,YAAY,CAAC,WAAW,EAAE,CAAC,CAAC;IACpE,MAAM,aAAa,GAAG,GAAG,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;IACpD,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;IACpF,CAAC;IAED,MAAM,WAAW,GAAG,MAAM,aAAa,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IACxD,MAAM,OAAO,GA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E,aAAa,CAAC,CAAC,CAAC;IAClE,CAAC;AACH,CAAC;AAED,SAAS,QAAQ,CAAC,MAAe,EAAE,SAAiB;IAClD,OAAO,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,MAAM,CACvC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,YAAY,KAAK,IAAI,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS,CACxE,CAAC;AACJ,CAAC","sourcesContent":["import JSZip from 'jszip';\nimport { RULE_TEXT } from './docx-html.js';\nimport { keepXmlDeclaration, WORDPROCESSING_NS as W_NS } from './xml-part.js';\n\n/** What the source HTML says about the document's rules and tables. */\nexport interface DocumentStructure {\n /** How many `<hr>` the HTML has. */\n rules: number;\n /** For each `<table>` in document order, whether its first row is a header. */\n headerRows: boolean[];\n}\n\n/**\n * Restores structure the generator drops or gets wrong:\n *\n * - Horizontal rules: each centered RULE_TEXT paragraph that stands in for an\n * `<hr>` (see prepareHtmlForDocx) becomes an empty paragraph with a bottom\n * border. They're matched in order against the number of `<hr>`, so a\n * paragraph that only happens to say `* * *` is left alone once they run out.\n * - Header rows: a table's header row gets `<w:tblHeader/>`, so Word repeats\n * it at the top of each page the table spans.\n * - Table grids: the generator writes a second `<w:tblGrid>` between rows,\n * which the schema doesn't allow; only the first is kept.\n */\nexport async function patchStructure(\n wordDocument: Blob,\n structure: DocumentStructure\n): Promise<Blob> {\n const zip = await JSZip.loadAsync(await wordDocument.arrayBuffer());\n const documentEntry = zip.file('word/document.xml');\n if (!documentEntry) {\n throw new Error('Cannot patch structure: document contains no word/document.xml');\n }\n\n const documentXml = await documentEntry.async('string');\n const patched = patchDocumentXml(documentXml, structure);\n if (patched === documentXml) {\n return wordDocument;\n }\n\n zip.file('word/document.xml', patched);\n return new Blob([await zip.generateAsync({ type: 'arraybuffer' })], {\n type: wordDocument.type,\n });\n}\n\n/** Exported for testing. */\nexport function patchDocumentXml(documentXml: string, structure: DocumentStructure): string {\n const dom = new DOMParser().parseFromString(documentXml, 'application/xml');\n let changed = false;\n\n let rulesLeft = structure.rules;\n for (const paragraph of Array.from(dom.getElementsByTagNameNS(W_NS, 'p'))) {\n if (rulesLeft === 0) break;\n if (isRulePlaceholder(paragraph)) {\n drawRule(paragraph);\n rulesLeft--;\n changed = true;\n }\n }\n\n const tables = Array.from(dom.getElementsByTagNameNS(W_NS, 'tbl'));\n for (const [index, table] of tables.entries()) {\n const grids = children(table, 'tblGrid');\n for (const extra of grids.slice(1)) {\n extra.remove();\n changed = true;\n }\n\n const firstRow = children(table, 'tr')[0];\n if (structure.headerRows[index] && firstRow) {\n markHeaderRow(firstRow);\n changed = true;\n }\n }\n\n if (!changed) {\n return documentXml;\n }\n return keepXmlDeclaration(documentXml, new XMLSerializer().serializeToString(dom));\n}\n\nfunction isRulePlaceholder(paragraph: Element): boolean {\n const text = Array.from(paragraph.getElementsByTagNameNS(W_NS, 't'))\n .map((t) => t.textContent ?? '')\n .join('');\n const jc = children(children(paragraph, 'pPr')[0] ?? paragraph, 'jc')[0];\n return text.trim() === RULE_TEXT && jc?.getAttributeNS(W_NS, 'val') === 'center';\n}\n\n/** Replaces the paragraph's content with nothing but a bottom border. */\nfunction drawRule(paragraph: Element): void {\n const document = paragraph.ownerDocument;\n while (paragraph.firstChild) {\n paragraph.removeChild(paragraph.firstChild);\n }\n\n const pPr = paragraph.appendChild(document.createElementNS(W_NS, 'w:pPr'));\n const pBdr = pPr.appendChild(document.createElementNS(W_NS, 'w:pBdr'));\n const bottom = pBdr.appendChild(document.createElementNS(W_NS, 'w:bottom'));\n bottom.setAttributeNS(W_NS, 'w:val', 'single');\n bottom.setAttributeNS(W_NS, 'w:sz', '6');\n bottom.setAttributeNS(W_NS, 'w:space', '1');\n bottom.setAttributeNS(W_NS, 'w:color', 'auto');\n}\n\nfunction markHeaderRow(row: Element): void {\n const document = row.ownerDocument;\n let trPr = children(row, 'trPr')[0];\n if (!trPr) {\n trPr = row.insertBefore(document.createElementNS(W_NS, 'w:trPr'), row.firstChild);\n }\n if (children(trPr, 'tblHeader').length === 0) {\n // The generator writes at most cantSplit here, which comes before tblHeader.\n trPr.appendChild(document.createElementNS(W_NS, 'w:tblHeader'));\n }\n}\n\nfunction children(parent: Element, localName: string): Element[] {\n return Array.from(parent.children).filter(\n (child) => child.namespaceURI === W_NS && child.localName === localName\n );\n}\n"]}
|