rstudio / blogdown

Create Blogs and Websites with R Markdown
https://pkgs.rstudio.com/blogdown/
1.74k stars 331 forks source link

Update Metadata changes YAML format structure of tags #693

Closed brshallo closed 2 years ago

brshallo commented 2 years ago

For example will change:

tags: ["tag1", "tag2"]

To:

tags:
  - tag1
  - tag2

format-of-tags-changed

Usually probably doesn't really matter except in cases like #647 when are dynamically generating tags.

yihui commented 2 years ago

I'm afraid that I can't do much about it. The output is generated from yaml::as.yaml(), which doesn't provide an option to write out arrays using the [] syntax.

Usually probably doesn't really matter except in cases like #647 when are dynamically generating tags.

That's true. In case of #647, you may want the function to call paste(..., collapse = '"\n "') instead, and use the bullet syntax for the array:

tags:
  - "`r your_func()`"
cderv commented 2 years ago

Regarding this

The output is generated from yaml::as.yaml(), which doesn't provide an option to write out arrays using the [] syntax.

Supporting this is still a feature request https://github.com/vubiostat/r-yaml/issues/77

brshallo commented 2 years ago

That's true. In case of #647, you may want the function to call paste(..., collapse = '"\n "') instead, and use the bullet syntax for the array:

@yihui per conversation at #647 / associated SO thread I thought programmatically passing arguments in here required using an array syntax (rather than bulleted)...?

For example this (and iterations on it that I tried) error:

tags: 
  - "`r paste(head(letters), collapse = '"\n  - "')`"
#> Error in yaml::yaml.load(..., eval.expr = TRUE) : 
#>   Parser error: while parsing a block collection at line 7, column 3 did not find expected '-' indicator at line 7, column 43
#> Calls: local ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
#>   Execution halted
#> Error: Failed to render content/post/2022-05-12-test-post/index.Rmd
#> Execution halted
yihui commented 2 years ago

@brshallo I'm not sure how to explain this hack born out of the constraints of both valid YAML syntax and valid R code:

tags: 
  - '`r paste(head(letters), collapse = "\u27\n  - \u27")`'

but I think it should work. If you really need me to explain, I can surely try. If you wrap the hack into a function, you wouldn't need so much hack (i.e., paste(..., collapse = '"\n "') should just work).

brshallo commented 2 years ago

Thanks @yihui, paste(..., collapse = '"\n - "') wrapped in the function works perfectly! Hadn't expected that to make a difference -- no need to explain 😊.

Feel free to close for my needs. (Unless wanting to keep open until the r-yaml issue mentioned above is resolved.)

Sidenote:

For the purposes of #647 / https://github.com/brshallo/funspotr/issues/8 I have spot_tags() default to first read-in the file and do a rough check on whether the function is used in either a bulleted or array format and then it sets collapse accordingly.

yihui commented 2 years ago

Great! Thanks for the update!