google / keep-sorted

keep-sorted is a language-agnostic formatter that sorts lines between two markers in a larger file.
Apache License 2.0
132 stars 15 forks source link

Skip first X lines, or skip last X lines, for table headers/footers #17

Closed Krellan closed 8 months ago

Krellan commented 10 months ago

Thought of a feature request.

Would be nice to have an option to skip the first X lines, to preserve an unsorted header, such as a Markdown table in which you need to preserve the first 2 lines and keep them unsorted.

For symmetry, also have an option to skip the last X lines, similarly.

Any thoughts on this? Thanks!

JeffFaer commented 10 months ago

Do you have an example of what you're describing that I could take a look at?

Krellan commented 10 months ago

Glad you asked. Here's Markdown. I'm not sure if GitHub lets you view source, but I'm hoping it does.

Name Value
Alpha Foo
Bravo Bar
Charlie Baz
Delta Qux

The objective is to avoid wrongly sorting the header row and the divdier row (between the header and the content), while making sure the content itself (all following lines) remains sorted.

JeffFaer commented 10 months ago

Ah okay, and putting the comment in the correct place without the skip-first directive breaks markdown formatting:

<!-- Example to want to avoid sorting the first two lines -->
Name | Value
---- | ----
<!-- keep-sorted start -->
Alpha|Foo
Bravo|Bar
Charlie | Baz
Delta|Qux
<!-- keep-sorted end -->
Name Value

Alpha|Foo Bravo|Bar Charlie | Baz Delta|Qux


keep-sorted doesn't actually require that its start directive be on its own line, but that also breaks markdown formatting:

<!-- Example to want to avoid sorting the first two lines -->
Name | Value
---- | ---- <!-- keep-sorted start -->
Alpha|Foo
Bravo|Bar
Charlie | Baz
Delta|Qux
<!-- keep-sorted end -->

Name | Value ---- | ---- Alpha|Foo Bravo|Bar Charlie | Baz Delta|Qux


This seems like reasonable functionality to add to keep-sorted

Krellan commented 10 months ago

Yep, you got it! That's exactly the use case. Thanks for taking a look at it.