From 1821556d6c82dd393ca62547aaa49f33febfe78e Mon Sep 17 00:00:00 2001 From: lizard-heart Date: Sun, 19 Feb 2023 15:33:13 -0800 Subject: [PATCH] Initial commit --- README.md | 3 +- manifest.json | 6 +- src/main.ts | 128 +++---------- src/settings.ts | 479 +----------------------------------------------- 4 files changed, 37 insertions(+), 579 deletions(-) diff --git a/README.md b/README.md index 7504eb6..7cfcc02 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# Obsidian Admonition to Quarto +# Obsidian to OmniFocus +This plugin will extract tasks from the current note open in Obsidian and create them in OmniFocus. \ No newline at end of file diff --git a/manifest.json b/manifest.json index 1017181..139dc0d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,9 @@ { - "id": "admonition-to-quarto", - "name": "Convert Between Admonition and Quarto", + "id": "obsidian-to-omnifocus", + "name": "Obsidian to OmniFocus", "version": "1.0.4", "minAppVersion": "0.12.0", - "description": "An Obsidian plugin to convert your admonition-style callouts to Quarto-style callouts and vice versa.", + "description": "An Obsidian plugin will extract tasks from the current note and create them in OmniFocus.", "author": "Henry Gustafson", "authorUrl": "https://lizard-heart.github.io", "isDesktopOnly": false diff --git a/src/main.ts b/src/main.ts index aa6e132..1cc5c0b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,7 @@ import { Notice, Plugin, TFile, + Vault, } from "obsidian"; import { @@ -14,29 +15,17 @@ import { } from "./settings"; - - export default class ListModified extends Plugin { settings: ListModifiedSettings; async onload() { await this.loadSettings(); this.addCommand({ - id: "admonition-to-quarto", - name: "Admonition to Quarto", - callback: () => this.createAndPush(), + id: "extract-tasks", + name: "Extract Tasks Into OmniFocus", + callback: () => this.sendToOF(), }); - this.addCommand({ - id: "quarto-to-admonition", - name: "Quarto to Admonition", - callback: () => this.createAndPush2(), - }); - - // this.registerEvent( - // this.app.metadataCache.on("changed", this.automaticPush) - // ); - this.addSettingTab(new ListModifiedSettingTab(this.app, this)); } @@ -46,111 +35,40 @@ export default class ListModified extends Plugin { onunload() {} - async createAndPush2() { - // const app = window.app as App; - const view = this.app.workspace.getActiveViewOfType(MarkdownView); - if (view != null) { - const editor = view.editor; - const editorText = editor.getValue(); - // const indicatorCharacter = this.settings.indicatorCharacter - // let newContent = "" - // let inlineSetting = "" - // let newTitle = "" - // let newAlias = "" - // let shouldPrepend = this.settings.shouldPrepend - - // check if syntax to push file is present - try { - let currentTitle = view.file.path - let qmdTitle = currentTitle.split(".")[0] + ".qmd" - let newText = editorText.replace(/(:::)({.*\n)([\s\S]*?\n)(:::)/g, "```ad-note \n$3:```") - - - await this.app.vault.create(qmdTitle, newText); - - } catch (err) { - // if (this.settings.automaticPush == false) { - // new Notice(`Didn't detect correct syntax. Doing nothing`) - // } - } - } - } - // the first function that runs - async createAndPush() { + async sendToOF() { // const app = window.app as App; const view = this.app.workspace.getActiveViewOfType(MarkdownView); if (view!=null) { const editor = view.editor; const editorText = editor.getValue(); - // const indicatorCharacter = this.settings.indicatorCharacter - // let newContent = "" - // let inlineSetting = "" - // let newTitle = "" - // let newAlias = "" - // let shouldPrepend = this.settings.shouldPrepend - // check if syntax to push file is present try { - let currentTitle = view.file.path - let qmdTitle = currentTitle.split(".")[0] + ".qmd" - var newText = editorText.replace(/(```)(ad.*\n)([\s\S]*?\n)(```)/g, `::: $2\n$3:::`) - newText = newText.replace("ad-note", this.settings.adnote); - newText = newText.replace("ad-seealso", this.settings.adseealso); - newText = newText.replace("ad-abstract", this.settings.adabstract); - newText = newText.replace("ad-summary", this.settings.adsummary); - newText = newText.replace("ad-tldr", this.settings.adtldr); - newText = newText.replace("ad-info", this.settings.adinfo); - newText = newText.replace("ad-todo", this.settings.adtodo); - newText = newText.replace("ad-tip", this.settings.adtip); - newText = newText.replace("ad-hint", this.settings.adhint); - newText = newText.replace("ad-important", this.settings.adimportant); - newText = newText.replace("ad-success", this.settings.adsuccess); - newText = newText.replace("ad-check", this.settings.adcheck); - newText = newText.replace("ad-done", this.settings.addone); - newText = newText.replace("ad-question", this.settings.adquestion); - newText = newText.replace("ad-help", this.settings.adhelp); - newText = newText.replace("ad-faq", this.settings.adfaq); - newText = newText.replace("ad-warning", this.settings.adwarning); - newText = newText.replace("ad-caution", this.settings.adcaution); - newText = newText.replace("ad-attention", this.settings.adattention); - newText = newText.replace("ad-failure", this.settings.adfailure); - newText = newText.replace("ad-fail", this.settings.adfail); - newText = newText.replace("ad-missing", this.settings.admissing); - newText = newText.replace("ad-danger", this.settings.addanger); - newText = newText.replace("ad-error", this.settings.aderror); - newText = newText.replace("ad-bug", this.settings.adbug); - newText = newText.replace("ad-example", this.settings.adexample); - newText = newText.replace("ad-quote", this.settings.adquote); - newText = newText.replace("ad-cite", this.settings.adcite); + let tasks = editorText.match(/- \[ \] .*/g); - await this.app.vault.create(qmdTitle, newText); + for (let task of tasks) { + let taskName = task.replace("- [ ] ", ""); + let taskNameEncoded = encodeURIComponent(taskName); + let noteURL = view.file.path.replace(/ /g, "%20").replace(/\//g, "%2F"); + let vaultName = app.vault.getName(); + let taskNoteEncoded = encodeURIComponent("obsidian://open?=" + vaultName + "&file="+noteURL); + + window.open( + `omnifocus:///add?name=${taskNameEncoded}¬e=${taskNoteEncoded}` + ); + } + + if (this.settings.markComplete) { + let completedText = editorText.replace(/- \[ \]/g, "- [x]"); + editor.setValue(completedText); + } } catch (err) { - // if (this.settings.automaticPush == false) { - // new Notice(`Didn't detect correct syntax. Doing nothing`) - // } + } } } - // private automaticPush = serialize( - // async (file: TFile, _data: string, cache: CachedMetadata) => { - // const view = this.app.workspace.getActiveViewOfType(MarkdownView); - // if (view!= null) { - // const indicatorCharacter = this.settings.indicatorCharacter - // const editor = view.editor; - // const currentPos = editor.getCursor(); - // const nextPos = { line: currentPos.line, ch: currentPos.ch + 1 }; - // const zeroPos = { line: currentPos.line, ch: 0 }; - // const lineString = editor.getRange(zeroPos, nextPos); - // if (this.settings.automaticPush == true && lineString.includes("]]" + indicatorCharacter + "{") == false) { - // await this.createAndPush(); - // } - // } - // } - // ); - private async loadSettings() { this.settings = Object.assign( diff --git a/src/settings.ts b/src/settings.ts index 423bf48..dc1662b 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -2,65 +2,11 @@ import { App, PluginSettingTab, Setting } from "obsidian"; import ListModified from "./main"; export interface ListModifiedSettings { - adnote: string; - adseealso: string; - adabstract: string; - adsummary: string; - adtldr: string; - adinfo: string; - adtodo: string; - adtip: string; - adhint: string; - adimportant: string; - adsuccess: string; - adcheck: string; - addone: string; - adquestion: string; - adhelp: string; - adfaq: string; - adwarning: string; - adcaution: string; - adattention: string; - adfailure: string; - adfail: string; - admissing: string; - addanger: string; - aderror: string; - adbug: string; - adexample: string; - adquote: string; - adcite: string; + markComplete: boolean; } export const DEFAULT_SETTINGS: ListModifiedSettings = { - adnote: "{.callout-note}", - adseealso: "{.callout-note}", - adabstract: "{.callout-note}", - adsummary: "{.callout-note}", - adtldr: "{.callout-note}", - adinfo: "{.callout-tip}", - adtodo: "{.callout-note}", - adtip: "{.callout-tip}", - adhint: "{.callout-tip}", - adimportant: "{.callout-important}", - adsuccess: "{.callout-note}", - adcheck: "{.callout-note}", - addone: "{.callout-note}", - adquestion: "{.callout-note}", - adhelp: "{.callout-note}", - adfaq: "{.callout-note}", - adwarning: "{.callout-warning}", - adcaution: "{.callout-caution}", - adattention: "{.callout-important}", - adfailure: "{.callout-caution}", - adfail: "{.callout-caution}", - admissing: "{.callout-caution}", - addanger: "{.callout-caution}", - aderror: "{.callout-warning}", - adbug: "{.callout-warning}", - adexample: "{.callout-note}", - adquote: "{.callout-note}", - adcite: "{.callout-note}", + markComplete: true, }; @@ -81,424 +27,17 @@ export class ListModifiedSettingTab extends PluginSettingTab { containerEl.createEl("h2", { text: "Settings" }); new Setting(containerEl) - .setName("ad-note") - .setDesc( - "ad-note" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adnote) + .setName("Mark Complete") + .setDesc("Mark tasks as complete in Obsidian when they are sent to OmniFocus.") + .addToggle((toggle) => + toggle + .setValue(this.plugin.settings.markComplete) .onChange(async (value) => { - this.plugin.settings.adnote = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-seealso") - .setDesc( - "ad-seealso" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adseealso) - .onChange(async (value) => { - this.plugin.settings.adseealso = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-abstract") - .setDesc( - "ad-abstract" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adabstract) - .onChange(async (value) => { - this.plugin.settings.adabstract = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-summary") - .setDesc( - "ad-summary" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adsummary) - .onChange(async (value) => { - this.plugin.settings.adsummary = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-tldr") - .setDesc( - "ad-tldr" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adtldr) - .onChange(async (value) => { - this.plugin.settings.adtldr = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-info") - .setDesc( - "ad-info" - ) - .addText((text) => - text - .setPlaceholder("{.callout-tip}") - .setValue(this.plugin.settings.adinfo) - .onChange(async (value) => { - this.plugin.settings.adinfo = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-todo") - .setDesc( - "ad-todo" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adtodo) - .onChange(async (value) => { - this.plugin.settings.adtodo = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-tip") - .setDesc( - "ad-tip" - ) - .addText((text) => - text - .setPlaceholder("{.callout-tip}") - .setValue(this.plugin.settings.adtip) - .onChange(async (value) => { - this.plugin.settings.adtip = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-hint") - .setDesc( - "ad-hint" - ) - .addText((text) => - text - .setPlaceholder("{.callout-tip}") - .setValue(this.plugin.settings.adhint) - .onChange(async (value) => { - this.plugin.settings.adhint = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-important") - .setDesc( - "ad-important" - ) - .addText((text) => - text - .setPlaceholder("{.callout-important}") - .setValue(this.plugin.settings.adimportant) - .onChange(async (value) => { - this.plugin.settings.adimportant = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-success") - .setDesc( - "ad-success" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adsuccess) - .onChange(async (value) => { - this.plugin.settings.adsuccess = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-check") - .setDesc( - "ad-check" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adcheck) - .onChange(async (value) => { - this.plugin.settings.adcheck = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-done") - .setDesc( - "ad-done" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.addone) - .onChange(async (value) => { - this.plugin.settings.addone = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-question") - .setDesc( - "ad-question" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adquestion) - .onChange(async (value) => { - this.plugin.settings.adquestion = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-help") - .setDesc( - "ad-help" - ) - .addText((text) => - text - .setPlaceholder("{.callout-help}") - .setValue(this.plugin.settings.adhelp) - .onChange(async (value) => { - this.plugin.settings.adhelp = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-faq") - .setDesc( - "ad-faq" - ) - .addText((text) => - text - .setPlaceholder("{.callout-faq}") - .setValue(this.plugin.settings.adfaq) - .onChange(async (value) => { - this.plugin.settings.adfaq = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-warning") - .setDesc( - "ad-warning" - ) - .addText((text) => - text - .setPlaceholder("{.callout-warning}") - .setValue(this.plugin.settings.adwarning) - .onChange(async (value) => { - this.plugin.settings.adwarning = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-caution") - .setDesc( - "ad-caution" - ) - .addText((text) => - text - .setPlaceholder("{.callout-caution}") - .setValue(this.plugin.settings.adcaution) - .onChange(async (value) => { - this.plugin.settings.adcaution = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-attention") - .setDesc( - "ad-attention" - ) - .addText((text) => - text - .setPlaceholder("{.callout-important}") - .setValue(this.plugin.settings.adattention) - .onChange(async (value) => { - this.plugin.settings.adattention = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-failure") - .setDesc( - "ad-failure" - ) - .addText((text) => - text - .setPlaceholder("{.callout-caution}") - .setValue(this.plugin.settings.adfailure) - .onChange(async (value) => { - this.plugin.settings.adfailure = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-fail") - .setDesc( - "ad-fail" - ) - .addText((text) => - text - .setPlaceholder("{.callout-caution}") - .setValue(this.plugin.settings.adfail) - .onChange(async (value) => { - this.plugin.settings.adfail = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-missing") - .setDesc( - "ad-missing" - ) - .addText((text) => - text - .setPlaceholder("{.callout-caution}") - .setValue(this.plugin.settings.admissing) - .onChange(async (value) => { - this.plugin.settings.admissing = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-danger") - .setDesc( - "ad-danger" - ) - .addText((text) => - text - .setPlaceholder("{.callout-caution}") - .setValue(this.plugin.settings.addanger) - .onChange(async (value) => { - this.plugin.settings.addanger = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-error") - .setDesc( - "ad-error" - ) - .addText((text) => - text - .setPlaceholder("{.callout-warning}") - .setValue(this.plugin.settings.aderror) - .onChange(async (value) => { - this.plugin.settings.aderror = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-bug") - .setDesc( - "ad-bug" - ) - .addText((text) => - text - .setPlaceholder("{.callout-warning}") - .setValue(this.plugin.settings.adbug) - .onChange(async (value) => { - this.plugin.settings.adbug = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-example") - .setDesc( - "ad-example" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adexample) - .onChange(async (value) => { - this.plugin.settings.adexample = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-quote") - .setDesc( - "ad-quote" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adquote) - .onChange(async (value) => { - this.plugin.settings.adquote = value; - await this.plugin.saveSettings(); - }) - ); - - new Setting(containerEl) - .setName("ad-cite") - .setDesc( - "ad-cite" - ) - .addText((text) => - text - .setPlaceholder("{.callout-note}") - .setValue(this.plugin.settings.adcite) - .onChange(async (value) => { - this.plugin.settings.adcite = value; + this.plugin.settings.markComplete = value; await this.plugin.saveSettings(); }) ); + } }