platers / obsidian-linter

An Obsidian plugin that formats and styles your notes with a focus on configurability and extensibility.
https://platers.github.io/obsidian-linter/
MIT License
1.22k stars 81 forks source link

FR: Add Rule to Remove Blank Lines After YAML #1046

Open igrybkov opened 7 months ago

igrybkov commented 7 months ago

Describe the Bug

I do not use H1 headings in my files, so my notes usually start with a paragraph like this:

---
aliases: []
tags: []
---
Hello, it's a note.

With paragraph-blank-lines, linter adds empty line between frontmatter and the text, which is not what I expect. The problem is this extra line is rendered by obsidian in the Live Preview mode, so it's a little bit annoying.

How to Reproduce

Steps to reproduce the behavior:

  1. Enable Paragraph blank lines
  2. Create a note like this:
    ---
    aliases: []
    tags: []
    ---
    Hello, it's a note.
  3. Format the note

Expected Behavior

The rule does not add a space between front matter and the body

---
aliases: []
tags: []
---
Hello, it's a note.

Device

Additional Context

Add any other context about the problem here.

pjkaufman commented 7 months ago

Hey @igrybkov . Thanks for putting in this issue.

I think to be consistent with other blank line rules, it would be necessary to add the blank line between the frontmatter and the paragraph content. However it should not add a blank line after a paragraph that ends a file. So the thing that would need to change here is the addition of a blank line between the frontmatter and the paragraph provided in steps to reproduce.

If you would like to remove it and do not use --- in your code, you can use a custom replace like: search: ^(---)\n{2,} replace: $1\n flags: m

Please test this regex as I am creating it from memory.

igrybkov commented 7 months ago

Hey @pjkaufman,

There's a separate rule "Add Blank Line After YAML" and I think it should be responsible for adding empty line in this case, not the paragraph rule. For me current behavior looks like an unexpected side effect. Or maybe adding a new rule like "Remove Blank Line After YAML" will be a proper solution. I'll leave it to you to decide what needs to be done.

The regex worked thought, thanks! I didn't know Linter supports custom regex rules.

pjkaufman commented 7 months ago

Gotcha. The custom regex is designed specifically for scenarios like this where the Linter has no options to change it and has been relatively consistent in what it means to do something (it is not always consistent) amd a different result is desired. Feel free to read the docs to see if there are other things like custom regex/replacement that would help out.

pjkaufman commented 7 months ago

But to sum up my opinion on this, this sounds more like a feature request to change the logic of all blank line before logic to not add it right after the YAML frontmatter. Woukd that be a correct statement?

igrybkov commented 7 months ago

That's the thing. Most of other Spacing rules end with this sentence: "unless they start or end a document". Or "except where the heading is at the beginning or end of the document" for headings. Or "Math blocks that start a document do not get an empty line before them." is one of the examples for math blocks. From what I see, the standard is to not add blank line at the beginning by any spacing rule except by "Add Blank Line After YAML".

igrybkov commented 7 months ago

And just to be specific, my testing shows that "beginning of the document" includes front matter. Just tested for both tables and code blocks and neither of them adds a blank line after front matter.

pjkaufman commented 7 months ago

And just to be specific, my testing shows that "beginning of the document" includes front matter.

The begininng of the document should be defined as the first line in the file (generally the first character). This gets a little tricky with indentation, but essentially that is the check that happens. Live Preview is something that came after this definition was added. But even then, YAML frontmatter is considered to be the required start of the document in Obsidian if it is present, so I stick with that definition. If there is a good place to clarify this, I am open to adding clarification.

Just tested for both tables and code blocks and neither of them adds a blank line after front matter.

I have not tested all scenarios referenced here recently, but so far as I recall, all of the blank line before and after logic is pretty much the same for deciding when a blank line is added. If code blocks and tables are acting differently, I need to look at that and see why since it could be that the input is not quite right which is causing the issue.

Thanks for the feddback!

igrybkov commented 7 months ago

I just realized that I had your fix with regex in place when I tested. So I removed it and rechecked again. And you're right: all spacing linters are adding the empty line after the frontmatter. Except for heading, and only because it has an option to configure this behavior called "Empty Line Between YAML and Header". So yeah, I guess it could be considered a feature request then. Or closed since there's a fix with the regex available. Thank you!

pjkaufman commented 3 weeks ago

So there is also the following issue that does embody this feature request as well: https://github.com/platers/obsidian-linter/issues/926