I am using https://github.com/waiting-for-dev/front_matter_parser to parse and update values in the frontmatter of my markdown files.
The following code removes the original indentation of two spaces for array values:
require 'front_matter_parser'
class FrontMatterUpdater
def self.run(path)
parsed = FrontMatterParser::Parser.parse_file(path)
front_matter = parsed.front_matter
front_matter['redirect_from'] = Array(front_matter['redirect_from'])
front_matter['redirect_from'] << 'new_entry2'
front_matter['redirect_from'].uniq!
new_content = [YAML.dump(front_matter), '---', "\n\n", parsed.content].join
File.write(path, new_content)
end
end
FrontMatterUpdater.run('test.md')
Content of my test.md file (same path):.
---
redirect_from:
- new_entry0
- new_entry1
---
Script result (indentation has been removed by the parser):
---
redirect_from:
- new_entry0
- new_entry1
- new_entry2
---
YAML indentation for array in hash confirms that both versions (indented and not) is valid YAML syntax.
But I'd love to keep the indentation for readability.
Do you see any option to keep the indentation of 2 spaces for the array values?