@bendyline/squisq-formats 0.1.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/dist/chunk-532L4D5D.js +21 -0
- package/dist/chunk-532L4D5D.js.map +1 -0
- package/dist/chunk-67KIJHV2.js +21 -0
- package/dist/chunk-67KIJHV2.js.map +1 -0
- package/dist/chunk-KAK4V57E.js +387 -0
- package/dist/chunk-KAK4V57E.js.map +1 -0
- package/dist/chunk-MQHCXI56.js +1035 -0
- package/dist/chunk-MQHCXI56.js.map +1 -0
- package/dist/chunk-TBPD5PCU.js +1136 -0
- package/dist/chunk-TBPD5PCU.js.map +1 -0
- package/dist/chunk-ULLIPBEJ.js +271 -0
- package/dist/chunk-ULLIPBEJ.js.map +1 -0
- package/dist/docx/index.d.ts +108 -0
- package/dist/docx/index.js +14 -0
- package/dist/docx/index.js.map +1 -0
- package/dist/html/index.d.ts +166 -0
- package/dist/html/index.js +17 -0
- package/dist/html/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/ooxml/index.d.ts +351 -0
- package/dist/ooxml/index.js +99 -0
- package/dist/ooxml/index.js.map +1 -0
- package/dist/pdf/index.d.ts +130 -0
- package/dist/pdf/index.js +15 -0
- package/dist/pdf/index.js.map +1 -0
- package/dist/pptx/index.d.ts +58 -0
- package/dist/pptx/index.js +13 -0
- package/dist/pptx/index.js.map +1 -0
- package/dist/xlsx/index.d.ts +58 -0
- package/dist/xlsx/index.js +13 -0
- package/dist/xlsx/index.js.map +1 -0
- package/package.json +85 -0
- package/src/__tests__/docxExport.test.ts +457 -0
- package/src/__tests__/docxImport.test.ts +410 -0
- package/src/__tests__/html.test.ts +295 -0
- package/src/__tests__/ooxml.test.ts +197 -0
- package/src/__tests__/pdfExport.test.ts +322 -0
- package/src/__tests__/pdfImport.test.ts +290 -0
- package/src/__tests__/roundTrip.test.ts +201 -0
- package/src/docx/export.ts +978 -0
- package/src/docx/import.ts +909 -0
- package/src/docx/index.ts +26 -0
- package/src/docx/styles.ts +145 -0
- package/src/html/htmlTemplate.ts +307 -0
- package/src/html/imageUtils.ts +66 -0
- package/src/html/index.ts +168 -0
- package/src/index.ts +51 -0
- package/src/ooxml/index.ts +87 -0
- package/src/ooxml/namespaces.ts +160 -0
- package/src/ooxml/reader.ts +218 -0
- package/src/ooxml/types.ts +104 -0
- package/src/ooxml/writer.ts +321 -0
- package/src/ooxml/xmlUtils.ts +123 -0
- package/src/pdf/export.ts +1029 -0
- package/src/pdf/import.ts +835 -0
- package/src/pdf/index.ts +29 -0
- package/src/pdf/styles.ts +180 -0
- package/src/pptx/index.ts +78 -0
- package/src/xlsx/index.ts +78 -0
|
@@ -0,0 +1,909 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOCX Import
|
|
3
|
+
*
|
|
4
|
+
* Parses a .docx file (Office Open XML WordprocessingML) and converts
|
|
5
|
+
* its content into a squisq MarkdownDocument (or Doc).
|
|
6
|
+
*
|
|
7
|
+
* Uses JSZip + DOMParser to read the archive and parse the XML — no
|
|
8
|
+
* third-party docx library. Handles headings, paragraphs, inline
|
|
9
|
+
* formatting (bold, italic, strikethrough), hyperlinks, lists, tables,
|
|
10
|
+
* blockquotes, code blocks, images, and footnotes.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { docxToMarkdownDoc } from '@bendyline/squisq-formats/docx';
|
|
15
|
+
*
|
|
16
|
+
* const response = await fetch('document.docx');
|
|
17
|
+
* const data = await response.arrayBuffer();
|
|
18
|
+
* const doc = await docxToMarkdownDoc(data);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { Doc } from '@bendyline/squisq/schemas';
|
|
23
|
+
import { markdownToDoc } from '@bendyline/squisq/doc';
|
|
24
|
+
import type {
|
|
25
|
+
MarkdownDocument,
|
|
26
|
+
MarkdownBlockNode,
|
|
27
|
+
MarkdownInlineNode,
|
|
28
|
+
MarkdownHeading,
|
|
29
|
+
MarkdownParagraph,
|
|
30
|
+
MarkdownBlockquote,
|
|
31
|
+
MarkdownList,
|
|
32
|
+
MarkdownListItem,
|
|
33
|
+
MarkdownCodeBlock,
|
|
34
|
+
MarkdownTable,
|
|
35
|
+
MarkdownTableRow,
|
|
36
|
+
MarkdownTableCell,
|
|
37
|
+
MarkdownText,
|
|
38
|
+
MarkdownEmphasis,
|
|
39
|
+
MarkdownStrong,
|
|
40
|
+
MarkdownStrikethrough,
|
|
41
|
+
MarkdownInlineCode,
|
|
42
|
+
MarkdownLink,
|
|
43
|
+
MarkdownImage,
|
|
44
|
+
MarkdownBreak,
|
|
45
|
+
MarkdownFootnoteReference,
|
|
46
|
+
MarkdownFootnoteDefinition,
|
|
47
|
+
} from '@bendyline/squisq/markdown';
|
|
48
|
+
|
|
49
|
+
import { openPackage, getPartXml, getPartRelationships } from '../ooxml/reader.js';
|
|
50
|
+
import type { OoxmlPackage, Relationship } from '../ooxml/types.js';
|
|
51
|
+
import { NS_WML, NS_R } from '../ooxml/namespaces.js';
|
|
52
|
+
import {
|
|
53
|
+
HEADING_STYLE_MAP,
|
|
54
|
+
QUOTE_STYLE_IDS,
|
|
55
|
+
CODE_STYLE_IDS,
|
|
56
|
+
INLINE_CODE_STYLE_IDS,
|
|
57
|
+
BULLET_NUM_FORMATS,
|
|
58
|
+
} from './styles.js';
|
|
59
|
+
|
|
60
|
+
// ============================================
|
|
61
|
+
// Public API
|
|
62
|
+
// ============================================
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Options for DOCX import.
|
|
66
|
+
*/
|
|
67
|
+
export interface DocxImportOptions {
|
|
68
|
+
/**
|
|
69
|
+
* Whether to extract embedded images as base64 data URIs.
|
|
70
|
+
* When false, images are represented as `[Image]` placeholders.
|
|
71
|
+
* Default: false
|
|
72
|
+
*/
|
|
73
|
+
extractImages?: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Convert a .docx file to a MarkdownDocument.
|
|
78
|
+
*
|
|
79
|
+
* @param data - The raw .docx file as ArrayBuffer or Blob
|
|
80
|
+
* @param options - Import options
|
|
81
|
+
* @returns A MarkdownDocument representing the document content
|
|
82
|
+
*/
|
|
83
|
+
export async function docxToMarkdownDoc(
|
|
84
|
+
data: ArrayBuffer | Blob,
|
|
85
|
+
options: DocxImportOptions = {},
|
|
86
|
+
): Promise<MarkdownDocument> {
|
|
87
|
+
const pkg = await openPackage(data);
|
|
88
|
+
const ctx = await buildImportContext(pkg, options);
|
|
89
|
+
|
|
90
|
+
const documentXml = await getPartXml(pkg, 'word/document.xml');
|
|
91
|
+
if (!documentXml) {
|
|
92
|
+
return { type: 'document', children: [] };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const body = getFirstElement(documentXml, 'body');
|
|
96
|
+
if (!body) {
|
|
97
|
+
return { type: 'document', children: [] };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const blocks = await convertBody(body, ctx);
|
|
101
|
+
|
|
102
|
+
return { type: 'document', children: blocks };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Convert a .docx file to a squisq Doc.
|
|
107
|
+
*
|
|
108
|
+
* Convenience wrapper: DOCX → MarkdownDocument → Doc.
|
|
109
|
+
*
|
|
110
|
+
* @param data - The raw .docx file as ArrayBuffer or Blob
|
|
111
|
+
* @param options - Import options
|
|
112
|
+
* @returns A squisq Doc
|
|
113
|
+
*/
|
|
114
|
+
export async function docxToDoc(
|
|
115
|
+
data: ArrayBuffer | Blob,
|
|
116
|
+
options: DocxImportOptions = {},
|
|
117
|
+
): Promise<Doc> {
|
|
118
|
+
const markdownDoc = await docxToMarkdownDoc(data, options);
|
|
119
|
+
return markdownToDoc(markdownDoc);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ============================================
|
|
123
|
+
// Import Context
|
|
124
|
+
// ============================================
|
|
125
|
+
|
|
126
|
+
interface ImportContext {
|
|
127
|
+
/** Style ID → heading depth mapping (from styles.xml) */
|
|
128
|
+
headingStyles: Map<string, number>;
|
|
129
|
+
/** Style IDs that represent blockquotes */
|
|
130
|
+
quoteStyles: Set<string>;
|
|
131
|
+
/** Style IDs that represent code blocks */
|
|
132
|
+
codeStyles: Set<string>;
|
|
133
|
+
/** Character style IDs that represent inline code */
|
|
134
|
+
inlineCodeStyles: Set<string>;
|
|
135
|
+
/** Document relationship map: rId → Relationship */
|
|
136
|
+
documentRels: Map<string, Relationship>;
|
|
137
|
+
/** Numbering definitions: numId → { levels: Map<ilvl, isOrdered> } */
|
|
138
|
+
numbering: Map<string, NumberingInfo>;
|
|
139
|
+
/** Footnote bodies: footnoteId → Element */
|
|
140
|
+
footnotes: Map<string, Element>;
|
|
141
|
+
/** Reference to the OOXML package (for extracting images) */
|
|
142
|
+
pkg: OoxmlPackage;
|
|
143
|
+
/** Import options */
|
|
144
|
+
options: DocxImportOptions;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
interface NumberingInfo {
|
|
148
|
+
levels: Map<number, boolean>; // ilvl → isOrdered
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function buildImportContext(
|
|
152
|
+
pkg: OoxmlPackage,
|
|
153
|
+
options: DocxImportOptions,
|
|
154
|
+
): Promise<ImportContext> {
|
|
155
|
+
const ctx: ImportContext = {
|
|
156
|
+
headingStyles: new Map(),
|
|
157
|
+
quoteStyles: new Set(),
|
|
158
|
+
codeStyles: new Set(),
|
|
159
|
+
inlineCodeStyles: new Set(),
|
|
160
|
+
documentRels: new Map(),
|
|
161
|
+
numbering: new Map(),
|
|
162
|
+
footnotes: new Map(),
|
|
163
|
+
pkg,
|
|
164
|
+
options,
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// Initialize with built-in defaults
|
|
168
|
+
for (const [id, depth] of Object.entries(HEADING_STYLE_MAP)) {
|
|
169
|
+
ctx.headingStyles.set(id, depth);
|
|
170
|
+
}
|
|
171
|
+
for (const id of QUOTE_STYLE_IDS) {
|
|
172
|
+
ctx.quoteStyles.add(id);
|
|
173
|
+
}
|
|
174
|
+
for (const id of CODE_STYLE_IDS) {
|
|
175
|
+
ctx.codeStyles.add(id);
|
|
176
|
+
}
|
|
177
|
+
for (const id of INLINE_CODE_STYLE_IDS) {
|
|
178
|
+
ctx.inlineCodeStyles.add(id);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Parse styles.xml for custom heading/quote/code mappings
|
|
182
|
+
await parseStyles(pkg, ctx);
|
|
183
|
+
|
|
184
|
+
// Parse document relationships
|
|
185
|
+
const rels = await getPartRelationships(pkg, 'word/document.xml');
|
|
186
|
+
for (const rel of rels) {
|
|
187
|
+
ctx.documentRels.set(rel.id, rel);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Parse numbering.xml
|
|
191
|
+
await parseNumbering(pkg, ctx);
|
|
192
|
+
|
|
193
|
+
// Parse footnotes.xml
|
|
194
|
+
await parseFootnotes(pkg, ctx);
|
|
195
|
+
|
|
196
|
+
return ctx;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ============================================
|
|
200
|
+
// Styles Parsing
|
|
201
|
+
// ============================================
|
|
202
|
+
|
|
203
|
+
async function parseStyles(pkg: OoxmlPackage, ctx: ImportContext): Promise<void> {
|
|
204
|
+
const doc = await getPartXml(pkg, 'word/styles.xml');
|
|
205
|
+
if (!doc) return;
|
|
206
|
+
|
|
207
|
+
const styles = doc.getElementsByTagNameNS(NS_WML, 'style');
|
|
208
|
+
// Fallback for documents that don't use namespace prefixes properly
|
|
209
|
+
const stylesList = styles.length > 0 ? styles : doc.getElementsByTagName('style');
|
|
210
|
+
|
|
211
|
+
for (let i = 0; i < stylesList.length; i++) {
|
|
212
|
+
const style = stylesList[i];
|
|
213
|
+
const styleId = style.getAttributeNS(NS_WML, 'styleId') ?? style.getAttribute('w:styleId');
|
|
214
|
+
if (!styleId) continue;
|
|
215
|
+
|
|
216
|
+
const nameEl = getFirstChildElement(style, 'name');
|
|
217
|
+
const styleName = nameEl?.getAttributeNS(NS_WML, 'val') ?? nameEl?.getAttribute('w:val') ?? '';
|
|
218
|
+
|
|
219
|
+
// Check if this is a heading style by name
|
|
220
|
+
const headingMatch = styleName.match(/^heading\s+(\d+)$/i);
|
|
221
|
+
if (headingMatch) {
|
|
222
|
+
const depth = parseInt(headingMatch[1], 10);
|
|
223
|
+
if (depth >= 1 && depth <= 6) {
|
|
224
|
+
ctx.headingStyles.set(styleId, depth);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Check pPr > outlineLvl for heading detection
|
|
229
|
+
const pPr = getFirstChildElement(style, 'pPr');
|
|
230
|
+
if (pPr) {
|
|
231
|
+
const outlineLvl = getFirstChildElement(pPr, 'outlineLvl');
|
|
232
|
+
if (outlineLvl) {
|
|
233
|
+
const val = outlineLvl.getAttributeNS(NS_WML, 'val') ?? outlineLvl.getAttribute('w:val');
|
|
234
|
+
if (val !== null) {
|
|
235
|
+
const depth = parseInt(val, 10) + 1;
|
|
236
|
+
if (depth >= 1 && depth <= 6) {
|
|
237
|
+
ctx.headingStyles.set(styleId, depth);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ============================================
|
|
246
|
+
// Numbering Parsing
|
|
247
|
+
// ============================================
|
|
248
|
+
|
|
249
|
+
async function parseNumbering(pkg: OoxmlPackage, ctx: ImportContext): Promise<void> {
|
|
250
|
+
const doc = await getPartXml(pkg, 'word/numbering.xml');
|
|
251
|
+
if (!doc) return;
|
|
252
|
+
|
|
253
|
+
// Parse abstract numbering definitions
|
|
254
|
+
const abstractNums = new Map<string, Map<number, boolean>>(); // abstractNumId → levels(ilvl → isOrdered)
|
|
255
|
+
|
|
256
|
+
const abstractNumEls = getAllElements(doc, 'abstractNum');
|
|
257
|
+
for (const absNum of abstractNumEls) {
|
|
258
|
+
const absId = getAttr(absNum, 'abstractNumId');
|
|
259
|
+
if (!absId) continue;
|
|
260
|
+
|
|
261
|
+
const levels = new Map<number, boolean>();
|
|
262
|
+
const lvlEls = getAllChildElements(absNum, 'lvl');
|
|
263
|
+
for (const lvl of lvlEls) {
|
|
264
|
+
const ilvlStr = getAttr(lvl, 'ilvl');
|
|
265
|
+
if (ilvlStr === null) continue;
|
|
266
|
+
const ilvl = parseInt(ilvlStr, 10);
|
|
267
|
+
|
|
268
|
+
const numFmtEl = getFirstChildElement(lvl, 'numFmt');
|
|
269
|
+
const numFmt = numFmtEl ? getAttr(numFmtEl, 'val') : null;
|
|
270
|
+
|
|
271
|
+
const isOrdered = numFmt !== null && !BULLET_NUM_FORMATS.has(numFmt);
|
|
272
|
+
levels.set(ilvl, isOrdered);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
abstractNums.set(absId, levels);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Parse concrete num → abstractNum mappings
|
|
279
|
+
const numEls = getAllElements(doc, 'num');
|
|
280
|
+
for (const num of numEls) {
|
|
281
|
+
const numId = getAttr(num, 'numId');
|
|
282
|
+
if (!numId) continue;
|
|
283
|
+
|
|
284
|
+
const abstractNumIdEl = getFirstChildElement(num, 'abstractNumId');
|
|
285
|
+
const absId = abstractNumIdEl ? getAttr(abstractNumIdEl, 'val') : null;
|
|
286
|
+
if (!absId) continue;
|
|
287
|
+
|
|
288
|
+
const levels = abstractNums.get(absId);
|
|
289
|
+
if (levels) {
|
|
290
|
+
ctx.numbering.set(numId, { levels });
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// ============================================
|
|
296
|
+
// Footnotes Parsing
|
|
297
|
+
// ============================================
|
|
298
|
+
|
|
299
|
+
async function parseFootnotes(pkg: OoxmlPackage, ctx: ImportContext): Promise<void> {
|
|
300
|
+
const doc = await getPartXml(pkg, 'word/footnotes.xml');
|
|
301
|
+
if (!doc) return;
|
|
302
|
+
|
|
303
|
+
const footnoteEls = getAllElements(doc, 'footnote');
|
|
304
|
+
for (const fn of footnoteEls) {
|
|
305
|
+
const id = getAttr(fn, 'id');
|
|
306
|
+
const type = getAttr(fn, 'type');
|
|
307
|
+
// Skip separator and continuation separator footnotes
|
|
308
|
+
if (!id || type === 'separator' || type === 'continuationSeparator') continue;
|
|
309
|
+
ctx.footnotes.set(id, fn);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ============================================
|
|
314
|
+
// Body Conversion
|
|
315
|
+
// ============================================
|
|
316
|
+
|
|
317
|
+
async function convertBody(body: Element, ctx: ImportContext): Promise<MarkdownBlockNode[]> {
|
|
318
|
+
const result: MarkdownBlockNode[] = [];
|
|
319
|
+
const children = Array.from(body.children);
|
|
320
|
+
|
|
321
|
+
let i = 0;
|
|
322
|
+
while (i < children.length) {
|
|
323
|
+
const el = children[i];
|
|
324
|
+
const localName = el.localName;
|
|
325
|
+
|
|
326
|
+
if (localName === 'p') {
|
|
327
|
+
// Check if this is part of a list
|
|
328
|
+
const numPr = getNumPr(el);
|
|
329
|
+
if (numPr) {
|
|
330
|
+
// Collect consecutive list paragraphs
|
|
331
|
+
const { node, consumed } = await collectList(children, i, ctx);
|
|
332
|
+
result.push(node);
|
|
333
|
+
i += consumed;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const block = await convertParagraph(el, ctx);
|
|
338
|
+
if (block) {
|
|
339
|
+
result.push(block);
|
|
340
|
+
}
|
|
341
|
+
i++;
|
|
342
|
+
} else if (localName === 'tbl') {
|
|
343
|
+
const table = await convertTable(el, ctx);
|
|
344
|
+
if (table) result.push(table);
|
|
345
|
+
i++;
|
|
346
|
+
} else {
|
|
347
|
+
// Skip unknown elements (sectPr, bookmarkStart, etc.)
|
|
348
|
+
i++;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Append footnote definitions at the end
|
|
353
|
+
const footnoteNodes = await convertFootnoteDefinitions(ctx);
|
|
354
|
+
result.push(...footnoteNodes);
|
|
355
|
+
|
|
356
|
+
return result;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// ============================================
|
|
360
|
+
// Paragraph Conversion
|
|
361
|
+
// ============================================
|
|
362
|
+
|
|
363
|
+
async function convertParagraph(
|
|
364
|
+
el: Element,
|
|
365
|
+
ctx: ImportContext,
|
|
366
|
+
): Promise<MarkdownBlockNode | null> {
|
|
367
|
+
const pPr = getFirstChildElement(el, 'pPr');
|
|
368
|
+
const styleId = getParagraphStyleId(pPr);
|
|
369
|
+
|
|
370
|
+
// Check for heading
|
|
371
|
+
if (styleId && ctx.headingStyles.has(styleId)) {
|
|
372
|
+
const depth = ctx.headingStyles.get(styleId)!;
|
|
373
|
+
const inlines = await convertRuns(el, ctx);
|
|
374
|
+
if (inlines.length === 0) return null;
|
|
375
|
+
return {
|
|
376
|
+
type: 'heading',
|
|
377
|
+
depth: Math.min(Math.max(depth, 1), 6) as 1 | 2 | 3 | 4 | 5 | 6,
|
|
378
|
+
children: inlines,
|
|
379
|
+
} satisfies MarkdownHeading;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Check for blockquote
|
|
383
|
+
if (styleId && ctx.quoteStyles.has(styleId)) {
|
|
384
|
+
const inlines = await convertRuns(el, ctx);
|
|
385
|
+
if (inlines.length === 0) return null;
|
|
386
|
+
const paragraph: MarkdownParagraph = { type: 'paragraph', children: inlines };
|
|
387
|
+
return { type: 'blockquote', children: [paragraph] } satisfies MarkdownBlockquote;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Check for code block
|
|
391
|
+
if (styleId && ctx.codeStyles.has(styleId)) {
|
|
392
|
+
const text = getElementTextContent(el);
|
|
393
|
+
return { type: 'code', value: text } satisfies MarkdownCodeBlock;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Regular paragraph
|
|
397
|
+
const inlines = await convertRuns(el, ctx);
|
|
398
|
+
if (inlines.length === 0) return null;
|
|
399
|
+
return { type: 'paragraph', children: inlines } satisfies MarkdownParagraph;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// ============================================
|
|
403
|
+
// Run (Inline) Conversion
|
|
404
|
+
// ============================================
|
|
405
|
+
|
|
406
|
+
async function convertRuns(
|
|
407
|
+
paragraphEl: Element,
|
|
408
|
+
ctx: ImportContext,
|
|
409
|
+
): Promise<MarkdownInlineNode[]> {
|
|
410
|
+
const result: MarkdownInlineNode[] = [];
|
|
411
|
+
const children = Array.from(paragraphEl.children);
|
|
412
|
+
|
|
413
|
+
for (const child of children) {
|
|
414
|
+
const localName = child.localName;
|
|
415
|
+
|
|
416
|
+
if (localName === 'r') {
|
|
417
|
+
const inlines = await convertRun(child, ctx);
|
|
418
|
+
result.push(...inlines);
|
|
419
|
+
} else if (localName === 'hyperlink') {
|
|
420
|
+
const link = await convertHyperlink(child, ctx);
|
|
421
|
+
if (link) result.push(link);
|
|
422
|
+
}
|
|
423
|
+
// Skip pPr, bookmarkStart, bookmarkEnd, etc.
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return mergeAdjacentText(result);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
async function convertRun(runEl: Element, ctx: ImportContext): Promise<MarkdownInlineNode[]> {
|
|
430
|
+
const result: MarkdownInlineNode[] = [];
|
|
431
|
+
const rPr = getFirstChildElement(runEl, 'rPr');
|
|
432
|
+
const format = parseRunFormat(rPr, ctx);
|
|
433
|
+
|
|
434
|
+
for (const child of Array.from(runEl.children)) {
|
|
435
|
+
const localName = child.localName;
|
|
436
|
+
|
|
437
|
+
if (localName === 't') {
|
|
438
|
+
const text = child.textContent ?? '';
|
|
439
|
+
if (!text) continue;
|
|
440
|
+
|
|
441
|
+
if (format.code) {
|
|
442
|
+
result.push({ type: 'inlineCode', value: text } satisfies MarkdownInlineCode);
|
|
443
|
+
} else {
|
|
444
|
+
let node: MarkdownInlineNode = { type: 'text', value: text } satisfies MarkdownText;
|
|
445
|
+
if (format.strike) {
|
|
446
|
+
node = { type: 'delete', children: [node] } satisfies MarkdownStrikethrough;
|
|
447
|
+
}
|
|
448
|
+
if (format.italic) {
|
|
449
|
+
node = { type: 'emphasis', children: [node] } satisfies MarkdownEmphasis;
|
|
450
|
+
}
|
|
451
|
+
if (format.bold) {
|
|
452
|
+
node = { type: 'strong', children: [node] } satisfies MarkdownStrong;
|
|
453
|
+
}
|
|
454
|
+
result.push(node);
|
|
455
|
+
}
|
|
456
|
+
} else if (localName === 'br') {
|
|
457
|
+
result.push({ type: 'break' } satisfies MarkdownBreak);
|
|
458
|
+
} else if (localName === 'footnoteReference') {
|
|
459
|
+
const fnId = getAttr(child, 'id');
|
|
460
|
+
if (fnId && fnId !== '0' && fnId !== '-1') {
|
|
461
|
+
result.push({
|
|
462
|
+
type: 'footnoteReference',
|
|
463
|
+
identifier: `fn${fnId}`,
|
|
464
|
+
} satisfies MarkdownFootnoteReference);
|
|
465
|
+
}
|
|
466
|
+
} else if (localName === 'drawing' || localName === 'pict') {
|
|
467
|
+
const img = await extractImage(child, ctx);
|
|
468
|
+
if (img) result.push(img);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return result;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
interface RunFormat {
|
|
476
|
+
bold: boolean;
|
|
477
|
+
italic: boolean;
|
|
478
|
+
strike: boolean;
|
|
479
|
+
code: boolean;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function parseRunFormat(rPr: Element | null, ctx: ImportContext): RunFormat {
|
|
483
|
+
if (!rPr) return { bold: false, italic: false, strike: false, code: false };
|
|
484
|
+
|
|
485
|
+
const bold = hasChildElement(rPr, 'b') && !isFalseToggle(getFirstChildElement(rPr, 'b')!);
|
|
486
|
+
const italic = hasChildElement(rPr, 'i') && !isFalseToggle(getFirstChildElement(rPr, 'i')!);
|
|
487
|
+
const strike =
|
|
488
|
+
hasChildElement(rPr, 'strike') && !isFalseToggle(getFirstChildElement(rPr, 'strike')!);
|
|
489
|
+
|
|
490
|
+
// Check for inline code via character style
|
|
491
|
+
const rStyle = getFirstChildElement(rPr, 'rStyle');
|
|
492
|
+
const charStyleId = rStyle ? getAttr(rStyle, 'val') : null;
|
|
493
|
+
const isCodeStyle = charStyleId ? ctx.inlineCodeStyles.has(charStyleId) : false;
|
|
494
|
+
|
|
495
|
+
// Check for monospace font as a code indicator
|
|
496
|
+
const rFonts = getFirstChildElement(rPr, 'rFonts');
|
|
497
|
+
const fontName = rFonts ? (getAttr(rFonts, 'ascii') ?? getAttr(rFonts, 'hAnsi') ?? '') : '';
|
|
498
|
+
const isMonospace = /consolas|courier|mono/i.test(fontName);
|
|
499
|
+
|
|
500
|
+
return { bold, italic, strike, code: isCodeStyle || isMonospace };
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function isFalseToggle(el: Element): boolean {
|
|
504
|
+
const val = getAttr(el, 'val');
|
|
505
|
+
return val === '0' || val === 'false';
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// ============================================
|
|
509
|
+
// Hyperlink Conversion
|
|
510
|
+
// ============================================
|
|
511
|
+
|
|
512
|
+
async function convertHyperlink(el: Element, ctx: ImportContext): Promise<MarkdownLink | null> {
|
|
513
|
+
const rId = el.getAttributeNS(NS_R, 'id') ?? el.getAttribute('r:id');
|
|
514
|
+
|
|
515
|
+
let url = '';
|
|
516
|
+
if (rId) {
|
|
517
|
+
const rel = ctx.documentRels.get(rId);
|
|
518
|
+
if (rel) url = rel.target;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Also check for w:anchor (internal bookmarks)
|
|
522
|
+
if (!url) {
|
|
523
|
+
const anchor = el.getAttributeNS(NS_WML, 'anchor') ?? el.getAttribute('w:anchor');
|
|
524
|
+
if (anchor) url = `#${anchor}`;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const inlines: MarkdownInlineNode[] = [];
|
|
528
|
+
for (const child of Array.from(el.children)) {
|
|
529
|
+
if (child.localName === 'r') {
|
|
530
|
+
inlines.push(...(await convertRun(child, ctx)));
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (inlines.length === 0) return null;
|
|
535
|
+
|
|
536
|
+
return {
|
|
537
|
+
type: 'link',
|
|
538
|
+
url,
|
|
539
|
+
children: mergeAdjacentText(inlines),
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// ============================================
|
|
544
|
+
// Image Extraction
|
|
545
|
+
// ============================================
|
|
546
|
+
|
|
547
|
+
async function extractImage(_el: Element, _ctx: ImportContext): Promise<MarkdownImage | null> {
|
|
548
|
+
// Image extraction is complex (DrawingML with blip references).
|
|
549
|
+
// For v1, emit a placeholder. Full implementation requires:
|
|
550
|
+
// 1. Find <a:blip r:embed="rId_"/> in the drawing tree
|
|
551
|
+
// 2. Resolve the rId to a media path
|
|
552
|
+
// 3. Extract the binary data and encode as data URI
|
|
553
|
+
// This is planned for a future enhancement.
|
|
554
|
+
return {
|
|
555
|
+
type: 'image',
|
|
556
|
+
url: '',
|
|
557
|
+
alt: 'Image',
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// ============================================
|
|
562
|
+
// List Collection
|
|
563
|
+
// ============================================
|
|
564
|
+
|
|
565
|
+
interface ListResult {
|
|
566
|
+
node: MarkdownList;
|
|
567
|
+
consumed: number;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
interface NumPrInfo {
|
|
571
|
+
numId: string;
|
|
572
|
+
ilvl: number;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
async function collectList(
|
|
576
|
+
elements: Element[],
|
|
577
|
+
startIdx: number,
|
|
578
|
+
ctx: ImportContext,
|
|
579
|
+
): Promise<ListResult> {
|
|
580
|
+
const firstNumPr = getNumPr(elements[startIdx])!;
|
|
581
|
+
const { numId } = firstNumPr;
|
|
582
|
+
|
|
583
|
+
// Determine if ordered from numbering definition
|
|
584
|
+
const numInfo = ctx.numbering.get(numId);
|
|
585
|
+
const isOrdered = numInfo?.levels.get(0) ?? false;
|
|
586
|
+
|
|
587
|
+
const items: MarkdownListItem[] = [];
|
|
588
|
+
let consumed = 0;
|
|
589
|
+
|
|
590
|
+
let i = startIdx;
|
|
591
|
+
while (i < elements.length) {
|
|
592
|
+
const el = elements[i];
|
|
593
|
+
if (el.localName !== 'p') break;
|
|
594
|
+
|
|
595
|
+
const numPr = getNumPr(el);
|
|
596
|
+
if (!numPr || numPr.numId !== numId) break;
|
|
597
|
+
|
|
598
|
+
// Convert this paragraph's inline content
|
|
599
|
+
const inlines = await convertRuns(el, ctx);
|
|
600
|
+
if (inlines.length > 0) {
|
|
601
|
+
const paragraph: MarkdownParagraph = { type: 'paragraph', children: inlines };
|
|
602
|
+
|
|
603
|
+
// Check if this is a nested list item
|
|
604
|
+
if (numPr.ilvl > firstNumPr.ilvl) {
|
|
605
|
+
// Collect nested list items
|
|
606
|
+
const nested = await collectNestedList(elements, i, ctx, firstNumPr.ilvl);
|
|
607
|
+
if (items.length > 0) {
|
|
608
|
+
// Attach nested list to the last item
|
|
609
|
+
const lastItem = items[items.length - 1];
|
|
610
|
+
const nestedIsOrdered = numInfo?.levels.get(numPr.ilvl) ?? false;
|
|
611
|
+
const nestedList: MarkdownList = {
|
|
612
|
+
type: 'list',
|
|
613
|
+
ordered: nestedIsOrdered,
|
|
614
|
+
children: nested.items,
|
|
615
|
+
};
|
|
616
|
+
lastItem.children.push(nestedList);
|
|
617
|
+
}
|
|
618
|
+
i += nested.consumed;
|
|
619
|
+
consumed += nested.consumed;
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
items.push({
|
|
624
|
+
type: 'listItem',
|
|
625
|
+
children: [paragraph],
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
i++;
|
|
630
|
+
consumed++;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
return {
|
|
634
|
+
node: {
|
|
635
|
+
type: 'list',
|
|
636
|
+
ordered: isOrdered,
|
|
637
|
+
children: items,
|
|
638
|
+
},
|
|
639
|
+
consumed,
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
interface NestedListResult {
|
|
644
|
+
items: MarkdownListItem[];
|
|
645
|
+
consumed: number;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
async function collectNestedList(
|
|
649
|
+
elements: Element[],
|
|
650
|
+
startIdx: number,
|
|
651
|
+
ctx: ImportContext,
|
|
652
|
+
parentIlvl: number,
|
|
653
|
+
): Promise<NestedListResult> {
|
|
654
|
+
const items: MarkdownListItem[] = [];
|
|
655
|
+
let consumed = 0;
|
|
656
|
+
|
|
657
|
+
let i = startIdx;
|
|
658
|
+
while (i < elements.length) {
|
|
659
|
+
const el = elements[i];
|
|
660
|
+
if (el.localName !== 'p') break;
|
|
661
|
+
|
|
662
|
+
const numPr = getNumPr(el);
|
|
663
|
+
if (!numPr) break;
|
|
664
|
+
if (numPr.ilvl <= parentIlvl) break;
|
|
665
|
+
|
|
666
|
+
const inlines = await convertRuns(el, ctx);
|
|
667
|
+
if (inlines.length > 0) {
|
|
668
|
+
const paragraph: MarkdownParagraph = { type: 'paragraph', children: inlines };
|
|
669
|
+
items.push({ type: 'listItem', children: [paragraph] });
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
i++;
|
|
673
|
+
consumed++;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
return { items, consumed };
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
function getNumPr(el: Element): NumPrInfo | null {
|
|
680
|
+
const pPr = getFirstChildElement(el, 'pPr');
|
|
681
|
+
if (!pPr) return null;
|
|
682
|
+
|
|
683
|
+
const numPr = getFirstChildElement(pPr, 'numPr');
|
|
684
|
+
if (!numPr) return null;
|
|
685
|
+
|
|
686
|
+
const ilvlEl = getFirstChildElement(numPr, 'ilvl');
|
|
687
|
+
const numIdEl = getFirstChildElement(numPr, 'numId');
|
|
688
|
+
|
|
689
|
+
const ilvlVal = ilvlEl ? getAttr(ilvlEl, 'val') : null;
|
|
690
|
+
const numIdVal = numIdEl ? getAttr(numIdEl, 'val') : null;
|
|
691
|
+
|
|
692
|
+
if (!numIdVal || numIdVal === '0') return null; // numId 0 means "no list"
|
|
693
|
+
|
|
694
|
+
return {
|
|
695
|
+
numId: numIdVal,
|
|
696
|
+
ilvl: ilvlVal ? parseInt(ilvlVal, 10) : 0,
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
// ============================================
|
|
701
|
+
// Table Conversion
|
|
702
|
+
// ============================================
|
|
703
|
+
|
|
704
|
+
async function convertTable(tblEl: Element, ctx: ImportContext): Promise<MarkdownTable | null> {
|
|
705
|
+
const rows: MarkdownTableRow[] = [];
|
|
706
|
+
|
|
707
|
+
const trEls = getAllChildElements(tblEl, 'tr');
|
|
708
|
+
for (let ri = 0; ri < trEls.length; ri++) {
|
|
709
|
+
const row = await convertTableRow(trEls[ri], ctx, ri === 0);
|
|
710
|
+
rows.push(row);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
if (rows.length === 0) return null;
|
|
714
|
+
|
|
715
|
+
// If first row isn't explicitly a header, treat it as one anyway
|
|
716
|
+
// (Markdown tables require a header row)
|
|
717
|
+
return {
|
|
718
|
+
type: 'table',
|
|
719
|
+
children: rows,
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
async function convertTableRow(
|
|
724
|
+
trEl: Element,
|
|
725
|
+
ctx: ImportContext,
|
|
726
|
+
isHeader: boolean,
|
|
727
|
+
): Promise<MarkdownTableRow> {
|
|
728
|
+
const cells: MarkdownTableCell[] = [];
|
|
729
|
+
const tcEls = getAllChildElements(trEl, 'tc');
|
|
730
|
+
|
|
731
|
+
for (const tc of tcEls) {
|
|
732
|
+
const cell = await convertTableCell(tc, ctx, isHeader);
|
|
733
|
+
cells.push(cell);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
return { type: 'tableRow', children: cells };
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
async function convertTableCell(
|
|
740
|
+
tcEl: Element,
|
|
741
|
+
ctx: ImportContext,
|
|
742
|
+
isHeader: boolean,
|
|
743
|
+
): Promise<MarkdownTableCell> {
|
|
744
|
+
const inlines: MarkdownInlineNode[] = [];
|
|
745
|
+
|
|
746
|
+
// A cell can contain multiple paragraphs; flatten them with breaks
|
|
747
|
+
const paragraphs = getAllChildElements(tcEl, 'p');
|
|
748
|
+
for (let pi = 0; pi < paragraphs.length; pi++) {
|
|
749
|
+
if (pi > 0) {
|
|
750
|
+
inlines.push({ type: 'break' } as MarkdownBreak);
|
|
751
|
+
}
|
|
752
|
+
const runs = await convertRuns(paragraphs[pi], ctx);
|
|
753
|
+
inlines.push(...runs);
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
return {
|
|
757
|
+
type: 'tableCell',
|
|
758
|
+
isHeader,
|
|
759
|
+
children: mergeAdjacentText(inlines),
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
// ============================================
|
|
764
|
+
// Footnote Definition Conversion
|
|
765
|
+
// ============================================
|
|
766
|
+
|
|
767
|
+
async function convertFootnoteDefinitions(
|
|
768
|
+
ctx: ImportContext,
|
|
769
|
+
): Promise<MarkdownFootnoteDefinition[]> {
|
|
770
|
+
const results: MarkdownFootnoteDefinition[] = [];
|
|
771
|
+
|
|
772
|
+
for (const [id, el] of ctx.footnotes) {
|
|
773
|
+
const children: MarkdownBlockNode[] = [];
|
|
774
|
+
const paragraphs = getAllChildElements(el, 'p');
|
|
775
|
+
|
|
776
|
+
for (const p of paragraphs) {
|
|
777
|
+
const inlines = await convertRuns(p, ctx);
|
|
778
|
+
if (inlines.length > 0) {
|
|
779
|
+
children.push({
|
|
780
|
+
type: 'paragraph',
|
|
781
|
+
children: inlines,
|
|
782
|
+
} satisfies MarkdownParagraph);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
if (children.length > 0) {
|
|
787
|
+
results.push({
|
|
788
|
+
type: 'footnoteDefinition',
|
|
789
|
+
identifier: `fn${id}`,
|
|
790
|
+
children,
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
return results;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// ============================================
|
|
799
|
+
// XML Helper Utilities
|
|
800
|
+
// ============================================
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* Get the first element child with a given local name.
|
|
804
|
+
* Handles both namespaced and non-namespaced elements.
|
|
805
|
+
*/
|
|
806
|
+
function getFirstChildElement(parent: Element | Document, localName: string): Element | null {
|
|
807
|
+
for (const child of Array.from(parent.children ?? [])) {
|
|
808
|
+
if (child.localName === localName) return child;
|
|
809
|
+
}
|
|
810
|
+
return null;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Get all direct child elements with a given local name.
|
|
815
|
+
*/
|
|
816
|
+
function getAllChildElements(parent: Element, localName: string): Element[] {
|
|
817
|
+
const result: Element[] = [];
|
|
818
|
+
for (const child of Array.from(parent.children)) {
|
|
819
|
+
if (child.localName === localName) result.push(child);
|
|
820
|
+
}
|
|
821
|
+
return result;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Get all elements with a given local name in the document.
|
|
826
|
+
*/
|
|
827
|
+
function getAllElements(doc: Document | Element, localName: string): Element[] {
|
|
828
|
+
// Try namespace-aware first
|
|
829
|
+
const nsEls =
|
|
830
|
+
'getElementsByTagNameNS' in doc ? doc.getElementsByTagNameNS(NS_WML, localName) : null;
|
|
831
|
+
if (nsEls && nsEls.length > 0) return Array.from(nsEls);
|
|
832
|
+
|
|
833
|
+
// Fallback: try with w: prefix
|
|
834
|
+
const prefixed = doc.getElementsByTagName(`w:${localName}`);
|
|
835
|
+
if (prefixed.length > 0) return Array.from(prefixed);
|
|
836
|
+
|
|
837
|
+
// Final fallback: bare name
|
|
838
|
+
return Array.from(doc.getElementsByTagName(localName));
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* Get the first element with given local name in the document or subtree.
|
|
843
|
+
*/
|
|
844
|
+
function getFirstElement(doc: Document | Element, localName: string): Element | null {
|
|
845
|
+
const els = getAllElements(doc, localName);
|
|
846
|
+
return els.length > 0 ? els[0] : null;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Get a w:-prefixed attribute, trying namespace-aware first then fallback.
|
|
851
|
+
*/
|
|
852
|
+
function getAttr(el: Element, localName: string): string | null {
|
|
853
|
+
return el.getAttributeNS(NS_WML, localName) || el.getAttribute(`w:${localName}`) || null;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* Check if an element has a direct child with the given local name.
|
|
858
|
+
*/
|
|
859
|
+
function hasChildElement(parent: Element, localName: string): boolean {
|
|
860
|
+
return getFirstChildElement(parent, localName) !== null;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/**
|
|
864
|
+
* Get the paragraph style ID from a pPr element.
|
|
865
|
+
*/
|
|
866
|
+
function getParagraphStyleId(pPr: Element | null): string | null {
|
|
867
|
+
if (!pPr) return null;
|
|
868
|
+
const pStyle = getFirstChildElement(pPr, 'pStyle');
|
|
869
|
+
if (!pStyle) return null;
|
|
870
|
+
return getAttr(pStyle, 'val');
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Get all text content from an element (concatenating all w:t descendants).
|
|
875
|
+
*/
|
|
876
|
+
function getElementTextContent(el: Element): string {
|
|
877
|
+
const parts: string[] = [];
|
|
878
|
+
|
|
879
|
+
function walk(node: Element): void {
|
|
880
|
+
if (node.localName === 't') {
|
|
881
|
+
parts.push(node.textContent ?? '');
|
|
882
|
+
}
|
|
883
|
+
for (const child of Array.from(node.children)) {
|
|
884
|
+
walk(child);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
walk(el);
|
|
889
|
+
return parts.join('');
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Merge adjacent MarkdownText nodes to reduce fragmentation.
|
|
894
|
+
*/
|
|
895
|
+
function mergeAdjacentText(nodes: MarkdownInlineNode[]): MarkdownInlineNode[] {
|
|
896
|
+
if (nodes.length <= 1) return nodes;
|
|
897
|
+
|
|
898
|
+
const result: MarkdownInlineNode[] = [];
|
|
899
|
+
for (const node of nodes) {
|
|
900
|
+
const prev = result[result.length - 1];
|
|
901
|
+
if (node.type === 'text' && prev?.type === 'text') {
|
|
902
|
+
// Merge into previous text node
|
|
903
|
+
(prev as MarkdownText).value += (node as MarkdownText).value;
|
|
904
|
+
} else {
|
|
905
|
+
result.push(node);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
return result;
|
|
909
|
+
}
|