@intuned/runtime-dev 1.7.0-dev-52-3 → 1.7.0-dev-52-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,2580 @@
1
+ (function () {
2
+ "use strict";
1
3
 
4
+ var MatchSource;
5
+ (function (MatchSource) {
6
+ MatchSource["ATTRIBUTE"] = "attribute";
7
+ MatchSource["TEXT_CONTENT"] = "text_content";
8
+ MatchSource["DIRECT_TEXT_NODE"] = "direct_text_node";
9
+ })(MatchSource || (MatchSource = {}));
10
+ var MatchMode;
11
+ (function (MatchMode) {
12
+ MatchMode["FULL"] = "full";
13
+ MatchMode["PARTIAL"] = "partial";
14
+ MatchMode["FUZZY"] = "fuzzy";
15
+ })(MatchMode || (MatchMode = {}));
16
+
17
+ function* searchExact(needle, haystack, startIndex = 0, endIndex = null) {
18
+ const needleLen = needle.length;
19
+ if (needleLen === 0) return;
20
+ if (endIndex === null) {
21
+ endIndex = haystack.length;
22
+ }
23
+ let index;
24
+ while ((index = haystack.indexOf(needle, startIndex)) > -1) {
25
+ if (index + needle.length > endIndex) break;
26
+ yield index;
27
+ startIndex = index + 1;
28
+ }
29
+ }
30
+ function reverse(string) {
31
+ return string.split("").reverse().join("");
32
+ }
33
+
34
+ function makeChar2needleIdx(needle, maxDist) {
35
+ const res = {};
36
+ for (let i = Math.min(needle.length - 1, maxDist); i >= 0; i--) {
37
+ res[needle[i]] = i;
38
+ }
39
+ return res;
40
+ }
41
+ function* fuzzySearch(needle, haystack, maxDist) {
42
+ if (needle.length > haystack.length + maxDist) return;
43
+ const ngramLen = Math.floor(needle.length / (maxDist + 1));
44
+ if (maxDist === 0) {
45
+ for (const index of searchExact(needle, haystack)) {
46
+ yield {
47
+ start: index,
48
+ end: index + needle.length,
49
+ dist: 0,
50
+ };
51
+ }
52
+ } else if (ngramLen >= 10) {
53
+ yield* fuzzySearchNgrams(needle, haystack, maxDist);
54
+ } else {
55
+ yield* fuzzySearchCandidates(needle, haystack, maxDist);
56
+ }
57
+ }
58
+ function _expand(needle, haystack, maxDist) {
59
+ maxDist = +maxDist;
60
+ let firstDiff;
61
+ for (
62
+ firstDiff = 0;
63
+ firstDiff < Math.min(needle.length, haystack.length);
64
+ firstDiff++
65
+ ) {
66
+ if (needle.charCodeAt(firstDiff) !== haystack.charCodeAt(firstDiff))
67
+ break;
68
+ }
69
+ if (firstDiff) {
70
+ needle = needle.slice(firstDiff);
71
+ haystack = haystack.slice(firstDiff);
72
+ }
73
+ if (!needle) {
74
+ return [0, firstDiff];
75
+ } else if (!haystack) {
76
+ if (needle.length <= maxDist) {
77
+ return [needle.length, firstDiff];
78
+ } else {
79
+ return [null, null];
80
+ }
81
+ }
82
+ if (maxDist === 0) return [null, null];
83
+ let scores = new Array(needle.length + 1);
84
+ for (let i = 0; i <= maxDist; i++) {
85
+ scores[i] = i;
86
+ }
87
+ let newScores = new Array(needle.length + 1);
88
+ let minScore = null;
89
+ let minScoreIdx = null;
90
+ let maxGoodScore = maxDist;
91
+ let firstGoodScoreIdx = 0;
92
+ let lastGoodScoreIdx = needle.length - 1;
93
+ for (let haystackIdx = 0; haystackIdx < haystack.length; haystackIdx++) {
94
+ const char = haystack.charCodeAt(haystackIdx);
95
+ const needleIdxStart = Math.max(0, firstGoodScoreIdx - 1);
96
+ const needleIdxLimit = Math.min(
97
+ haystackIdx + maxDist,
98
+ needle.length - 1,
99
+ lastGoodScoreIdx
100
+ );
101
+ newScores[0] = scores[0] + 1;
102
+ firstGoodScoreIdx = newScores[0] <= maxGoodScore ? 0 : null;
103
+ lastGoodScoreIdx = newScores[0] <= maxGoodScore ? 0 : -1;
104
+ let needleIdx;
105
+ for (
106
+ needleIdx = needleIdxStart;
107
+ needleIdx < needleIdxLimit;
108
+ needleIdx++
109
+ ) {
110
+ const score = (newScores[needleIdx + 1] = Math.min(
111
+ scores[needleIdx] + +(char !== needle.charCodeAt(needleIdx)),
112
+ scores[needleIdx + 1] + 1,
113
+ newScores[needleIdx] + 1
114
+ ));
115
+ if (score <= maxGoodScore) {
116
+ if (firstGoodScoreIdx === null) firstGoodScoreIdx = needleIdx + 1;
117
+ lastGoodScoreIdx = Math.max(
118
+ lastGoodScoreIdx,
119
+ needleIdx + 1 + (maxGoodScore - score)
120
+ );
121
+ }
122
+ }
123
+ const lastScore = (newScores[needleIdx + 1] = Math.min(
124
+ scores[needleIdx] + +(char !== needle.charCodeAt(needleIdx)),
125
+ newScores[needleIdx] + 1
126
+ ));
127
+ if (lastScore <= maxGoodScore) {
128
+ if (firstGoodScoreIdx === null) firstGoodScoreIdx = needleIdx + 1;
129
+ lastGoodScoreIdx = needleIdx + 1;
130
+ }
131
+ if (
132
+ needleIdx === needle.length - 1 &&
133
+ (minScore === null || lastScore <= minScore)
134
+ ) {
135
+ minScore = lastScore;
136
+ minScoreIdx = haystackIdx;
137
+ if (minScore < maxGoodScore) maxGoodScore = minScore;
138
+ }
139
+ [scores, newScores] = [newScores, scores];
140
+ if (firstGoodScoreIdx === null) break;
141
+ }
142
+ if (minScore !== null && minScore <= maxDist) {
143
+ return [minScore, minScoreIdx + 1 + firstDiff];
144
+ } else {
145
+ return [null, null];
146
+ }
147
+ }
148
+ function* fuzzySearchNgrams(needle, haystack, maxDist) {
149
+ // use n-gram search
150
+ const ngramLen = Math.floor(needle.length / (maxDist + 1));
151
+ const needleLen = needle.length;
152
+ const haystackLen = haystack.length;
153
+ for (
154
+ let ngramStartIdx = 0;
155
+ ngramStartIdx <= needle.length - ngramLen;
156
+ ngramStartIdx += ngramLen
157
+ ) {
158
+ const ngram = needle.slice(ngramStartIdx, ngramStartIdx + ngramLen);
159
+ const ngramEnd = ngramStartIdx + ngramLen;
160
+ const needleBeforeReversed = reverse(needle.slice(0, ngramStartIdx));
161
+ const needleAfter = needle.slice(ngramEnd);
162
+ const startIdx = Math.max(0, ngramStartIdx - maxDist);
163
+ const endIdx = Math.min(
164
+ haystackLen,
165
+ haystackLen - needleLen + ngramEnd + maxDist
166
+ );
167
+ for (const haystackMatchIdx of searchExact(
168
+ ngram,
169
+ haystack,
170
+ startIdx,
171
+ endIdx
172
+ )) {
173
+ // try to expand left
174
+ const [distRight, rightExpandSize] = _expand(
175
+ needleAfter,
176
+ haystack.slice(
177
+ haystackMatchIdx + ngramLen,
178
+ haystackMatchIdx - ngramStartIdx + needleLen + maxDist
179
+ ),
180
+ maxDist
181
+ );
182
+ if (distRight === null) continue;
183
+ const [distLeft, leftExpandSize] = _expand(
184
+ needleBeforeReversed,
185
+ reverse(
186
+ haystack.slice(
187
+ Math.max(
188
+ 0,
189
+ haystackMatchIdx - ngramStartIdx - (maxDist - distRight)
190
+ ),
191
+ haystackMatchIdx
192
+ )
193
+ ),
194
+ maxDist - distRight
195
+ );
196
+ if (distLeft === null) continue;
197
+ yield {
198
+ start: haystackMatchIdx - leftExpandSize,
199
+ end: haystackMatchIdx + ngramLen + rightExpandSize,
200
+ dist: distLeft + distRight,
201
+ };
202
+ }
203
+ }
204
+ }
205
+ function* fuzzySearchCandidates(needle, haystack, maxDist) {
206
+ const needleLen = needle.length;
207
+ const haystackLen = haystack.length;
208
+ if (needleLen > haystackLen + maxDist) return;
209
+ const char2needleIdx = makeChar2needleIdx(needle, maxDist);
210
+ let prevCandidates = new Map(); // candidates from the last iteration
211
+ let candidates = new Map(); // new candidates from the current iteration
212
+ // iterate over the chars in the haystack, updating the candidates for each
213
+ for (let i = 0; i < haystack.length; i++) {
214
+ const haystackChar = haystack[i];
215
+ prevCandidates = candidates;
216
+ candidates = new Map();
217
+ const needleIdx = char2needleIdx[haystackChar];
218
+ if (needleIdx !== undefined) {
219
+ if (needleIdx + 1 === needleLen) {
220
+ yield {
221
+ start: i,
222
+ end: i + 1,
223
+ dist: needleIdx,
224
+ };
225
+ } else {
226
+ candidates.set(`${i},${needleIdx + 1},${needleIdx}`, {
227
+ startIdx: i,
228
+ needleIdx: needleIdx + 1,
229
+ dist: needleIdx,
230
+ });
231
+ }
232
+ }
233
+ for (const [, candidate] of prevCandidates) {
234
+ // if this sequence char is the candidate's next expected char
235
+ if (needle[candidate.needleIdx] === haystackChar) {
236
+ // if reached the end of the needle, return a match
237
+ if (candidate.needleIdx + 1 === needleLen) {
238
+ yield {
239
+ start: candidate.startIdx,
240
+ end: i + 1,
241
+ dist: candidate.dist,
242
+ };
243
+ } else {
244
+ // otherwise, update the candidate's needleIdx and keep it
245
+ candidates.set(
246
+ `${candidate.startIdx},${candidate.needleIdx + 1},${
247
+ candidate.dist
248
+ }`,
249
+ {
250
+ startIdx: candidate.startIdx,
251
+ needleIdx: candidate.needleIdx + 1,
252
+ dist: candidate.dist,
253
+ }
254
+ );
255
+ }
256
+ } else {
257
+ if (candidate.dist === maxDist) continue;
258
+ candidates.set(
259
+ `${candidate.startIdx},${candidate.needleIdx},${
260
+ candidate.dist + 1
261
+ }`,
262
+ {
263
+ startIdx: candidate.startIdx,
264
+ needleIdx: candidate.needleIdx,
265
+ dist: candidate.dist + 1,
266
+ }
267
+ );
268
+ for (
269
+ let nSkipped = 1;
270
+ nSkipped <= maxDist - candidate.dist;
271
+ nSkipped++
272
+ ) {
273
+ if (candidate.needleIdx + nSkipped === needleLen) {
274
+ yield {
275
+ start: candidate.startIdx,
276
+ end: i + 1,
277
+ dist: candidate.dist + nSkipped,
278
+ };
279
+ break;
280
+ } else if (
281
+ needle[candidate.needleIdx + nSkipped] === haystackChar
282
+ ) {
283
+ if (candidate.needleIdx + nSkipped + 1 === needleLen) {
284
+ yield {
285
+ start: candidate.startIdx,
286
+ end: i + 1,
287
+ dist: candidate.dist + nSkipped,
288
+ };
289
+ } else {
290
+ candidates.set(
291
+ `${candidate.startIdx},${
292
+ candidate.needleIdx + 1 + nSkipped
293
+ },${candidate.dist + nSkipped}`,
294
+ {
295
+ startIdx: candidate.startIdx,
296
+ needleIdx: candidate.needleIdx + 1 + nSkipped,
297
+ dist: candidate.dist + nSkipped,
298
+ }
299
+ );
300
+ }
301
+ break;
302
+ }
303
+ }
304
+ if (i + 1 < haystackLen && candidate.needleIdx + 1 < needleLen) {
305
+ candidates.set(
306
+ `${candidate.startIdx},${candidate.needleIdx + 1},${
307
+ candidate.dist + 1
308
+ }`,
309
+ {
310
+ startIdx: candidate.startIdx,
311
+ needleIdx: candidate.needleIdx + 1,
312
+ dist: candidate.dist + 1,
313
+ }
314
+ );
315
+ }
316
+ }
317
+ }
318
+ }
319
+ for (const [, candidate] of candidates) {
320
+ candidate.dist += needle.length - candidate.needleIdx;
321
+ if (candidate.dist <= maxDist) {
322
+ yield {
323
+ start: candidate.startIdx,
324
+ end: haystack.length,
325
+ dist: candidate.dist,
326
+ };
327
+ }
328
+ }
329
+ }
330
+
331
+ function findClosestMatch(searchTerm, content, maxLDist) {
332
+ const results = [];
333
+ for (const result of fuzzySearch(searchTerm, content, maxLDist)) {
334
+ results.push(result);
335
+ }
336
+ results.sort((a, b) => {
337
+ if (a.dist === b.dist) {
338
+ return b.end - b.start - (a.end - a.start); // Sort by match length if distances are equal
339
+ }
340
+ return a.dist - b.dist; // Sort by distance
341
+ });
342
+ return results[0];
343
+ }
344
+ function normalizeSpacing(text) {
345
+ if (!text) {
346
+ return "";
347
+ }
348
+ // Replace newlines and tabs with spaces
349
+ let normalized = text.replace(/\n/g, " ").replace(/\t/g, " ");
350
+ // Replace multiple spaces with a single space
351
+ normalized = normalized.split(/\s+/).join(" ");
352
+ return normalized.trim();
353
+ }
354
+ function isMatchExact(data, value) {
355
+ if (!data || !value) {
356
+ return [false, null];
357
+ }
358
+ const normalizedData = normalizeSpacing(data);
359
+ const normalizedValue = normalizeSpacing(value);
360
+ return [normalizedData === normalizedValue, normalizedValue];
361
+ }
362
+ function calculateMaxLDist(value) {
363
+ const length = value.length;
364
+ const Pmax = 0.2;
365
+ const Pmin = 0.05;
366
+ const lengthAtPmax = 10;
367
+ let percentage;
368
+ if (length <= lengthAtPmax) {
369
+ percentage = Pmax;
370
+ } else {
371
+ const k = -Math.log(Pmin / Pmax) / (600 - lengthAtPmax);
372
+ percentage = Pmax * Math.exp(-k * (length - lengthAtPmax));
373
+ }
374
+ percentage = Math.max(Pmin, percentage);
375
+ return Math.max(1, Math.floor(length * percentage));
376
+ }
377
+ function isFuzzMatch(searchTerm, content) {
378
+ if (!searchTerm || !content) {
379
+ return {
380
+ found: false,
381
+ matchedValue: null,
382
+ distance: null,
383
+ matchedSourceValue: null,
384
+ };
385
+ }
386
+ const maxLDist = calculateMaxLDist(searchTerm);
387
+ const normalizedSearchTerm = normalizeSpacing(searchTerm);
388
+ const normalizedContent = normalizeSpacing(content);
389
+ const match = findClosestMatch(
390
+ normalizedSearchTerm.toLowerCase(),
391
+ normalizedContent.toLowerCase(),
392
+ maxLDist
393
+ );
394
+ if (!match) {
395
+ return {
396
+ found: false,
397
+ matchedValue: null,
398
+ distance: null,
399
+ matchedSourceValue: null,
400
+ };
401
+ }
402
+ return {
403
+ found: true,
404
+ matchedValue: normalizedContent.slice(match.start, match.end),
405
+ matchedSourceValue: normalizedContent,
406
+ distance: match.dist,
407
+ };
408
+ }
409
+ function hasNonFuzzyOrCloseFuzzyMatch(matches) {
410
+ const hasNonFuzzyMatch = matches.some(
411
+ (match) => match.match_mode !== MatchMode.FUZZY
412
+ );
413
+ const hasVeryCloseFuzzyMatch = matches.some(
414
+ (match) =>
415
+ match.match_mode === MatchMode.FUZZY &&
416
+ match.fuzzy_distance &&
417
+ match.fuzzy_distance < 5
418
+ );
419
+ return hasNonFuzzyMatch || hasVeryCloseFuzzyMatch;
420
+ }
421
+ function getElementXPath(element) {
422
+ if (!element || !element.parentNode || element.nodeName === "#document") {
423
+ return null;
424
+ }
425
+ let siblingsCount = 1;
426
+ const parent = element.parentNode;
427
+ const nodeName = element.nodeName.toLowerCase();
428
+ const siblings = Array.from(parent.childNodes).filter(
429
+ (node) => node.nodeType === 1 // Node.ELEMENT_NODE
430
+ );
431
+ for (const sibling of siblings) {
432
+ if (sibling === element) {
433
+ break;
434
+ }
435
+ if (sibling.nodeName.toLowerCase() === nodeName) {
436
+ siblingsCount++;
437
+ }
438
+ }
439
+ const parentXPath = getElementXPath(parent);
440
+ if (element.nodeName === "#text") {
441
+ return parentXPath;
442
+ }
443
+ return parentXPath
444
+ ? `${parentXPath}/${nodeName}[${siblingsCount}]`
445
+ : `${nodeName}[${siblingsCount}]`;
446
+ }
447
+ function traverseAndPrune(node, conditionFunc) {
448
+ const children = Array.from(node.children ?? []);
449
+ children.forEach((child) => {
450
+ if (child.children) {
451
+ if (!conditionFunc(child)) {
452
+ traverseAndPrune(child, conditionFunc);
453
+ }
454
+ }
455
+ });
456
+ }
457
+ function isPartOfString(input, dom) {
458
+ if (!input || !dom) {
459
+ return [false, null, null];
460
+ }
461
+ const normalizedInput = normalizeSpacing(input);
462
+ const normalizedDom = normalizeSpacing(dom);
463
+ const matchIndex = normalizedDom
464
+ .toLowerCase()
465
+ .indexOf(normalizedInput.toLowerCase());
466
+ const matchedText =
467
+ matchIndex !== -1
468
+ ? normalizedDom.substring(
469
+ matchIndex,
470
+ matchIndex + normalizedInput.length
471
+ )
472
+ : null;
473
+ return [matchIndex !== -1, matchedText, normalizedDom];
474
+ }
475
+
476
+ function matchStringsWithDomContent(domNode, stringsList) {
477
+ const exactMatchedMap = matchExactStrings(domNode, stringsList);
478
+ const stringsWithNoExactMatch = stringsList.filter(
479
+ (data) => !hasNonFuzzyOrCloseFuzzyMatch(exactMatchedMap[data])
480
+ );
481
+ if (stringsWithNoExactMatch.length === 0) {
482
+ return exactMatchedMap;
483
+ }
484
+ const fuzzMatchedMap = matchFuzzyStrings(domNode, stringsWithNoExactMatch);
485
+ for (const [data, fuzzyMatches] of Object.entries(fuzzMatchedMap)) {
486
+ if (data in exactMatchedMap) {
487
+ exactMatchedMap[data].push(...fuzzyMatches);
488
+ } else {
489
+ exactMatchedMap[data] = fuzzyMatches;
490
+ }
491
+ }
492
+ // attributes to try fuzzy match attributes on
493
+ const stringsWithNoMatch = stringsList.filter(
494
+ (data) => !hasNonFuzzyOrCloseFuzzyMatch(exactMatchedMap[data])
495
+ );
496
+ const attributesFuzzyMatchedMap = matchFuzzyAttributes(
497
+ domNode,
498
+ stringsWithNoMatch
499
+ );
500
+ for (const [data, attributeFuzzyMatches] of Object.entries(
501
+ attributesFuzzyMatchedMap
502
+ )) {
503
+ if (data in exactMatchedMap) {
504
+ exactMatchedMap[data].push(...attributeFuzzyMatches);
505
+ } else {
506
+ exactMatchedMap[data] = attributeFuzzyMatches;
507
+ }
508
+ }
509
+ return exactMatchedMap;
510
+ }
511
+ function matchExactStrings(domNode, stringsList) {
512
+ const allNodes = [
513
+ domNode,
514
+ ...Array.from(domNode.querySelectorAll("*")),
515
+ ].reverse();
516
+ const matchesMap = Object.fromEntries(
517
+ stringsList.map((data) => [data, []])
518
+ );
519
+ for (const tag of allNodes) {
520
+ const xpath = getElementXPath(tag);
521
+ for (const stringValue of stringsList) {
522
+ const matchesXPaths = matchesMap[stringValue].map(
523
+ (match) => match.xpath || ""
524
+ );
525
+ const xpathIsChildOfMatch = matchesXPaths.some(
526
+ (matchXPath) => matchXPath !== xpath && matchXPath.startsWith(xpath)
527
+ );
528
+ if (xpathIsChildOfMatch) continue;
529
+ const attributeNames = tag.getAttributeNames();
530
+ for (const attr of attributeNames) {
531
+ const attributeValue = tag.getAttribute(attr) || "";
532
+ const [isPartOfStringResult, matchedValue] = isPartOfString(
533
+ stringValue,
534
+ attributeValue
535
+ );
536
+ if (isPartOfStringResult) {
537
+ const [isExact] = isMatchExact(stringValue, attributeValue);
538
+ matchesMap[stringValue].push({
539
+ attribute: attr,
540
+ fuzzy_distance: null,
541
+ match_mode: isExact ? MatchMode.FULL : MatchMode.PARTIAL,
542
+ match_source: MatchSource.ATTRIBUTE,
543
+ matched_value: matchedValue,
544
+ matched_source_value: attributeValue,
545
+ tag: tag.tagName.toLowerCase(),
546
+ xpath,
547
+ });
548
+ }
549
+ }
550
+ if (tag["href"]) {
551
+ const result = matchHref(tag, stringValue);
552
+ if (result) {
553
+ matchesMap[stringValue].push(result);
554
+ }
555
+ }
556
+ // Check for direct text nodes
557
+ for (const childNode of tag.childNodes) {
558
+ // Node.TEXT_NODE
559
+ if (childNode.nodeType === 3) {
560
+ const directTextContent = childNode.textContent?.trim() || "";
561
+ if (directTextContent) {
562
+ const [isPartOfStringResult, matchedValue, source_value] =
563
+ isPartOfString(stringValue, directTextContent);
564
+ if (isPartOfStringResult) {
565
+ const [isExact] = isMatchExact(stringValue, directTextContent);
566
+ matchesMap[stringValue].push({
567
+ attribute: null,
568
+ fuzzy_distance: null,
569
+ match_mode: isExact ? MatchMode.FULL : MatchMode.PARTIAL,
570
+ match_source: MatchSource.DIRECT_TEXT_NODE,
571
+ matched_value: matchedValue,
572
+ matched_source_value: source_value,
573
+ tag: tag.tagName.toLowerCase(),
574
+ xpath,
575
+ });
576
+ }
577
+ }
578
+ }
579
+ }
580
+ const tagTextContent = tag.textContent || "";
581
+ const [isPartOfStringResult, matchedValue, source_value] =
582
+ isPartOfString(stringValue, tagTextContent);
583
+ if (isPartOfStringResult) {
584
+ const [isExact] = isMatchExact(stringValue, tagTextContent);
585
+ matchesMap[stringValue].push({
586
+ attribute: null,
587
+ fuzzy_distance: null,
588
+ match_mode: isExact ? MatchMode.FULL : MatchMode.PARTIAL,
589
+ match_source: MatchSource.TEXT_CONTENT,
590
+ matched_value: matchedValue,
591
+ matched_source_value: source_value,
592
+ tag: tag.tagName.toLowerCase(),
593
+ xpath,
594
+ });
595
+ }
596
+ }
597
+ }
598
+ return matchesMap;
599
+ }
600
+ function matchFuzzyStrings(domNode, stringsToMatch) {
601
+ const matchesMap = Object.fromEntries(
602
+ stringsToMatch.map((data) => [data, []])
603
+ );
604
+ const conditionFunc = (stringToMatch, node) => {
605
+ let foundMatch = false;
606
+ const currentXPath = getElementXPath(node);
607
+ for (const attr of node.getAttributeNames()) {
608
+ const attributeValue = node.getAttribute(attr) || "";
609
+ const {
610
+ found: isFuzzMatchFound,
611
+ matchedValue,
612
+ distance: dist,
613
+ matchedSourceValue,
614
+ } = isFuzzMatch(stringToMatch, attributeValue);
615
+ if (isFuzzMatchFound) {
616
+ matchesMap[stringToMatch].push({
617
+ attribute: attr,
618
+ fuzzy_distance: dist,
619
+ match_mode: MatchMode.FUZZY,
620
+ match_source: MatchSource.ATTRIBUTE,
621
+ matched_value: matchedValue,
622
+ tag: node.tagName.toLowerCase(),
623
+ xpath: currentXPath,
624
+ matched_source_value: matchedSourceValue,
625
+ });
626
+ foundMatch = true;
627
+ }
628
+ }
629
+ const tagTextContent = node.textContent || "";
630
+ if (tagTextContent) {
631
+ const {
632
+ found: isFuzzMatchFound,
633
+ matchedValue,
634
+ distance: dist,
635
+ matchedSourceValue,
636
+ } = isFuzzMatch(stringToMatch, tagTextContent);
637
+ if (isFuzzMatchFound) {
638
+ matchesMap[stringToMatch].push({
639
+ attribute: null,
640
+ fuzzy_distance: dist,
641
+ match_mode: MatchMode.FUZZY,
642
+ match_source: MatchSource.TEXT_CONTENT,
643
+ matched_value: matchedValue,
644
+ tag: node.tagName.toLowerCase(),
645
+ xpath: currentXPath,
646
+ matched_source_value: matchedSourceValue,
647
+ });
648
+ foundMatch = true;
649
+ }
650
+ }
651
+ // Check for direct text nodes
652
+ for (const childNode of node.childNodes) {
653
+ // Node.TEXT_NODE
654
+ if (childNode.nodeType === 3) {
655
+ const directTextContent = childNode.textContent?.trim() || "";
656
+ if (directTextContent) {
657
+ const {
658
+ found: isFuzzMatchFound,
659
+ matchedValue,
660
+ distance: dist,
661
+ matchedSourceValue,
662
+ } = isFuzzMatch(stringToMatch, directTextContent);
663
+ if (isFuzzMatchFound) {
664
+ matchesMap[stringToMatch].push({
665
+ attribute: null,
666
+ fuzzy_distance: dist,
667
+ match_mode: MatchMode.FUZZY,
668
+ match_source: MatchSource.DIRECT_TEXT_NODE,
669
+ matched_value: matchedValue,
670
+ tag: node.tagName.toLowerCase(),
671
+ xpath: currentXPath,
672
+ matched_source_value: matchedSourceValue,
673
+ });
674
+ foundMatch = true;
675
+ }
676
+ }
677
+ }
678
+ }
679
+ return !foundMatch;
680
+ };
681
+ for (const stringToMatch of stringsToMatch) {
682
+ conditionFunc(stringToMatch, domNode);
683
+ traverseAndPrune(domNode, (node) => conditionFunc(stringToMatch, node));
684
+ }
685
+ for (const [stringToMatch, matches] of Object.entries(matchesMap)) {
686
+ const matchesToRemove = new Set();
687
+ matches.forEach((match, i) => {
688
+ for (const otherMatch of matches.slice(i + 1)) {
689
+ if ((otherMatch.xpath || "").startsWith((match.xpath || "") + "/")) {
690
+ matchesToRemove.add(i);
691
+ break;
692
+ }
693
+ }
694
+ });
695
+ matchesMap[stringToMatch] = matches.filter(
696
+ (_, i) => !matchesToRemove.has(i)
697
+ );
698
+ }
699
+ return matchesMap;
700
+ }
701
+ function matchFuzzyAttributes(domNode, stringsToMatch) {
702
+ const matchesMap = Object.fromEntries(
703
+ stringsToMatch.map((data) => [data, []])
704
+ );
705
+ const allAttributes = getAllAttributes(domNode);
706
+ for (const stringToMatch of stringsToMatch) {
707
+ const stringToSearchIn = allAttributes
708
+ .filter((attr) => attr.value.length > 10)
709
+ .filter((attr) => {
710
+ const lengthDiff = Math.abs(attr.value.length - stringToMatch.length);
711
+ return lengthDiff <= 0.2 * stringToMatch.length;
712
+ })
713
+ .map((attr) => attr.value)
714
+ .join("\n");
715
+ const {
716
+ found: isFuzzMatchFound,
717
+ matchedValue,
718
+ distance: dist,
719
+ } = isFuzzMatch(stringToMatch, stringToSearchIn);
720
+ if (isFuzzMatchFound) {
721
+ const matchLine = allAttributes.find(
722
+ (attr) => matchedValue && attr.value.includes(matchedValue)
723
+ );
724
+ if (!matchLine) continue;
725
+ matchesMap[stringToMatch].push({
726
+ attribute: matchLine.attr,
727
+ fuzzy_distance: dist,
728
+ match_mode: MatchMode.FUZZY,
729
+ match_source: MatchSource.ATTRIBUTE,
730
+ matched_value: matchedValue,
731
+ xpath: matchLine.node,
732
+ matched_source_value: matchLine.value,
733
+ tag: matchLine.tag,
734
+ });
735
+ }
736
+ }
737
+ return matchesMap;
738
+ }
739
+ function getAllAttributes(node) {
740
+ const allNodes = [
741
+ node,
742
+ ...Array.from(node.querySelectorAll("*")),
743
+ ].reverse();
744
+ return allNodes.flatMap((node) =>
745
+ node
746
+ .getAttributeNames()
747
+ .map((attr) => ({
748
+ node: getElementXPath(node),
749
+ attr,
750
+ value: node.getAttribute(attr) || "",
751
+ tag: node.tagName.toLowerCase(),
752
+ }))
753
+ .filter((i) => i.value.length > 10)
754
+ );
755
+ }
756
+ function matchHref(node, stringToMatch) {
757
+ if (!node["href"] || typeof node["href"] !== "string") {
758
+ return;
759
+ }
760
+ const attributeValue = node["href"] || "";
761
+ let [isPartOfStringResult, matchedValue] = isPartOfString(
762
+ stringToMatch,
763
+ attributeValue
764
+ );
765
+ if (isPartOfStringResult) {
766
+ const [isExact] = isMatchExact(stringToMatch, attributeValue);
767
+ return {
768
+ attribute: "href",
769
+ fuzzy_distance: null,
770
+ match_mode: isExact ? MatchMode.FULL : MatchMode.PARTIAL,
771
+ match_source: MatchSource.ATTRIBUTE,
772
+ matched_value: matchedValue,
773
+ matched_source_value: attributeValue,
774
+ tag: node.tagName.toLowerCase(),
775
+ xpath: getElementXPath(node),
776
+ };
777
+ }
778
+ let decodedStringToMatch;
779
+ try {
780
+ decodedStringToMatch = decodeURI(stringToMatch);
781
+ } catch (e) {
782
+ console.log("failed to decode stringToMatch", stringToMatch);
783
+ return;
784
+ }
785
+ [isPartOfStringResult, matchedValue] = isPartOfString(
786
+ decodedStringToMatch,
787
+ attributeValue
788
+ );
789
+ if (isPartOfStringResult) {
790
+ const [isExact] = isMatchExact(stringToMatch, attributeValue);
791
+ return {
792
+ attribute: "href",
793
+ fuzzy_distance: null,
794
+ match_mode: isExact ? MatchMode.FULL : MatchMode.PARTIAL,
795
+ match_source: MatchSource.ATTRIBUTE,
796
+ matched_value: matchedValue,
797
+ matched_source_value: attributeValue,
798
+ tag: node.tagName.toLowerCase(),
799
+ xpath: getElementXPath(node),
800
+ };
801
+ }
802
+ }
803
+
804
+ function convertElementToMarkdown(element) {
805
+ const mdCharsMatcher = /([\\[\]()])/g;
806
+ function escapeMd(text) {
807
+ // Escapes markdown-sensitive characters within other markdown constructs.
808
+ return text.replace(mdCharsMatcher, "\\$1");
809
+ }
810
+ function listNumberingStart(attrs) {
811
+ const start = attrs.getNamedItem("start")?.value;
812
+ if (start) {
813
+ return parseInt(start, 10) - 1;
814
+ } else {
815
+ return 0;
816
+ }
817
+ }
818
+ // Define the characters that require escaping
819
+ const slashChars = "\\`*_{}[]()#+-.!";
820
+ // Escape any special regex characters in slashChars
821
+ const escapedSlashChars = slashChars.replace(
822
+ /[-/\\^$*+?.()|[\]{}]/g,
823
+ "\\$&"
824
+ );
825
+ // Create the regular expression
826
+ const mdBackslashMatcher = new RegExp(
827
+ `\\\\(?=[${escapedSlashChars}])`,
828
+ "g"
829
+ );
830
+ const mdDotMatcher = new RegExp(`^(\\s*\\d+)(\\.)(?=\\s)`, "gm");
831
+ const mdPlusMatcher = new RegExp(`^(\\s*)(\\+)(?=\\s)`, "gm");
832
+ const mdDashMatcher = new RegExp(`^(\\s*)(-)(?=\\s|-)`, "gm");
833
+ function escapeMdSection(text) {
834
+ text = text.replace(mdBackslashMatcher, "\\\\");
835
+ text = text.replace(mdDotMatcher, "$1\\$2");
836
+ text = text.replace(mdPlusMatcher, "$1\\$2");
837
+ text = text.replace(mdDashMatcher, "$1\\$2");
838
+ return text;
839
+ }
840
+ function isFirstTbody(element) {
841
+ const previousSibling = element.previousSibling;
842
+ return (
843
+ element.nodeName === "TBODY" &&
844
+ (!previousSibling ||
845
+ (previousSibling.nodeName === "THEAD" &&
846
+ /^\s*$/i.test(previousSibling.textContent ?? "")))
847
+ );
848
+ }
849
+ function isHeadingRow(tr) {
850
+ const parentNode = tr.parentNode;
851
+ return (
852
+ parentNode.nodeName === "THEAD" ||
853
+ (parentNode.firstChild === tr &&
854
+ (parentNode.nodeName === "TABLE" || isFirstTbody(parentNode)) &&
855
+ Array.from(tr.childNodes).every(function (n) {
856
+ return n.nodeName === "TH";
857
+ }))
858
+ );
859
+ }
860
+ class Html2Text {
861
+ p_p = 0;
862
+ abbrData; // last inner HTML (for abbr being defined)
863
+ pre = false;
864
+ code = false;
865
+ startPre = false;
866
+ blockquote = 0;
867
+ list = [];
868
+ start = true;
869
+ breakToggle = "";
870
+ space;
871
+ lastWasNewLine = false;
872
+ a = null;
873
+ outCount = 0;
874
+ baseurl;
875
+ abbrList = {};
876
+ outText = "";
877
+ outTextList = [];
878
+ abbr_title;
879
+ skipInternalLinks = true;
880
+ aStack = [];
881
+ maybeAutomaticLink;
882
+ lastWasList = false;
883
+ absoluteUrlMatcher = new RegExp("^[a-zA-Z+]+://");
884
+ emphasis_mark = "_";
885
+ strong_mark = "**";
886
+ break() {
887
+ if (this.p_p === 0) {
888
+ this.p_p = 1;
889
+ }
890
+ }
891
+ softBreak() {
892
+ this.break();
893
+ this.breakToggle = " ";
894
+ }
895
+ processOutput(data, pureData = 0, force = 0) {
896
+ if (this.abbrData !== undefined) {
897
+ this.abbrData += data;
898
+ }
899
+ if (pureData && !this.pre) {
900
+ data = data.replace(/\s+/g, " ");
901
+ if (data && data[0] === " ") {
902
+ this.space = 1;
903
+ data = data.substring(1);
904
+ }
905
+ }
906
+ if (!data && force !== "end") return;
907
+ if (this.startPre) {
908
+ if (!data.startsWith("\n")) {
909
+ data = "\n" + data;
910
+ }
911
+ }
912
+ let newLineIndent = ">".repeat(this.blockquote ?? 0);
913
+ if (!(force === "end" && data && data[0] === ">") && this.blockquote) {
914
+ newLineIndent += " ";
915
+ }
916
+ if (this.pre) {
917
+ if (this.list.length === 0) {
918
+ newLineIndent += " ";
919
+ } else {
920
+ for (let i = 0; i < this.list.length + 1; i++) {
921
+ newLineIndent += " ";
922
+ }
923
+ }
924
+ data = data.replace(/\n/g, `\n${newLineIndent}`);
925
+ }
926
+ if (this.startPre) {
927
+ this.startPre = false;
928
+ if (this.list.length > 0) {
929
+ data = data.trimStart();
930
+ }
931
+ }
932
+ if (this.start) {
933
+ this.space = 0;
934
+ this.p_p = 0;
935
+ this.start = false;
936
+ }
937
+ if (force === "end") {
938
+ this.p_p = 0;
939
+ this.out("\n");
940
+ this.space = 0;
941
+ }
942
+ if (this.p_p) {
943
+ this.out((this.breakToggle + "\n" + newLineIndent).repeat(this.p_p));
944
+ this.space = 0;
945
+ this.breakToggle = "";
946
+ }
947
+ if (this.space) {
948
+ if (!this.lastWasNewLine) {
949
+ this.out(" ");
950
+ }
951
+ this.space = 0;
952
+ }
953
+ if (this.a && force === "end") {
954
+ if (force === "end") {
955
+ this.out("\n");
956
+ }
957
+ const newA = this.a.filter((link) => {
958
+ if (this.outCount > link.outcount) {
959
+ this.out(
960
+ " [" +
961
+ link.count +
962
+ "]: " +
963
+ new URL(link.href, this.baseurl).toString()
964
+ );
965
+ if (link.title) {
966
+ this.out(" (" + link.title + ")");
967
+ }
968
+ this.out("\n");
969
+ return false;
970
+ }
971
+ return true;
972
+ });
973
+ if (this.a.length !== newA.length) {
974
+ this.out("\n");
975
+ }
976
+ this.a = newA;
977
+ }
978
+ if (this.abbrList && force === "end") {
979
+ for (const [abbr, definition] of Object.entries(this.abbrList)) {
980
+ this.out("\n *[" + abbr + "]: " + definition + "\n");
981
+ }
982
+ }
983
+ this.p_p = 0;
984
+ this.out(data);
985
+ this.outCount++;
986
+ }
987
+ out(string) {
988
+ this.outTextList.push(string);
989
+ if (string) {
990
+ this.lastWasNewLine = string.charAt(string.length - 1) === "\n";
991
+ }
992
+ }
993
+ getResult() {
994
+ this.processOutput("", 0, "end");
995
+ this.outText = this.outTextList.join("");
996
+ this.outText = this.outText.replace("&nbsp_place_holder;", " ");
997
+ return this.outText;
998
+ }
999
+ getHeadingLevel(tag) {
1000
+ if (tag[0] === "h" && tag.length === 2) {
1001
+ try {
1002
+ const n = parseInt(tag[1]);
1003
+ if (!isNaN(n) && n >= 1 && n <= 9) {
1004
+ return n;
1005
+ }
1006
+ } catch (error) {
1007
+ return 0;
1008
+ }
1009
+ }
1010
+ return 0;
1011
+ }
1012
+ padding() {
1013
+ this.p_p = 2;
1014
+ }
1015
+ handleData(node) {
1016
+ if (this.maybeAutomaticLink) {
1017
+ const href = this.maybeAutomaticLink;
1018
+ if (
1019
+ href?.value === node.nodeValue &&
1020
+ this.absoluteUrlMatcher.test(href.value)
1021
+ ) {
1022
+ this.processOutput(`<${node.nodeValue}>`);
1023
+ return;
1024
+ } else {
1025
+ this.processOutput("[");
1026
+ this.maybeAutomaticLink = null;
1027
+ }
1028
+ }
1029
+ if (!this.code && !this.pre && node.nodeValue) {
1030
+ const data = escapeMdSection(node.nodeValue);
1031
+ this.processOutput(data, 1);
1032
+ return;
1033
+ }
1034
+ this.processOutput(node.textContent || "", 1);
1035
+ }
1036
+ handleTag(node) {
1037
+ const tag = node.nodeName.toLowerCase();
1038
+ if (["head", "style", "script"].includes(tag)) {
1039
+ return;
1040
+ }
1041
+ if (this.getHeadingLevel(tag)) {
1042
+ this.padding();
1043
+ this.processOutput("#".repeat(this.getHeadingLevel(tag)) + " ");
1044
+ }
1045
+ if (tag == "br") this.processOutput(" \n");
1046
+ if (tag == "hr") {
1047
+ this.padding();
1048
+ this.processOutput("---");
1049
+ this.padding();
1050
+ }
1051
+ if (tag == "blockquote") {
1052
+ this.padding();
1053
+ this.processOutput("> ", 0, 1);
1054
+ }
1055
+ }
1056
+ handleTagPrefix(node) {
1057
+ const nodeName = node.nodeName.toLowerCase();
1058
+ let attrs =
1059
+ node.nodeType === node.ELEMENT_NODE ? node.attributes : null;
1060
+ if (["table"].includes(nodeName)) {
1061
+ this.padding();
1062
+ }
1063
+ if (nodeName == "td" || nodeName == "th") {
1064
+ const index = Array.from(node.parentNode?.children ?? []).indexOf(
1065
+ node
1066
+ );
1067
+ let prefix = " ";
1068
+ if (index === 0) prefix = "| ";
1069
+ this.processOutput(prefix);
1070
+ // this.break();
1071
+ }
1072
+ if (["div", "p"].includes(nodeName)) {
1073
+ this.padding();
1074
+ }
1075
+ if (nodeName === "blockquote") {
1076
+ this.blockquote += 1;
1077
+ }
1078
+ if (nodeName === "pre") {
1079
+ this.pre = true;
1080
+ this.startPre = true;
1081
+ this.padding();
1082
+ }
1083
+ if (["code", "tt"].includes(nodeName)) {
1084
+ this.processOutput("`");
1085
+ }
1086
+ if (["em", "i", "u"].includes(nodeName)) {
1087
+ this.processOutput(this.emphasis_mark);
1088
+ }
1089
+ if (["strong", "b"].includes(nodeName)) {
1090
+ this.processOutput(this.strong_mark);
1091
+ }
1092
+ if (["del", "strike", "s"].includes(nodeName)) {
1093
+ this.processOutput("<" + nodeName + ">");
1094
+ }
1095
+ if (nodeName === "abbr") {
1096
+ this.abbr_title = null;
1097
+ this.abbrData = "";
1098
+ const title = attrs && attrs.getNamedItem("title");
1099
+ if (attrs && title) {
1100
+ this.abbr_title = title.value;
1101
+ }
1102
+ }
1103
+ if (nodeName === "dl") {
1104
+ this.padding();
1105
+ }
1106
+ if (nodeName === "dd") {
1107
+ this.processOutput(" ");
1108
+ }
1109
+ if (nodeName == "a") {
1110
+ const href = attrs ? attrs.getNamedItem("href") : null;
1111
+ if (href && !(this.skipInternalLinks && href.value.startsWith("#"))) {
1112
+ this.aStack.push(attrs);
1113
+ this.maybeAutomaticLink = href;
1114
+ } else {
1115
+ this.aStack.push(null);
1116
+ }
1117
+ }
1118
+ if (nodeName === "img") {
1119
+ const src = attrs ? attrs.getNamedItem("src") : null;
1120
+ if (src) {
1121
+ node.setAttribute("href", src.value);
1122
+ attrs = node.attributes;
1123
+ const alt = attrs.getNamedItem("alt")?.value;
1124
+ this.processOutput("![" + escapeMd(alt ?? "") + "]");
1125
+ this.processOutput(
1126
+ "(" + escapeMd(attrs.getNamedItem("href")?.value ?? "") + ")"
1127
+ );
1128
+ }
1129
+ }
1130
+ if (["ul", "ol"].includes(nodeName)) {
1131
+ const listStyle = nodeName;
1132
+ const numberingStart = listNumberingStart(node.attributes);
1133
+ this.list.push({ name: listStyle, num: numberingStart });
1134
+ this.lastWasList = true;
1135
+ } else {
1136
+ this.lastWasList = false;
1137
+ }
1138
+ if (nodeName === "li") {
1139
+ let li;
1140
+ this.break();
1141
+ if (this.list.length > 0) {
1142
+ li = this.list[this.list.length - 1];
1143
+ } else {
1144
+ li = { name: "ul", num: 0 };
1145
+ }
1146
+ const nestCount = this.list.length;
1147
+ this.processOutput(" ".repeat(nestCount));
1148
+ if (li["name"] == "ul") this.processOutput("*" + " ");
1149
+ else if (li["name"] == "ol") {
1150
+ li["num"] += 1;
1151
+ this.processOutput(li["num"] + ". ");
1152
+ }
1153
+ this.start = true;
1154
+ }
1155
+ }
1156
+ handleTagSuffix(node) {
1157
+ const nodeName = node.nodeName.toLowerCase();
1158
+ if (nodeName === "blockquote") {
1159
+ this.blockquote -= 1;
1160
+ }
1161
+ if (nodeName == "td" || nodeName == "th") {
1162
+ this.processOutput(" |");
1163
+ }
1164
+ if (nodeName == "tr") {
1165
+ const cell = (content, node) => {
1166
+ const index = Array.from(node.parentNode.childNodes).indexOf(node);
1167
+ let prefix = " ";
1168
+ if (index === 0) prefix = "| ";
1169
+ return prefix + content + " |";
1170
+ };
1171
+ let borderCells = "";
1172
+ const alignMap = { left: ":--", right: "--:", center: ":-:" };
1173
+ if (isHeadingRow(node)) {
1174
+ for (let i = 0; i < node.children.length; i++) {
1175
+ let border = "---";
1176
+ const align = (
1177
+ node.children[i].getAttribute("align") || ""
1178
+ ).toLowerCase();
1179
+ if (align) border = alignMap[align] || border;
1180
+ borderCells += cell(border, node.childNodes[i]);
1181
+ }
1182
+ }
1183
+ this.processOutput(borderCells ? "\n" + borderCells + "\n" : "\n");
1184
+ }
1185
+ if (nodeName === "pre") {
1186
+ this.pre = false;
1187
+ this.padding();
1188
+ }
1189
+ if (["code", "tt"].includes(nodeName)) {
1190
+ this.processOutput("`");
1191
+ }
1192
+ if (["em", "i", "u"].includes(nodeName)) {
1193
+ this.processOutput(this.emphasis_mark);
1194
+ }
1195
+ if (["strong", "b"].includes(nodeName)) {
1196
+ this.processOutput(this.strong_mark);
1197
+ }
1198
+ if (["div", "p"].includes(nodeName)) {
1199
+ this.padding();
1200
+ }
1201
+ if (["del", "strike", "s"].includes(nodeName)) {
1202
+ this.processOutput("</" + nodeName + ">");
1203
+ }
1204
+ if (nodeName === "abbr") {
1205
+ if (this.abbr_title && this.abbrData) {
1206
+ this.abbrList[this.abbrData] = this.abbr_title;
1207
+ this.abbr_title = null;
1208
+ }
1209
+ this.abbrData = "";
1210
+ }
1211
+ if (nodeName === "dt") {
1212
+ this.break();
1213
+ }
1214
+ if (nodeName === "dd") {
1215
+ this.break();
1216
+ }
1217
+ if (nodeName === "a") {
1218
+ if (this.aStack.length > 0) {
1219
+ const a = this.aStack.pop();
1220
+ if (this.maybeAutomaticLink) {
1221
+ this.maybeAutomaticLink = null;
1222
+ } else if (a) {
1223
+ this.processOutput(
1224
+ `](${escapeMd(a.getNamedItem("href")?.value || "")})`
1225
+ );
1226
+ }
1227
+ }
1228
+ }
1229
+ if (["ul", "ol"].includes(nodeName)) {
1230
+ if (this.list.length > 0) this.list.pop();
1231
+ this.lastWasList = true;
1232
+ } else {
1233
+ this.lastWasList = false;
1234
+ }
1235
+ if (nodeName === "li") {
1236
+ this.break();
1237
+ }
1238
+ }
1239
+ previousIndex(attrs) {
1240
+ // Returns the index of a certain set of attributes (of a link) in the
1241
+ // this.a list.
1242
+ // If the set of attributes is not found, returns null.
1243
+ const href = attrs.getNamedItem("href");
1244
+ if (!attrs.getNamedItem("href")) return null;
1245
+ let itemIndex = -1;
1246
+ for (const a of this.a ?? []) {
1247
+ itemIndex += 1;
1248
+ let match = false;
1249
+ if (a.getNamedItem("href") === href) {
1250
+ if (a.getNamedItem("title") || attrs.getNamedItem("title")) {
1251
+ if (
1252
+ a.getNamedItem("title") &&
1253
+ attrs.getNamedItem("title") &&
1254
+ a.getNamedItem("title") === attrs.getNamedItem("title")
1255
+ ) {
1256
+ match = true;
1257
+ }
1258
+ } else {
1259
+ match = true;
1260
+ }
1261
+ }
1262
+ if (match) return itemIndex;
1263
+ }
1264
+ return null;
1265
+ }
1266
+ handle(htmlElement) {
1267
+ // jsdom failed to parse hilton page due to invalid stylesheet
1268
+ // Nodes to be removed
1269
+ const filteredNodes = ["style", "script", "noscript"];
1270
+ for (const node of filteredNodes) {
1271
+ const nodeSelectors = htmlElement.querySelectorAll(node);
1272
+ nodeSelectors.forEach((nodeSelector) => {
1273
+ if (nodeSelector && nodeSelector.parentNode) {
1274
+ nodeSelector.parentNode.removeChild(nodeSelector);
1275
+ }
1276
+ });
1277
+ }
1278
+ // Get the cleaned-up HTML content
1279
+ const htmlContent = htmlElement.outerHTML;
1280
+ const parser = new DOMParser();
1281
+ const doc = parser.parseFromString(htmlContent, "text/html");
1282
+ const traverseDOM = (node) => {
1283
+ const tag = node.nodeName.toLowerCase();
1284
+ if (node.nodeType === node.TEXT_NODE) {
1285
+ const element = node;
1286
+ this.handleData(element);
1287
+ return;
1288
+ }
1289
+ if (node.nodeType === node.ELEMENT_NODE) {
1290
+ const element = node;
1291
+ this.handleTag(element);
1292
+ }
1293
+ if (!["head", "style", "script"].includes(tag)) {
1294
+ this.handleTagPrefix(node);
1295
+ node.childNodes.forEach((child) => traverseDOM(child));
1296
+ this.handleTagSuffix(node);
1297
+ }
1298
+ };
1299
+ traverseDOM(doc.documentElement);
1300
+ return this.getResult();
1301
+ }
1302
+ }
1303
+ const converter = new Html2Text();
1304
+ const result = converter.handle(element);
1305
+ return result;
1306
+ }
1307
+
1308
+ var node = {};
1309
+
1310
+ var htmlToMarkdownAST$1 = {};
1311
+
1312
+ var ElementNode = {};
1313
+
1314
+ Object.defineProperty(ElementNode, "__esModule", { value: true });
1315
+ ElementNode._Node = void 0;
1316
+ // this is by value copy of the global Node
1317
+ ElementNode._Node = {
1318
+ /** node is an element. */
1319
+ ELEMENT_NODE: 1,
1320
+ ATTRIBUTE_NODE: 2,
1321
+ /** node is a Text node. */
1322
+ TEXT_NODE: 3,
1323
+ /** node is a CDATASection node. */
1324
+ CDATA_SECTION_NODE: 4,
1325
+ ENTITY_REFERENCE_NODE: 5,
1326
+ ENTITY_NODE: 6,
1327
+ /** node is a ProcessingInstruction node. */
1328
+ PROCESSING_INSTRUCTION_NODE: 7,
1329
+ /** node is a Comment node. */
1330
+ COMMENT_NODE: 8,
1331
+ /** node is a document. */
1332
+ DOCUMENT_NODE: 9,
1333
+ /** node is a doctype. */
1334
+ DOCUMENT_TYPE_NODE: 10,
1335
+ /** node is a DocumentFragment node. */
1336
+ DOCUMENT_FRAGMENT_NODE: 11,
1337
+ NOTATION_NODE: 12,
1338
+ /** Set when node and other are not in the same tree. */
1339
+ DOCUMENT_POSITION_DISCONNECTED: 0x01,
1340
+ /** Set when other is preceding node. */
1341
+ DOCUMENT_POSITION_PRECEDING: 0x02,
1342
+ /** Set when other is following node. */
1343
+ DOCUMENT_POSITION_FOLLOWING: 0x04,
1344
+ /** Set when other is an ancestor of node. */
1345
+ DOCUMENT_POSITION_CONTAINS: 0x08,
1346
+ /** Set when other is a descendant of node. */
1347
+ DOCUMENT_POSITION_CONTAINED_BY: 0x10,
1348
+ DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 0x20,
1349
+ };
1350
+
1351
+ Object.defineProperty(htmlToMarkdownAST$1, "__esModule", { value: true });
1352
+ htmlToMarkdownAST$1.htmlToMarkdownAST = htmlToMarkdownAST;
1353
+ const ElementNode_1$1 = ElementNode;
1354
+ function htmlToMarkdownAST(element, options, indentLevel = 0) {
1355
+ let result = [];
1356
+ const debugLog = (message) => {
1357
+ if (options?.debug) {
1358
+ console.log(message);
1359
+ }
1360
+ };
1361
+ element.childNodes.forEach((childElement) => {
1362
+ const overriddenElementProcessing = options?.overrideElementProcessing?.(
1363
+ childElement,
1364
+ options,
1365
+ indentLevel
1366
+ );
1367
+ if (overriddenElementProcessing) {
1368
+ debugLog(`Element Processing Overridden: '${childElement.nodeType}'`);
1369
+ result.push(...overriddenElementProcessing);
1370
+ } else if (childElement.nodeType === ElementNode_1$1._Node.TEXT_NODE) {
1371
+ const textContent = escapeMarkdownCharacters(
1372
+ childElement.textContent?.trim() ?? ""
1373
+ );
1374
+ if (textContent && !!childElement.textContent) {
1375
+ debugLog(`Text Node: '${textContent}'`);
1376
+ // preserve whitespaces when text childElement is not empty
1377
+ result.push({
1378
+ type: "text",
1379
+ content: childElement.textContent?.trim(),
1380
+ });
1381
+ }
1382
+ } else if (childElement.nodeType === ElementNode_1$1._Node.ELEMENT_NODE) {
1383
+ const elem = childElement;
1384
+ if (/^h[1-6]$/i.test(elem.tagName)) {
1385
+ const level = parseInt(elem.tagName.substring(1));
1386
+ const content = escapeMarkdownCharacters(
1387
+ elem.textContent || ""
1388
+ ).trim();
1389
+ if (content) {
1390
+ debugLog(`Heading ${level}: '${elem.textContent}'`);
1391
+ result.push({ type: "heading", level, content });
1392
+ }
1393
+ } else if (elem.tagName.toLowerCase() === "p") {
1394
+ debugLog("Paragraph");
1395
+ result.push(...htmlToMarkdownAST(elem, options));
1396
+ // Add a new line after the paragraph
1397
+ result.push({ type: "text", content: "\n\n" });
1398
+ } else if (elem.tagName.toLowerCase() === "a") {
1399
+ debugLog(`Link: '${elem.href}' with text '${elem.textContent}'`);
1400
+ // Check if the href is a data URL for an image
1401
+ if (
1402
+ typeof elem.href === "string" &&
1403
+ elem.href.startsWith("data:image")
1404
+ ) {
1405
+ // If it's a data URL for an image, skip this link
1406
+ result.push({
1407
+ type: "link",
1408
+ href: "-",
1409
+ content: htmlToMarkdownAST(elem, options),
1410
+ });
1411
+ } else {
1412
+ // Process the link as usual
1413
+ let href = elem.href;
1414
+ if (typeof href === "string") {
1415
+ href =
1416
+ options?.websiteDomain && href.startsWith(options.websiteDomain)
1417
+ ? href.substring(options.websiteDomain.length)
1418
+ : href;
1419
+ } else {
1420
+ href = "#"; // Use a default value when href is not a string
1421
+ }
1422
+ // if all children are text,
1423
+ if (
1424
+ Array.from(elem.childNodes).every(
1425
+ (_) => _.nodeType === ElementNode_1$1._Node.TEXT_NODE
1426
+ )
1427
+ ) {
1428
+ result.push({
1429
+ type: "link",
1430
+ href: href,
1431
+ content: [
1432
+ { type: "text", content: elem.textContent?.trim() ?? "" },
1433
+ ],
1434
+ });
1435
+ } else {
1436
+ result.push({
1437
+ type: "link",
1438
+ href: href,
1439
+ content: htmlToMarkdownAST(elem, options),
1440
+ });
1441
+ }
1442
+ }
1443
+ } else if (elem.tagName.toLowerCase() === "img") {
1444
+ debugLog(`Image: src='${elem.src}', alt='${elem.alt}'`);
1445
+ if (elem.src?.startsWith("data:image")) {
1446
+ result.push({
1447
+ type: "image",
1448
+ src: "-",
1449
+ alt: escapeMarkdownCharacters(elem.alt),
1450
+ });
1451
+ } else {
1452
+ const src =
1453
+ options?.websiteDomain &&
1454
+ elem.src?.startsWith(options.websiteDomain)
1455
+ ? elem.src?.substring(options.websiteDomain.length)
1456
+ : elem.src;
1457
+ result.push({
1458
+ type: "image",
1459
+ src,
1460
+ alt: escapeMarkdownCharacters(elem.alt),
1461
+ });
1462
+ }
1463
+ } else if (elem.tagName.toLowerCase() === "video") {
1464
+ debugLog(
1465
+ `Video: src='${elem.src}', poster='${elem.poster}', controls='${elem.controls}'`
1466
+ );
1467
+ result.push({
1468
+ type: "video",
1469
+ src: elem.src,
1470
+ poster: escapeMarkdownCharacters(elem.poster),
1471
+ controls: elem.controls,
1472
+ });
1473
+ } else if (
1474
+ elem.tagName.toLowerCase() === "ul" ||
1475
+ elem.tagName.toLowerCase() === "ol"
1476
+ ) {
1477
+ debugLog(
1478
+ `${
1479
+ elem.tagName.toLowerCase() === "ul" ? "Unordered" : "Ordered"
1480
+ } List`
1481
+ );
1482
+ result.push({
1483
+ type: "list",
1484
+ ordered: elem.tagName.toLowerCase() === "ol",
1485
+ items: Array.from(elem.children).map((li) => ({
1486
+ type: "listItem",
1487
+ content: htmlToMarkdownAST(li, options, indentLevel + 1),
1488
+ })),
1489
+ });
1490
+ } else if (elem.tagName.toLowerCase() === "br") {
1491
+ debugLog("Line Break");
1492
+ result.push({ type: "text", content: "\n" });
1493
+ } else if (elem.tagName.toLowerCase() === "table") {
1494
+ debugLog("Table");
1495
+ let colIds = [];
1496
+ if (options?.enableTableColumnTracking) {
1497
+ // Generate unique column IDs
1498
+ const headerCells = Array.from(elem.querySelectorAll("th, td"));
1499
+ headerCells.forEach((_, index) => {
1500
+ colIds.push(`col-${index}`);
1501
+ });
1502
+ }
1503
+ const tableRows = Array.from(elem.querySelectorAll("tr"));
1504
+ const markdownTableRows = tableRows.map((row) => {
1505
+ let columnIndex = 0;
1506
+ const cells = Array.from(row.querySelectorAll("th, td")).map(
1507
+ (cell) => {
1508
+ const colspan = parseInt(
1509
+ cell.getAttribute("colspan") || "1",
1510
+ 10
1511
+ );
1512
+ const rowspan = parseInt(
1513
+ cell.getAttribute("rowspan") || "1",
1514
+ 10
1515
+ );
1516
+ const cellNode = {
1517
+ type: "tableCell",
1518
+ content:
1519
+ cell.nodeType === ElementNode_1$1._Node.TEXT_NODE
1520
+ ? escapeMarkdownCharacters(cell.textContent?.trim() ?? "")
1521
+ : htmlToMarkdownAST(cell, options, indentLevel + 1),
1522
+ colId: colIds[columnIndex],
1523
+ colspan: colspan > 1 ? colspan : undefined,
1524
+ rowspan: rowspan > 1 ? rowspan : undefined,
1525
+ };
1526
+ columnIndex += colspan;
1527
+ return cellNode;
1528
+ }
1529
+ );
1530
+ return { type: "tableRow", cells };
1531
+ });
1532
+ if (markdownTableRows.length > 0) {
1533
+ // Check if the first row contains header cells
1534
+ const hasHeaders = tableRows[0].querySelector("th") !== null;
1535
+ if (hasHeaders) {
1536
+ // Create a header separator row
1537
+ const headerSeparatorCells = Array.from(
1538
+ tableRows[0].querySelectorAll("th, td")
1539
+ ).map(() => ({
1540
+ type: "tableCell",
1541
+ content: "---",
1542
+ colId: undefined,
1543
+ colspan: undefined,
1544
+ rowspan: undefined,
1545
+ }));
1546
+ const headerSeparatorRow = {
1547
+ type: "tableRow",
1548
+ cells: headerSeparatorCells,
1549
+ };
1550
+ markdownTableRows.splice(1, 0, headerSeparatorRow);
1551
+ }
1552
+ }
1553
+ result.push({ type: "table", rows: markdownTableRows, colIds });
1554
+ } else if (
1555
+ elem.tagName.toLowerCase() === "head" &&
1556
+ !!options?.includeMetaData
1557
+ ) {
1558
+ const node = {
1559
+ type: "meta",
1560
+ content: {
1561
+ standard: {},
1562
+ openGraph: {},
1563
+ twitter: {},
1564
+ },
1565
+ };
1566
+ elem.querySelectorAll("title").forEach((titleElem) => {
1567
+ node.content.standard["title"] = escapeMarkdownCharacters(
1568
+ titleElem.text
1569
+ );
1570
+ });
1571
+ // Extract meta tags
1572
+ const metaTags = elem.querySelectorAll("meta");
1573
+ const nonSemanticTagNames = [
1574
+ "viewport",
1575
+ "referrer",
1576
+ "Content-Security-Policy",
1577
+ ];
1578
+ metaTags.forEach((metaTag) => {
1579
+ const name = metaTag.getAttribute("name");
1580
+ const property = metaTag.getAttribute("property");
1581
+ const content = metaTag.getAttribute("content");
1582
+ if (property && property.startsWith("og:") && content) {
1583
+ if (options.includeMetaData === "extended") {
1584
+ node.content.openGraph[property.substring(3)] = content;
1585
+ }
1586
+ } else if (name && name.startsWith("twitter:") && content) {
1587
+ if (options.includeMetaData === "extended") {
1588
+ node.content.twitter[name.substring(8)] = content;
1589
+ }
1590
+ } else if (name && !nonSemanticTagNames.includes(name) && content) {
1591
+ node.content.standard[name] = content;
1592
+ }
1593
+ });
1594
+ // Extract JSON-LD data
1595
+ if (options.includeMetaData === "extended") {
1596
+ const jsonLdData = [];
1597
+ const jsonLDScripts = elem.querySelectorAll(
1598
+ 'script[type="application/ld+json"]'
1599
+ );
1600
+ jsonLDScripts.forEach((script) => {
1601
+ try {
1602
+ const jsonContent = script.textContent;
1603
+ if (jsonContent) {
1604
+ const parsedData = JSON.parse(jsonContent);
1605
+ jsonLdData.push(parsedData);
1606
+ }
1607
+ } catch (error) {
1608
+ console.error("Failed to parse JSON-LD", error);
1609
+ }
1610
+ });
1611
+ node.content.jsonLd = jsonLdData;
1612
+ }
1613
+ result.push(node);
1614
+ } else {
1615
+ const content = escapeMarkdownCharacters(elem.textContent || "");
1616
+ switch (elem.tagName.toLowerCase()) {
1617
+ case "noscript":
1618
+ case "script":
1619
+ case "style":
1620
+ case "html":
1621
+ // blackhole..
1622
+ break;
1623
+ case "strong":
1624
+ case "b":
1625
+ if (content) {
1626
+ debugLog(`Bold: '${content}'`);
1627
+ result.push({
1628
+ type: "bold",
1629
+ content: htmlToMarkdownAST(elem, options, indentLevel + 1),
1630
+ });
1631
+ }
1632
+ break;
1633
+ case "em":
1634
+ case "i":
1635
+ if (content) {
1636
+ debugLog(`Italic: '${content}'`);
1637
+ result.push({
1638
+ type: "italic",
1639
+ content: htmlToMarkdownAST(elem, options, indentLevel + 1),
1640
+ });
1641
+ }
1642
+ break;
1643
+ case "s":
1644
+ case "strike":
1645
+ if (content) {
1646
+ debugLog(`Strikethrough: '${content}'`);
1647
+ result.push({
1648
+ type: "strikethrough",
1649
+ content: htmlToMarkdownAST(elem, options, indentLevel + 1),
1650
+ });
1651
+ }
1652
+ break;
1653
+ case "code":
1654
+ if (content) {
1655
+ // Handling inline code differently
1656
+ const isCodeBlock =
1657
+ elem.parentNode &&
1658
+ elem.parentNode.nodeName.toLowerCase() === "pre";
1659
+ debugLog(
1660
+ `${isCodeBlock ? "Code Block" : "Inline Code"}: '${content}'`
1661
+ );
1662
+ const languageClass = elem.className
1663
+ ?.split(" ")
1664
+ .find((cls) => cls.startsWith("language-"));
1665
+ const language = languageClass
1666
+ ? languageClass.replace("language-", "")
1667
+ : "";
1668
+ result.push({
1669
+ type: "code",
1670
+ content: elem.textContent?.trim() ?? "",
1671
+ language,
1672
+ inline: !isCodeBlock,
1673
+ });
1674
+ }
1675
+ break;
1676
+ case "blockquote":
1677
+ debugLog(`Blockquote`);
1678
+ result.push({
1679
+ type: "blockquote",
1680
+ content: htmlToMarkdownAST(elem, options),
1681
+ });
1682
+ break;
1683
+ case "article":
1684
+ case "aside":
1685
+ case "details":
1686
+ case "figcaption":
1687
+ case "figure":
1688
+ case "footer":
1689
+ case "header":
1690
+ case "main":
1691
+ case "mark":
1692
+ case "nav":
1693
+ case "section":
1694
+ case "summary":
1695
+ case "time":
1696
+ debugLog(`Semantic HTML Element: '${elem.tagName}'`);
1697
+ result.push({
1698
+ type: "semanticHtml",
1699
+ htmlType: elem.tagName.toLowerCase(),
1700
+ content: htmlToMarkdownAST(elem, options),
1701
+ });
1702
+ break;
1703
+ default:
1704
+ const unhandledElementProcessing =
1705
+ options?.processUnhandledElement?.(elem, options, indentLevel);
1706
+ if (unhandledElementProcessing) {
1707
+ debugLog(`Processing Unhandled Element: '${elem.tagName}'`);
1708
+ result.push(...unhandledElementProcessing);
1709
+ } else {
1710
+ debugLog(`Generic HTMLElement: '${elem.tagName}'`);
1711
+ result.push(
1712
+ ...htmlToMarkdownAST(elem, options, indentLevel + 1)
1713
+ );
1714
+ }
1715
+ break;
1716
+ }
1717
+ }
1718
+ }
1719
+ });
1720
+ return result;
1721
+ }
1722
+ function escapeMarkdownCharacters(text, isInlineCode = false) {
1723
+ if (isInlineCode || !text?.trim()) {
1724
+ // In inline code, we don't escape any characters
1725
+ return text;
1726
+ }
1727
+ // First, replace special HTML characters with their entity equivalents
1728
+ let escapedText = text
1729
+ .replace(/&/g, "&amp;") // Replace & first
1730
+ .replace(/</g, "&lt;")
1731
+ .replace(/>/g, "&gt;");
1732
+ // Then escape characters that have special meaning in Markdown
1733
+ escapedText = escapedText.replace(/([\\`*_{}[\]#+!|])/g, "\\$1");
1734
+ return escapedText;
1735
+ }
1736
+
1737
+ var markdownASTToString = {};
1738
+
1739
+ var hasRequiredMarkdownASTToString;
1740
+
1741
+ function requireMarkdownASTToString() {
1742
+ if (hasRequiredMarkdownASTToString) return markdownASTToString;
1743
+ hasRequiredMarkdownASTToString = 1;
1744
+ Object.defineProperty(markdownASTToString, "__esModule", { value: true });
1745
+ markdownASTToString.markdownASTToString = markdownASTToString$1;
1746
+ const index_1 = requireNode();
1747
+ function markdownASTToString$1(nodes, options, indentLevel = 0) {
1748
+ let markdownString = "";
1749
+ markdownString += markdownMetaASTToString(nodes, options, indentLevel);
1750
+ markdownString += markdownContentASTToString(nodes, options, indentLevel);
1751
+ return markdownString;
1752
+ }
1753
+ function markdownMetaASTToString(nodes, options, indentLevel = 0) {
1754
+ let markdownString = "";
1755
+ if (options?.includeMetaData) {
1756
+ // include meta-data
1757
+ markdownString += "---\n";
1758
+ const node = (0, index_1.findInMarkdownAST)(
1759
+ nodes,
1760
+ (_) => _.type === "meta"
1761
+ );
1762
+ if (node?.type === "meta") {
1763
+ if (node.content.standard) {
1764
+ Object.keys(node.content.standard).forEach((key) => {
1765
+ markdownString += `${key}: "${node.content.standard[key]}"\n`;
1766
+ });
1767
+ }
1768
+ if (options.includeMetaData === "extended") {
1769
+ if (node.content.openGraph) {
1770
+ if (Object.keys(node.content.openGraph).length > 0) {
1771
+ markdownString += "openGraph:\n";
1772
+ for (const [key, value] of Object.entries(
1773
+ node.content.openGraph
1774
+ )) {
1775
+ markdownString += ` ${key}: "${value}"\n`;
1776
+ }
1777
+ }
1778
+ }
1779
+ if (node.content.twitter) {
1780
+ if (Object.keys(node.content.twitter).length > 0) {
1781
+ markdownString += "twitter:\n";
1782
+ for (const [key, value] of Object.entries(
1783
+ node.content.twitter
1784
+ )) {
1785
+ markdownString += ` ${key}: "${value}"\n`;
1786
+ }
1787
+ }
1788
+ }
1789
+ if (node.content.jsonLd && node.content.jsonLd.length > 0) {
1790
+ markdownString += "schema:\n";
1791
+ node.content.jsonLd.forEach((item) => {
1792
+ const {
1793
+ "@context": jldContext,
1794
+ "@type": jldType,
1795
+ ...semanticData
1796
+ } = item;
1797
+ markdownString += ` ${jldType ?? "(unknown type)"}:\n`;
1798
+ Object.keys(semanticData).forEach((key) => {
1799
+ markdownString += ` ${key}: ${JSON.stringify(
1800
+ semanticData[key]
1801
+ )}\n`;
1802
+ });
1803
+ });
1804
+ }
1805
+ }
1806
+ }
1807
+ markdownString += "---\n\n";
1808
+ }
1809
+ return markdownString;
1810
+ }
1811
+ function markdownContentASTToString(nodes, options, indentLevel = 0) {
1812
+ let markdownString = "";
1813
+ nodes.forEach((node) => {
1814
+ const indent = " ".repeat(indentLevel * 2); // Adjust the multiplier for different indent sizes
1815
+ const nodeRenderingOverride = options?.overrideNodeRenderer?.(
1816
+ node,
1817
+ options,
1818
+ indentLevel
1819
+ );
1820
+ if (nodeRenderingOverride) {
1821
+ markdownString += nodeRenderingOverride;
1822
+ } else {
1823
+ switch (node.type) {
1824
+ case "text":
1825
+ case "bold":
1826
+ case "italic":
1827
+ case "strikethrough":
1828
+ case "link":
1829
+ let content = node.content; // might be a nodes array but we take care of that below
1830
+ if (Array.isArray(node.content)) {
1831
+ content = markdownContentASTToString(
1832
+ node.content,
1833
+ options,
1834
+ indentLevel
1835
+ );
1836
+ }
1837
+ const isMarkdownStringNotEmpty = markdownString.length > 0;
1838
+ const isFirstCharOfContentWhitespace = /\s/.test(
1839
+ content.slice(0, 1)
1840
+ );
1841
+ const isLastCharOfMarkdownWhitespace = /\s/.test(
1842
+ markdownString.slice(-1)
1843
+ );
1844
+ const isContentPunctuation =
1845
+ content.length === 1 && /^[.,!?;:]/.test(content);
1846
+ if (
1847
+ isMarkdownStringNotEmpty &&
1848
+ !isContentPunctuation &&
1849
+ !isFirstCharOfContentWhitespace &&
1850
+ !isLastCharOfMarkdownWhitespace
1851
+ ) {
1852
+ markdownString += " ";
1853
+ }
1854
+ if (node.type === "text") {
1855
+ markdownString += `${indent}${content}`;
1856
+ } else {
1857
+ if (node.type === "bold") {
1858
+ markdownString += `**${content}**`;
1859
+ } else if (node.type === "italic") {
1860
+ markdownString += `*${content}*`;
1861
+ } else if (node.type === "strikethrough") {
1862
+ markdownString += `~~${content}~~`;
1863
+ } else if (node.type === "link") {
1864
+ // check if the link contains only text
1865
+ if (
1866
+ node.content.length === 1 &&
1867
+ node.content[0].type === "text"
1868
+ ) {
1869
+ // use native markdown syntax for text-only links
1870
+ markdownString += `[${content}](${encodeURI(node.href)})`;
1871
+ } else {
1872
+ // Use HTML <a> tag for links with rich content
1873
+ markdownString += `<a href="${node.href}">${content}</a>`;
1874
+ }
1875
+ }
1876
+ }
1877
+ break;
1878
+ case "heading":
1879
+ const isEndsWithNewLine = markdownString.slice(-1) === "\n";
1880
+ if (!isEndsWithNewLine) {
1881
+ markdownString += "\n";
1882
+ }
1883
+ markdownString += `${"#".repeat(node.level)} ${node.content}\n\n`;
1884
+ break;
1885
+ case "image":
1886
+ if (!node.alt?.trim() || !!node.src?.trim()) {
1887
+ markdownString += `![${node.alt || ""}](${node.src})`;
1888
+ }
1889
+ break;
1890
+ case "list":
1891
+ node.items.forEach((item, i) => {
1892
+ const listItemPrefix = node.ordered ? `${i + 1}.` : "-";
1893
+ const contents = markdownContentASTToString(
1894
+ item.content,
1895
+ options,
1896
+ indentLevel + 1
1897
+ ).trim();
1898
+ if (markdownString.slice(-1) !== "\n") {
1899
+ markdownString += "\n";
1900
+ }
1901
+ if (contents) {
1902
+ markdownString += `${indent}${listItemPrefix} ${contents}\n`;
1903
+ }
1904
+ });
1905
+ markdownString += "\n";
1906
+ break;
1907
+ case "video":
1908
+ markdownString += `\n![Video](${node.src})\n`;
1909
+ if (node.poster) {
1910
+ markdownString += `![Poster](${node.poster})\n`;
1911
+ }
1912
+ if (node.controls) {
1913
+ markdownString += `Controls: ${node.controls}\n`;
1914
+ }
1915
+ markdownString += "\n";
1916
+ break;
1917
+ case "table":
1918
+ const maxColumns = Math.max(
1919
+ ...node.rows.map((row) =>
1920
+ row.cells.reduce((sum, cell) => sum + (cell.colspan || 1), 0)
1921
+ )
1922
+ );
1923
+ node.rows.forEach((row) => {
1924
+ let currentColumn = 0;
1925
+ row.cells.forEach((cell) => {
1926
+ let cellContent =
1927
+ typeof cell.content === "string"
1928
+ ? cell.content
1929
+ : markdownContentASTToString(
1930
+ cell.content,
1931
+ options,
1932
+ indentLevel + 1
1933
+ ).trim();
1934
+ if (cell.colId) {
1935
+ cellContent += ` <!-- ${cell.colId} -->`;
1936
+ }
1937
+ if (cell.colspan && cell.colspan > 1) {
1938
+ cellContent += ` <!-- colspan: ${cell.colspan} -->`;
1939
+ }
1940
+ if (cell.rowspan && cell.rowspan > 1) {
1941
+ cellContent += ` <!-- rowspan: ${cell.rowspan} -->`;
1942
+ }
1943
+ markdownString += `| ${cellContent} `;
1944
+ currentColumn += cell.colspan || 1;
1945
+ // Add empty cells for colspan
1946
+ for (let i = 1; i < (cell.colspan || 1); i++) {
1947
+ markdownString += "| ";
1948
+ }
1949
+ });
1950
+ // Fill remaining columns with empty cells
1951
+ while (currentColumn < maxColumns) {
1952
+ markdownString += "| ";
1953
+ currentColumn++;
1954
+ }
1955
+ markdownString += "|\n";
1956
+ });
1957
+ markdownString += "\n";
1958
+ break;
1959
+ case "code":
1960
+ if (node.inline) {
1961
+ const isLsatWhitespace = /\s/.test(markdownString.slice(-1));
1962
+ if (!isLsatWhitespace) {
1963
+ markdownString += " ";
1964
+ }
1965
+ markdownString += `\`${node.content}\``;
1966
+ } else {
1967
+ // For code blocks, we do not escape characters and preserve formatting
1968
+ markdownString += "\n```" + (node.language ?? "") + "\n";
1969
+ markdownString += `${node.content}\n`;
1970
+ markdownString += "```\n\n";
1971
+ }
1972
+ break;
1973
+ case "blockquote":
1974
+ markdownString += `> ${markdownContentASTToString(
1975
+ node.content,
1976
+ options
1977
+ ).trim()}\n\n`;
1978
+ break;
1979
+ case "meta":
1980
+ // already handled
1981
+ break;
1982
+ case "semanticHtml":
1983
+ switch (node.htmlType) {
1984
+ case "article":
1985
+ markdownString +=
1986
+ "\n\n" + markdownContentASTToString(node.content, options);
1987
+ break;
1988
+ case "summary":
1989
+ case "time":
1990
+ case "aside":
1991
+ case "nav":
1992
+ case "figcaption":
1993
+ case "main":
1994
+ case "mark":
1995
+ case "header":
1996
+ case "footer":
1997
+ case "details":
1998
+ case "figure":
1999
+ markdownString +=
2000
+ `\n\n<-${node.htmlType}->\n` +
2001
+ markdownContentASTToString(node.content, options) +
2002
+ `\n\n</-${node.htmlType}->\n`;
2003
+ break;
2004
+ case "section":
2005
+ markdownString += "---\n\n";
2006
+ markdownString += markdownContentASTToString(
2007
+ node.content,
2008
+ options
2009
+ );
2010
+ markdownString += "\n\n";
2011
+ markdownString += "---\n\n";
2012
+ break;
2013
+ }
2014
+ break;
2015
+ case "custom":
2016
+ const customNodeRendering = options?.renderCustomNode?.(
2017
+ node,
2018
+ options,
2019
+ indentLevel
2020
+ );
2021
+ if (customNodeRendering) {
2022
+ markdownString += customNodeRendering;
2023
+ }
2024
+ break;
2025
+ }
2026
+ }
2027
+ });
2028
+ return markdownString;
2029
+ }
2030
+ return markdownASTToString;
2031
+ }
2032
+
2033
+ var domUtils = {};
2034
+
2035
+ Object.defineProperty(domUtils, "__esModule", { value: true });
2036
+ domUtils.findMainContent = findMainContent;
2037
+ domUtils.wrapMainContent = wrapMainContent;
2038
+ domUtils.isElementVisible = isElementVisible;
2039
+ domUtils.getVisibleText = getVisibleText;
2040
+ const ElementNode_1 = ElementNode;
2041
+ const debugMessage = (message) => {};
2042
+ /**
2043
+ * Attempts to find the main content of a web page.
2044
+ * @param document The Document object to search.
2045
+ * @returns The Element containing the main content, or the body if no main content is found.
2046
+ */
2047
+ function findMainContent(document) {
2048
+ const mainElement = document.querySelector("main");
2049
+ if (mainElement) {
2050
+ return mainElement;
2051
+ }
2052
+ if (!document.body) {
2053
+ return document.documentElement;
2054
+ }
2055
+ return detectMainContent(document.body);
2056
+ }
2057
+ function wrapMainContent(mainContentElement, document) {
2058
+ if (mainContentElement.tagName.toLowerCase() !== "main") {
2059
+ const mainElement = document.createElement("main");
2060
+ mainContentElement.before(mainElement);
2061
+ mainElement.appendChild(mainContentElement);
2062
+ mainElement.id = "detected-main-content";
2063
+ }
2064
+ }
2065
+ function detectMainContent(rootElement) {
2066
+ const candidates = [];
2067
+ const minScore = 20;
2068
+ collectCandidates(rootElement, candidates, minScore);
2069
+ if (candidates.length === 0) {
2070
+ return rootElement;
2071
+ }
2072
+ candidates.sort((a, b) => calculateScore(b) - calculateScore(a));
2073
+ let bestIndependentCandidate = candidates[0];
2074
+ for (let i = 1; i < candidates.length; i++) {
2075
+ if (
2076
+ !candidates.some(
2077
+ (otherCandidate, j) =>
2078
+ j !== i && otherCandidate.contains(candidates[i])
2079
+ )
2080
+ ) {
2081
+ if (
2082
+ calculateScore(candidates[i]) >
2083
+ calculateScore(bestIndependentCandidate)
2084
+ ) {
2085
+ bestIndependentCandidate = candidates[i];
2086
+ debugMessage(
2087
+ `New best independent candidate found: ${elementToString(
2088
+ bestIndependentCandidate
2089
+ )}`
2090
+ );
2091
+ }
2092
+ }
2093
+ }
2094
+ debugMessage(
2095
+ `Final main content candidate: ${elementToString(
2096
+ bestIndependentCandidate
2097
+ )}`
2098
+ );
2099
+ return bestIndependentCandidate;
2100
+ }
2101
+ function elementToString(element) {
2102
+ if (!element) {
2103
+ return "No element";
2104
+ }
2105
+ return `${element.tagName}#${element.id || "no-id"}.${Array.from(
2106
+ element.classList
2107
+ ).join(".")}`;
2108
+ }
2109
+ function collectCandidates(element, candidates, minScore) {
2110
+ const score = calculateScore(element);
2111
+ if (score >= minScore) {
2112
+ candidates.push(element);
2113
+ debugMessage(
2114
+ `Candidate found: ${elementToString(element)}, score: ${score}`
2115
+ );
2116
+ }
2117
+ Array.from(element.children).forEach((child) => {
2118
+ collectCandidates(child, candidates, minScore);
2119
+ });
2120
+ }
2121
+ function calculateScore(element) {
2122
+ let score = 0;
2123
+ let scoreLog = [];
2124
+ // High impact attributes
2125
+ const highImpactAttributes = [
2126
+ "article",
2127
+ "content",
2128
+ "main-container",
2129
+ "main",
2130
+ "main-content",
2131
+ ];
2132
+ highImpactAttributes.forEach((attr) => {
2133
+ if (element.classList.contains(attr) || element.id.includes(attr)) {
2134
+ score += 10;
2135
+ scoreLog.push(
2136
+ `High impact attribute found: ${attr}, score increased by 10`
2137
+ );
2138
+ }
2139
+ });
2140
+ // High impact tags
2141
+ const highImpactTags = ["article", "main", "section"];
2142
+ if (highImpactTags.includes(element.tagName.toLowerCase())) {
2143
+ score += 5;
2144
+ scoreLog.push(
2145
+ `High impact tag found: ${element.tagName}, score increased by 5`
2146
+ );
2147
+ }
2148
+ // Paragraph count
2149
+ const paragraphCount = element.getElementsByTagName("p").length;
2150
+ const paragraphScore = Math.min(paragraphCount, 5);
2151
+ if (paragraphScore > 0) {
2152
+ score += paragraphScore;
2153
+ scoreLog.push(
2154
+ `Paragraph count: ${paragraphCount}, score increased by ${paragraphScore}`
2155
+ );
2156
+ }
2157
+ // Text content length
2158
+ const textContentLength = element.textContent?.trim().length || 0;
2159
+ if (textContentLength > 200) {
2160
+ const textScore = Math.min(Math.floor(textContentLength / 200), 5);
2161
+ score += textScore;
2162
+ scoreLog.push(
2163
+ `Text content length: ${textContentLength}, score increased by ${textScore}`
2164
+ );
2165
+ }
2166
+ // Link density
2167
+ const linkDensity = calculateLinkDensity(element);
2168
+ if (linkDensity < 0.3) {
2169
+ score += 5;
2170
+ scoreLog.push(
2171
+ `Link density: ${linkDensity.toFixed(2)}, score increased by 5`
2172
+ );
2173
+ }
2174
+ // Data attributes
2175
+ if (
2176
+ element.hasAttribute("data-main") ||
2177
+ element.hasAttribute("data-content")
2178
+ ) {
2179
+ score += 10;
2180
+ scoreLog.push(
2181
+ "Data attribute for main content found, score increased by 10"
2182
+ );
2183
+ }
2184
+ // Role attribute
2185
+ if (element.getAttribute("role")?.includes("main")) {
2186
+ score += 10;
2187
+ scoreLog.push(
2188
+ "Role attribute indicating main content found, score increased by 10"
2189
+ );
2190
+ }
2191
+ if (scoreLog.length > 0) {
2192
+ debugMessage(`Scoring for ${elementToString(element)}:`);
2193
+ }
2194
+ return score;
2195
+ }
2196
+ function calculateLinkDensity(element) {
2197
+ const linkLength = Array.from(element.getElementsByTagName("a")).reduce(
2198
+ (sum, link) => sum + (link.textContent?.length || 0),
2199
+ 0
2200
+ );
2201
+ const textLength = element.textContent?.length || 1; // Avoid division by zero
2202
+ return linkLength / textLength;
2203
+ }
2204
+ function isElementVisible(element) {
2205
+ if (!(element instanceof HTMLElement)) {
2206
+ return true; // Non-HTMLElements are considered visible
2207
+ }
2208
+ const style = window.getComputedStyle(element);
2209
+ return (
2210
+ style.display !== "none" &&
2211
+ style.visibility !== "hidden" &&
2212
+ style.opacity !== "0"
2213
+ );
2214
+ }
2215
+ function getVisibleText(element) {
2216
+ if (!isElementVisible(element)) {
2217
+ return "";
2218
+ }
2219
+ let text = "";
2220
+ for (const child of Array.from(element.childNodes)) {
2221
+ if (child.nodeType === ElementNode_1._Node.TEXT_NODE) {
2222
+ text += child.textContent;
2223
+ } else if (child.nodeType === ElementNode_1._Node.ELEMENT_NODE) {
2224
+ text += getVisibleText(child);
2225
+ }
2226
+ }
2227
+ return text.trim();
2228
+ }
2229
+
2230
+ var urlUtils = {};
2231
+
2232
+ Object.defineProperty(urlUtils, "__esModule", { value: true });
2233
+ urlUtils.refifyUrls = refifyUrls;
2234
+ const mediaSuffixes = [
2235
+ "jpeg",
2236
+ "jpg",
2237
+ "png",
2238
+ "gif",
2239
+ "bmp",
2240
+ "tiff",
2241
+ "tif",
2242
+ "svg",
2243
+ "webp",
2244
+ "ico",
2245
+ "avi",
2246
+ "mov",
2247
+ "mp4",
2248
+ "mkv",
2249
+ "flv",
2250
+ "wmv",
2251
+ "webm",
2252
+ "mpeg",
2253
+ "mpg",
2254
+ "mp3",
2255
+ "wav",
2256
+ "aac",
2257
+ "ogg",
2258
+ "flac",
2259
+ "m4a",
2260
+ "pdf",
2261
+ "doc",
2262
+ "docx",
2263
+ "ppt",
2264
+ "pptx",
2265
+ "xls",
2266
+ "xlsx",
2267
+ "txt",
2268
+ "css",
2269
+ "js",
2270
+ "xml",
2271
+ "json",
2272
+ "html",
2273
+ "htm",
2274
+ ];
2275
+ const addRefPrefix = (prefix, prefixesToRefs) => {
2276
+ if (!prefixesToRefs[prefix]) {
2277
+ prefixesToRefs[prefix] = "ref" + Object.values(prefixesToRefs).length;
2278
+ }
2279
+ return prefixesToRefs[prefix];
2280
+ };
2281
+ const processUrl = (url, prefixesToRefs) => {
2282
+ if (!url.startsWith("http")) {
2283
+ return url;
2284
+ } else {
2285
+ const mediaSuffix = url.split(".").slice(-1)[0];
2286
+ if (mediaSuffix && mediaSuffixes.includes(mediaSuffix)) {
2287
+ const parts = url.split("/"); // Split URL keeping the slash before text
2288
+ const prefix = parts.slice(0, -1).join("/"); // Get the prefix by removing last part
2289
+ const refPrefix = addRefPrefix(prefix, prefixesToRefs);
2290
+ return `${refPrefix}://${parts.slice(-1).join("")}`;
2291
+ } else {
2292
+ if (url.split("/").length > 4) {
2293
+ return addRefPrefix(url, prefixesToRefs);
2294
+ } else {
2295
+ return url;
2296
+ }
2297
+ }
2298
+ }
2299
+ };
2300
+ function refifyUrls(markdownElement, prefixesToRefs = {}) {
2301
+ if (Array.isArray(markdownElement)) {
2302
+ markdownElement.forEach((element) => refifyUrls(element, prefixesToRefs));
2303
+ } else {
2304
+ switch (markdownElement.type) {
2305
+ case "link":
2306
+ markdownElement.href = processUrl(
2307
+ markdownElement.href,
2308
+ prefixesToRefs
2309
+ );
2310
+ refifyUrls(markdownElement.content, prefixesToRefs);
2311
+ break;
2312
+ case "image":
2313
+ case "video":
2314
+ markdownElement.src = processUrl(markdownElement.src, prefixesToRefs);
2315
+ break;
2316
+ case "list":
2317
+ markdownElement.items.forEach((item) =>
2318
+ item.content.forEach((_) => refifyUrls(_, prefixesToRefs))
2319
+ );
2320
+ break;
2321
+ case "table":
2322
+ markdownElement.rows.forEach((row) =>
2323
+ row.cells.forEach((cell) =>
2324
+ typeof cell.content === "string"
2325
+ ? null
2326
+ : refifyUrls(cell.content, prefixesToRefs)
2327
+ )
2328
+ );
2329
+ break;
2330
+ case "blockquote":
2331
+ case "semanticHtml":
2332
+ refifyUrls(markdownElement.content, prefixesToRefs);
2333
+ break;
2334
+ }
2335
+ }
2336
+ return prefixesToRefs;
2337
+ }
2338
+
2339
+ var astUtils = {};
2340
+
2341
+ (function (exports) {
2342
+ Object.defineProperty(exports, "__esModule", { value: true });
2343
+ exports.isNot = exports.getMainContent = void 0;
2344
+ exports.findInAST = findInAST;
2345
+ exports.findAllInAST = findAllInAST;
2346
+ const getMainContent = (markdownStr) => {
2347
+ if (markdownStr.includes("<-main->")) {
2348
+ const regex = /(?<=<-main->)[\s\S]*?(?=<\/-main->)/;
2349
+ const match = markdownStr.match(regex);
2350
+ return match?.[0] ?? "";
2351
+ } else {
2352
+ const removeSectionsRegex =
2353
+ /(<-nav->[\s\S]*?<\/-nav->)|(<-footer->[\s\S]*?<\/-footer->)|(<-header->[\s\S]*?<\/-header->)|(<-aside->[\s\S]*?<\/-aside->)/g;
2354
+ return markdownStr.replace(removeSectionsRegex, "");
2355
+ }
2356
+ };
2357
+ exports.getMainContent = getMainContent;
2358
+ const isNot = (tPred) => (t) => !tPred(t);
2359
+ exports.isNot = isNot;
2360
+ const isString = (x) => typeof x === "string";
2361
+ function findInAST(markdownElement, checker) {
2362
+ const loopCheck = (z) => {
2363
+ for (const element of z) {
2364
+ const found = findInAST(element, checker);
2365
+ if (found) {
2366
+ return found;
2367
+ }
2368
+ }
2369
+ return undefined;
2370
+ };
2371
+ if (Array.isArray(markdownElement)) {
2372
+ return loopCheck(markdownElement);
2373
+ } else {
2374
+ if (checker(markdownElement)) {
2375
+ return markdownElement;
2376
+ }
2377
+ switch (markdownElement.type) {
2378
+ case "link":
2379
+ return loopCheck(markdownElement.content);
2380
+ case "list":
2381
+ return loopCheck(
2382
+ markdownElement.items.map((_) => _.content).flat()
2383
+ );
2384
+ case "table":
2385
+ return loopCheck(
2386
+ markdownElement.rows
2387
+ .map((row) =>
2388
+ row.cells
2389
+ .map((_) => _.content)
2390
+ .filter((0, exports.isNot)(isString))
2391
+ )
2392
+ .flat()
2393
+ );
2394
+ case "blockquote":
2395
+ case "semanticHtml":
2396
+ return loopCheck(markdownElement.content);
2397
+ }
2398
+ return undefined;
2399
+ }
2400
+ }
2401
+ function findAllInAST(markdownElement, checker) {
2402
+ const loopCheck = (z) => {
2403
+ let out = [];
2404
+ for (const element of z) {
2405
+ const found = findAllInAST(element, checker);
2406
+ out = [...out, ...found];
2407
+ }
2408
+ return out;
2409
+ };
2410
+ if (Array.isArray(markdownElement)) {
2411
+ return loopCheck(markdownElement);
2412
+ } else {
2413
+ if (checker(markdownElement)) {
2414
+ return [markdownElement];
2415
+ }
2416
+ switch (markdownElement.type) {
2417
+ case "link":
2418
+ return loopCheck(markdownElement.content);
2419
+ case "list":
2420
+ return loopCheck(
2421
+ markdownElement.items.map((_) => _.content).flat()
2422
+ );
2423
+ case "table":
2424
+ return loopCheck(
2425
+ markdownElement.rows
2426
+ .map((row) =>
2427
+ row.cells
2428
+ .map((_) => _.content)
2429
+ .filter((0, exports.isNot)(isString))
2430
+ )
2431
+ .flat()
2432
+ );
2433
+ case "blockquote":
2434
+ case "semanticHtml":
2435
+ return loopCheck(markdownElement.content);
2436
+ }
2437
+ return [];
2438
+ }
2439
+ }
2440
+ })(astUtils);
2441
+
2442
+ var hasRequiredNode;
2443
+
2444
+ function requireNode() {
2445
+ if (hasRequiredNode) return node;
2446
+ hasRequiredNode = 1;
2447
+ (function (exports) {
2448
+ Object.defineProperty(exports, "__esModule", { value: true });
2449
+ exports.wrapMainContent =
2450
+ exports.refifyUrls =
2451
+ exports.findMainContent =
2452
+ exports.markdownASTToString =
2453
+ exports.htmlToMarkdownAST =
2454
+ void 0;
2455
+ exports.convertHtmlToMarkdown = convertHtmlToMarkdown;
2456
+ exports.convertElementToMarkdown = convertElementToMarkdown;
2457
+ exports.findInMarkdownAST = findInMarkdownAST;
2458
+ exports.findAllInMarkdownAST = findAllInMarkdownAST;
2459
+ const htmlToMarkdownAST_1 = htmlToMarkdownAST$1;
2460
+ Object.defineProperty(exports, "htmlToMarkdownAST", {
2461
+ enumerable: true,
2462
+ get: function () {
2463
+ return htmlToMarkdownAST_1.htmlToMarkdownAST;
2464
+ },
2465
+ });
2466
+ const markdownASTToString_1 = requireMarkdownASTToString();
2467
+ Object.defineProperty(exports, "markdownASTToString", {
2468
+ enumerable: true,
2469
+ get: function () {
2470
+ return markdownASTToString_1.markdownASTToString;
2471
+ },
2472
+ });
2473
+ const domUtils_1 = domUtils;
2474
+ Object.defineProperty(exports, "findMainContent", {
2475
+ enumerable: true,
2476
+ get: function () {
2477
+ return domUtils_1.findMainContent;
2478
+ },
2479
+ });
2480
+ Object.defineProperty(exports, "wrapMainContent", {
2481
+ enumerable: true,
2482
+ get: function () {
2483
+ return domUtils_1.wrapMainContent;
2484
+ },
2485
+ });
2486
+ const urlUtils_1 = urlUtils;
2487
+ Object.defineProperty(exports, "refifyUrls", {
2488
+ enumerable: true,
2489
+ get: function () {
2490
+ return urlUtils_1.refifyUrls;
2491
+ },
2492
+ });
2493
+ const astUtils_1 = astUtils;
2494
+ /**
2495
+ * Converts an HTML string to Markdown.
2496
+ * @param html The HTML string to convert.
2497
+ * @param options Conversion options.
2498
+ * @returns The converted Markdown string.
2499
+ */
2500
+ function convertHtmlToMarkdown(html, options) {
2501
+ const parser =
2502
+ options?.overrideDOMParser ??
2503
+ (typeof DOMParser !== "undefined" ? new DOMParser() : null);
2504
+ if (!parser) {
2505
+ throw new Error(
2506
+ "DOMParser is not available. Please provide an overrideDOMParser in options."
2507
+ );
2508
+ }
2509
+ const doc = parser.parseFromString(html, "text/html");
2510
+ let element;
2511
+ if (options?.extractMainContent) {
2512
+ element = (0, domUtils_1.findMainContent)(doc);
2513
+ if (
2514
+ options.includeMetaData &&
2515
+ !!doc.querySelector("head")?.innerHTML &&
2516
+ !element.querySelector("head")
2517
+ ) {
2518
+ // content container was found and extracted, re-attaching the head for meta-data extraction
2519
+ element = parser.parseFromString(
2520
+ `<html>${doc.head.outerHTML}${element.outerHTML}`,
2521
+ "text/html"
2522
+ ).documentElement;
2523
+ }
2524
+ } else {
2525
+ // If there's a body, use it; otherwise, use the document element
2526
+ if (
2527
+ options?.includeMetaData &&
2528
+ !!doc.querySelector("head")?.innerHTML
2529
+ ) {
2530
+ element = doc.documentElement;
2531
+ } else {
2532
+ element = doc.body || doc.documentElement;
2533
+ }
2534
+ }
2535
+ return convertElementToMarkdown(element, options);
2536
+ }
2537
+ /**
2538
+ * Converts an HTML Element to Markdown.
2539
+ * @param element The HTML Element to convert.
2540
+ * @param options Conversion options.
2541
+ * @returns The converted Markdown string.
2542
+ */
2543
+ function convertElementToMarkdown(element, options) {
2544
+ let ast = (0, htmlToMarkdownAST_1.htmlToMarkdownAST)(element, options);
2545
+ if (options?.refifyUrls) {
2546
+ options.urlMap = (0, urlUtils_1.refifyUrls)(ast);
2547
+ }
2548
+ return (0, markdownASTToString_1.markdownASTToString)(ast, options);
2549
+ }
2550
+ /**
2551
+ * Finds a node in the Markdown AST that matches the given predicate.
2552
+ * @param ast The Markdown AST to search.
2553
+ * @param predicate A function that returns true for the desired node.
2554
+ * @returns The first matching node, or undefined if not found.
2555
+ */
2556
+ function findInMarkdownAST(ast, predicate) {
2557
+ return (0, astUtils_1.findInAST)(ast, predicate);
2558
+ }
2559
+ /**
2560
+ * Finds all nodes in the Markdown AST that match the given predicate.
2561
+ * @param ast The Markdown AST to search.
2562
+ * @param predicate A function that returns true for the desired nodes.
2563
+ * @returns An array of all matching nodes.
2564
+ */
2565
+ function findAllInMarkdownAST(ast, predicate) {
2566
+ return (0, astUtils_1.findAllInAST)(ast, predicate);
2567
+ }
2568
+ })(node);
2569
+ return node;
2570
+ }
2571
+
2572
+ var nodeExports = requireNode();
2573
+
2574
+ //@ts-ignore
2575
+ window.__INTUNED__ = {
2576
+ matchStringsWithDomContent,
2577
+ convertElementToMarkdown,
2578
+ convertHtmlStringToSemanticMarkdown: nodeExports.convertHtmlToMarkdown,
2579
+ };
2580
+ })();