import "@johnlindquist/kit"
interface Link {
url: string;
referencingFile: string[];
}
const droppedFiles = await drop();if (!droppedFiles || droppedFiles.length === 0) {
await div("No files dropped.");
exit();
}
const allLinks: { [url: string]: Link } = {};
for (const file of droppedFiles) {
if (typeof file === 'string') continue;
try {
const content = await readFile(file.path, "utf-8");
const regex = /\[.*?\]\((https?:\/\/[^\s]+)\)/g;
let match;
while ((match = regex.exec(content)) !== null) {
const url = match[1];
if (allLinks[url]) {
if (!allLinks[url].referencingFile.includes(file.name)) {
allLinks[url].referencingFile.push(file.name);
}
} else {
allLinks[url] = { url, referencingFile: [file.name] };
}
}
} catch (error) {
console.error(`Error processing file ${file.path}:`, error);
await div(md(`Error processing file ${file.name}. Check console for details.`))
}
}
const allLinksArray = Object.values(allLinks);
try {
await writeFile(kenvPath("all-links.json"), JSON.stringify(allLinksArray, null, 2));
await div(md(`Links extracted to \`all-links.json\``));
} catch (error) {
console.error("Error writing to all-links.json:", error);
await div(md(`Error saving links to \`all-links.json\`. Check console for details.`))
}