Skip to content

Using --metadata-file to set metadata does not seem to work properly #613

Closed

Description

@cderv

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 Document

I 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.yml

But 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.

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

quarto-cli/src/core/yaml.ts

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

// 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

  1. self-assigned this
    on Apr 12, 2022
  2. cscheid commented on Apr 12, 2022

    @cscheid
    Member

    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.

  3. cderv commented on Apr 12, 2022

    @cderv
    MemberAuthor

    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
    diff --git a/src/command/preview/cmd.ts b/src/command/preview/cmd.ts
    index 795de7304..51e4dd747 100644
    --- a/src/command/preview/cmd.ts
    +++ b/src/command/preview/cmd.ts
    @@ -262,7 +262,7 @@ export const previewCommand = new Command()
    
         // extract pandoc flag values we know/care about, then fixup args as
         // necessary (remove our flags that pandoc doesn't know about)
    -    const flags = await parseRenderFlags(args);
    +    const flags = parseRenderFlags(args);
         args = fixupPandocArgs(args, flags);
    
         // if this is a single-file html preview within a project
    diff --git a/src/command/render/cmd.ts b/src/command/render/cmd.ts
    index 61c412c26..43ca47a7b 100644
    --- a/src/command/render/cmd.ts
    +++ b/src/command/render/cmd.ts
    :...skipping...
    diff --git a/src/command/preview/cmd.ts b/src/command/preview/cmd.ts
    index 795de7304..51e4dd747 100644
    --- a/src/command/preview/cmd.ts
    +++ b/src/command/preview/cmd.ts
    @@ -262,7 +262,7 @@ export const previewCommand = new Command()
    
         // extract pandoc flag values we know/care about, then fixup args as
         // necessary (remove our flags that pandoc doesn't know about)
    -    const flags = await parseRenderFlags(args);
    +    const flags = parseRenderFlags(args);
         args = fixupPandocArgs(args, flags);
    
         // if this is a single-file html preview within a project
    diff --git a/src/command/render/cmd.ts b/src/command/render/cmd.ts
    index 61c412c26..43ca47a7b 100644
    --- a/src/command/render/cmd.ts
    +++ b/src/command/render/cmd.ts
    @@ -164,7 +164,7 @@ export const renderCommand = new Command()
    
         // extract pandoc flag values we know/care about, then fixup args as
         // necessary (remove our flags that pandoc doesn't know about)
    -    const flags = await parseRenderFlags(args);
    +    const flags = parseRenderFlags(args);
         args = fixupPandocArgs(args, flags);
    
         // run render on input files
    diff --git a/src/command/render/flags.ts b/src/command/render/flags.ts
    index 5772f3707..b9b46b43c 100644
    --- a/src/command/render/flags.ts
    +++ b/src/command/render/flags.ts
    @@ -30,7 +30,7 @@ import { RenderFlags } from "./types.ts";
    
     export const kStdOut = "-";
    
    -export async function parseRenderFlags(args: string[]) {
    +export function parseRenderFlags(args: string[]) {
       const flags: RenderFlags = {};
    
       const argsStack = [...args];
    @@ -287,7 +287,7 @@ export async function parseRenderFlags(args: string[]) {
             arg = argsStack.shift();
             if (arg) {
               if (existsSync(arg)) {
    -            const metadata = await readYamlFromMarkdownFile(arg);
    +            const metadata = readYamlFromMarkdownFile(arg);
                 flags.metadata = { ...flags.metadata, ...metadata };
               }
             }

    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.

  4. cscheid commented on May 4, 2022

    @cscheid
    Member

    @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.

  5. jjallaire commented on May 4, 2022

    @jjallaire
    Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions