-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_markdown_list.py
42 lines (33 loc) · 1.16 KB
/
sort_markdown_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Sorts the list items in a markdown file in-place."""
import re
import sys
from pathlib import Path
def sort_markdown_list(file_path: str) -> None:
"""Sorts the list items in a markdown file in-place."""
with Path(file_path).open() as file:
lines = file.readlines()
sorted_lines = []
in_list = False
list_block = []
for line in lines:
# Detect list items (unordered or ordered)
if re.match(r"^\s*[-*+]\s", line) or re.match(r"^\s*\d+\.\s", line):
list_block.append(line)
in_list = True
else:
if in_list:
sorted_lines += sorted(
list_block,
key=lambda x: x.lower(),
) # Sort ignoring case
list_block = []
sorted_lines.append(line)
in_list = False
if list_block:
sorted_lines += sorted(list_block, key=lambda x: x.lower())
# Write the sorted content back to the file
with Path(file_path).open("w") as file:
file.writelines(sorted_lines)
if __name__ == "__main__":
for markdown_file in sys.argv[1:]:
sort_markdown_list(markdown_file)