Repository navigation
Description
Opening this issue to discuss how --metadata-files is supposed to work.
This short example is working
---
author: "me"
format: html
metadata-files:
- meta.yml
---
# Hello
World where meta.yml is
title: My DocumentI would have expected the equivalent command line to work too if the file was not passed through yaml
---
author: "me"
format: html
---
# Hello
World quarto render test.qmd --metadata-file=meta.ymlBut in that case the title is not set. The metadata does not see
I look through the code @dragonstyle and have some question - maybe you can help.
quarto-cli/src/command/render/flags.ts
Lines 286 to 294 in 1fd5db8
| case "--metadata-file": | |
| arg = argsStack.shift(); | |
| if (arg) { | |
| if (existsSync(arg)) { | |
| const metadata = readYamlFromMarkdownFile(arg); | |
| flags.metadata = { ...flags.metadata, ...metadata }; | |
| } | |
| } | |
| break; |
readYamlFromMarkdownFile returns a promise I believe after calling readAndValidateYamlFromMarkdown
Lines 179 to 185 in 1fd5db8
| export async function readYamlFromMarkdownFile( | |
| file: string, | |
| ): Promise<{ [key: string]: unknown }> { | |
| const markdown = Deno.readTextFileSync(file); | |
| const result = await readAndValidateYamlFromMarkdown(markdown); | |
| return result; | |
| } |
and readAndValidateYamlFromMarkdown seems to expect a YAML block with --- surrounding. I don't think --metadata-file is expecting that with Pandoc.
When metadata-files is uses in YAML, the function used is readAndValidateYamlFromFile
quarto-cli/src/config/metadata.ts
Lines 60 to 70 in 1fd5db8
| // Read the yaml | |
| const filesMetadata = await Promise.all(yamlFiles.map(async (yamlFile) => { | |
| if (await exists(yamlFile)) { | |
| try { | |
| const yaml = await readAndValidateYamlFromFile( | |
| yamlFile, | |
| schema, | |
| `Validation of metadata file ${yamlFile} failed.`, | |
| ); | |
| return yaml; | |
| } catch (e) { |
which is a different function so the two way of reading the same file - it does not seem right 🤔
Anyway, I'll keep looking, but wanted to share so that maybe you can help @dragonstyle - am I missing something ?
thanks!
Activity
cscheid commentedon Apr 12, 2022
Yup, you're right that there's a problem there.
The different code paths exist because of the interaction with schema validation (which is why I'm assigning it to myself), and that's somewhat inevitable. But whatever code path is being used with the command-line option is not correctly merging in the metadata info. Let me look into it.
cderv commentedon Apr 12, 2022
Yeah I was looking into that myself, and schema validation is what makes me struggle.
FWIW, there are several issue behind since. First one is a Promise issue in the CLI parser. Here is the fix:
Git diff patch
Regarding
--metadata-file, CLI flag I would expect it to be read as-is, and then merged with all the metadata and then having validation, but not sure it works this way, and not sure it knows how to report a validation error in the external file 🤔I think this would also be possible for this external yaml file to not be validated. After all when writing the YAML, I don't think the IDE offers special linting and validation for this YAML which is not a frontmatter or a known config file.
And that is all for my 2 cents 😄 Since you are taking this over, maybe I should stop looking into that.
cscheid commentedon May 4, 2022
@jjallaire I've addressed this on e7bb9d9. Note that @cderv pointed out a discrepancy between the two behaviors, which I made uniform. Specifically, the CLI flag now expects a "pure" yaml file, while in the past it expected yaml to be extracted from a markdown file (ie. collections of
----enclosed yaml chunks). I'm flagging it here so that you can make sure this is ok.jjallaire commentedon May 4, 2022