Skip to content

Commit dd9d840

Browse files
cscheidcwickham
andauthored
document quarto.metadata and quarto.variables (#1798)
* document q.metadata and q.variables * Update docs/extensions/lua-api.qmd Co-authored-by: Charlotte Wickham <charlotte.wickham@posit.co> * Update docs/extensions/lua-api.qmd Co-authored-by: Charlotte Wickham <charlotte.wickham@posit.co> * Update docs/extensions/lua-api.qmd Co-authored-by: Charlotte Wickham <charlotte.wickham@posit.co> --------- Co-authored-by: Charlotte Wickham <charlotte.wickham@posit.co>
1 parent c3d19c6 commit dd9d840

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

docs/extensions/lua-api.qmd

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,114 @@ Quarto offers the following helper functions for shortcode developers, to be typ
266266
|----------|-------------|
267267
| `quarto.shortcode.read_arg(args, [n])` | Returns the `n`-th argument of the shortcode invocation |
268268
| `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.
273+
274+
| Function | Description |
275+
|---------------------|---------------------------------------------------|
276+
| `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:
279+
280+
``` lua
281+
function Meta(meta)
282+
-- Get a simple metadata value
283+
local title = quarto.metadata.get("title")
284+
285+
-- Check if a value exists before using it
286+
local customField = quarto.metadata.get("custom-field")
287+
if customField then
288+
-- Process the metadata value
289+
local value = pandoc.utils.stringify(customField)
290+
end
291+
292+
return meta
293+
end
294+
```
295+
296+
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:
297+
298+
``` lua
299+
function Pandoc(doc)
300+
-- Get metadata as MetaValue
301+
local metaValue = quarto.metadata.get("description")
302+
303+
-- Convert to string for processing
304+
local description = pandoc.utils.stringify(metaValue)
305+
306+
-- Handle complex metadata structures
307+
local keywords = quarto.metadata.get("keywords")
308+
if type(keywords) == "table" then
309+
-- Process as a list or map
310+
for k, v in pairs(keywords) do
311+
quarto.log.output(pandoc.utils.stringify(v))
312+
end
313+
end
314+
315+
return doc
316+
end
317+
```
318+
319+
### Variables Access
320+
321+
Quarto projects can define variables in `_variables.yml` files. These variables can be accessed programmatically from Lua using the `quarto.variables.get()` function.
322+
323+
| Function | Description |
324+
|---------------------|---------------------------------------------------|
325+
| `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")
350+
if featureFlag then
351+
quarto.log.output("Feature flag: " .. featureFlag)
352+
end
353+
354+
return doc
355+
end
356+
```
357+
358+
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")
373+
if enabled == "true" then
374+
-- Feature is enabled
375+
end
376+
377+
return meta
378+
end
379+
```

0 commit comments

Comments
 (0)