python-frontmatter icon indicating copy to clipboard operation
python-frontmatter copied to clipboard

How to write metadata with indentation?

Open Guts opened this issue 5 months ago • 0 comments

Thanks for this useful package!

I'm using it to homogenize YAML frontmatter through thousands of Markdown files but I can't figure out how to keep the source file indentation (to 4):

Input Markdown:

---
date: 2023-05-12
title: Sample
tags:
    - test
    - sample
---

# Title 1

Script:

[...]
# write new version
with md_filepath.open(mode="w", encoding="UTF-8") as out_file:
    # frontmatter.dump(content, out_file) # not working, using workaround
    out_file.write(
        frontmatter.dumps(content, sort_keys=False, **{"indent": 4})
    )

Output Markdown:

See tags values

---
processed: true
date: 2023-05-12
title: Sample
tags:
- test
- sample
---

# Title 1

Guts avatar Feb 21 '24 14:02 Guts