Skip to content

Commit

Permalink
Support block links
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
schemar committed May 22, 2021
1 parent e641852 commit 30c0dfb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ For example: `📅 2021-04-09` means the task is due on the 9th of April, 2021.
Instead of adding the emoji and the date manually, you can use the `Tasks: Crete or edit` command when creating or editing a task.
When you use the command, you can also set a due date like "Monday", "tomorrow", or "next week" and Tasks will automatically save the date in the correct format.

**You can not put anything behind the due/done dates. Also not a global filter. Everything after the dates will be removed by Tasks.**
**You can only put block links (`^link-name`) behind the due/done dates. Everything else after the dates will be removed by Tasks.**

### Recurring tasks (repetition)

Expand Down
11 changes: 10 additions & 1 deletion src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,29 @@ export class Commands {
sectionStart: 0,
sectionIndex: 0,
precedingHeader: null,
blockLink: '',
});
}

const indentation: string = nonTaskMatch[1];
const statusString: string = nonTaskMatch[3] ?? ' ';
const status = statusString === ' ' ? Status.Todo : Status.Done;
const description: string = nonTaskMatch[4];
let description: string = nonTaskMatch[4];

const blockLinkMatch = line.match(Task.blockLinkRegex);
const blockLink = blockLinkMatch !== null ? blockLinkMatch[0] : '';

if (blockLink !== '') {
description = description.replace(Task.blockLinkRegex, '');
}

return new Task({
status,
description,
path,
indentation,
originalStatusCharacter: statusString,
blockLink,
dueDate: null,
doneDate: null,
recurrenceRule: null,
Expand Down
32 changes: 29 additions & 3 deletions src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ export class Task {
public readonly dueDate: Moment | null;
public readonly doneDate: Moment | null;
public readonly recurrenceRule: RRule | null;
/** The blockLink is a "^" annotation after the dates/recurrence rules. */
public readonly blockLink: string;

public static readonly dateFormat = 'YYYY-MM-DD';
public static readonly taskRegex =
/^([\s\t]*)[-*] +\[(.)\] *([^🔁📅📆🗓]*)(.*)/u;
public static readonly dueDateRegex = /[📅📆🗓] ?(\d{4}-\d{2}-\d{2})/u;
public static readonly doneDateRegex = / ?(\d{4}-\d{2}-\d{2})/u;
public static readonly recurrenceRegex = /🔁([a-zA-Z0-9, !]+)/u;
public static readonly blockLinkRegex = / \^[a-zA-Z0-9-]+$/u;

constructor({
status,
Expand All @@ -48,6 +51,7 @@ export class Task {
dueDate,
doneDate,
recurrenceRule,
blockLink,
}: {
status: Status;
description: string;
Expand All @@ -60,6 +64,7 @@ export class Task {
dueDate: moment.Moment | null;
doneDate: moment.Moment | null;
recurrenceRule: RRule | null;
blockLink: string;
}) {
this.status = status;
this.description = description;
Expand All @@ -72,6 +77,7 @@ export class Task {
this.dueDate = dueDate;
this.doneDate = doneDate;
this.recurrenceRule = recurrenceRule;
this.blockLink = blockLink;
}

public static fromLine({
Expand All @@ -94,7 +100,14 @@ export class Task {

const indentation = regexMatch[1];
const statusString = regexMatch[2].toLowerCase();
const description = regexMatch[3].trim();

const blockLinkMatch = line.match(this.blockLinkRegex);
const blockLink = blockLinkMatch !== null ? blockLinkMatch[0] : '';

let description = regexMatch[3].trim();
if (blockLink !== '') {
description = description.replace(this.blockLinkRegex, '');
}

const { globalFilter } = getSettings();
if (!description.includes(globalFilter)) {
Expand Down Expand Up @@ -151,6 +164,7 @@ export class Task {
dueDate,
doneDate,
recurrenceRule,
blockLink,
});

return task;
Expand Down Expand Up @@ -183,10 +197,19 @@ export class Task {
// Unwrap the p-tag that was created by the MarkdownRenderer:
const pElement = li.querySelector('p');
if (pElement !== null) {
while (pElement.firstChild) li.appendChild(pElement.firstChild);
while (pElement.firstChild) {
li.insertBefore(pElement.firstChild, pElement);
}
pElement.remove();
}

// Remove an empty trailing p-tag that the MarkdownRenderer appends when there is a block link:
li.findAll('p').forEach((pElement) => {
if (!pElement.hasChildNodes()) {
pElement.remove();
}
});

const checkbox = li.createEl('input');
checkbox.addClass('task-list-item-checkbox');
checkbox.type = 'checkbox';
Expand Down Expand Up @@ -230,7 +253,7 @@ export class Task {
? ` ✅ ${this.doneDate.format(Task.dateFormat)}`
: '';

return `${this.description}${recurrenceRule}${dueDate}${doneDate}`;
return `${this.description}${recurrenceRule}${dueDate}${doneDate}${this.blockLink}`;
}

public toFileLineString(): string {
Expand Down Expand Up @@ -282,6 +305,9 @@ export class Task {
const nextTask = new Task({
...this,
dueDate: nextOccurrence,
// New occurrences cannot have the same block link.
// And random block links don't help.
blockLink: '',
});
newTasks.push(nextTask);
}
Expand Down

0 comments on commit 30c0dfb

Please sign in to comment.