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) 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 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()) 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()