Skip to content

Commit

Permalink
Zettelizer docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jul 13, 2021
1 parent f8cb730 commit 267be76
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Take a look at some examples...
- [Capture: Add a task to a Kanban board](docs/Examples/Capture_AddTaskToKanbanBoard.md)
- [Macro: Easily change properties in your daily note (requires MetaEdit)](docs/Examples/Macro_ChangePropertyInDailyNotes.md)
- [Capture: Fetch tasks from Todoist and capture to a file](docs/Examples/Capture_FetchTasksFromTodoist.md)
- [Macro: Zettelizer - easily create new notes from headings while keeping the contents in the file](docs/Examples/Macro_Zettelizer.md)

#### Create powerful scripts and macros to automate my workflow
Take a look at the [QuickAdd API](docs/QuickAddAPI.md), [format syntax](docs/FormatSyntax.md), [inline scripts](docs/InlineScripts.md), and [macros](docs/Choices/MacroChoice.md).
Expand Down
47 changes: 47 additions & 0 deletions docs/Examples/Attachments/zettelizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 = "40 Slipbox/44 Zettels";
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!");
}
19 changes: 19 additions & 0 deletions docs/Examples/Macro_Zettelizer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Zettelizer
![Zettelizer Demo](../Images/zettelizer_demo.gif)

You can get the `.js` file for this userscript [here](./Attachments/zettelizer.js).
To install it, you can follow the same process as in the [fetch tasks from Todoist example - with video](./Capture_FetchTasksFromTodoist.md).

## Setup
You will need to define the folder you want the script to place the new notes in.

This can be done on line 19, where it says ``const folder = "..."``. Change the text inside the `""` to match the desired folder path.

Currently, the script _only_ looks for level 3 headers. This means headers with three pound symbols, like so ``### header``.

You can freely change this. On line 29 it says ``if (heading.level === 3)``. You can change this to any other number, denoting the heading level desired. You can also, rather than checking for equality (`===`), check for other conditions, such as `heading.level >= 1`, which denotes headers of level 1 or greater.

The script looks for headers in your active file with the desired level.
If such a header is found, it will ignore the first 'word' (any sequence of characters - i.e., letters, numbers, symbols, etc - followed by a space). Then, it will create a file with a name containing the remaining text in the heading.

In that file, it will link to the heading it created the file from.
Binary file added docs/Images/zettelizer_demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 267be76

Please sign in to comment.