rubocop / rubocop-md

RuboCop for Markdown code snippets
MIT License
71 stars 13 forks source link

Indented codeblocks throw off all future codeblocks #28

Open Earlopain opened 8 months ago

Earlopain commented 8 months ago

rubocop.yml

require:
  - rubocop-md

AllCops:
  TargetRubyVersion: 3.2
  NewCops: disable

grafik grafik

test.md since I still don't know how to properly escape markdown here.

I'll try to check this out myself if that's fine with you @palkan, but in case I don't manage like last time I'll let you know. That good?

palkan commented 8 months ago

I'll try to check this out myself if that's fine with you @palkan, but in case I don't manage like last time I'll let you know. That good?

Yeah, sounds good. Feel free to reach out / submit a draft PR if you encounter any struggles

palkan commented 8 months ago

What would you consider a solution here? Since we lint MD files by "converting" them into Ruby files, the Layout/InitialIndentation is an expected behaviour (though annoying, for sure). Probably, we can un-indent code blocks during pre-processing and restore them back in the end; the question is how to distinguish between valid (within lists) and invalid indentation.

Earlopain commented 8 months ago

Is this extension supposed to care about the indentation of codeblocks themselves in the first place? If I put my codeblock 10 spaces to the right then all I really care about is that the code inside is properly indented relative to that. Anything else feels out of scope, to me at least.

One thing I feel that would be a big help here is by using rubocops 3rd party template support that got added in 1.45. https://github.com/rubocop/rubocop/pull/10839 It allows extensions like this one to process non-rb files and preprocess them appropriately. Basically what is already happening now but "officially sanctioned". Autocomplete like this would be easier to implement I believe, without the comment workaround going on right now.

Would you be open to exploring that? It is pretty involved for this issue and probably more work that necessary. Just want to get your feelings on this, perhaps for something in the future.

palkan commented 8 months ago

using rubocops 3rd party template support that got added in 1.45.

Oh, haven't seen this; awesome! Yeah, we should migrate to this new API.

Earlopain commented 8 months ago

Great to hear! I think I'll try to start with that as I feel it will make fixing this as a second step easier. I feel like nobody has complainted about this issue yet so you can take a bit of time for this.

Ravlen commented 2 months ago

Coming here as I tested this to link our 2k+ markdown doc files, and see the same issue.

@palkan Re: Probably, we can un-indent code blocks during pre-processing and restore them back in the end; the question is how to distinguish between valid (within lists) and invalid indentation.

My suggestion would be to count the number of spaces before the start of the code block, and trim that amount until the end of the code block. So:

List item:

- ```ruby
  valid, trim 2 characters from every line.

After trimming:

List item:

```ruby
valid, trim 2 characters from every line.
valid, trim 4 from every line
  invalid indentation, trim 4 from every line, but this line still ends up with 2 extra spaces