rstudio / blogdown

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

New post writes incorrect yaml if archetype contains yaml unnamed list #684

Closed cderv closed 2 years ago

cderv commented 2 years ago

Take this test.Rmd file

---
links:
  - icon: images
    icon_pack: fas
    name: slides
    url:
  - icon: github
    icon_pack: fab
    name: code
    url:
---

Content

new_post() will at some point try to modify the YAML header, and this is where we loose the correct YAML format:

save_copy <- file.copy("test.Rmd", "test.Rmd.save")
xfun::file_string("test.Rmd")
#> ---
#> links:
#>   - icon: images
#>     icon_pack: fas
#>     name: slides
#>     url:
#>   - icon: github
#>     icon_pack: fab
#>     name: code
#>     url:
#> ---
#> 
#> Content
blogdown:::modify_yaml("test.Rmd")
xfun::file_string("test.Rmd")
#> ---
#> links:
#>   icon: images
#>   icon_pack: fas
#>   name: slides
#>   url: ~
#>   icon: github
#>   icon_pack: fab
#>   name: code
#>   url: ~
#> ---
#> 
#> Content
rmarkdown::yaml_front_matter("test.Rmd")
#> Error in yaml::yaml.load(..., eval.expr = TRUE): Duplicate map key: 'icon'

# restore
file.rename("test.Rmd.save", "test.Rmd")
#> [1] TRUE

All the - in front of the YAML list item are removed which leads to incorrect YAML.

First reported in https://community.rstudio.com/t/having-nested-elements-on-blogdown-hugo-archetypes/127761/2 by @llrs

yihui commented 2 years ago

Should be fixed now. Please try the dev version:

remotes::install_github('rstudio/blogdown')

Thanks!

The problem was from this line introduced from the PR #118: https://github.com/rstudio/blogdown/blob/be26f3a9fa68c1b3794b0c23825005108e26c413/R/utils.R#L803

cderv commented 2 years ago

Thanks