@ryangarber/markitdown-ts 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/index.js ADDED
@@ -0,0 +1,516 @@
1
+ import turndownPluginGfm from "@joplin/turndown-plugin-gfm";
2
+ import TurndownService from "turndown";
3
+ import { parseHTML } from "linkedom";
4
+ import mammoth from "mammoth";
5
+ import * as XLSX from "xlsx";
6
+ import { DOMParser } from "@xmldom/xmldom";
7
+ //#region src/bytes.ts
8
+ async function toUint8Array(source) {
9
+ if (source instanceof Uint8Array) return source;
10
+ if (isBlob(source)) return new Uint8Array(await source.arrayBuffer());
11
+ if (ArrayBuffer.isView(source)) return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
12
+ return new Uint8Array(source);
13
+ }
14
+ function toArrayBuffer(source) {
15
+ return source.slice().buffer;
16
+ }
17
+ function decodeText(source) {
18
+ return new TextDecoder().decode(source);
19
+ }
20
+ function isBlob(value) {
21
+ return typeof Blob !== "undefined" && value instanceof Blob;
22
+ }
23
+ //#endregion
24
+ //#region src/custom-turndown.ts
25
+ var CustomTurnDown = class {
26
+ convertSoup(document) {
27
+ const service = new TurndownService({ headingStyle: "atx" });
28
+ service.use(turndownPluginGfm.gfm);
29
+ service.addRule("anchor tags", {
30
+ filter: ["a"],
31
+ replacement(content, node) {
32
+ if (!content) return "";
33
+ const prefix = content.startsWith(" ") ? " " : "";
34
+ const suffix = content.endsWith(" ") ? " " : "";
35
+ const text = content.trim().replace(/\n\n.*/g, "");
36
+ if (!text) return "";
37
+ const element = node;
38
+ const href = element.getAttribute("href");
39
+ let title = element.getAttribute("title") ?? "";
40
+ if (!href) return `${prefix}${text}${suffix}`;
41
+ try {
42
+ const url = new URL(href);
43
+ if (url.protocol !== "https:" && url.protocol !== "http:") return `${prefix}${text}${suffix}`;
44
+ } catch {
45
+ if (!href.startsWith("/") && !href.startsWith("#") && !href.startsWith("./") && !href.startsWith("../")) return `${prefix}${text}${suffix}`;
46
+ }
47
+ if (text.replace(/\\_/g, "_") === href && !title) return `<${href}>`;
48
+ if (!title) title = href;
49
+ return `${prefix}[${text}](${href}${title ? ` "${title}"` : ""})${suffix}`;
50
+ }
51
+ });
52
+ service.addRule("img tags", {
53
+ filter: ["img"],
54
+ replacement(_content, node) {
55
+ const element = node;
56
+ const alt = element.getAttribute("alt") ?? "";
57
+ let source = element.getAttribute("src") ?? "";
58
+ const title = element.getAttribute("title");
59
+ if (source.startsWith("data:")) source = `${source.split(",", 1)[0]}...`;
60
+ return `![${alt}](${source}${title ? ` "${title}"` : ""})`;
61
+ }
62
+ });
63
+ return service.turndown(document);
64
+ }
65
+ };
66
+ //#endregion
67
+ //#region src/html-document.ts
68
+ function parseHtmlDocument(html) {
69
+ return parseHTML(html).document;
70
+ }
71
+ //#endregion
72
+ //#region src/converters/bingserp.ts
73
+ const BING_SEARCH_URL = /^https:\/\/www\.bing\.com\/search\?q=/i;
74
+ const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
75
+ var BingSerpConverter = class {
76
+ async convert(source, options = {}) {
77
+ const extension = options.file_extension?.toLowerCase() ?? "";
78
+ const url = options.url ?? "";
79
+ if (extension !== ".html" && extension !== ".htm" || !BING_SEARCH_URL.test(url)) return null;
80
+ const document = parseHtmlDocument(decodeText(source));
81
+ const query = new URL(url).searchParams.get("q") ?? "";
82
+ document.querySelectorAll(".tptt").forEach((element) => {
83
+ if (element.textContent) element.textContent += " ";
84
+ });
85
+ document.querySelectorAll(".algoSlug_icon").forEach((element) => {
86
+ element.remove();
87
+ });
88
+ const turndown = new CustomTurnDown();
89
+ const markdown = `## A Bing search for '${query}' found the following results:\n\n${[...document.querySelectorAll(".b_algo")].map((result) => {
90
+ result.querySelectorAll("a[href]").forEach((anchor) => {
91
+ const href = anchor.getAttribute("href");
92
+ if (!href) return;
93
+ try {
94
+ const encoded = new URL(href, url).searchParams.get("u");
95
+ if (encoded) anchor.setAttribute("href", decodeBingUrl(encoded));
96
+ } catch {}
97
+ });
98
+ return turndown.convertSoup(result).split(/\n+/).map((line) => line.trim()).filter(Boolean).join("\n");
99
+ }).join("\n\n")}`;
100
+ return {
101
+ title: document.title || null,
102
+ markdown,
103
+ text_content: markdown
104
+ };
105
+ }
106
+ };
107
+ function decodeBingUrl(encoded) {
108
+ const input = encoded.slice(2).replace(/-/g, "+").replace(/_/g, "/").replace(/=+$/, "");
109
+ const bytes = [];
110
+ let bits = 0;
111
+ let bitCount = 0;
112
+ for (const character of input) {
113
+ const value = BASE64_ALPHABET.indexOf(character);
114
+ if (value < 0) return encoded;
115
+ bits = bits << 6 | value;
116
+ bitCount += 6;
117
+ if (bitCount >= 8) {
118
+ bitCount -= 8;
119
+ bytes.push(bits >> bitCount & 255);
120
+ }
121
+ }
122
+ return new TextDecoder().decode(new Uint8Array(bytes));
123
+ }
124
+ //#endregion
125
+ //#region src/converters/html.ts
126
+ var HtmlConverter = class {
127
+ async convert(source, options) {
128
+ const extension = options.file_extension?.toLowerCase() ?? "";
129
+ if (extension !== ".html" && extension !== ".htm") return null;
130
+ return this.convertHtml(decodeText(source));
131
+ }
132
+ async convertHtml(html) {
133
+ const document = parseHtmlDocument(html);
134
+ document.querySelectorAll("script, style").forEach((element) => {
135
+ element.remove();
136
+ });
137
+ const markdown = new CustomTurnDown().convertSoup(document.body ?? document);
138
+ return {
139
+ title: document.title || null,
140
+ markdown,
141
+ text_content: markdown
142
+ };
143
+ }
144
+ };
145
+ //#endregion
146
+ //#region src/converters/docx.ts
147
+ var DocxConverter = class extends HtmlConverter {
148
+ async convert(source, options) {
149
+ if (options.file_extension?.toLowerCase() !== ".docx") return null;
150
+ const html = await mammoth.convertToHtml({ arrayBuffer: toArrayBuffer(source) }, {
151
+ styleMap: options.styleMap,
152
+ includeEmbeddedStyleMap: options.includeEmbeddedStyleMap,
153
+ includeDefaultStyleMap: options.includeDefaultStyleMap,
154
+ ignoreEmptyParagraphs: options.ignoreEmptyParagraphs,
155
+ idPrefix: options.idPrefix,
156
+ transformDocument: options.transformDocument
157
+ });
158
+ return this.convertHtml(html.value);
159
+ }
160
+ };
161
+ //#endregion
162
+ //#region src/converters/ipynb.ts
163
+ var IpynbConverter = class {
164
+ async convert(source, options = {}) {
165
+ if (options.file_extension?.toLowerCase() !== ".ipynb") return null;
166
+ const notebook = JSON.parse(decodeText(source));
167
+ const output = [];
168
+ let title = notebook.metadata?.title ?? null;
169
+ for (const cell of notebook.cells ?? []) {
170
+ const lines = cell.source ?? [];
171
+ if (cell.cell_type === "markdown") {
172
+ output.push(lines.join(""));
173
+ title ?? (title = lines.find((line) => line.startsWith("# "))?.slice(2).trim() ?? null);
174
+ } else if (cell.cell_type === "code") output.push(`\`\`\`python\n${lines.join("")}\n\`\`\``);
175
+ else if (cell.cell_type === "raw") output.push(`\`\`\`\n${lines.join("")}\n\`\`\``);
176
+ }
177
+ const markdown = output.join("\n\n");
178
+ return {
179
+ title,
180
+ markdown,
181
+ text_content: markdown
182
+ };
183
+ }
184
+ };
185
+ //#endregion
186
+ //#region src/mime.ts
187
+ const MIME_EXTENSIONS = {
188
+ "application/atom+xml": ".atom",
189
+ "application/json": ".json",
190
+ "application/rss+xml": ".rss",
191
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
192
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
193
+ "application/xml": ".xml",
194
+ "text/csv": ".csv",
195
+ "text/html": ".html",
196
+ "text/markdown": ".md",
197
+ "text/plain": ".txt",
198
+ "text/xml": ".xml"
199
+ };
200
+ const TEXT_EXTENSIONS = /* @__PURE__ */ new Set([
201
+ ".csv",
202
+ ".json",
203
+ ".log",
204
+ ".markdown",
205
+ ".md",
206
+ ".text",
207
+ ".txt"
208
+ ]);
209
+ function extensionFromMime(contentType) {
210
+ if (!contentType) return null;
211
+ const mime = contentType.split(";", 1)[0]?.trim().toLowerCase() ?? "";
212
+ return MIME_EXTENSIONS[mime] ?? (mime.startsWith("text/") ? ".txt" : null);
213
+ }
214
+ function isTextExtension(extension) {
215
+ return TEXT_EXTENSIONS.has(extension.toLowerCase());
216
+ }
217
+ function extensionFromPathname(pathname) {
218
+ const filename = pathname.split("/").pop() ?? "";
219
+ const dot = filename.lastIndexOf(".");
220
+ return dot > 0 ? filename.slice(dot).toLowerCase() : null;
221
+ }
222
+ //#endregion
223
+ //#region src/converters/plain-text.ts
224
+ var PlainTextConverter = class {
225
+ async convert(source, options = {}) {
226
+ if (!isTextExtension(options.file_extension ?? "")) return null;
227
+ const content = decodeText(source);
228
+ return {
229
+ title: null,
230
+ markdown: content,
231
+ text_content: content
232
+ };
233
+ }
234
+ };
235
+ //#endregion
236
+ //#region src/converters/wikipedia.ts
237
+ const WIKIPEDIA_URL = /^https?:\/\/[a-z]{2,3}\.wikipedia\.org\//i;
238
+ var WikipediaConverter = class {
239
+ async convert(source, options = {}) {
240
+ const extension = options.file_extension?.toLowerCase() ?? "";
241
+ if (extension !== ".html" && extension !== ".htm" || !WIKIPEDIA_URL.test(options.url ?? "")) return null;
242
+ const document = parseHtmlDocument(decodeText(source));
243
+ document.querySelectorAll("script, style").forEach((element) => {
244
+ element.remove();
245
+ });
246
+ const body = document.querySelector("div#mw-content-text");
247
+ const title = document.querySelector("span.mw-page-title-main")?.textContent?.trim() || document.title || null;
248
+ const markdown = body ? `# ${title ?? "Wikipedia"}\n\n${new CustomTurnDown().convertSoup(body)}` : new CustomTurnDown().convertSoup(document);
249
+ return {
250
+ title,
251
+ markdown,
252
+ text_content: markdown
253
+ };
254
+ }
255
+ };
256
+ //#endregion
257
+ //#region src/converters/xlsx.ts
258
+ var XlsxConverter = class extends HtmlConverter {
259
+ async convert(source, options) {
260
+ if (options.file_extension?.toLowerCase() !== ".xlsx") return null;
261
+ const workbook = XLSX.read(source, { type: "array" });
262
+ const sections = [];
263
+ for (const sheetName of workbook.SheetNames) {
264
+ const sheet = workbook.Sheets[sheetName];
265
+ if (sheet?.["!ref"]) {
266
+ const result = await this.convertHtml(XLSX.utils.sheet_to_html(sheet));
267
+ sections.push(`## ${sheetName}\n${result?.markdown.trim() ?? ""}`);
268
+ }
269
+ }
270
+ const markdown = sections.join("\n\n");
271
+ return {
272
+ title: workbook.Props?.Title || "Untitled",
273
+ markdown,
274
+ text_content: markdown
275
+ };
276
+ }
277
+ };
278
+ //#endregion
279
+ //#region src/converters/xml-rss-atom.ts
280
+ var RSSConverter = class {
281
+ async convert(source, options = {}) {
282
+ const extension = options.file_extension?.toLowerCase() ?? "";
283
+ if (![
284
+ ".xml",
285
+ ".rss",
286
+ ".atom"
287
+ ].includes(extension)) return null;
288
+ const document = new DOMParser().parseFromString(decodeText(source), "text/xml");
289
+ if (document.getElementsByTagName("rss").length > 0) return this.parseRss(document);
290
+ if (document.getElementsByTagName("feed")[0]?.getElementsByTagName("entry").length) return this.parseAtom(document);
291
+ return null;
292
+ }
293
+ parseAtom(document) {
294
+ const root = document.getElementsByTagName("feed")[0];
295
+ if (!root) return null;
296
+ const title = getText(root, "title");
297
+ const subtitle = getText(root, "subtitle");
298
+ const sections = [`# ${title ?? "Feed"}`];
299
+ if (subtitle) sections.push(subtitle);
300
+ for (const entry of [...root.getElementsByTagName("entry")]) {
301
+ const entryTitle = getText(entry, "title");
302
+ const updated = getText(entry, "updated");
303
+ const summary = getText(entry, "summary");
304
+ const content = getText(entry, "content");
305
+ const lines = [];
306
+ if (entryTitle) lines.push(`## ${entryTitle}`);
307
+ if (updated) lines.push(`Updated on: ${updated}`);
308
+ if (summary) lines.push(parseContent(summary));
309
+ if (content) lines.push(parseContent(content));
310
+ sections.push(lines.join("\n"));
311
+ }
312
+ const markdown = sections.join("\n\n");
313
+ return {
314
+ title,
315
+ markdown,
316
+ text_content: markdown
317
+ };
318
+ }
319
+ parseRss(document) {
320
+ const channel = document.getElementsByTagName("rss")[0]?.getElementsByTagName("channel")[0];
321
+ if (!channel) return null;
322
+ const title = getText(channel, "title");
323
+ const description = getText(channel, "description");
324
+ const sections = title ? [`# ${title}`] : [];
325
+ if (description) sections.push(description);
326
+ for (const item of [...channel.getElementsByTagName("item")]) {
327
+ const itemTitle = getText(item, "title");
328
+ const published = getText(item, "pubDate");
329
+ const itemDescription = getText(item, "description");
330
+ const content = getText(item, "content:encoded");
331
+ const lines = [];
332
+ if (itemTitle) lines.push(`## ${itemTitle}`);
333
+ if (published) lines.push(`Published on: ${published}`);
334
+ if (itemDescription) lines.push(parseContent(itemDescription));
335
+ if (content) lines.push(parseContent(content));
336
+ sections.push(lines.join("\n"));
337
+ }
338
+ const markdown = sections.join("\n\n");
339
+ return {
340
+ title,
341
+ markdown,
342
+ text_content: markdown
343
+ };
344
+ }
345
+ };
346
+ function parseContent(content) {
347
+ return new CustomTurnDown().convertSoup(parseHtmlDocument(content).documentElement);
348
+ }
349
+ function getText(element, tagName) {
350
+ return element.getElementsByTagName(tagName)[0]?.textContent?.trim() || null;
351
+ }
352
+ //#endregion
353
+ //#region src/converters/youtube.ts
354
+ var YouTubeConverter = class {
355
+ async convert(source, options = {}) {
356
+ const extension = options.file_extension?.toLowerCase() ?? "";
357
+ if (extension !== ".html" && extension !== ".htm" || !options.url?.startsWith("https://www.youtube.com/watch?")) return null;
358
+ const document = parseHtmlDocument(decodeText(source));
359
+ const metadata = { title: document.title };
360
+ document.querySelectorAll("meta").forEach((meta) => {
361
+ const content = meta.getAttribute("content");
362
+ const key = meta.getAttribute("itemprop") ?? meta.getAttribute("property") ?? meta.getAttribute("name");
363
+ if (key && content) metadata[key] = content;
364
+ });
365
+ try {
366
+ for (const script of document.querySelectorAll("script")) {
367
+ const content = script.textContent ?? "";
368
+ if (!content.includes("ytInitialData")) continue;
369
+ const start = content.indexOf("{");
370
+ const end = content.lastIndexOf("}");
371
+ if (start >= 0 && end > start) {
372
+ const description = findKey(JSON.parse(content.slice(start, end + 1)), "attributedDescriptionBodyText");
373
+ if (isRecord(description) && typeof description.content === "string") metadata.description = description.content;
374
+ }
375
+ break;
376
+ }
377
+ } catch {}
378
+ const title = first(metadata, [
379
+ "title",
380
+ "og:title",
381
+ "name"
382
+ ]) || document.title || null;
383
+ const lines = ["# YouTube"];
384
+ if (title) lines.push(`## ${title}`);
385
+ const stats = [];
386
+ const views = first(metadata, ["interactionCount"]);
387
+ const keywords = first(metadata, ["keywords"]);
388
+ const runtime = first(metadata, ["duration"]);
389
+ if (views) stats.push(`- **Views:** ${views}`);
390
+ if (keywords) stats.push(`- **Keywords:** ${keywords}`);
391
+ if (runtime) stats.push(`- **Runtime:** ${runtime}`);
392
+ if (stats.length > 0) lines.push(`### Video Metadata\n${stats.join("\n")}`);
393
+ const description = first(metadata, ["description", "og:description"]);
394
+ if (description) lines.push(`### Description\n${description}`);
395
+ const markdown = lines.join("\n\n");
396
+ return {
397
+ title,
398
+ markdown,
399
+ text_content: markdown
400
+ };
401
+ }
402
+ };
403
+ function first(metadata, keys) {
404
+ for (const key of keys) if (metadata[key]) return metadata[key];
405
+ return null;
406
+ }
407
+ function findKey(value, key) {
408
+ if (Array.isArray(value)) for (const item of value) {
409
+ const match = findKey(item, key);
410
+ if (match !== void 0) return match;
411
+ }
412
+ else if (isRecord(value)) {
413
+ if (key in value) return value[key];
414
+ for (const item of Object.values(value)) {
415
+ const match = findKey(item, key);
416
+ if (match !== void 0) return match;
417
+ }
418
+ }
419
+ }
420
+ function isRecord(value) {
421
+ return typeof value === "object" && value !== null;
422
+ }
423
+ //#endregion
424
+ //#region src/markitdown.ts
425
+ var MarkItDown = class {
426
+ constructor() {
427
+ this.converters = [];
428
+ this.registerConverter(new PlainTextConverter());
429
+ this.registerConverter(new HtmlConverter());
430
+ this.registerConverter(new RSSConverter());
431
+ this.registerConverter(new WikipediaConverter());
432
+ this.registerConverter(new YouTubeConverter());
433
+ this.registerConverter(new BingSerpConverter());
434
+ this.registerConverter(new DocxConverter());
435
+ this.registerConverter(new XlsxConverter());
436
+ this.registerConverter(new IpynbConverter());
437
+ }
438
+ /** Convert a URL, response, blob, array buffer, data view, or typed array. */
439
+ async convert(source, options = {}) {
440
+ if (typeof source === "string" || source instanceof URL) return this.convertUrl(source.toString(), options);
441
+ if (isResponse(source)) return this.convertResponse(source, options);
442
+ const extensions = /* @__PURE__ */ new Set();
443
+ this.addExtension(extensions, options.file_extension);
444
+ if (isBlob(source)) {
445
+ this.addExtension(extensions, extensionFromMime(source.type));
446
+ if ("name" in source && typeof source.name === "string") this.addExtension(extensions, extensionFromPathname(source.name));
447
+ }
448
+ if (extensions.size === 0) throw new Error("Could not determine the file type. Provide `file_extension` when converting bytes.");
449
+ return this.convertBytes(await toUint8Array(source), extensions, options);
450
+ }
451
+ /**
452
+ * @deprecated Pass the byte source directly to `convert`.
453
+ */
454
+ async convertBuffer(source, options) {
455
+ return this.convert(source, options);
456
+ }
457
+ async convertUrl(source, { fetch: fetchImplementation = globalThis.fetch, ...options }) {
458
+ const url = new URL(source);
459
+ if (url.protocol !== "http:" && url.protocol !== "https:") throw new Error(`Unsupported URL protocol: ${url.protocol}`);
460
+ if (!fetchImplementation) throw new Error("No fetch implementation is available.");
461
+ const response = await fetchImplementation(url);
462
+ if (!response.ok) throw new Error(`Failed to fetch URL: ${url}, status: ${response.status}`);
463
+ return this.convertResponse(response, {
464
+ ...options,
465
+ url: url.toString()
466
+ });
467
+ }
468
+ async convertResponse(response, options) {
469
+ const extensions = /* @__PURE__ */ new Set();
470
+ this.addExtension(extensions, options.file_extension);
471
+ this.addExtension(extensions, extensionFromMime(response.headers.get("content-type")));
472
+ const filename = (response.headers.get("content-disposition") ?? "").match(/filename\*?=(?:UTF-8''|["']?)([^;"']+)/i)?.[1];
473
+ if (filename) this.addExtension(extensions, extensionFromPathname(decodeURIComponent(filename.trim())));
474
+ const responseUrl = options.url || response.url;
475
+ if (responseUrl) this.addExtension(extensions, extensionFromPathname(new URL(responseUrl).pathname));
476
+ if (extensions.size === 0) throw new Error("Could not determine the file type. Provide `file_extension` or a Content-Type header.");
477
+ return this.convertBytes(new Uint8Array(await response.arrayBuffer()), extensions, {
478
+ ...options,
479
+ url: responseUrl
480
+ });
481
+ }
482
+ async convertBytes(source, extensions, options) {
483
+ let lastError;
484
+ for (const extension of extensions) for (const converter of this.converters) try {
485
+ const result = await converter.convert(source, {
486
+ ...options,
487
+ file_extension: extension,
488
+ _parent_converters: this.converters
489
+ });
490
+ if (result) {
491
+ result.markdown = result.markdown.replace(/\r\n|\r|\n/g, "\n").trim().replace(/\n{3,}/g, "\n\n");
492
+ result.text_content = result.markdown;
493
+ return result;
494
+ }
495
+ } catch (error) {
496
+ lastError = error;
497
+ }
498
+ const formats = [...extensions].join(", ");
499
+ if (lastError) throw new Error(`Could not convert ${formats} to Markdown: ${String(lastError)}`);
500
+ throw new Error(`Unsupported file type: ${formats}.`);
501
+ }
502
+ addExtension(extensions, extension) {
503
+ if (!extension) return;
504
+ extensions.add(extension.startsWith(".") ? extension.toLowerCase() : `.${extension.toLowerCase()}`);
505
+ }
506
+ registerConverter(converter) {
507
+ this.converters.unshift(converter);
508
+ }
509
+ };
510
+ function isResponse(value) {
511
+ return typeof Response !== "undefined" && value instanceof Response;
512
+ }
513
+ //#endregion
514
+ export { MarkItDown };
515
+
516
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/bytes.ts","../src/custom-turndown.ts","../src/html-document.ts","../src/converters/bingserp.ts","../src/converters/html.ts","../src/converters/docx.ts","../src/converters/ipynb.ts","../src/mime.ts","../src/converters/plain-text.ts","../src/converters/wikipedia.ts","../src/converters/xlsx.ts","../src/converters/xml-rss-atom.ts","../src/converters/youtube.ts","../src/markitdown.ts"],"sourcesContent":["import type { ByteSource } from \"./types\";\n\nexport async function toUint8Array(source: ByteSource): Promise<Uint8Array> {\n if (source instanceof Uint8Array) {\n return source;\n }\n\n if (isBlob(source)) {\n return new Uint8Array(await source.arrayBuffer());\n }\n\n if (ArrayBuffer.isView(source)) {\n return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);\n }\n\n return new Uint8Array(source);\n}\n\nexport function toArrayBuffer(source: Uint8Array): ArrayBuffer {\n return source.slice().buffer;\n}\n\nexport function decodeText(source: Uint8Array): string {\n return new TextDecoder().decode(source);\n}\n\nexport function isBlob(value: unknown): value is Blob {\n return typeof Blob !== \"undefined\" && value instanceof Blob;\n}\n\nexport function normalizeExtension(extension: string): string {\n if (!extension) {\n return \"\";\n }\n\n const normalized = extension.toLowerCase();\n return normalized.startsWith(\".\") ? normalized : `.${normalized}`;\n}\n","import turndownPluginGfm from \"@joplin/turndown-plugin-gfm\";\nimport TurndownService from \"turndown\";\n\nexport class CustomTurnDown {\n convertSoup(document: string | Node): string {\n const service = new TurndownService({ headingStyle: \"atx\" });\n service.use(turndownPluginGfm.gfm);\n\n service.addRule(\"anchor tags\", {\n filter: [\"a\"],\n replacement(content, node) {\n if (!content) return \"\";\n\n const prefix = content.startsWith(\" \") ? \" \" : \"\";\n const suffix = content.endsWith(\" \") ? \" \" : \"\";\n const text = content.trim().replace(/\\n\\n.*/g, \"\");\n if (!text) return \"\";\n\n const element = node as unknown as Element;\n const href = element.getAttribute(\"href\");\n let title = element.getAttribute(\"title\") ?? \"\";\n if (!href) return `${prefix}${text}${suffix}`;\n\n try {\n const url = new URL(href);\n if (url.protocol !== \"https:\" && url.protocol !== \"http:\") {\n return `${prefix}${text}${suffix}`;\n }\n } catch {\n if (\n !href.startsWith(\"/\") &&\n !href.startsWith(\"#\") &&\n !href.startsWith(\"./\") &&\n !href.startsWith(\"../\")\n ) {\n return `${prefix}${text}${suffix}`;\n }\n }\n\n if (text.replace(/\\\\_/g, \"_\") === href && !title) {\n return `<${href}>`;\n }\n if (!title) title = href;\n return `${prefix}[${text}](${href}${title ? ` \"${title}\"` : \"\"})${suffix}`;\n }\n });\n\n service.addRule(\"img tags\", {\n filter: [\"img\"],\n replacement(_content, node) {\n const element = node as unknown as Element;\n const alt = element.getAttribute(\"alt\") ?? \"\";\n let source = element.getAttribute(\"src\") ?? \"\";\n const title = element.getAttribute(\"title\");\n if (source.startsWith(\"data:\")) {\n source = `${source.split(\",\", 1)[0]}...`;\n }\n return `![${alt}](${source}${title ? ` \"${title}\"` : \"\"})`;\n }\n });\n\n return service.turndown(document as TurndownService.Node);\n }\n}\n","import { parseHTML } from \"linkedom\";\n\nexport function parseHtmlDocument(html: string): Document {\n return parseHTML(html).document as unknown as Document;\n}\n","import { decodeText } from \"../bytes\";\nimport { CustomTurnDown } from \"../custom-turndown\";\nimport { parseHtmlDocument } from \"../html-document\";\nimport type { ConverterOptions, ConverterResult, DocumentConverter } from \"../types\";\n\nconst BING_SEARCH_URL = /^https:\\/\\/www\\.bing\\.com\\/search\\?q=/i;\nconst BASE64_ALPHABET = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\nexport class BingSerpConverter implements DocumentConverter {\n async convert(source: Uint8Array, options: ConverterOptions = {}): Promise<ConverterResult> {\n const extension = options.file_extension?.toLowerCase() ?? \"\";\n const url = options.url ?? \"\";\n if ((extension !== \".html\" && extension !== \".htm\") || !BING_SEARCH_URL.test(url)) {\n return null;\n }\n\n const document = parseHtmlDocument(decodeText(source));\n const query = new URL(url).searchParams.get(\"q\") ?? \"\";\n\n document.querySelectorAll(\".tptt\").forEach((element) => {\n if (element.textContent) {\n element.textContent += \" \";\n }\n });\n document.querySelectorAll(\".algoSlug_icon\").forEach((element) => {\n element.remove();\n });\n\n const turndown = new CustomTurnDown();\n const results = [...document.querySelectorAll(\".b_algo\")].map((result) => {\n result.querySelectorAll(\"a[href]\").forEach((anchor) => {\n const href = anchor.getAttribute(\"href\");\n if (!href) {\n return;\n }\n try {\n const encoded = new URL(href, url).searchParams.get(\"u\");\n if (encoded) {\n anchor.setAttribute(\"href\", decodeBingUrl(encoded));\n }\n } catch {\n // Preserve malformed links as they appeared in the source document.\n }\n });\n\n return turndown\n .convertSoup(result)\n .split(/\\n+/)\n .map((line) => line.trim())\n .filter(Boolean)\n .join(\"\\n\");\n });\n\n const markdown = `## A Bing search for '${query}' found the following results:\\n\\n${results.join(\"\\n\\n\")}`;\n return { title: document.title || null, markdown, text_content: markdown };\n }\n}\n\nfunction decodeBingUrl(encoded: string): string {\n const input = encoded.slice(2).replace(/-/g, \"+\").replace(/_/g, \"/\").replace(/=+$/, \"\");\n const bytes: number[] = [];\n let bits = 0;\n let bitCount = 0;\n\n for (const character of input) {\n const value = BASE64_ALPHABET.indexOf(character);\n if (value < 0) {\n return encoded;\n }\n bits = (bits << 6) | value;\n bitCount += 6;\n if (bitCount >= 8) {\n bitCount -= 8;\n bytes.push((bits >> bitCount) & 0xff);\n }\n }\n\n return new TextDecoder().decode(new Uint8Array(bytes));\n}\n","import { decodeText } from \"../bytes\";\nimport { CustomTurnDown } from \"../custom-turndown\";\nimport { parseHtmlDocument } from \"../html-document\";\nimport type { ConverterOptions, ConverterResult, DocumentConverter } from \"../types\";\n\nexport class HtmlConverter implements DocumentConverter {\n async convert(source: Uint8Array, options: ConverterOptions): Promise<ConverterResult> {\n const extension = options.file_extension?.toLowerCase() ?? \"\";\n if (extension !== \".html\" && extension !== \".htm\") {\n return null;\n }\n\n return this.convertHtml(decodeText(source));\n }\n\n async convertHtml(html: string): Promise<ConverterResult> {\n const document = parseHtmlDocument(html);\n document.querySelectorAll(\"script, style\").forEach((element) => {\n element.remove();\n });\n\n const markdown = new CustomTurnDown().convertSoup(document.body ?? document);\n return {\n title: document.title || null,\n markdown,\n text_content: markdown\n };\n }\n}\n","import mammoth from \"mammoth\";\nimport { toArrayBuffer } from \"../bytes\";\nimport type { ConverterOptions, ConverterResult } from \"../types\";\nimport { HtmlConverter } from \"./html\";\n\nexport class DocxConverter extends HtmlConverter {\n async convert(source: Uint8Array, options: ConverterOptions): Promise<ConverterResult> {\n if (options.file_extension?.toLowerCase() !== \".docx\") {\n return null;\n }\n\n const html = await mammoth.convertToHtml(\n { arrayBuffer: toArrayBuffer(source) },\n {\n styleMap: options.styleMap,\n includeEmbeddedStyleMap: options.includeEmbeddedStyleMap,\n includeDefaultStyleMap: options.includeDefaultStyleMap,\n ignoreEmptyParagraphs: options.ignoreEmptyParagraphs,\n idPrefix: options.idPrefix,\n transformDocument: options.transformDocument\n }\n );\n return this.convertHtml(html.value);\n }\n}\n","import { decodeText } from \"../bytes\";\nimport type { ConverterOptions, ConverterResult, DocumentConverter } from \"../types\";\n\ntype NotebookCell = {\n cell_type?: string;\n source?: string[];\n};\n\ntype Notebook = {\n cells?: NotebookCell[];\n metadata?: { title?: string };\n};\n\nexport class IpynbConverter implements DocumentConverter {\n async convert(source: Uint8Array, options: ConverterOptions = {}): Promise<ConverterResult> {\n if (options.file_extension?.toLowerCase() !== \".ipynb\") {\n return null;\n }\n\n const notebook = JSON.parse(decodeText(source)) as Notebook;\n const output: string[] = [];\n let title: string | null = notebook.metadata?.title ?? null;\n\n for (const cell of notebook.cells ?? []) {\n const lines = cell.source ?? [];\n if (cell.cell_type === \"markdown\") {\n output.push(lines.join(\"\"));\n title ??=\n lines\n .find((line) => line.startsWith(\"# \"))\n ?.slice(2)\n .trim() ?? null;\n } else if (cell.cell_type === \"code\") {\n output.push(`\\`\\`\\`python\\n${lines.join(\"\")}\\n\\`\\`\\``);\n } else if (cell.cell_type === \"raw\") {\n output.push(`\\`\\`\\`\\n${lines.join(\"\")}\\n\\`\\`\\``);\n }\n }\n\n const markdown = output.join(\"\\n\\n\");\n return { title, markdown, text_content: markdown };\n }\n}\n","const MIME_EXTENSIONS: Record<string, string> = {\n \"application/atom+xml\": \".atom\",\n \"application/json\": \".json\",\n \"application/rss+xml\": \".rss\",\n \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\": \".xlsx\",\n \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\": \".docx\",\n \"application/xml\": \".xml\",\n \"text/csv\": \".csv\",\n \"text/html\": \".html\",\n \"text/markdown\": \".md\",\n \"text/plain\": \".txt\",\n \"text/xml\": \".xml\"\n};\n\nconst TEXT_EXTENSIONS = new Set([\".csv\", \".json\", \".log\", \".markdown\", \".md\", \".text\", \".txt\"]);\n\nexport function extensionFromMime(contentType: string | null | undefined): string | null {\n if (!contentType) {\n return null;\n }\n\n const mime = contentType.split(\";\", 1)[0]?.trim().toLowerCase() ?? \"\";\n return MIME_EXTENSIONS[mime] ?? (mime.startsWith(\"text/\") ? \".txt\" : null);\n}\n\nexport function isTextExtension(extension: string): boolean {\n return TEXT_EXTENSIONS.has(extension.toLowerCase());\n}\n\nexport function extensionFromPathname(pathname: string): string | null {\n const filename = pathname.split(\"/\").pop() ?? \"\";\n const dot = filename.lastIndexOf(\".\");\n return dot > 0 ? filename.slice(dot).toLowerCase() : null;\n}\n","import { decodeText } from \"../bytes\";\nimport { isTextExtension } from \"../mime\";\nimport type { ConverterOptions, ConverterResult, DocumentConverter } from \"../types\";\n\nexport class PlainTextConverter implements DocumentConverter {\n async convert(source: Uint8Array, options: ConverterOptions = {}): Promise<ConverterResult> {\n if (!isTextExtension(options.file_extension ?? \"\")) {\n return null;\n }\n\n const content = decodeText(source);\n return { title: null, markdown: content, text_content: content };\n }\n}\n","import { decodeText } from \"../bytes\";\nimport { CustomTurnDown } from \"../custom-turndown\";\nimport { parseHtmlDocument } from \"../html-document\";\nimport type { ConverterOptions, ConverterResult, DocumentConverter } from \"../types\";\n\nconst WIKIPEDIA_URL = /^https?:\\/\\/[a-z]{2,3}\\.wikipedia\\.org\\//i;\n\nexport class WikipediaConverter implements DocumentConverter {\n async convert(source: Uint8Array, options: ConverterOptions = {}): Promise<ConverterResult> {\n const extension = options.file_extension?.toLowerCase() ?? \"\";\n if ((extension !== \".html\" && extension !== \".htm\") || !WIKIPEDIA_URL.test(options.url ?? \"\")) {\n return null;\n }\n\n const document = parseHtmlDocument(decodeText(source));\n document.querySelectorAll(\"script, style\").forEach((element) => {\n element.remove();\n });\n\n const body = document.querySelector(\"div#mw-content-text\");\n const pageTitle = document.querySelector(\"span.mw-page-title-main\")?.textContent?.trim();\n const title = pageTitle || document.title || null;\n const markdown = body\n ? `# ${title ?? \"Wikipedia\"}\\n\\n${new CustomTurnDown().convertSoup(body)}`\n : new CustomTurnDown().convertSoup(document);\n\n return { title, markdown, text_content: markdown };\n }\n}\n","import * as XLSX from \"xlsx\";\nimport type { ConverterOptions, ConverterResult } from \"../types\";\nimport { HtmlConverter } from \"./html\";\n\nexport class XlsxConverter extends HtmlConverter {\n async convert(source: Uint8Array, options: ConverterOptions): Promise<ConverterResult> {\n if (options.file_extension?.toLowerCase() !== \".xlsx\") {\n return null;\n }\n\n const workbook = XLSX.read(source, { type: \"array\" });\n const sections: string[] = [];\n\n for (const sheetName of workbook.SheetNames) {\n const sheet = workbook.Sheets[sheetName];\n if (sheet?.[\"!ref\"]) {\n const result = await this.convertHtml(XLSX.utils.sheet_to_html(sheet));\n sections.push(`## ${sheetName}\\n${result?.markdown.trim() ?? \"\"}`);\n }\n }\n\n const markdown = sections.join(\"\\n\\n\");\n return {\n title: workbook.Props?.Title || \"Untitled\",\n markdown,\n text_content: markdown\n };\n }\n}\n","import type { Document, Element } from \"@xmldom/xmldom\";\nimport { DOMParser } from \"@xmldom/xmldom\";\nimport { decodeText } from \"../bytes\";\nimport { CustomTurnDown } from \"../custom-turndown\";\nimport { parseHtmlDocument } from \"../html-document\";\nimport type { ConverterOptions, ConverterResult, DocumentConverter } from \"../types\";\n\nexport class RSSConverter implements DocumentConverter {\n async convert(source: Uint8Array, options: ConverterOptions = {}): Promise<ConverterResult> {\n const extension = options.file_extension?.toLowerCase() ?? \"\";\n if (![\".xml\", \".rss\", \".atom\"].includes(extension)) {\n return null;\n }\n\n const document = new DOMParser().parseFromString(decodeText(source), \"text/xml\");\n if (document.getElementsByTagName(\"rss\").length > 0) {\n return this.parseRss(document);\n }\n if (document.getElementsByTagName(\"feed\")[0]?.getElementsByTagName(\"entry\").length) {\n return this.parseAtom(document);\n }\n return null;\n }\n\n private parseAtom(document: Document): ConverterResult {\n const root = document.getElementsByTagName(\"feed\")[0];\n if (!root) return null;\n\n const title = getText(root, \"title\");\n const subtitle = getText(root, \"subtitle\");\n const sections = [`# ${title ?? \"Feed\"}`];\n if (subtitle) sections.push(subtitle);\n\n for (const entry of [...root.getElementsByTagName(\"entry\")]) {\n const entryTitle = getText(entry, \"title\");\n const updated = getText(entry, \"updated\");\n const summary = getText(entry, \"summary\");\n const content = getText(entry, \"content\");\n const lines: string[] = [];\n if (entryTitle) lines.push(`## ${entryTitle}`);\n if (updated) lines.push(`Updated on: ${updated}`);\n if (summary) lines.push(parseContent(summary));\n if (content) lines.push(parseContent(content));\n sections.push(lines.join(\"\\n\"));\n }\n\n const markdown = sections.join(\"\\n\\n\");\n return { title, markdown, text_content: markdown };\n }\n\n private parseRss(document: Document): ConverterResult {\n const channel = document.getElementsByTagName(\"rss\")[0]?.getElementsByTagName(\"channel\")[0];\n if (!channel) return null;\n\n const title = getText(channel, \"title\");\n const description = getText(channel, \"description\");\n const sections: string[] = title ? [`# ${title}`] : [];\n if (description) sections.push(description);\n\n for (const item of [...channel.getElementsByTagName(\"item\")]) {\n const itemTitle = getText(item, \"title\");\n const published = getText(item, \"pubDate\");\n const itemDescription = getText(item, \"description\");\n const content = getText(item, \"content:encoded\");\n const lines: string[] = [];\n if (itemTitle) lines.push(`## ${itemTitle}`);\n if (published) lines.push(`Published on: ${published}`);\n if (itemDescription) lines.push(parseContent(itemDescription));\n if (content) lines.push(parseContent(content));\n sections.push(lines.join(\"\\n\"));\n }\n\n const markdown = sections.join(\"\\n\\n\");\n return { title, markdown, text_content: markdown };\n }\n}\n\nfunction parseContent(content: string): string {\n return new CustomTurnDown().convertSoup(parseHtmlDocument(content).documentElement);\n}\n\nfunction getText(element: Element, tagName: string): string | null {\n const node = element.getElementsByTagName(tagName)[0];\n return node?.textContent?.trim() || null;\n}\n","import { decodeText } from \"../bytes\";\nimport { parseHtmlDocument } from \"../html-document\";\nimport type { ConverterOptions, ConverterResult, DocumentConverter } from \"../types\";\n\nexport class YouTubeConverter implements DocumentConverter {\n async convert(source: Uint8Array, options: ConverterOptions = {}): Promise<ConverterResult> {\n const extension = options.file_extension?.toLowerCase() ?? \"\";\n if (\n (extension !== \".html\" && extension !== \".htm\") ||\n !options.url?.startsWith(\"https://www.youtube.com/watch?\")\n ) {\n return null;\n }\n\n const document = parseHtmlDocument(decodeText(source));\n const metadata: Record<string, string> = { title: document.title };\n\n document.querySelectorAll(\"meta\").forEach((meta) => {\n const content = meta.getAttribute(\"content\");\n const key =\n meta.getAttribute(\"itemprop\") ?? meta.getAttribute(\"property\") ?? meta.getAttribute(\"name\");\n if (key && content) {\n metadata[key] = content;\n }\n });\n\n try {\n for (const script of document.querySelectorAll(\"script\")) {\n const content = script.textContent ?? \"\";\n if (!content.includes(\"ytInitialData\")) {\n continue;\n }\n const start = content.indexOf(\"{\");\n const end = content.lastIndexOf(\"}\");\n if (start >= 0 && end > start) {\n const description = findKey(\n JSON.parse(content.slice(start, end + 1)),\n \"attributedDescriptionBodyText\"\n );\n if (isRecord(description) && typeof description.content === \"string\") {\n metadata.description = description.content;\n }\n }\n break;\n }\n } catch {\n // Meta description remains available when the page implementation changes.\n }\n\n const title = first(metadata, [\"title\", \"og:title\", \"name\"]) || document.title || null;\n const lines = [\"# YouTube\"];\n if (title) {\n lines.push(`## ${title}`);\n }\n\n const stats: string[] = [];\n const views = first(metadata, [\"interactionCount\"]);\n const keywords = first(metadata, [\"keywords\"]);\n const runtime = first(metadata, [\"duration\"]);\n if (views) stats.push(`- **Views:** ${views}`);\n if (keywords) stats.push(`- **Keywords:** ${keywords}`);\n if (runtime) stats.push(`- **Runtime:** ${runtime}`);\n if (stats.length > 0) {\n lines.push(`### Video Metadata\\n${stats.join(\"\\n\")}`);\n }\n\n const description = first(metadata, [\"description\", \"og:description\"]);\n if (description) {\n lines.push(`### Description\\n${description}`);\n }\n\n const markdown = lines.join(\"\\n\\n\");\n return { title, markdown, text_content: markdown };\n }\n}\n\nfunction first(metadata: Record<string, string>, keys: string[]): string | null {\n for (const key of keys) {\n if (metadata[key]) return metadata[key];\n }\n return null;\n}\n\nfunction findKey(value: unknown, key: string): unknown {\n if (Array.isArray(value)) {\n for (const item of value) {\n const match = findKey(item, key);\n if (match !== undefined) return match;\n }\n } else if (isRecord(value)) {\n if (key in value) return value[key];\n for (const item of Object.values(value)) {\n const match = findKey(item, key);\n if (match !== undefined) return match;\n }\n }\n return undefined;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null;\n}\n","import { isBlob, toUint8Array } from \"./bytes\";\nimport { BingSerpConverter } from \"./converters/bingserp\";\nimport { DocxConverter } from \"./converters/docx\";\nimport { HtmlConverter } from \"./converters/html\";\nimport { IpynbConverter } from \"./converters/ipynb\";\nimport { PlainTextConverter } from \"./converters/plain-text\";\nimport { WikipediaConverter } from \"./converters/wikipedia\";\nimport { XlsxConverter } from \"./converters/xlsx\";\nimport { RSSConverter } from \"./converters/xml-rss-atom\";\nimport { YouTubeConverter } from \"./converters/youtube\";\nimport { extensionFromMime, extensionFromPathname } from \"./mime\";\nimport type { ByteSource, ConverterOptions, ConverterResult, DocumentConverter } from \"./types\";\n\nexport type ConversionSource = ByteSource | Response | URL | string;\n\nexport class MarkItDown {\n private readonly converters: DocumentConverter[] = [];\n\n constructor() {\n this.registerConverter(new PlainTextConverter());\n this.registerConverter(new HtmlConverter());\n this.registerConverter(new RSSConverter());\n this.registerConverter(new WikipediaConverter());\n this.registerConverter(new YouTubeConverter());\n this.registerConverter(new BingSerpConverter());\n this.registerConverter(new DocxConverter());\n this.registerConverter(new XlsxConverter());\n this.registerConverter(new IpynbConverter());\n }\n\n /** Convert a URL, response, blob, array buffer, data view, or typed array. */\n async convert(\n source: ConversionSource,\n options: ConverterOptions = {}\n ): Promise<ConverterResult> {\n if (typeof source === \"string\" || source instanceof URL) {\n return this.convertUrl(source.toString(), options);\n }\n\n if (isResponse(source)) {\n return this.convertResponse(source, options);\n }\n\n const extensions = new Set<string>();\n this.addExtension(extensions, options.file_extension);\n\n if (isBlob(source)) {\n this.addExtension(extensions, extensionFromMime(source.type));\n if (\"name\" in source && typeof source.name === \"string\") {\n this.addExtension(extensions, extensionFromPathname(source.name));\n }\n }\n\n if (extensions.size === 0) {\n throw new Error(\n \"Could not determine the file type. Provide `file_extension` when converting bytes.\"\n );\n }\n\n return this.convertBytes(await toUint8Array(source), extensions, options);\n }\n\n /**\n * @deprecated Pass the byte source directly to `convert`.\n */\n async convertBuffer(\n source: ByteSource,\n options: ConverterOptions & { file_extension: string }\n ): Promise<ConverterResult> {\n return this.convert(source, options);\n }\n\n private async convertUrl(\n source: string,\n { fetch: fetchImplementation = globalThis.fetch, ...options }: ConverterOptions\n ): Promise<ConverterResult> {\n const url = new URL(source);\n if (url.protocol !== \"http:\" && url.protocol !== \"https:\") {\n throw new Error(`Unsupported URL protocol: ${url.protocol}`);\n }\n if (!fetchImplementation) {\n throw new Error(\"No fetch implementation is available.\");\n }\n\n const response = await fetchImplementation(url);\n if (!response.ok) {\n throw new Error(`Failed to fetch URL: ${url}, status: ${response.status}`);\n }\n\n return this.convertResponse(response, { ...options, url: url.toString() });\n }\n\n private async convertResponse(\n response: Response,\n options: ConverterOptions\n ): Promise<ConverterResult> {\n const extensions = new Set<string>();\n this.addExtension(extensions, options.file_extension);\n this.addExtension(extensions, extensionFromMime(response.headers.get(\"content-type\")));\n\n const disposition = response.headers.get(\"content-disposition\") ?? \"\";\n const filename = disposition.match(/filename\\*?=(?:UTF-8''|[\"']?)([^;\"']+)/i)?.[1];\n if (filename) {\n this.addExtension(extensions, extensionFromPathname(decodeURIComponent(filename.trim())));\n }\n\n const responseUrl = options.url || response.url;\n if (responseUrl) {\n this.addExtension(extensions, extensionFromPathname(new URL(responseUrl).pathname));\n }\n\n if (extensions.size === 0) {\n throw new Error(\n \"Could not determine the file type. Provide `file_extension` or a Content-Type header.\"\n );\n }\n\n return this.convertBytes(new Uint8Array(await response.arrayBuffer()), extensions, {\n ...options,\n url: responseUrl\n });\n }\n\n private async convertBytes(\n source: Uint8Array,\n extensions: Set<string>,\n options: ConverterOptions\n ): Promise<ConverterResult> {\n let lastError: unknown;\n\n for (const extension of extensions) {\n for (const converter of this.converters) {\n try {\n const result = await converter.convert(source, {\n ...options,\n file_extension: extension,\n _parent_converters: this.converters\n });\n if (result) {\n result.markdown = result.markdown\n .replace(/\\r\\n|\\r|\\n/g, \"\\n\")\n .trim()\n .replace(/\\n{3,}/g, \"\\n\\n\");\n result.text_content = result.markdown;\n return result;\n }\n } catch (error) {\n lastError = error;\n }\n }\n }\n\n const formats = [...extensions].join(\", \");\n if (lastError) {\n throw new Error(`Could not convert ${formats} to Markdown: ${String(lastError)}`);\n }\n throw new Error(`Unsupported file type: ${formats}.`);\n }\n\n private addExtension(extensions: Set<string>, extension: string | null | undefined): void {\n if (!extension) {\n return;\n }\n extensions.add(\n extension.startsWith(\".\") ? extension.toLowerCase() : `.${extension.toLowerCase()}`\n );\n }\n\n private registerConverter(converter: DocumentConverter): void {\n this.converters.unshift(converter);\n }\n}\n\nfunction isResponse(value: unknown): value is Response {\n return typeof Response !== \"undefined\" && value instanceof Response;\n}\n"],"mappings":";;;;;;;AAEA,eAAsB,aAAa,QAAyC;CAC1E,IAAI,kBAAkB,YACpB,OAAO;CAGT,IAAI,OAAO,MAAM,GACf,OAAO,IAAI,WAAW,MAAM,OAAO,YAAY,CAAC;CAGlD,IAAI,YAAY,OAAO,MAAM,GAC3B,OAAO,IAAI,WAAW,OAAO,QAAQ,OAAO,YAAY,OAAO,UAAU;CAG3E,OAAO,IAAI,WAAW,MAAM;AAC9B;AAEA,SAAgB,cAAc,QAAiC;CAC7D,OAAO,OAAO,MAAM,CAAC,CAAC;AACxB;AAEA,SAAgB,WAAW,QAA4B;CACrD,OAAO,IAAI,YAAY,CAAC,CAAC,OAAO,MAAM;AACxC;AAEA,SAAgB,OAAO,OAA+B;CACpD,OAAO,OAAO,SAAS,eAAe,iBAAiB;AACzD;;;ACzBA,IAAa,iBAAb,MAA4B;CAC1B,YAAY,UAAiC;EAC3C,MAAM,UAAU,IA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