|
| 1 | +-- Version-aware shortcode for linking to prerelease docs. |
| 2 | +-- |
| 3 | +-- Usage: {{< prerelease-docs-url 1.9 >}} |
| 4 | +-- |
| 5 | +-- Returns "prerelease." when the referenced version's docs live on |
| 6 | +-- prerelease.quarto.org, or "" when they're on quarto.org. |
| 7 | + |
| 8 | +local function handler(args, kwargs, meta, raw_args, context) |
| 9 | + local ref_version = quarto.shortcode.read_arg(args, 1) |
| 10 | + if ref_version == nil then |
| 11 | + return quarto.shortcode.error_output( |
| 12 | + "prerelease-docs-url", |
| 13 | + "requires a version argument, e.g. {{< prerelease-docs-url 1.9 >}}", |
| 14 | + context |
| 15 | + ) |
| 16 | + end |
| 17 | + -- Strip surrounding quotes that may be preserved in text contexts |
| 18 | + ref_version = ref_version:gsub('^"(.*)"$', '%1'):gsub("^'(.*)'$", '%1') |
| 19 | + |
| 20 | + -- On the prerelease site, always link to prerelease |
| 21 | + if quarto.project.profile:includes("prerelease-docs") then |
| 22 | + return pandoc.Str("prerelease.") |
| 23 | + end |
| 24 | + |
| 25 | + -- Guard against missing or invalid version metadata |
| 26 | + local version_str = meta["version"] and pandoc.utils.stringify(meta["version"]) or nil |
| 27 | + if not version_str or version_str == "" then |
| 28 | + return quarto.shortcode.error_output( |
| 29 | + "prerelease-docs-url", |
| 30 | + "missing 'version' in document metadata", |
| 31 | + context |
| 32 | + ) |
| 33 | + end |
| 34 | + |
| 35 | + -- Compare referenced version to this branch's version using |
| 36 | + -- pandoc.types.Version for correct element-wise comparison (1.12 > 1.9) |
| 37 | + local ok_branch, branch_version = pcall(pandoc.types.Version, version_str) |
| 38 | + local ok_ref, ref = pcall(pandoc.types.Version, ref_version) |
| 39 | + |
| 40 | + if not ok_branch then |
| 41 | + return quarto.shortcode.error_output( |
| 42 | + "prerelease-docs-url", |
| 43 | + "invalid metadata version '" .. version_str .. "'", |
| 44 | + context |
| 45 | + ) |
| 46 | + end |
| 47 | + if not ok_ref then |
| 48 | + return quarto.shortcode.error_output( |
| 49 | + "prerelease-docs-url", |
| 50 | + "invalid version argument '" .. ref_version .. "'", |
| 51 | + context |
| 52 | + ) |
| 53 | + end |
| 54 | + |
| 55 | + if ref <= branch_version then |
| 56 | + return pandoc.Str("") |
| 57 | + else |
| 58 | + return pandoc.Str("prerelease.") |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +return {["prerelease-docs-url"] = handler} |
0 commit comments