-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessor.ts
More file actions
657 lines (574 loc) · 27.8 KB
/
processor.ts
File metadata and controls
657 lines (574 loc) · 27.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import puppeteer, { type Browser, type Page } from "puppeteer-core";
import { join, dirname } from "path";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import { JSDOM, VirtualConsole } from "jsdom";
import { Defuddle } from "defuddle/node";
import { ConfigManager, Parser, ReturnFormat } from "./config";
import { ProcessResult } from "./types";
import { LinkUtils, Logger, FileUtils } from "./utils";
export class LinkProcessor {
private config: ConfigManager;
private browser: Browser | null = null;
constructor(config: ConfigManager) {
this.config = config;
}
private async initializePuppeteer(): Promise<Browser> {
if (this.browser) {
return this.browser;
}
// Find the installed Chrome executable
const fs = await import('fs');
const path = await import('path');
const os = await import('os');
let chromePath: string | undefined;
// Determine cache directory based on environment or platform
let cacheDir = process.env.PUPPETEER_CACHE_DIR;
if (!cacheDir) {
// Use platform-specific defaults
const homeDir = os.homedir();
if (process.platform === 'darwin') {
// macOS
cacheDir = path.join(homeDir, '.cache', 'puppeteer');
} else if (process.platform === 'linux') {
// Linux
cacheDir = process.env.XDG_CACHE_HOME
? path.join(process.env.XDG_CACHE_HOME, 'puppeteer')
: path.join(homeDir, '.cache', 'puppeteer');
} else if (process.platform === 'win32') {
// Windows
cacheDir = path.join(process.env.LOCALAPPDATA || path.join(homeDir, 'AppData', 'Local'), 'puppeteer');
} else {
cacheDir = path.join(homeDir, '.cache', 'puppeteer');
}
}
Logger.debug(`Searching for Chrome in: ${cacheDir}`);
// Helper function to find Chrome executable in directory
const findChromeExecutable = (dir: string): string | undefined => {
if (!fs.existsSync(dir)) return undefined;
try {
const entries = fs.readdirSync(dir, { withFileTypes: true });
const versionDirs = entries
.filter(entry => entry.isDirectory() && /^(linux|mac_arm|mac|win)-/.test(entry.name))
.map(entry => entry.name)
.sort((a, b) => b.localeCompare(a)); // Sort descending for latest version
for (const versionDir of versionDirs) {
const versionPath = path.join(dir, versionDir);
const possiblePaths = [
path.join(versionPath, 'chrome-linux64', 'chrome'),
path.join(versionPath, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium'),
path.join(versionPath, 'chrome-win', 'chrome.exe'),
path.join(versionPath, 'chrome-headless-shell-mac-arm64', 'chrome-headless-shell'),
path.join(versionPath, 'chrome-headless-shell-linux', 'chrome-headless-shell'),
path.join(versionPath, 'chrome-headless-shell-win', 'chrome-headless-shell.exe'),
];
for (const possiblePath of possiblePaths) {
if (fs.existsSync(possiblePath)) {
return possiblePath;
}
}
}
} catch (error) {
console.warn(`Error scanning ${dir}:`, error);
}
return undefined;
};
// Try to find Chrome in cache directory
chromePath = findChromeExecutable(path.join(cacheDir, 'chrome'));
if (chromePath) {
Logger.debug(`Using Chrome at: ${chromePath}`);
} else {
// Try to find Chrome-headless-shell in cache directory
chromePath = findChromeExecutable(path.join(cacheDir, 'chrome-headless-shell'));
if (chromePath) {
Logger.debug(`Using Chrome-headless-shell at: ${chromePath}`);
}
}
// Final fallback: try system Chrome
if (!chromePath) {
const systemPaths = [
'/usr/bin/google-chrome',
'/usr/bin/chromium',
'/usr/bin/chromium-browser',
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
'/Applications/Chromium.app/Contents/MacOS/Chromium',
];
for (const sysPath of systemPaths) {
if (fs.existsSync(sysPath)) {
chromePath = sysPath;
Logger.debug(`Using system Chrome at: ${chromePath}`);
break;
}
}
}
if (!chromePath) {
// List cache contents for debugging
try {
if (fs.existsSync(cacheDir)) {
Logger.debug(`Cache directory contents (${cacheDir}):`);
const cacheContents = fs.readdirSync(cacheDir, { recursive: true });
cacheContents.slice(0, 20).forEach(item => Logger.debug(` ${item}`));
}
} catch (error) {
console.warn('Could not list cache contents:', error);
}
throw new Error('Chrome executable not found. Please install with: bunx puppeteer browsers install chrome or bunx puppeteer browsers install chrome-headless-shell');
}
this.browser = await puppeteer.launch({
headless: true,
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
"--disable-software-rasterizer"
],
executablePath: chromePath,
});
return this.browser;
}
private async processSingleLinkWithPuppeteer(
link: string,
browser: Browser,
returnMarkdown: boolean,
saveToDisk: boolean
): Promise<{ updatedLink: string; markdown?: string; frontmatter?: any; body?: string }> {
const line = link.trim();
if (LinkUtils.isProcessed(line)) {
Logger.debug(`Skipping! already processed: ${line}`);
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
const task = LinkUtils.sanitizeLink(line);
if (!LinkUtils.isValidHttpLink(task)) {
Logger.debug(`Skipping! non-URL task: ${task}`);
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
const youtubeId = LinkUtils.getYouTubeId(task);
const isChannel = LinkUtils.isYouTubeChannel(task);
if (youtubeId && !isChannel) {
const canonical = LinkUtils.canonicalizeYouTubeUrl(task, youtubeId);
Logger.info(`Embedding YouTube link: ${canonical}`);
const ytTitle = await FileUtils.fetchYouTubeTitle(canonical);
const embed = FileUtils.buildYouTubeEmbed(ytTitle || "", canonical, youtubeId);
const fileName = LinkUtils.sanitizeFileName(embed.title || `YouTube_${youtubeId}`) + ".md";
let filePath = "";
if (saveToDisk) {
filePath = join(this.config.getClippingPath(), fileName);
const dir = dirname(filePath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(filePath, embed.content, "utf-8");
Logger.success(`Saved: ${filePath}`);
} else {
Logger.info(`Would save to: ${fileName}`);
}
return {
updatedLink: LinkUtils.markAsProcessed(canonical),
markdown: returnMarkdown ? embed.content : undefined,
frontmatter: embed.frontmatterObj,
body: embed.body
};
}
if (LinkUtils.isBlockedDomain(task)) {
Logger.info(`Skipping blocked domain: ${task}`);
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
if (LinkUtils.hasNonHtmlExtension(task)) {
Logger.debug(`Skipping! non-HTML file extension: ${task}`);
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
try {
Logger.debug(`Processing link (JSDOM): ${task}`);
const page: Page = await browser.newPage();
const response = await page.goto(task, { waitUntil: "domcontentloaded", timeout: 30000 });
// Check content type
const contentType = response?.headers()["content-type"] || "";
if (!contentType.includes("text/html")) {
Logger.debug(`Skipping! non-HTML content type: ${contentType} for ${task}`);
await page.close();
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
const pageData = await page.evaluate(() => ({
html: document.documentElement.outerHTML,
url: document.URL,
}));
const virtualConsole = new VirtualConsole();
const dom = new JSDOM(pageData.html, { url: pageData.url, virtualConsole });
const result = await Defuddle(dom, pageData.url, {
markdown: true,
debug: false,
});
if (!result.content) {
Logger.debug(`Extraction details: title="${result.title || 'none'}", domain="${result.domain || 'none'}", html length=${pageData.html.length}`);
throw new Error(`Failed to extract article content from ${pageData.url}`);
}
// Fix relative image paths to absolute
result.content = FileUtils.fixImagePaths(result.content, pageData.url);
const frontmatterObj = {
title: result.title || "Untitled",
source: result.domain ? `https://${result.domain}` : pageData.url,
url: pageData.url,
author: result.author || "",
published: result.published || "",
clipped: new Date().toISOString().split("T")[0],
tags: ["clippings"],
description: result.description || "",
image: result.image || "",
favicon: result.favicon || "",
};
// Filter out empty values for JSON response
const cleanedFrontmatter = Object.fromEntries(
Object.entries(frontmatterObj).filter(([_, value]) => {
if (typeof value === "string" && value === "") return false;
if (Array.isArray(value) && value.length === 0) return false;
return true;
})
);
const fileContent = this.buildFileContent(result, pageData.url);
const fileName = LinkUtils.sanitizeFileName(result.title || "Untitled") + ".md";
let filePath = "";
if (saveToDisk) {
filePath = join(this.config.getClippingPath(), fileName);
const dir = dirname(filePath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(filePath, fileContent, "utf-8");
Logger.success(`✓ Saved: ${filePath}`);
} else {
Logger.info(`Would save to: ${fileName}`);
}
await page.close();
return {
updatedLink: LinkUtils.markAsProcessed(task),
markdown: returnMarkdown ? fileContent : undefined,
frontmatter: cleanedFrontmatter,
body: result.content
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
// Detect CloudFlare challenge
if (task.includes('__cf_chl') || task.includes('cf_chl_rt') || errorMessage.toLowerCase().includes('cloudflare')) {
Logger.error(`Failed to process link: ${task}. Error: ${errorMessage}`);
Logger.warn(`Cloudflare challenge detected - site may be blocking automated access`);
} else {
Logger.error(`Failed to process link: ${task}. Error: ${errorMessage}`);
}
// Fallback to fetch/JSDOM extraction
Logger.warn(`Falling back to fetch for: ${task}`);
const fallback = await this.processSingleLinkWithFetch(link, returnMarkdown, saveToDisk, true);
return fallback;
}
}
private async processSingleLinkWithFetch(
link: string,
returnMarkdown: boolean,
saveToDisk: boolean,
allowStub: boolean = false
): Promise<{ updatedLink: string; markdown?: string; frontmatter?: any; body?: string }> {
const line = link.trim();
if (LinkUtils.isProcessed(line)) {
Logger.debug(`Skipping! already processed: ${line}`);
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
const task = LinkUtils.sanitizeLink(line);
if (!LinkUtils.isValidHttpLink(task)) {
Logger.debug(`Skipping! non-URL task: ${task}`);
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
const youtubeId = LinkUtils.getYouTubeId(task);
const isChannel = LinkUtils.isYouTubeChannel(task);
if (youtubeId && !isChannel) {
const canonical = LinkUtils.canonicalizeYouTubeUrl(task, youtubeId);
Logger.info(`Embedding YouTube link: ${canonical}`);
const ytTitle = await FileUtils.fetchYouTubeTitle(canonical);
const embed = FileUtils.buildYouTubeEmbed(ytTitle || "", canonical, youtubeId);
const fileName = LinkUtils.sanitizeFileName(embed.title || `YouTube_${youtubeId}`) + ".md";
let filePath = "";
if (saveToDisk) {
filePath = join(this.config.getClippingPath(), fileName);
const dir = dirname(filePath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(filePath, embed.content, "utf-8");
Logger.success(`Saved: ${filePath}`);
} else {
Logger.info(`Would save to: ${fileName}`);
}
return {
updatedLink: LinkUtils.markAsProcessed(canonical),
markdown: returnMarkdown ? embed.content : undefined,
frontmatter: embed.frontmatterObj,
body: embed.body
};
}
if (LinkUtils.isBlockedDomain(task)) {
Logger.info(`Skipping blocked domain: ${task}`);
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
if (LinkUtils.hasNonHtmlExtension(task)) {
Logger.debug(`Skipping! non-HTML file extension: ${task}`);
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
try {
Logger.debug(`Processing link (JSDOM): ${task}`);
const response = await fetch(task);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
// Check content type
const contentType = response.headers.get("content-type") || "";
if (!contentType.includes("text/html")) {
Logger.debug(`Skipping! non-HTML content type: ${contentType} for ${task}`);
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
const html = await response.text();
// Parse with Defuddle (used by official obsidian-clipper)
const virtualConsole = new VirtualConsole();
const dom = new JSDOM(html, { url: task, virtualConsole });
const result = await Defuddle(dom, task, {
markdown: true,
debug: false,
});
if (!result.content) {
Logger.debug(`Extraction details: title="${result.title || 'none'}", domain="${result.domain || 'none'}", html length=${html.length}`);
throw new Error(`Failed to extract article content from ${task}`);
}
// Fix relative image paths to absolute
result.content = FileUtils.fixImagePaths(result.content, task);
const frontmatterObj = {
title: result.title || "Untitled",
source: result.domain ? `https://${result.domain}` : task,
url: task,
author: result.author || "",
published: result.published || "",
clipped: new Date().toISOString().split("T")[0],
tags: ["clippings"],
description: result.description || "",
image: result.image || "",
favicon: result.favicon || "",
};
// Filter out empty values for JSON response
const cleanedFrontmatter = Object.fromEntries(
Object.entries(frontmatterObj).filter(([_, value]) => {
if (typeof value === "string" && value === "") return false;
if (Array.isArray(value) && value.length === 0) return false;
return true;
})
);
const fileContent = this.buildFileContent(result, task);
const fileName = LinkUtils.sanitizeFileName(result.title || "Untitled") + ".md";
let filePath = "";
if (saveToDisk) {
filePath = join(this.config.getClippingPath(), fileName);
const dir = dirname(filePath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(filePath, fileContent, "utf-8");
Logger.success(`✓ Saved: ${filePath}`);
} else {
Logger.info(`Would save to: ${fileName}`);
}
return {
updatedLink: LinkUtils.markAsProcessed(task),
markdown: returnMarkdown ? fileContent : undefined,
frontmatter: cleanedFrontmatter,
body: result.content
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
// Detect CloudFlare challenge
if (task.includes('__cf_chl') || task.includes('cf_chl_rt') || errorMessage.toLowerCase().includes('cloudflare')) {
Logger.error(`Failed to process link: ${task}. Error: ${errorMessage}`);
Logger.warn(`Cloudflare challenge detected - site may be blocking automated access`);
} else {
Logger.error(`Failed to process link: ${task}. Error: ${errorMessage}`);
}
if (allowStub) {
const stub = FileUtils.buildStubMarkdown("", task);
if (saveToDisk) {
const fileName = LinkUtils.sanitizeFileName("Link_Fallback") + ".md";
const filePath = join(this.config.getClippingPath(), fileName);
const dir = dirname(filePath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(filePath, stub, "utf-8");
Logger.warn(`Saved fallback stub: ${filePath}`);
}
return {
updatedLink: LinkUtils.markAsProcessed(task),
markdown: returnMarkdown ? stub : undefined,
frontmatter: {},
body: stub
};
}
return { updatedLink: line, markdown: returnMarkdown ? "" : undefined };
}
}
private buildFileContent(result: any, url: string): string {
return FileUtils.buildMarkdownContent(
result.title,
url,
result.domain,
result.author,
result.published,
result.description,
result.image,
result.favicon,
result.content
);
}
async processLinks(
links: string[],
returnFormat?: ReturnFormat,
parser?: Parser,
saveToDisk?: boolean,
onLinkProcessed?: (index: number, updatedLink: string) => void
): Promise<ProcessResult | string[] | any[]> {
const finalReturnFormat = returnFormat || this.config.returnFormat;
const finalParser = parser || this.config.parser;
const finalSaveToDisk = saveToDisk !== undefined ? saveToDisk : this.config.saveToDisk;
const total = links.length;
Logger.info(`Processing ${total} link(s) [parser=${finalParser}, format=${finalReturnFormat}, save=${finalSaveToDisk ? "yes" : "no"}]`);
if (finalSaveToDisk) {
Logger.debug(`Clippings path: ${this.config.getClippingPath()}`);
}
const updatedLinks: string[] = [];
const markdownResults: string[] = [];
const jsonResults: any[] = [];
const returnMarkdown = finalReturnFormat === "md";
let processedCount = 0;
if (finalParser === "puppeteer") {
const pending: Array<{ link: string; idx: number; task: string }> = [];
for (const link of links) {
const line = link.trim();
const task = LinkUtils.sanitizeLink(line);
const idx = ++processedCount;
if (LinkUtils.isProcessed(line)) {
if (total > 1) {
Logger.info(`${idx}/${total} Already checked off`);
} else {
Logger.info(`Already checked off`);
}
updatedLinks.push(line);
if (onLinkProcessed) onLinkProcessed(updatedLinks.length - 1, line);
continue;
}
const youtubeId = LinkUtils.getYouTubeId(task);
const isChannel = LinkUtils.isYouTubeChannel(task);
if (youtubeId && !isChannel) {
const canonical = LinkUtils.canonicalizeYouTubeUrl(task, youtubeId);
if (total > 1) {
Logger.info(`${idx}/${total} Embedding YouTube: ${canonical}`);
} else {
Logger.info(`Embedding YouTube: ${canonical}`);
}
const ytTitle = await FileUtils.fetchYouTubeTitle(canonical);
const embed = FileUtils.buildYouTubeEmbed(ytTitle || "", canonical, youtubeId);
const fileName = LinkUtils.sanitizeFileName(embed.title || `YouTube_${youtubeId}`) + ".md";
if (finalSaveToDisk) {
const filePath = join(this.config.getClippingPath(), fileName);
const dir = dirname(filePath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(filePath, embed.content, "utf-8");
Logger.success(`✓ Saved: ${filePath}`);
} else {
Logger.debug(`Would save to: ${fileName}`);
}
updatedLinks.push(LinkUtils.markAsProcessed(canonical));
if (onLinkProcessed) onLinkProcessed(updatedLinks.length - 1, updatedLinks[updatedLinks.length - 1]);
if (returnMarkdown) {
markdownResults.push(embed.content);
} else {
jsonResults.push({ url: link, frontmatter: embed.frontmatterObj, body: embed.body });
}
continue;
}
if (!LinkUtils.isValidHttpLink(task)) {
Logger.debug(`${idx}/${total} Skipping non-URL: ${task}`);
updatedLinks.push(line);
if (onLinkProcessed) onLinkProcessed(updatedLinks.length - 1, line);
continue;
}
if (LinkUtils.isBlockedDomain(task)) {
Logger.debug(`${idx}/${total} Skipping blocked domain: ${task}`);
updatedLinks.push(line);
if (onLinkProcessed) onLinkProcessed(updatedLinks.length - 1, line);
continue;
}
if (LinkUtils.hasNonHtmlExtension(task)) {
Logger.debug(`${idx}/${total} Skipping non-HTML: ${task}`);
updatedLinks.push(line);
if (onLinkProcessed) onLinkProcessed(updatedLinks.length - 1, line);
continue;
}
pending.push({ link, idx, task });
}
if (pending.length > 0) {
const browser = await this.initializePuppeteer();
try {
for (const item of pending) {
const result = await this.processSingleLinkWithPuppeteer(item.link, browser, returnMarkdown, finalSaveToDisk);
if (total > 1) {
Logger.info(`${item.idx}/${total} Processed: ${item.task}`);
} else {
Logger.info(`Processed: ${item.task}`);
}
updatedLinks.push(result.updatedLink);
if (onLinkProcessed) onLinkProcessed(updatedLinks.length - 1, result.updatedLink);
if (returnMarkdown && result.markdown !== undefined) {
markdownResults.push(result.markdown);
}
if (!returnMarkdown && result.frontmatter && result.body) {
jsonResults.push({
url: item.link,
frontmatter: result.frontmatter,
body: result.body
});
}
}
} finally {
if (this.browser) {
await this.browser.close();
this.browser = null;
}
}
} else {
Logger.debug("No eligible links for Puppeteer; skipping browser startup.");
}
Logger.info("All links processed.");
} else {
// Use JSDOM mode
for (const link of links) {
const line = link.trim();
const task = LinkUtils.sanitizeLink(line);
const idx = ++processedCount;
if (total > 1) {
Logger.info(`${idx}/${total} Processing: ${task}`);
} else {
Logger.info(`Processing: ${task}`);
}
const result = await this.processSingleLinkWithFetch(link, returnMarkdown, finalSaveToDisk);
updatedLinks.push(result.updatedLink);
if (onLinkProcessed) onLinkProcessed(updatedLinks.length - 1, result.updatedLink);
if (returnMarkdown && result.markdown !== undefined) {
markdownResults.push(result.markdown);
}
if (!returnMarkdown && result.frontmatter && result.body) {
jsonResults.push({
url: link,
frontmatter: result.frontmatter,
body: result.body
});
}
}
Logger.info("All links processed.");
}
return returnMarkdown
? { updatedLinks, markdown: markdownResults }
: jsonResults.length > 0 ? jsonResults : updatedLinks;
}
}