Skip to content

Commit

Permalink
Tasks now adds all known obsidian attributes
Browse files Browse the repository at this point in the history
The way it currently works means that it will not automatically pick up
new attributes as they get added by obsidian.

Fixes #45
  • Loading branch information
schemar committed Apr 29, 2021
1 parent d83beba commit f9f9c46
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/InlineRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ export class InlineRenderer {
continue;
}

const cachedElement = await task.toLi({ parentUlElement: element });
const dataLine: string =
renderedElement.getAttr('data-line') ?? '0';
const listIndex: number = Number.parseInt(dataLine, 10);
const cachedElement = await task.toLi({
parentUlElement: element,
listIndex,
});

// If the rendered element contains a sub-list, we need to keep it.
renderedElement.findAll('ul').map((renderedSubUl) => {
cachedElement.appendChild(renderedSubUl);
Expand Down
13 changes: 11 additions & 2 deletions src/QueryRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,25 @@ class QueryRenderChild extends MarkdownRenderChild {
tasks = tasks.filter(filter);
}

const tasksSorted = Sort.byDateThenPath(tasks);
const tasksCount = tasksSorted.length;

const taskList = content.createEl('ul');
taskList.addClass('contains-task-list');
for (const task of Sort.byDateThenPath(tasks)) {
for (let i = 0; i < tasksCount; i++) {
const task = tasksSorted[i];

let fileName: string | undefined;
const fileNameMatch = task.path.match(/([^/]+)\.md$/);
if (fileNameMatch !== null) {
fileName = fileNameMatch[1];
}

const listItem = await task.toLi({ parentUlElement: taskList });
const listItem = await task.toLi({
parentUlElement: taskList,
listIndex: i,
});

if (fileName !== undefined) {
const link = listItem.createEl('a');
link.href = fileName;
Expand Down
9 changes: 8 additions & 1 deletion src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,14 @@ export class Task {

public async toLi({
parentUlElement,
listIndex,
}: {
parentUlElement: HTMLElement;
/** The nth item in this list (including non-tasks). */
listIndex: number;
}): Promise<HTMLLIElement> {
const li: HTMLLIElement = parentUlElement.createEl('li');
li.addClass('task-list-item');
li.setAttr('data-task', this.originalStatusCharacter.trim()); // Trim to ensure empty attribute for space. Same way as obsidian.

let taskAsString = this.toString();
const { globalFilter, removeGlobalFilter } = getSettings();
Expand Down Expand Up @@ -204,6 +206,11 @@ export class Task {

li.prepend(checkbox);

// Set these to be compatible with stock obsidian lists:
li.setAttr('data-task', this.originalStatusCharacter.trim()); // Trim to ensure empty attribute for space. Same way as obsidian.
li.setAttr('data-line', listIndex);
checkbox.setAttr('data-line', listIndex);

return li;
}

Expand Down

0 comments on commit f9f9c46

Please sign in to comment.