import fs from "fs/promises"; import path from "path"; const vault = "./vault"; async function walk(dir) { const entries = await fs.readdir(dir, { withFileTypes: true }); for (const entry of entries) { const full = path.join(dir, entry.name); if (entry.isDirectory()) { await walk(full); continue; } if (!entry.name.endsWith(".md")) continue; let md = await fs.readFile(full, "utf8"); // Images / embeds md = md.replace( /!\[\[([^|\]]+)(?:\|([^\]]+))?\]\]/g, (_, target, alt) => { const ext = path.extname(target).toLowerCase(); if ([".png",".jpg",".jpeg",".gif",".webp",".svg",".avif"].includes(ext)) return `![${alt ?? ""}](/${target})`; if ([".mp4",".webm"].includes(ext)) return ``; if ([".mp3",".wav",".ogg"].includes(ext)) return ``; return `[${alt ?? target}](/${target})`; } ); // Wikilinks md = md.replace( /\[\[([^|\]]+)(?:\|([^\]]+))?\]\]/g, (_, target, title) => { const text = title ?? path.basename(target); return `[${text}](/${target})`; } ); // Remove block ids md = md.replace(/\s\^[A-Za-z0-9-]+$/gm, ""); // Remove Obsidian comments md = md.replace(/%%[\s\S]*?%%/g, ""); await fs.writeFile(full, md); console.log(full); } } walk(vault);