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.13k stars 79 forks source link

Bug: YAML Key Sort doesn't support some YAML syntax #912

Open redactedscribe opened 9 months ago

redactedscribe commented 9 months ago

Apologies if this issue has already been created. Not quite sure what to search for.

Describe the Bug

According to https://www.yamllint.com/, this is valid:

---
AAA:
  - BBB: x
    CCC: x
  - DDD: x
    EEE: x
    FFF:
      - GGG: x
      - HHH
    III: x
FFF:
  -

FFF is present twice, but at different levels, and therefore should be unique, but obsidian-linter still complains with (even if line with GGG has : x removed):

plugin:obsidian-linter:43 [Obsidian Linter] Lint File Error in File 'Test.md':
Error "YAML Key Sort" encountered an error in the YAML:  bad indentation of a sequence entry (35:8)

 32 |     FFF:
 33 |   - GGG: x
 34 |   - HHH
 35 |     III: x
-------------^
 36 | FFF:
 37 |   - 
YAMLException: bad indentation of a sequence entry (35:8)

 32 |     FFF:
 33 |   - GGG: x
 34 |   - HHH
 35 |     III: x
-------------^
 36 | FFF:
 37 |   - 
    at Cc (plugin:obsidian-linter:18:944)
    at K (plugin:obsidian-linter:18:978)
    at dc (plugin:obsidian-linter:25:696)
    at Wt (plugin:obsidian-linter:25:4861)
    at Og (plugin:obsidian-linter:25:2150)
    at Wt (plugin:obsidian-linter:25:4870)
    at Ig (plugin:obsidian-linter:25:7247)
    at Mc (plugin:obsidian-linter:26:258)
    at Fg (plugin:obsidian-linter:26:469)
    at Mi (plugin:obsidian-linter:104:223)
Uc @ plugin:obsidian-linter:43

How to Reproduce

Steps to reproduce the behavior:

  1. Insert the front matter above into a file.
  2. Lint with YAML Key Sort enabled (screenshot below for settings).

Expected Behavior

Support this YAML syntax.

Screenshots

Obsidian_2023-10-11_00-21-34

Device

Thanks.

redactedscribe commented 9 months ago

Other observations:

Technically, the above doesn't happen with the example code in the first comment, but it's what I'm seeing happen with my real-world example because my FFF so-to-speak is earlier in the alphabet than the AAA. The reason seems to be because of the YAML Sort Order for Other Keys setting being set to Ascending Alphabetical.

---
AAA:
  - BBB: x
    CCC: x
  - DDD: x
    EEE: x
    A:
      - GGG: x
      - HHH: 
    III: x
A:
  -
---

... linted turns into:

---
A:
  - GGG: x
  - HHH: 
    III: x
AAA:
  - BBB: x
    CCC: x
  - DDD: x
    EEE: x
---

This bug report is a bit of a mess, sorry. Hopefully you can make sense of it.

pjkaufman commented 9 months ago

If this is an issue with the YAML after the Linter runs, I can do something about it. Jowever if this is from the parser, I cannot do anything about that at this time.

redactedscribe commented 9 months ago

I believe the linter is the cause as I'm manually triggering linting using Ctrl+Alt+L.

pjkaufman commented 9 months ago

It says there is bad indentation. Is the YAML line it is referring to the same as the initial value? If so, there may be nothing I can do. Generally bad identation means it was expecting more or less spaces than were provided.

pjkaufman commented 9 months ago

The error provided seems to happen before YAML Key Sort happens. That would mean I likely cannot fix it via the Linter code since it is an external library that parses the YAML.

pjkaufman commented 9 months ago

YAML Key Sort definitely needs work. It was a workaround to get it to work, but it cannot handle nesting due to its implementation. It would have to be rewritten to handle sorting nested values. However if it is pulling nested values out of their nesting level, that may ne addressable.

redactedscribe commented 9 months ago

However if it is pulling nested values out of their nesting level, that may ne addressable.

That it is doing, and at times removing them without them even being merged.

pjkaufman commented 8 months ago

I will need to double check what modified this. I am able to reproduce in Obsidian, but YAML Key Sort does not seem to affect this directly. Some other rule seems to be modifying it first which is causing the issue. I will have to see which rule is making that change and make the modification in that rule.

mariomui commented 8 months ago

@pjkaufman Do you want me to work on this? cuz there's overlap with my sort frontmatter, i just dont test on mobile so i'll need some help on that front.

pjkaufman commented 8 months ago

Hey @mariomui , that would be great. I can test mobile, so that should not be an issue.

pjkaufman commented 8 months ago

My main concern would be avoiding breaking existing UTs and keeping the original formatting as much as possible, but the latter is more lenient.