From 81d99d7813bac16ca785011d29ac7ff8096968d2 Mon Sep 17 00:00:00 2001 From: Kacper Kula Date: Fri, 14 Mar 2025 10:26:04 +0000 Subject: [PATCH 1/4] feat: exposing filename, path and extension --- src/codeblockHandler/CodeblockProcessor.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/codeblockHandler/CodeblockProcessor.ts b/src/codeblockHandler/CodeblockProcessor.ts index 57b60b0..819854a 100644 --- a/src/codeblockHandler/CodeblockProcessor.ts +++ b/src/codeblockHandler/CodeblockProcessor.ts @@ -114,7 +114,14 @@ export class CodeblockProcessor extends MarkdownRenderChild { this.explainEl.textContent = result } - const { data, columns } = await this.db.select(transformedQuery, fileCache?.frontmatter ?? {}) + const variables = { + ...fileCache?.frontmatter ?? {}, + path: file.path, + fileName: file.name, + extension: file.extension, + } + + const { data, columns } = await this.db.select(transformedQuery, variables) this.renderer.render({ data, columns, flags: this.flags }) } catch (e) { this.renderer.error(e.toString()) From 8cb185b40d7a3fc0c4ea6875f099f993e77be776 Mon Sep 17 00:00:00 2001 From: Kacper Kula Date: Thu, 6 Mar 2025 10:07:40 +0000 Subject: [PATCH 2/4] chore: adding extra variables to the inline query too --- src/codeblockHandler/inline/InlineProcessor.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/codeblockHandler/inline/InlineProcessor.ts b/src/codeblockHandler/inline/InlineProcessor.ts index 09c8aee..e28bc77 100644 --- a/src/codeblockHandler/inline/InlineProcessor.ts +++ b/src/codeblockHandler/inline/InlineProcessor.ts @@ -55,9 +55,18 @@ export class InlineProcessor extends MarkdownRenderChild { } const fileCache = this.app.metadataCache.getFileCache(file); + + // TODO: unify this between codeblock and inline handlers + const variables = { + ...fileCache?.frontmatter ?? {}, + path: file.path, + fileName: file.name, + extension: file.extension, + } + const { data, columns } = await this.db.select( - transformedQuery.sql, - fileCache?.frontmatter ?? {} + transformedQuery.sql, + variables ); this.el.empty() From e756657101fa016207c9e47a7695190ae62fb381 Mon Sep 17 00:00:00 2001 From: Kacper Kula Date: Fri, 14 Mar 2025 10:26:35 +0000 Subject: [PATCH 3/4] chore: updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 174ba79..28ab988 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Unreleased - Added TEMPLATE view that allow to render your template with custom Handlebars template. - Improved syntax highlighting - now lines with errors will get highlighted with appropriate colour to indicate the issue +- Added @path, @fileName and @extension variables you can use inside your SQL alongside other Frontmatter properties Fixes: - Fixed parsing arguments to the `file` function. Now parameters with symbols like `[]*` should work properly (i.e. JSONPath arguments) From 51339f061a0b9dcc89f31209dd876871219fea49 Mon Sep 17 00:00:00 2001 From: Kacper Kula Date: Tue, 11 Mar 2025 15:26:54 +0000 Subject: [PATCH 4/4] chore: updating documentation --- docs/using-properties.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/using-properties.md b/docs/using-properties.md index 29e99df..012de55 100644 --- a/docs/using-properties.md +++ b/docs/using-properties.md @@ -1,4 +1,25 @@ # Using properties You can use properties to customise your queries and make them dynamic. This can be helpful if you plan to reuse your queries accross multiple files or use them in the templates. -You can refer to your properties by prepending them with an `@` symbol, for example `@year` to refer to `year` property. \ No newline at end of file +You can refer to your properties by prepending them with an `@` symbol, for example `@year` to refer to `year` property. + +## Built-in variables +On top of properties current file exposes in the properties, you can also use the following properties that will automatically get exposed: + +| Property | Description | Sample value | +| ---------- | ------------------------------------------- | ---------------- | +| @path | Full path of the file you're editing | `folder/file.md` | +| @fileName | Filename with extension | `file.md` | +| @extension | Extension of the file (without leading dot) | `md` | + + +### Example 1: Selecting just the current file from the files table + +```sql +SELECT * FROM files WHERE path = @path +``` + +### Example2: Select tags of the current file +```sql +SELECT * FROM tags WHERE path = @path +``` \ No newline at end of file