Skip to content

Instantly share code, notes, and snippets.

@mnaoumov
Created January 16, 2023 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnaoumov/99b43f38a5e55ab2a56df9aaef6eb58d to your computer and use it in GitHub Desktop.
Save mnaoumov/99b43f38a5e55ab2a56df9aaef6eb58d to your computer and use it in GitHub Desktop.
Obsidian LeetCode replace urls with problem notes
(async () => {
debugger;
const mdFiles = app.vault.getMarkdownFiles().filter(f => f.path.startsWith('Programming/LeetCode/'));
const problemFiles = mdFiles.filter(f => f.path.startsWith('Programming/LeetCode/Problems/'));
const urlNoteMap = new Map();
for (const problemFile of problemFiles) {
const note = await app.vault.read(problemFile);
const match = note.match('source: (.+)');
if (!match) {
continue;
}
const url = match[1];
urlNoteMap.set(url, problemFile);
}
for (const mdFile of mdFiles) {
console.log(mdFile.path);
await app.vault.process(mdFile, note => {
const newNote = note.replaceAll(/\((https:\/\/leetcode.com\/problems\/.+?)\)/g, function(_, url) {
const problemFile = urlNoteMap.get(url);
if (!problemFile) {
return `(${url})`;
}
return app.fileManager.generateMarkdownLink(problemFile, mdFile.path, '' , 'x').substring('[x]'.length);
});
if (note !== newNote){
console.log('CHANGED');
} else {
console.log('NOT CHANGED');
}
return newNote;
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment