Last active
December 16, 2022 03:59
-
-
Save chris-cadev/27fb9a49b254d1ad799ab8c138d38dcc to your computer and use it in GitHub Desktop.
This file contains 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
<%* | |
const bulletRegex = /- [^\[]/; | |
const taskRegex = /- \[ \] /; | |
function createTasksFromBullets(content) { | |
let result = content.split('\n') | |
result = result.map(l => { | |
if (!bulletRegex.test(l)) { | |
return l | |
} | |
const spaceBefore = l.split('- ')[0] | |
return l.replace(/^\s?- /, `${spaceBefore || ''}- [ ] `) | |
}).filter(l => !/^-$/.test(l)); | |
return result.join('\n') | |
} | |
function projectDirectory(name) { | |
return `on-table/todo-list/projects/${name}` | |
} | |
function projectIndex(name) { | |
return `${projectDirectory(name)}/${name}.md` | |
} | |
async function createProject(name) { | |
const content = createTasksFromBullets(tp.file.content) | |
const directory = projectDirectory(name) | |
const directoryExists = await app.vault.exists(directory); | |
if (!directoryExists) { | |
await app.vault.createFolder(directory) | |
} | |
return app.vault.create(projectIndex(name), content); | |
} | |
async function addToProject(name) { | |
const projectNoteName = projectIndex(name).replace('.md', '') | |
const projectContent = await getNoteContent(`[[${projectNoteName}]]`) | |
const content = [ | |
projectContent, | |
createTasksFromBullets(tp.file.content) | |
].join("\n") | |
return app.vault.modify( | |
await tp.file.find_tfile(projectNoteName), | |
content | |
); | |
} | |
function getNoteContent(link) { | |
return tp.file.include(link) | |
} | |
function transformToTodayTask(line) { | |
if (!taskRegex.test(line)) { | |
return line | |
} | |
return `${line} ⏳ ${tp.date.now("YYYY-MM-DD")}` | |
} | |
function getContextContent() { | |
const selection = tp.file.selection(); | |
let content = tp.file.content; | |
if (selection) { | |
content = selection | |
} | |
return content; | |
} | |
async function addToInbox(noteContent) { | |
const content = createTasksFromBullets(noteContent).split("\n").map(transformToTodayTask).join("\n") | |
const inboxContent = await getNoteContent("[[on-table/todo-list/Inbox]]") | |
const newInboxContent = [ | |
inboxContent, | |
`## ${tp.date.now("YYYY-MM-DD")}`, | |
content, | |
"---", | |
].join("\n"); | |
return app.vault.modify( | |
await tp.file.find_tfile("on-table/todo-list/Inbox"), | |
newInboxContent | |
); | |
} | |
function hasBullets(content) { | |
return content.split('\n').some(l => bulletRegex.test(l)) | |
} | |
function hasTasks(content) { | |
return content.split('\n').some(l => taskRegex.test(l)) | |
} | |
function isProject(title) { | |
const hasTitle = Boolean(title) | |
const lowerTitle = title.toLowerCase() | |
return hasTitle | |
&& lowerTitle != 'untitled' | |
&& lowerTitle != 'inbox' | |
&& lowerTitle.contains('.p') | |
} | |
function deleteCurrentNote() { | |
return app.vault.delete( | |
app.vault.getAbstractFileByPath(tp.file.path(true)), | |
true, | |
); | |
} | |
async function openFile(filePath) { | |
const file = await tp.file.find_tfile("on-table/todo-list/Inbox"); | |
await app.commands.executeCommandById("openFile", { file }); | |
} | |
async function processNote() { | |
try { | |
const {content} = tp.file | |
if (!hasBullets(content) && !hasTasks(content)) { | |
console.error('not bullet list found in the note') | |
return | |
} | |
if (!isProject(tp.file.title)) { | |
const content = getContextContent() | |
addToInbox(content) | |
openFile("on-table/todo-list/Inbox") | |
return; | |
} | |
const projectName = tp.file.title.replace('.p', '').trim() | |
const projectExists = await app.vault.exists(projectIndex(projectName)) | |
if (!projectExists) { | |
createProject(projectName) | |
} else { | |
addToProject(projectName) | |
} | |
await deleteCurrentNote() | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
processNote() | |
-%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment