lizard-heart_obsidian-to-om.../src/main.ts

98 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-02-19 21:20:12 +00:00
import {
MarkdownView,
Plugin,
2023-03-18 19:29:55 +00:00
Editor,
2023-02-19 21:20:12 +00:00
} from "obsidian";
import {
2023-03-24 16:15:16 +00:00
TasksToOmnifocusSettings,
2023-02-19 21:20:12 +00:00
DEFAULT_SETTINGS,
2023-03-24 16:15:16 +00:00
TasksToOmnifocusSettingTab,
2023-02-19 21:20:12 +00:00
} from "./settings";
2023-03-24 16:15:16 +00:00
export default class TasksToOmnifocus extends Plugin {
settings: TasksToOmnifocusSettings;
2023-02-19 21:20:12 +00:00
async onload() {
await this.loadSettings();
2023-03-18 19:29:55 +00:00
2023-02-19 21:20:12 +00:00
this.addCommand({
2023-03-18 19:29:55 +00:00
id: 'extract-tasks',
name: 'Extract Tasks Into OmniFocus',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.addToOmnifocus(false, editor, view);
2023-03-26 14:17:08 +00:00
},
});
2023-03-26 14:17:08 +00:00
this.addCommand({
id: 'extract-tasks-selection',
name: 'Extract Tasks from selection Into OmniFocus',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.addToOmnifocus(true, editor, view);
2023-03-18 19:29:55 +00:00
},
2023-02-19 21:20:12 +00:00
});
2023-03-24 16:15:16 +00:00
this.addSettingTab(new TasksToOmnifocusSettingTab(this.app, this));
2023-02-19 21:20:12 +00:00
}
2024-04-02 15:25:38 +00:00
async addToOmnifocus(isSelection: boolean, editor: Editor, view: MarkdownView) {
2024-07-13 06:22:23 +00:00
let editorText;
if (isSelection) {
editorText = editor.getSelection();
} else {
editorText = editor.getValue();
}
2023-03-26 18:20:53 +00:00
try {
const tasks = editorText.match(/[-*] \[ \] .*/g);
2023-03-26 18:20:53 +00:00
2024-07-13 06:22:23 +00:00
for (const task of tasks) {
let taskName = task.replace(/[-*] \[ \] /, "");
// check if taskName has "//" followed by a date, and if so extract the date for later use and remove it from taskName
const dateMatch = taskName.match(/(\/\/\s*)(\d{4}-\d{2}-\d{2})/);
let taskDate = "";
if (dateMatch) {
taskDate = dateMatch[2];
taskName = taskName.replace(dateMatch[0], "");
console.log(`Setting taskDate to ${taskDate}`);
}
2024-07-13 06:22:23 +00:00
const taskNameEncoded = encodeURIComponent(taskName);
const noteURL = view.file.path.replace(/ /g, "%20").replace(/\//g, "%2F");
const vaultName = app.vault.getName().replace(/\s/g, "%20");
const taskNoteEncoded = encodeURIComponent("obsidian://open?=" + vaultName + "&file=" + noteURL);
2023-03-26 18:20:53 +00:00
window.open(
`omnifocus:///add?name=${taskNameEncoded}&note=${taskNoteEncoded}&due=${taskDate}`
2023-03-26 18:20:53 +00:00
);
}
if (this.settings.markComplete) {
const completedText = editorText.replace(/([-*]) \[ \]/g, "$1 [x]");
if (isSelection) {
editor.replaceSelection(completedText);
} else {
editor.setValue(completedText)
}
2023-03-26 18:20:53 +00:00
}
} catch (err) {
2024-07-13 06:22:23 +00:00
console.error('Error extracting tasks', err);
2023-03-26 18:20:53 +00:00
}
}
2023-02-19 21:20:12 +00:00
async saveSettings() {
await this.saveData(this.settings);
}
onunload() { }
2023-02-19 21:20:12 +00:00
private async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
}