Created
November 29, 2022 08:42
-
-
Save ransurf/fd18f1c0c685ea31d40b563a9e531dfb to your computer and use it in GitHub Desktop.
Eleanor's Zettelizer script but modified for my workflow
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
module.exports = async (params) => { | |
console.log("Starting...") | |
console.log(params); | |
const currentFile = params.app.workspace.getActiveFile(); | |
if (!currentFile) { | |
new Notice("No active file."); | |
return; | |
} | |
console.log("Found active file: ", currentFile.basename); | |
const currentFileContent = await params.app.vault.cachedRead(currentFile); | |
const currentFileCache = params.app.metadataCache.getFileCache(currentFile); | |
const headingsInFile = currentFileCache.headings; | |
if (!headingsInFile) { | |
new Notice(`No headers in file ${currentFile.name}`); | |
return; | |
} | |
console.log("Found headings in active file: ", headingsInFile); | |
const folder = ""; | |
if (!params.app.vault.adapter.exists(folder)) { | |
new Notice(`Could not find folder ${folder}`); | |
return; | |
} | |
console.log("Folder does exist: ", folder); | |
let addedFiles = []; | |
let updatedFileContent = [] | |
const indicesToRemove = []; | |
for (let i = 0; i < headingsInFile.length; i++) { | |
const heading = headingsInFile[i]; | |
console.log(`Checking ${heading.heading}. It is level ${heading.level}`); | |
if (heading.level === 4) { | |
const splitHeading = heading.heading.split(" "); | |
console.log('splitHeading', splitHeading); | |
const location = splitHeading[0].trim(); | |
const text = splitHeading.length > 1 ? splitHeading.join(' ').trim().replace(/[\\,#%&\{\}\/*<>$\'\":@]*/g, '') : ""; | |
console.log('text', text); | |
const headingStart = heading.position.start.line | |
const headingEnd = headingsInFile[i+1] ? headingsInFile[i+1].position.end.line - 1 : Infinity | |
const path = `${folder}/${text}.md`; | |
let content = `Status::\nTags::\nLinks:: <%tp.file.cursor(2)%>\n___\n# ${text}\n![[${currentFile.basename}#${text ? " " + text : ""}]]\n___\nCreated:: ${new Date(Date.now()).toISOString().split('T')[0]}`; | |
const tasksToMove = [] | |
currentFileContent.split("\n").forEach((line, index) => { | |
if (index >= headingStart && index <= headingEnd) { | |
if (line.startsWith(`- [n]`) || line.startsWith(`- [?]`)) { | |
indicesToRemove.push(index) | |
tasksToMove.push(line.trim()); | |
} | |
} | |
}); | |
content = content + "\n" + tasksToMove.join("\n") + "\n"; | |
console.log(`Path: ${path}.\nContent: ${content}`); | |
if (text && !(await params.app.vault.adapter.exists(path))) { | |
const file = await params.app.vault.create(path, content); | |
addedFiles = [...addedFiles, file]; | |
} | |
else if (text) | |
new Notice(`File ${path} already exists.`, 5000); | |
} | |
} | |
if (addedFiles.length > 0) { | |
const formattedAddedFiles = addedFiles.map(f => `[[${f.basename}]]`); | |
{ | |
const addedFilesBlock = `- ${formattedAddedFiles.join("\n - ")}`; | |
currentFileContent.split("\n").forEach((line, index) => { | |
if (indicesToRemove.includes(index)) { | |
return; | |
} | |
if (line === "## Notes") { | |
console.log('updatedFileContent', updatedFileContent) | |
updatedFileContent.splice(index, 0, addedFilesBlock) | |
console.log('updatedFileContent 2', updatedFileContent) | |
} | |
updatedFileContent.push(line); | |
}); | |
} | |
await params.app.vault.modify(currentFile, updatedFileContent.join("\n")); | |
} | |
console.log("Finished!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment