Created
December 5, 2023 15:50
-
-
Save jplattel/35012b77cfd5a391fdd0e18de5bee742 to your computer and use it in GitHub Desktop.
Simple Todoist tasks to placeholder in Obsidian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { App, Notice, Plugin, request } from 'obsidian'; | |
export default class OmnifocusPlugin extends Plugin { | |
onload() { | |
// Add a command to the pallete that autocompletes omnifocus tasks | |
this.addCommand({ | |
id: 'get-todoist-tasks', | |
name: 'Fetch todoist tasks', | |
callback: async () => { | |
// Current file and state | |
let currentView = this.app.workspace.activeLeaf.view | |
let currentState = currentView.editor.cm.state | |
let currentFile = currentView.file.basename | |
// Check placeholder | |
let placeholderPosition = currentState.doc.toString().indexOf("{todoist}") | |
if (!placeholderPosition) { new Notice(`No {todoist} placeholder found!`) } | |
const token = "<API KEY HERE>" | |
let response = await request({ | |
url: 'https://api.todoist.com/sync/v9/completed/get_all', | |
headers: { | |
"Authorization": `Bearer ${token}` | |
} | |
}) | |
let parsedResponse = JSON.parse(response) | |
let todos = parsedResponse.items | |
let report = [] | |
let otherTodos = [] | |
Object.keys(parsedResponse.projects).forEach(projectId => { | |
let project = parsedResponse.projects[projectId] | |
let projectTodos = todos.filter(todo => | |
todo.project_id == projectId && | |
todo.completed_at.startsWith(currentFile) | |
) | |
// If a project has a number, it has a note in Obsidian, so we can mathc | |
if (projectTodos.length > 0 && project.name.match(/^\d/)) { | |
report.push(`\n[[${project.name}]]`) | |
projectTodos.forEach(todo => { | |
report.push(`- [x] ${todo.content}`) | |
}); | |
} else { // Otherwise we link it in "Other" | |
projectTodos.forEach(todo => { | |
otherTodos.push(`- [x] ${todo.content}`) | |
}); | |
} | |
}); | |
let dailyTasks = report | |
if (otherTodos.length > 0) { | |
dailyTasks = report.concat(["\nOther tasks", ...otherTodos]) | |
} | |
if (dailyTasks.length > 0) { | |
// If todo's are found, add them inplace of the placeholder | |
currentView.editor.cm.dispatch({changes: {from: placeholderPosition, to: placeholderPosition + 9, insert: dailyTasks.join("\n")}}) | |
new Notice("Todoists tasks added!"); | |
} else { | |
// If no todo's are done, remove the placeholder... | |
currentView.editor.cm.dispatch({changes: {from: placeholderPosition, to: placeholderPosition + 9, insert: ""}}) | |
new Notice(`No toodoists tasks completed at ${currentFile}!`); | |
} | |
} | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment