From 3e32efaaf23fd3d1129d568c76acca47474f3a77 Mon Sep 17 00:00:00 2001 From: Kai Yorke Date: Wed, 26 Mar 2025 11:56:08 +1300 Subject: [PATCH] feat: Add heading and heading_level to tasks table --- docs/data-sources/vault-data.md | 21 +++++++++++++-------- src/vaultSync/tables/tasksTable.ts | 17 ++++++++++++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/docs/data-sources/vault-data.md b/docs/data-sources/vault-data.md index fe3f862..309a207 100644 --- a/docs/data-sources/vault-data.md +++ b/docs/data-sources/vault-data.md @@ -13,6 +13,7 @@ LIMIT 10 ## Table Structure ### `files` table Files table consists of the following columns: + | Column | Description | Introduced In | | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | | `id` | File Path | | @@ -27,6 +28,7 @@ Files table consists of the following columns: ### `tags` table Tags table consists of the following columns: + | Column | Description | Introduced In | | -------- | ------------------------------------------------------------------------------------------------------------------------ | ------------- | | `tag` | Full tag, including `#` symbol. For example `#todo` | | @@ -37,14 +39,17 @@ Tags table consists of the following columns: ### `tasks` table Tasks table consists of the following columns: -| Column | Description | Introduced In | -| ----------- | ------------------------------------------------------------------------------------------------------------------------ | ------------- | -| `task` | Content of the task (text) | | -| `completed` | 0 if not completed, 1 if completed | | -| `path` | Full path of the file the tag belongs to | 0.24.1 | -| `filePath` | (deprecated) same like `path`. Name changed for compatibility with other tables. Will get removed in the future versions | | -| `checkbox` | Interactive checkbox for the task that can be clicked to toggle completion state | 0.29.0 | -| `position` | Line number where the task appears in the original file | 0.29.0 | + +| Column | Description | Introduced In | +| --------------- |--------------------------------------------------------------------------------------------------------------------------| ------------- | +| `task` | Content of the task (text) | | +| `completed` | 0 if not completed, 1 if completed | | +| `path` | Full path of the file the tag belongs to | 0.24.1 | +| `filePath` | (deprecated) same like `path`. Name changed for compatibility with other tables. Will get removed in the future versions | | +| `checkbox` | Interactive checkbox for the task that can be clicked to toggle completion state | 0.29.0 | +| `position` | Line number where the task appears in the original file | 0.29.0 | +| `heading` | The name of the heading this task appears under (if any) | | +| `heading_level` | The level of the heading this task appears under (if any), i.e., the number of # in the heading declaration | | ### `links` table Table containing all the links between files. diff --git a/src/vaultSync/tables/tasksTable.ts b/src/vaultSync/tables/tasksTable.ts index f7a3188..96b8d12 100644 --- a/src/vaultSync/tables/tasksTable.ts +++ b/src/vaultSync/tables/tasksTable.ts @@ -14,17 +14,18 @@ export class TasksFileSyncTable extends AFileSyncTable { } async onFileCreate(file: TFile): Promise { - const tasks = await this.getFileTags(file) + const tasks = await this.getFileTasks(file) this.db.insertData('tasks', tasks) } - async getFileTags(file: TFile) { + async getFileTasks(file: TFile) { const cache = this.app.metadataCache.getFileCache(file); if (!cache || !cache.listItems) return []; const content = await this.app.vault.read(file); const lines = content.split('\n'); + const headings = cache.headings; return cache.listItems.map(listItem => { // Check if it's a task @@ -52,6 +53,10 @@ export class TasksFileSyncTable extends AFileSyncTable { } } }; + + const heading = headings ? + headings.filter(h => h.position.start.line < listItem.position.start.line) + .last() : undefined; return { filePath: file.path, @@ -59,13 +64,15 @@ export class TasksFileSyncTable extends AFileSyncTable { task: taskContent, completed: status ? 1 : 0, position: listItem.position.start.line, - checkbox: `SQLSEALCUSTOM(${JSON.stringify(checkboxData)})` + checkbox: `SQLSEALCUSTOM(${JSON.stringify(checkboxData)})`, + heading: heading ? heading.heading : undefined, + heading_level: heading ? heading.level : undefined } }).filter(t => !!t) } async onInit(): Promise { - await this.db.createTableNoTypes('tasks', ['checkbox', 'task', 'completed', 'filePath', 'path', 'position']) + await this.db.createTableNoTypes('tasks', ['checkbox', 'task', 'completed', 'filePath', 'path', 'position', 'heading', 'heading_level']) // Indexes const toIndex = ['filePath'] @@ -73,4 +80,4 @@ export class TasksFileSyncTable extends AFileSyncTable { this.db.createIndex(`tasks_${column}_idx`, this.tableName, [column]) )) } -} \ No newline at end of file +}