Skip to content

Commit

Permalink
feat: add file as property of the task for quering
Browse files Browse the repository at this point in the history
  • Loading branch information
sytone committed Jun 11, 2022
1 parent d44c36e commit eff7181
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/queries-x/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The following columns are available to be used in the WHERE clauses.
| status->nextStatusIndicator | The next indicator to be used when clicked on. ('x', ' ', '/', etc) | |
| description | The description of the task. | |
| path | The path to the note the task is in. | |
| file | The TFile object in Obsidian. | |
| precedingHeader | The heading that the task is under | |
| priority | The priority of the task. This has to be treated like a string ('1', '2', '3', '4') | |

Expand Down Expand Up @@ -60,6 +61,18 @@ WHERE ((dueDate->getUTCFullYear() = 2021 AND status->indicator = 'x') OR (dueDat

Look at the [SQL Compatibility](https://github.com/AlaSQL/alasql/wiki/SQL%20keywords) table to see what SQL commands are supported.

### Queries using file

basename is the name of the page

`WHERE status->indicator = '!' AND file->basename = '2021-10-13'`

You can also now query based on the creation date of the note the task is in.

`WHERE status->indicator = ' ' AND moment(file->stat->ctime)->month() = 3`

you can use `mtime` to access the modified time.

### Object Properties & Functions

Object property
Expand Down
1 change: 1 addition & 0 deletions src/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export class Cache {
const task = Task.fromLine({
line,
path: file.path,
file,
sectionStart: currentSection.position.start.line,
sectionIndex,
precedingHeader: this.getPrecedingHeader({
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/CreateOrEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const taskFromLine = ({ line, path }: { line: string; path: string }): Task => {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart: 0, // We don't need this to toggle it here in the editor.
sectionIndex: 0, // We don't need this to toggle it here in the editor.
precedingHeader: null, // We don't need this to toggle it here in the editor.
Expand All @@ -62,6 +63,7 @@ const taskFromLine = ({ line, path }: { line: string; path: string }): Task => {
status: Status.TODO,
description: '',
path,
file: null,
indentation: '',
priority: Priority.None,
startDate: null,
Expand Down Expand Up @@ -94,6 +96,7 @@ const taskFromLine = ({ line, path }: { line: string; path: string }): Task => {
status,
description,
path,
file: null,
indentation,
blockLink,
priority: Priority.None,
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ToggleDone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const toggleLine = ({ line, path }: { line: string; path: string }): string => {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart: 0, // We don't need this to toggle it here in the editor.
sectionIndex: 0, // We don't need this to toggle it here in the editor.
precedingHeader: null, // We don't need this to toggle it here in the editor.
Expand Down
1 change: 1 addition & 0 deletions src/InlineRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class InlineRenderer {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart: section.lineStart,
sectionIndex,
precedingHeader: null, // We don't need the preceding header for in-line rendering.
Expand Down
1 change: 1 addition & 0 deletions src/LivePreviewExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class LivePreviewExtension implements PluginValue {
// The task is created, toggled, and written back to the CM6 document,
// replacing the old task in-place.
path: '',
file: null,
sectionStart: 0,
sectionIndex: 0,
precedingHeader: null,
Expand Down
13 changes: 12 additions & 1 deletion src/Task.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Moment } from 'moment';
import { Component, MarkdownRenderer } from 'obsidian';
import { Component, MarkdownRenderer, TFile } from 'obsidian';
import moment from 'moment';

import { StatusRegistry } from './StatusRegistry';
Expand Down Expand Up @@ -29,6 +29,7 @@ export type TaskRecord = {
status: Status;
description: string;
path: string;
file: TFile | null;
indentation: string;
sectionStart: number;
sectionIndex: number;
Expand Down Expand Up @@ -87,6 +88,8 @@ export class Task {
public readonly status: Status;
public readonly description: string;
public readonly path: string;
public readonly file: TFile | null;

public readonly indentation: string;
/** Line number where the section starts that contains this task. */
public readonly sectionStart: number;
Expand All @@ -113,6 +116,7 @@ export class Task {
status,
description,
path,
file,
indentation,
sectionStart,
sectionIndex,
Expand All @@ -129,6 +133,7 @@ export class Task {
status: Status;
description: string;
path: string;
file: TFile | null;
indentation: string;
sectionStart: number;
sectionIndex: number;
Expand All @@ -145,6 +150,7 @@ export class Task {
this.status = status;
this.description = description;
this.path = path;
this.file = file;
this.indentation = indentation;
this.sectionStart = sectionStart;
this.sectionIndex = sectionIndex;
Expand Down Expand Up @@ -185,6 +191,7 @@ export class Task {
status: record.status,
description: record.description,
path: record.path,
file: record.file,
indentation: record.indentation,
sectionStart: record.sectionStart,
sectionIndex: record.sectionIndex,
Expand Down Expand Up @@ -215,12 +222,14 @@ export class Task {
public static fromLine({
line,
path,
file,
sectionStart,
sectionIndex,
precedingHeader,
}: {
line: string;
path: string;
file: TFile | null;
sectionStart: number;
sectionIndex: number;
precedingHeader: string | null;
Expand Down Expand Up @@ -355,6 +364,7 @@ export class Task {
status,
description,
path,
file,
indentation,
sectionStart,
sectionIndex,
Expand Down Expand Up @@ -578,6 +588,7 @@ export class Task {
status: this.status,
description: this.description,
path: this.path,
file: this.file,
indentation: this.indentation,
sectionStart: this.sectionStart,
sectionIndex: this.sectionIndex,
Expand Down
2 changes: 2 additions & 0 deletions tests/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Query', () => {
status: Status.TODO,
description: 'description',
path: 'Ab/C D',
file: null,
indentation: '',
sectionStart: 0,
sectionIndex: 0,
Expand All @@ -36,6 +37,7 @@ describe('Query', () => {
status: Status.TODO,
description: 'description',
path: 'FF/C D',
file: null,
indentation: '',
sectionStart: 0,
sectionIndex: 0,
Expand Down
4 changes: 4 additions & 0 deletions tests/QueryX.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('QuerySql', () => {
status: Status.TODO,
description: 'description',
path: 'Ab/C D',
file: null,
indentation: '',
sectionStart: 0,
sectionIndex: 0,
Expand All @@ -135,6 +136,7 @@ describe('QuerySql', () => {
status: Status.TODO,
description: 'description',
path: 'FF/C D',
file: null,
indentation: '',
sectionStart: 0,
sectionIndex: 0,
Expand Down Expand Up @@ -166,6 +168,7 @@ describe('QuerySql', () => {
status: Status.TODO,
description: 'description',
path: 'Ab/C D',
file: null,
indentation: '',
sectionStart: 0,
sectionIndex: 0,
Expand All @@ -183,6 +186,7 @@ describe('QuerySql', () => {
status: Status.DONE,
description: 'description',
path: 'FF/C D',
file: null,
indentation: '',
sectionStart: 0,
sectionIndex: 0,
Expand Down
3 changes: 3 additions & 0 deletions tests/StatusRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('StatusRegistry', () => {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart,
sectionIndex,
precedingHeader,
Expand Down Expand Up @@ -71,6 +72,7 @@ describe('StatusRegistry', () => {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart,
sectionIndex,
precedingHeader,
Expand Down Expand Up @@ -128,6 +130,7 @@ describe('StatusRegistry', () => {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart,
sectionIndex,
precedingHeader,
Expand Down
9 changes: 9 additions & 0 deletions tests/Task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('parsing', () => {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart,
sectionIndex,
precedingHeader,
Expand Down Expand Up @@ -61,6 +62,7 @@ describe('parsing', () => {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart,
sectionIndex,
precedingHeader,
Expand All @@ -85,6 +87,7 @@ describe('parsing', () => {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart,
sectionIndex,
precedingHeader,
Expand Down Expand Up @@ -112,6 +115,7 @@ describe('parsing', () => {
const task = Task.fromLine({
line,
path,
file: null,
sectionStart,
sectionIndex,
precedingHeader,
Expand Down Expand Up @@ -140,6 +144,7 @@ function constructTaskFromLine(line: string) {
const task = Task.fromLine({
line,
path: 'file.md',
file: null,
sectionStart: 0,
sectionIndex: 0,
precedingHeader: '',
Expand Down Expand Up @@ -267,6 +272,7 @@ describe('to string', () => {
const task: Task = Task.fromLine({
line,
path: '',
file: null,
sectionStart: 0,
sectionIndex: 0,
precedingHeader: '',
Expand All @@ -285,6 +291,7 @@ describe('to string', () => {
const task: Task = Task.fromLine({
line,
path: '',
file: null,
sectionStart: 0,
sectionIndex: 0,
precedingHeader: '',
Expand Down Expand Up @@ -360,6 +367,7 @@ describe('toggle done', () => {
const task: Task = Task.fromLine({
line,
path: '',
file: null,
sectionStart: 0,
sectionIndex: 0,
precedingHeader: '',
Expand Down Expand Up @@ -609,6 +617,7 @@ describe('toggle done', () => {
const task = Task.fromLine({
line,
path: '',
file: null,
precedingHeader: '',
sectionStart: 0,
sectionIndex: 0,
Expand Down
2 changes: 2 additions & 0 deletions tests/TestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function fromLine({
return Task.fromLine({
line,
path,
file: null,
precedingHeader,
sectionIndex: 0,
sectionStart: 0,
Expand All @@ -25,6 +26,7 @@ export function createTasksFromMarkdown(tasksAsMarkdown: string, path: string, p
const task = Task.fromLine({
line: line,
path: path,
file: null,
precedingHeader: precedingHeader,
sectionIndex: 0,
sectionStart: 0,
Expand Down
1 change: 1 addition & 0 deletions tests/TestingTools/FilterTestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function shouldSupportFiltering(
sectionStart: 0,
sectionIndex: 0,
path: '',
file: null,
precedingHeader: '',
}) as Task,
);
Expand Down
8 changes: 8 additions & 0 deletions tests/TestingTools/TaskBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Builder
import type { Moment } from 'moment';
import type { TFile } from 'obsidian';
import { StatusRegistry } from '../../src/StatusRegistry';
import { Priority, Task } from '../../src/Task';
import type { Recurrence } from '../../src/Recurrence';
Expand All @@ -22,6 +23,7 @@ export class TaskBuilder {
private _status: Status = Status.TODO;
private _description: string = 'my description';
private _path: string = '';
private _file: TFile | null = null;
private _indentation: string = '';

private _sectionStart: number = 0;
Expand Down Expand Up @@ -64,6 +66,7 @@ export class TaskBuilder {
status: this._status,
description: this._description,
path: this._path,
file: this._file,
indentation: this._indentation,
sectionStart: this._sectionStart,
sectionIndex: this._sectionIndex,
Expand Down Expand Up @@ -98,6 +101,11 @@ export class TaskBuilder {
return this;
}

public file(file: TFile | null): TaskBuilder {
this._file = file;
return this;
}

public indentation(indentation: string): TaskBuilder {
this._indentation = indentation;
return this;
Expand Down

0 comments on commit eff7181

Please sign in to comment.