Skip to content

Commit

Permalink
feat: add daily notes support
Browse files Browse the repository at this point in the history
close #2
  • Loading branch information
Vinzent03 committed Apr 2, 2021
1 parent 3743890 commit c2333fe
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 33 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@ Ensure that your values are properly URI encoded. For example, forward slash cha

This is especially important because an improperly encoded "reserved" character may break the interpretation of the URI. [See here for details](https://en.wikipedia.org/wiki/Percent-encoding)

## Writing
| / | parameters | explanation |
| --------------- | ----------------------------- | ------------------------------------------------------------------- |
| write | filepath, data | Only writes `data` to `filepath` if the file is not already present |
| overwrite | filepath, data, mode=overwrite| Writes `data` to `filepath` even if the file already exists |
| append | filepath, data, mode=append | Only appends `data` to `filepath` |
| prepend | filepath, data, mode=prepend | Only prepends `data` to `filepath` |
| workspace | workspace | Opens the workspace called `workspace` |
| heading | filepath, heading | Opens the `heading` in `filepath` |
| block reference | filepath, block | Opens the `block` in `filepath` |

## Examples
## Navigation
| / | parameters | explanation |
| --------------- | ----------------- | -------------------------------------- |
| workspace | workspace | Opens the workspace called `workspace` |
| heading | filepath, heading | Opens the `heading` in `filepath` |
| block reference | filepath, block | Opens the `block` in `filepath` |

## Daily notes
| / | parameters | explanation |
| --------- | -------------------------------- | -------------------------------------------------------------------------------------------------------- |
| write | daily=true, data | Only writes `data` to today's daily note if the note does not already exists |
| overwrite | daily=true, data, mode=overwrite | Writes `data` to today's daily note even if the file already exists |
| append | daily=true, data, mode=append | Only appends `data` to today's daily note. The file will be created, if the file does not already exist |
| prepend | daily=true, data, mode=prepend | Only prepends `data` to today's daily note. The file will be created, if the file does not already exist |

# Examples

**Write** "Hello World" to "my-file.md":
`obsidian://advanced-uri?vault=<your-vault>&filepath=my-file&data=Hello%20World`
Expand All @@ -34,7 +47,8 @@ Open **heading** "Goal" in "my-file.md" (**Important:** Without syntax, only `Go
Open **block**-id "12345" in "my-file.md" (**Important:** Without syntax, only `12345`):
`obsidian://advanced-uri?vault=<your-vault>&filepath=my-file&block=12345`


**Append** "Hello World" to today's **daily note**.
`obsidian://advanced-uri?vault=<your-vault>&daily=true&data=Hello%20World&mode=append`

## Compatibility
Custom plugins are only available for Obsidian v0.9.7+.
Expand Down
105 changes: 79 additions & 26 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { App, normalizePath, Notice, Plugin, PluginSettingTab, Setting, TFile } from "obsidian";
import { appHasDailyNotesPluginLoaded, createDailyNote, getAllDailyNotes, getDailyNote } from "obsidian-daily-notes-interface";

const DEFAULT_SETTINGS: AdvancedURISettings = {
openFileOnWrite: true
Expand All @@ -10,6 +11,7 @@ interface AdvancedURISettings {
interface Parameters {
workspace: string;
filepath: string;
daily: "true";
data: string;
mode: "overwrite" | "append" | "prepend";
heading: string;
Expand All @@ -27,58 +29,109 @@ export default class AdvancedURI extends Plugin {
const parameters = e as unknown as Parameters;

if (parameters.workspace) {
const workspaces = (this.app as any)?.internalPlugins?.plugins?.workspaces;
if (!workspaces) {
new Notice("Cannot find Workspaces plugin. Please file an issue.");
}
else if (workspaces.enabled) {
workspaces.instance.loadWorkspace(parameters.workspace);
} else {
new Notice("Workspaces plugin is not enabled");
}
}
else if (parameters.filepath && parameters.data) {
this.handleWorkspace(parameters.workspace);

} else if (parameters.filepath && parameters.data) {
this.handleWrite(parameters);

} else if (parameters.daily === "true" && parameters.data) {
this.handleDailyNote(parameters);

} else if (parameters.filepath && parameters.heading) {
this.app.workspace.openLinkText(parameters.filepath + "#" + parameters.heading, "");

} else if (parameters.filepath && parameters.block) {
this.app.workspace.openLinkText(parameters.filepath + "#^" + parameters.block, "");
}
});
}

handleWorkspace(workspace: string) {
const workspaces = (this.app as any)?.internalPlugins?.plugins?.workspaces;
if (!workspaces) {
new Notice("Cannot find Workspaces plugin. Please file an issue.");

} else if (workspaces.enabled) {
workspaces.instance.loadWorkspace(workspace);

} else {
new Notice("Workspaces plugin is not enabled");
}
}

async handleWrite(parameters: Parameters) {
let path = normalizePath(parameters.filepath);
if (!path.contains(".")) {
if (!path.endsWith(".md")) {
path = path + ".md";
}
const file = this.app.vault.getAbstractFileByPath(path);

if (parameters.mode === "overwrite") {
this.writeAndOpenFile(path, parameters.data);
}
else if (parameters.mode === "prepend") {

} else if (parameters.mode === "prepend") {
if (file instanceof TFile) {
const fileData = await this.app.vault.read(file);
const dataToWrite = parameters.data + "\n" + fileData;
this.writeAndOpenFile(path, dataToWrite);
this.prepend(file, parameters.data);
}
}
else if (parameters.mode === "append") {

} else if (parameters.mode === "append") {
if (file instanceof TFile) {
const fileData = await this.app.vault.read(file);
const dataToWrite = fileData + "\n" + parameters.data;
this.writeAndOpenFile(path, dataToWrite);
this.append(file, parameters.data);
}
}
else if (file instanceof TFile) {

} else if (file instanceof TFile) {
new Notice("File already exists");
}
else {

} else {
this.writeAndOpenFile(path, parameters.data);
}
}

async handleDailyNote(parameters: Parameters) {
if (!appHasDailyNotesPluginLoaded()) {
new Notice("Daily notes plugin is not loaded");
return;
}
const moment = (window as any).moment(Date.now());
const allDailyNotes = getAllDailyNotes();
let dailyNote = getDailyNote(moment, allDailyNotes);
if (parameters.mode === "overwrite") {
this.app.vault.adapter.write(dailyNote.path, parameters.data);

} else if (parameters.mode === "prepend") {
if (!dailyNote) {
dailyNote = await createDailyNote(moment);
}
this.prepend(dailyNote, parameters.data);

} else if (parameters.mode === "append") {
if (!dailyNote) {
dailyNote = await createDailyNote(moment);
}
this.append(dailyNote, parameters.data);

} else if (dailyNote) {
new Notice("File already exists");

} else {
dailyNote = await createDailyNote(moment);
this.writeAndOpenFile(dailyNote.path, parameters.data);
}

}

async append(file: TFile, data: string) {
const fileData = await this.app.vault.read(file);
const dataToWrite = fileData + "\n" + data;
this.writeAndOpenFile(file.path, dataToWrite);
}

async prepend(file: TFile, data: string) {
const fileData = await this.app.vault.read(file);
const dataToWrite = data + "\n" + fileData;
this.writeAndOpenFile(file.path, dataToWrite);
}

async writeAndOpenFile(outputFileName: string, text: string) {
await this.app.vault.adapter.write(outputFileName, text);
if (this.settings.openFileOnWrite) {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
"prettier": "2.1.2",
"rollup": "^2.32.1",
"standard-version": "^9.0.0",
"tslib": "^2.0.3",
"typescript": "^4.0.3",
"standard-version": "^9.0.0"
"typescript": "^4.0.3"
},
"dependencies": {
"obsidian-daily-notes-interface": "^0.9.0"
}
}

0 comments on commit c2333fe

Please sign in to comment.