Skip to content

Instantly share code, notes, and snippets.

@NatKarmios
Last active September 16, 2024 21:16
Show Gist options
  • Select an option

  • Save NatKarmios/b93f7b3f7f5d046f33f4c0f671c78fa2 to your computer and use it in GitHub Desktop.

Select an option

Save NatKarmios/b93f7b3f7f5d046f33f4c0f671c78fa2 to your computer and use it in GitHub Desktop.
Obsidian: redirect to daily note (for workspaces)

Redirecting to daily note…

throw new Error('testing mode!');  // Comment this out
const vault = app.vault;
const name = dv.current().file.name;
const swapToDaily = (tries) => () => {
    const leaves = [];
    for (const leaf of app.workspace.getLeavesOfType('markdown')) {
        if (leaf.getDisplayText() === name) {
            leaves.push(leaf);
        }
    }
    if (leaves.length === 0) {
        if (tries > 0) {
            window.setTimeout(swapToDaily(tries-1), 100);
        } else {
            dv.paragraph('Couldn\'t swap to daily note!');
        }
    } else {
        // MAKE SURE THIS MATCHES YOUR DAILY NOTE SETUP!
        const path = `06 - Periodic/Daily/${moment().format("YYYY-MM-DD")}.md`;
        if (!vault.getFileByPath(path)) vault.create(path, "");
        for (let leaf of leaves) {
          leaf.openFile(vault.getFileByPath(path), { active: false });
          leaf.setPinned(true);
        }
    }
};

window.setTimeout(swapToDaily(20), 100);
## *Redirecting to daily note…*
```dataviewjs
throw new Error('testing mode!'); // Comment this out
const vault = app.vault;
const name = dv.current().file.name;
const swapToDaily = (tries) => () => {
const leaves = [];
for (const leaf of app.workspace.getLeavesOfType('markdown')) {
if (leaf.getDisplayText() === name) {
leaves.push(leaf);
}
}
if (leaves.length === 0) {
if (tries > 0) {
window.setTimeout(swapToDaily(tries-1), 100);
} else {
dv.paragraph('Couldn\'t swap to daily note!');
}
} else {
// MAKE SURE THIS MATCHES YOUR DAILY NOTE SETUP!
const path = `06 - Periodic/Daily/${moment().format("YYYY-MM-DD")}.md`;
if (!vault.getFileByPath(path)) vault.create(path, "");
for (let leaf of leaves) {
leaf.openFile(vault.getFileByPath(path), { active: false });
leaf.setPinned(true);
}
}
};
window.setTimeout(swapToDaily(20), 100);
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment