quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.77k stars 308 forks source link

Date YAML is processed before shortcode is evaluated #9419

Open emitanaka opened 5 months ago

emitanaka commented 5 months ago

Bug description

It seems the date YAML is processed before the shortcode is evaluated.

Steps to reproduce

For index.qmd:

---
title: "{{< var date >}}"
date: "{{< var date >}}"
---

_variables.yml

date: 2024/04/19

Expected behavior

The result should be the same as below:

---
title: "{{< var date >}}"
date: "2024/04/19"
---
Screenshot 2024-04-19 at 6 18 31 PM

Actual behavior

Results in Invalid Date

Screenshot 2024-04-19 at 6 13 16 PM

Your environment

Quarto check output

Quarto 1.5.30 [✓] Checking versions of quarto binary dependencies... Pandoc version 3.1.13: OK Dart Sass version 1.70.0: OK Deno version 1.41.0: OK Typst version 0.11.0: OK [✓] Checking versions of quarto dependencies......OK [✓] Checking Quarto installation......OK Version: 1.5.30 Path: /Applications/quarto/bin

[✓] Checking tools....................OK TinyTeX: (not installed) Chromium: (not installed)

[✓] Checking LaTeX....................OK Using: Installation From Path Path: /Library/TeX/texbin Version: 2023

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK Version: 3.9.6 Path: /Users/emitanaka/.virtualenvs/r-reticulate/bin/python3 Jupyter: (None)

  Jupyter is not available in this Python installation.
  Install with python3 -m pip install jupyter

[✓] Checking R installation...........OK Version: 4.3.3 Path: /Library/Frameworks/R.framework/Resources LibPaths:

[✓] Checking Knitr engine render......OK

cscheid commented 5 months ago

This is a smaller repro:

---
title: "{{< meta date-val >}}"
date: "{{< meta date-val >}}"
date-val: 2024/04/19
---
cscheid commented 5 months ago

This is unfortunately something that won't be easy to fix. The date field is somewhat special and needs to be declared explicitly as a string (because it's handled in Typescript before Pandoc is called), while shortcodes are handled during Pandoc's rendering.

baptiste commented 4 months ago

I've been using a workaround for a while to avoid the special treatment of date: and I wonder if it's a common enough pattern/need that it could be made into a legit/documented alternative. Could one use, say, an alternative field such as rawdate:, which does not get pre-processed by Deno, but could perhaps be recognised by quarto and later used to overwrite the date (if not supplied explicitly) with a lightweight Lua filter?

mcanouil commented 4 months ago

I think, the date-format could be used for such purpose. With for instance the use of a keyword (to be defined), e.g., date-format: identity.

Note that you can use the date-format directly:

Quarto documentHTML
````qmd --- title: "Quarto Playground" format: html date: "2000-01-01" date-format: "[My custom date format]" --- This is a playground for Quarto. ```` image

Edit: ok, kind of unexpected/hilarious, but this works with shortcode

Quarto documentHTML
````qmd --- title: "Quarto Playground" format: html date: "2021-09-01" date-format: "[{{< meta date-val >}}]" date-val: 2024/04/19 --- This is a playground for Quarto. ```` image

Note that it means the date formatting is now for the user to deal with.

baptiste commented 4 months ago

True, and that does offer a workaround for the original query:

---
title: "{{< var date >}}"
format: html
date: "2021-09-01"
date-format: "[{{< var date >}}]"
---

This is a playground for Quarto.

in a project with _variables.yml does pick up the date: for both fields (obviously inserted as-is without normalising/formatting). I guess it might be worth adding an explicit note in the docs that this is a way to get arbitrary dates as-is, including those injected via shortcodes. It is not super elegant though, since date-format doesn't really sound like it should hold the data itself, so personally I'd prefer a date-format: raw (or identity) and an associated date-raw: field to hold the actual content.

emitanaka commented 4 months ago

Nick hack @mcanouil ! That'll work well for what I needed, thanks!