Skip to content

fix: preserve YAML string quotes in frontmatter#43

Open
brettdavies wants to merge 1 commit intobutler54:mainfrom
brettdavies:fix/preserve-yaml-quotes
Open

fix: preserve YAML string quotes in frontmatter#43
brettdavies wants to merge 1 commit intobutler54:mainfrom
brettdavies:fix/preserve-yaml-quotes

Conversation

@brettdavies
Copy link
Copy Markdown

Summary

Set preserve_quotes=True on the ruamel.yaml YAML instance so that quoted strings in frontmatter are preserved during round-trip formatting.

Problem

Without this, ruamel.yaml strips "unnecessary" quotes from YAML string values:

# Before mdformat
title: "My Post"

# After mdformat (current)
title: My Post

Fix

One-line change in plugin.py:

 yaml = ruamel.yaml.YAML()
+yaml.preserve_quotes = True

With preserve_quotes=True, ruamel.yaml loads strings into special subclasses (DoubleQuotedScalarString, SingleQuotedScalarString) that preserve their original quote style when dumped back. This is the standard ruamel.yaml approach for lossless round-tripping.

Fixes #42

Set `preserve_quotes=True` on the ruamel.yaml YAML instance so that
quoted strings in frontmatter are preserved during round-trip formatting.

Without this, ruamel.yaml strips "unnecessary" quotes, producing
unwanted diffs and potentially breaking tooling that relies on quoted
strings.

Fixes butler54#42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve YAML string quotes in frontmatter (preserve_quotes=True)

1 participant