Created
January 16, 2023 03:40
-
-
Save mnaoumov/99b43f38a5e55ab2a56df9aaef6eb58d to your computer and use it in GitHub Desktop.
Obsidian LeetCode replace urls with problem notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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