Michael Naumov71 downloadsKeeps the formatting of your YAML front matter - comments, quoting, blank lines, indentation and inline lists - when any plugin edits a property.
Keeps your YAML front matter the way you wrote it when a plugin changes a property. When any plugin edits a property of a note in Obsidian, even through the documented API, Obsidian rewrites the note's whole front matter block. Comments disappear, quotes are dropped, blank lines go, and inline lists such as [a, b] are expanded to one item per line. That happens to every property in the block, including the ones the plugin never touched, and even when it changed nothing at all.
With this plugin enabled, a property change rewrites only the property that changed, and a change that changes nothing writes nothing. It covers every plugin that edits front matter through app.fileManager.processFrontMatter(), which includes some of Obsidian's own edits, and every plugin built on obsidian-dev-utils. There are no settings: installing the plugin is the switch, and disabling it gives you Obsidian's own behavior back.
The documentation is a demo vault. Its notes explain what changes and why, with buttons that edit a sample note's properties the same way other plugins do, so you can compare the stored text with the plugin on and off.
Start reading here — it is plain markdown, so it works on GitHub with nothing installed.
A copy of the vault ships with every release. You can access it via any of the following:
preserve-frontmatter-formatting-demo-vault.zip from the Releases. It unzips into a single preserve-frontmatter-formatting-demo-vault-<version> folder.demo-vault/ in this repository.[a, b] lists, and folded or literal text. 01 Edit a property from any pluginapp.fileManager.processFrontMatter() are covered by a patch of that method. Plugins built on obsidian-dev-utils from 105.2.0 onwards write front matter through their own copy of the library, and this plugin switches that copy's formatting-preserving mode on for as long as it is enabled.A value that does change is written in the default style, so a changed quoted string comes back unquoted, the same as Obsidian would write it.
processFrontMatter() for Markdown files rather than wrapping it, because the formatting is lost inside that method, where a wrapper cannot reach. Another plugin that patched the same method before this one loaded is therefore not called for Markdown files; one that patches it after wraps this plugin as usual.yaml library can place them, which is not perfectly. That library documents its own comment handling as not completely stable, in particular for trailing comments. Comments on lines the change does not reach are never re-emitted at all, so they are safe. A comment beside a property that is removed or rewritten may go with it.The plugin is available in the official Community Plugins repository.
To install the latest beta release of this plugin (regardless if it is available in the official Community Plugins repository or not), follow these steps:
Add plugin button once and wait a few seconds for the plugin to install.By default, debug messages for this plugin are hidden.
To show them, run the following command in the DevTools Console:
window.DEBUG.enable('preserve-frontmatter-formatting');
For more details, refer to the documentation.
All notable changes to this project will be documented in the CHANGELOG.
Contributions are welcome — see CONTRIBUTING to get set up.
See my other Obsidian resources.