Skip to content

Instantly share code, notes, and snippets.

@andreiRS
Last active April 14, 2023 09:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreiRS/2310d5e84f4b049d6079ded7488f61f2 to your computer and use it in GitHub Desktop.
Save andreiRS/2310d5e84f4b049d6079ded7488f61f2 to your computer and use it in GitHub Desktop.
Extended the original zettelizer script to generate the zettels inside a folder with the same name as the source document. If you have also the plugin Zoottelkeeper installed, it will generate also an index file with the new notes. Example outcome: https://forum.obsidian.md/t/quickadd-plugin/20032/31
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 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 = `${currentFile.parent.path}/${currentFile.basename}`;
console.log("Create a new folder: ", folder);
if (!(await params.app.vault.adapter.exists(folder)))
await params.app.vault.createFolder(folder);
if (!params.app.vault.adapter.exists(folder)) {
new Notice(`Could not find folder ${folder}`);
return;
}
console.log("Folder does exist: ", folder);
headingsInFile.forEach(async heading => {
console.log(`Checking ${heading.heading}. It is level ${heading.level}`);
if (heading.level === 3) {
const splitHeading = heading.heading.split(" ");
const location = splitHeading[0].trim();
const text = splitHeading.length > 1 ? [...splitHeading.slice(1)].join(' ').trim() : "";
const path = `${folder}/${text.replace(/[\\,#%&\{\}\/*<>$\'\":@]*/g, '')}.md`;
const content = `![[${currentFile.basename}#${location}${text ? " " + text : ""}]]`;
console.log(`Path: ${path}.\nContent: ${content}`);
if (text && !(await params.app.vault.adapter.exists(path)))
await params.app.vault.create(path, content);
else if (text)
new Notice(`File ${path} already exists.`, 5000);
}
});
console.log("Finished!");
}
@lwb52
Copy link

lwb52 commented Oct 4, 2021

as a ·js novice, where does this script go, & how do i invoke it??

@andreiRS
Copy link
Author

andreiRS commented Oct 6, 2021

Hi @lwb52! You can find the original setup guide from @chhoumann documented here: https://github.com/chhoumann/quickadd/blob/master/docs/Examples/Macro_Zettelizer.md

@andreiRS
Copy link
Author

andreiRS commented Oct 6, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment