Merge pull request #98 from h-sphere/feat/exposing-more-file-variables

feat: exposing filename, path and extension
This commit is contained in:
Kacper Kula 2025-03-14 10:27:41 +00:00 committed by GitHub
commit c66b78f88d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 4 deletions

View file

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

View file

@ -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.
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
```

View file

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

View file

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