Skip to content

Commit

Permalink
feat: Extract block link from task as column to query (#35)
Browse files Browse the repository at this point in the history
* feat: Extract block link from task as column to query
Fixes #34

* docs: add new column to table reference
  • Loading branch information
sytone authored Nov 23, 2024
1 parent 4e392eb commit fd98967
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/data-tables/obsidian-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ Table Name: `obsidian_tasks`
| scheduledDate | string | When the task is scheduled next ['⏳', 'scheduled::'] |
| doDate | string | When to do the task ['💨', 'do::'] |
| priority | number | Priority of task based on indicator ['⏫🔼🔽', 'priority::'] |
| cleanTask | string | The task string with all metadata removed. |
| cleanTask | string | The task string with all metadata removed. |
| blockLink | string | manually specified block Link for the task |
19 changes: 17 additions & 2 deletions src/Parse/Parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ export const parseTask = (taskString: string) => {
let doDate: string | undefined;
let priority;
let cleanTask = taskString;
let blockLink: string | undefined;

for (let index = 0; index < tokens.length; index++) {
parseTags(tokens, index, tags, tagsNormalized);

// Check to see is the token is a block link and if it is then set the block link property. The
// block link is in the form of a '^' character followed by only Latin letters, numbers, and dashes.
blockLink = parseBlockLink(tokens, index);
dueDate = dueDate ?? parseDate(tokens, index, dueDatePrefixes);
doneDate = doneDate ?? parseDate(tokens, index, doneDatePrefixes);
startDate = startDate ?? parseDate(tokens, index, startDatePrefixes);
Expand Down Expand Up @@ -77,9 +80,13 @@ export const parseTask = (taskString: string) => {
}
}

if (blockLink) {
cleanTask = cleanTask.replace(`^${blockLink}`, '');
}

cleanTask = cleanTask.trim().slice(6);

return {tags, tagsNormalized, dueDate, doneDate, startDate, createDate, scheduledDate, doDate, priority, cleanTask};
return {tags, tagsNormalized, dueDate, doneDate, startDate, createDate, scheduledDate, doDate, priority, cleanTask, blockLink};
};

const regex = /[[|(](.+?):: (.+?)[\]|)]/g;
Expand Down Expand Up @@ -200,3 +207,11 @@ function parseTags(tokens: string[], index: number, tags: string[], tagsNormaliz
}
}
}

function parseBlockLink(tokens: string[], index: number) {
if (tokens[index].startsWith('^')) {
return tokens[index].slice(1);
}

return undefined;
}
3 changes: 3 additions & 0 deletions src/TaskItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Table Name: `obsidian_tasks`
| doDate | string | When to do the task ['💨', 'do::'] |
| priority | number | Priority of task based on indicator ['⏫🔼🔽', 'priority::'] |
| cleanTask | string | The task string with all metadata removed. |
| blockLink | string | Manually specified block Link for the task |
// << docs-tables-obsidian-tasks
*/
Expand All @@ -61,6 +62,7 @@ export class TaskItem {
public doDate: string;
public priority: number;
public cleanTask: string;
public blockLink: string;
public listItem: ListItem;

private _parsedTask: any;
Expand All @@ -78,6 +80,7 @@ export class TaskItem {
this.doDate = this._parsedTask.doDate;
this.priority = this._parsedTask.priority;
this.cleanTask = this._parsedTask.cleanTask;
this.blockLink = this._parsedTask.blockLink;

this.page = listItem.path;
this.path = listItem.path;
Expand Down
9 changes: 7 additions & 2 deletions tests/ParseTask.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import {describe, test} from 'node:test';
import assert from 'node:assert';
import {ListItem} from 'ListItem';
import {Note} from 'Note';
import {parseDataViewProperty, parseTask} from 'Parse/Parsers';
import {TaskItem} from 'TaskItem';
import {ListItem} from 'ListItem';

describe('parse functions', () => {
test('task parses tags correctly', () => {
Expand Down Expand Up @@ -113,6 +112,12 @@ describe('parse functions', () => {
assert.strictEqual(parsedTask.cleanTask, 'this is a task');
});

test('task parses block link correctly', () => {
const parsedTask = parseTask('- [ ] this is a task 📅 2022-03-05 [priority:: high] ^123ASD-asd');
assert.strictEqual(parsedTask.blockLink, '123ASD-asd');
assert.strictEqual(parsedTask.cleanTask, 'this is a task');
});

test('task parses are fast', () => {
const sampleTask = '- [ ] this is a task #TasK #test [priority:: high] 📅 2022-03-05 ✅ 2022-04-05';
let parsedTask = parseTask(sampleTask);
Expand Down

0 comments on commit fd98967

Please sign in to comment.