You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/extensions/lua-api.qmd
+111Lines changed: 111 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -266,3 +266,114 @@ Quarto offers the following helper functions for shortcode developers, to be typ
266
266
|----------|-------------|
267
267
|`quarto.shortcode.read_arg(args, [n])`| Returns the `n`-th argument of the shortcode invocation |
268
268
|`quarto.shortcode.error_output(name, message_or_args, context)`| Creates output to be used by shortcodes to depict an execution error, consistently with how Quarto shows such outputs |
269
+
270
+
### Metadata Access
271
+
272
+
Quarto provides programmatic access to document and project metadata from Lua filters and scripts. This is useful when you need to access metadata values within filter logic rather than in shortcode contexts.
|`quarto.metadata.get(key)`| Return the value of a metadata entry as a `pandoc.MetaValue`, or `nil` if the key is missing. This is the Lua equivalent of the `{{{< meta key >}}}` shortcode. |
277
+
278
+
The `key` parameter can reference top-level metadata or use dot notation to access nested values. For example:
The function returns a `pandoc.MetaValue`, which may be a `MetaString`, `MetaInlines`, `MetaList`, `MetaMap`, or other [Pandoc metadata types](https://pandoc.org/lua-filters.html#type-metavalue). You can use `pandoc.utils.stringify()` to convert these to strings when needed:
Quarto projects can define variables in `_variables.yml` files. These variables can be accessed programmatically from Lua using the `quarto.variables.get()` function.
|`quarto.variables.get(name)`| Return the value of a variable from `_variables.yml` as a string, or `nil` if the variable is not defined. This is the Lua equivalent of the `{{{< var name >}}}` shortcode. |
326
+
327
+
For example, if your `_variables.yml` contains:
328
+
329
+
```yaml
330
+
site-url: https://example.com
331
+
api-version: v2.1
332
+
feature-enabled: true
333
+
```
334
+
335
+
You can access these values in your Lua filter:
336
+
337
+
```lua
338
+
function Pandoc(doc)
339
+
-- Get a variable value
340
+
local siteUrl = quarto.variables.get("site-url")
341
+
342
+
-- Use in conditional logic
343
+
local apiVersion = quarto.variables.get("api-version")
344
+
if apiVersion == "v2.1" then
345
+
-- Add version-specific content
346
+
end
347
+
348
+
-- Check if a variable exists
349
+
local featureFlag = quarto.variables.get("feature-enabled")
Variables are always returned as strings (or `nil` if not found), so you may need to perform type conversions when working with numeric or boolean values:
359
+
360
+
``` lua
361
+
function Pandoc(doc)
362
+
-- Get a numeric variable
363
+
local maxItems = quarto.variables.get("max-items")
364
+
if maxItems then
365
+
local count = tonumber(maxItems)
366
+
if count and count > 10 then
367
+
-- Process with numeric value
368
+
end
369
+
end
370
+
371
+
-- Get a boolean variable
372
+
local enabled = quarto.variables.get("feature-enabled")
0 commit comments