Building phase one
This commit is contained in:
61
converter/convert.js
Normal file
61
converter/convert.js
Normal file
@@ -0,0 +1,61 @@
|
||||
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 ``;
|
||||
|
||||
if ([".mp4",".webm"].includes(ext))
|
||||
return `<video controls src="/${target}"></video>`;
|
||||
|
||||
if ([".mp3",".wav",".ogg"].includes(ext))
|
||||
return `<audio controls src="/${target}"></audio>`;
|
||||
|
||||
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);
|
||||
Reference in New Issue
Block a user