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.24k stars 82 forks source link

Bug: HTML comment treated as paragraph #1160

Closed usernotnull closed 1 month ago

usernotnull commented 2 months ago

Describe the Bug

I used the plugin Spaced Repetition, which adds an HTML comment below every paragraph that has been revised. The rule https://platers.github.io/obsidian-linter/settings/spacing-rules/#paragraph-blank-lines treats those comments which are added below the paragraph as another paragraph, and adds an extra empty line in between.

How to Reproduce

Input:

Question line 1.
?
Answer line 1.
<!--SR:!2024-07-10,59,310-->

Question line 2.
?
Answer line 2.
<!--SR:!2024-07-10,59,310-->

Current Output

Question line 1.
?
Answer line 1.

<!--SR:!2024-07-10,59,310-->

Question line 2.
?
Answer line 2.

<!--SR:!2024-07-10,59,310-->

Expected Behavior

Question line 1.
?
Answer line 1.
<!--SR:!2024-07-10,59,310-->

Question line 2.
?
Answer line 2.
<!--SR:!2024-07-10,59,310-->

Device

pjkaufman commented 1 month ago

Hey @usernotnull , I think this is actually working as expected from a dev perspective. I say this because if you look at the output of the AST parser for the input

Question line 1.
?
Answer line 1.
<!--SR:!2024-07-10,59,310-->

Question line 2.
?
Answer line 2.
<!--SR:!2024-07-10,59,310-->

You get the following output:

{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "Question line 1.\n?\nAnswer line 1.",
          "position": {
            "start": {
              "line": 1,
              "column": 1,
              "offset": 0
            },
            "end": {
              "line": 3,
              "column": 15,
              "offset": 33
            }
          }
        }
      ],
      "position": {
        "start": {
          "line": 1,
          "column": 1,
          "offset": 0
        },
        "end": {
          "line": 3,
          "column": 15,
          "offset": 33
        }
      }
    },
    {
      "type": "html",
      "value": "<!--SR:!2024-07-10,59,310-->",
      "position": {
        "start": {
          "line": 4,
          "column": 1,
          "offset": 34
        },
        "end": {
          "line": 4,
          "column": 29,
          "offset": 62
        }
      }
    },
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "Question line 2.\n?\nAnswer line 2.",
          "position": {
            "start": {
              "line": 9,
              "column": 1,
              "offset": 67
            },
            "end": {
              "line": 11,
              "column": 15,
              "offset": 100
            }
          }
        }
      ],
      "position": {
        "start": {
          "line": 9,
          "column": 1,
          "offset": 67
        },
        "end": {
          "line": 11,
          "column": 15,
          "offset": 100
        }
      }
    },
    {
      "type": "html",
      "value": "<!--SR:!2024-07-10,59,310-->",
      "position": {
        "start": {
          "line": 12,
          "column": 1,
          "offset": 101
        },
        "end": {
          "line": 12,
          "column": 29,
          "offset": 129
        }
      }
    }
  ],
  "position": {
    "start": {
      "line": 1,
      "column": 1,
      "offset": 0
    },
    "end": {
      "line": 12,
      "column": 29,
      "offset": 129
    }
  }
}

As you may have noticed, both html comments are their own AST element. This means that they are not considered to be a part of the paragraph, at least from a dev perspective. Thus when paragraph blank lines runs you see the blank line added after Answer line 1. and Answer line 2. and a blank line is added before Question line 2.. All of this is expected from my understanding of how things work.

Could you elaborate on what the issue is that this may be causing? I ask because I assume those comments are from another plugin which allows it to find values and thus the Linter is somehow breaking them.

My suggestion with just the information I know right now is to go ahead and just use ranged ignores to make sure the Linter does not lint these sections since it may be breaking another plugin.

I could be misunderstanding something so please feel free to elaborate on this issue.

I will close this issue at the end of the month if I do not hear back prior since I do need more information to be able to do anything about this.

usernotnull commented 1 month ago

I see the issue.

The plugin I mentioned unfortunately uses these html comments as a way to save the progress. Each html comment must not be separated from its previous paragraph.

Unfortunately it doesn't make sense to manually add ignore ranges as the comments are automatically generated.

This means the only way to do this is to ignore all files where I want to used Spaced Repetition, which is far from ideal.

Not sure if I should pursue the matter here or open an issue in the other plugin.

Kindly keep this open for longer as I'm on vacation. I'll reply in 2 weeks with what I got from the other plugin as well.

pjkaufman commented 1 month ago

One thing you could do is use a custom replacement to try to make it so that that blank line is removed. If I remember correctly, it runs after paragraph blank lines.

So something like the following should do the trick: find replace flags
\n\n<\!--SR: \n<!--SR: g

Please test that this does work before committing to its use.

This may need tweaking if you use these questions in blockquotes/callouts.

usernotnull commented 1 month ago

Using a custom regex should work since they run after the rules indeed. This seems like the most reasonable solution.