A YAML block inside a `~~~` fenced code block is applied as document metadata

Open Beginner friendly
#15,431 3 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
markdown, typescript, yaml
Domain
backend

Research direction

Start in src/core/yaml.ts at readYamlFromMarkdown and inspect kRegexFencedCode, also used by skipRegexpAll. Compare its handling of backtick fences with tilde fences, then find the related tests for YAML extraction. Done means YAML inside a tilde fenced code block is ignored as metadata, while the block remains code and existing backtick behavior still works.

Written by the indexing model from the issue text.

Description

enhancement
Bug description

Quarto strips backtick-fenced code before looking for YAML metadata blocks in the document body, but not tilde-fenced code. A --- ... --- block inside a ~~~ fence is therefore read as front matter: its keys (lang, toc, format, execute, filters, ...) take effect. Pandoc treats the same text as a code block and ignores it, as it does for a backtick fence. The two fence styles are equivalent in CommonMark and Pandoc Markdown, so this looks like an oversight.

It matters for documents that show front matter as an example, for instance a tutorial on Quarto itself written with ~~~ fences so that it can contain a literal triple backtick. The example silently changes the real document.

Reproducible example

x.qmd (markdown engine, so no R or Python is involved):

---
title: Tilde fence
engine: markdown
---

Text before the fence.

~~~
---
lang: fr
toc: true
---
~~~

Text after the fence.
quarto render x.qmd --to html

Expected: <html lang="en">, and the fenced block shown as a code block, as with a backtick fence (the same file with ``` in place of ~~~ behaves like this).

Actual: <html ... lang="fr" xml:lang="fr">. The block is applied as metadata, and also appears as a code block in the output. A Lua filter that dumps doc.meta sees lang = fr. (quarto pandoc -f markdown -s x.qmd alone leaves lang unset, with the block as a CodeBlock.)

With a heading in the document, toc: true from inside the fence also produces a table of contents. With the knitr engine, execute: echo: false inside the fence hides all code in the document.

Cause

src/core/yaml.ts: readYamlFromMarkdown deletes fenced code with

const kRegexFencedCode = /^([\t >]*`{3,})[^`\n]*\n[\W\w]*?\n\1\s*$/gm;

(line 49 at commit 83432c0). The pattern only matches a run of backticks, so a ~~~ fence is not removed before kRegExYAML is applied. Allowing ~{3,} as well (with the closing fence being the same character, at least as long) would fix it. The same constant is used by the skipRegexpAll call at line 144.

Environment
$ quarto --version
1.11.9
$ quarto pandoc --version | head -1
pandoc 3.10

macOS; the regex is platform independent.

Related

#3004: the same kind of problem for a --- inside a backtick fence, which kRegexFencedCode addressed.

Dominant language
JavaScript
Stars
6.2k
Forks
496
Avg merge
15h 47m
Merged PRs (30d)
87

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quarto-dev/quarto-cli

All issues in quarto-dev/quarto-cli

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.