Last active
December 16, 2024 19:17
-
-
Save fabiog1901/d6a41c4416eefe49c372f11cab2b2084 to your computer and use it in GitHub Desktop.
Set numbered headers on a markdown file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
counter = [0] | |
with open(sys.argv[1], "r") as f: | |
for line in f: | |
# identify a heading line | |
if line and line[0:2] == "##": | |
heading_md, heading_title = line.split(" ", 1) | |
# surtitle | |
for _ in range(len(counter) - (len(heading_md) - 1)): | |
counter.pop() | |
# subtitle | |
for _ in range((len(heading_md) - 1) - len(counter)): | |
counter.append(0) | |
counter[-1] += 1 | |
# inject the numbering between the heading markdown and the heading title | |
print( | |
f"{heading_md} {'.'.join(str(x) for x in counter)}. {heading_title}", | |
end="", | |
) | |
else: | |
print(line, end="") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
counter = [0] | |
with open(sys.argv[1], "r") as f: | |
for line in f: | |
# identify a heading line | |
if line and line[0:2] == "##": | |
heading_md, heading_title = line.split(" ", 1) | |
# surtitle | |
for _ in range(len(counter) - (len(heading_md) - 1)): | |
counter.pop() | |
# subtitle | |
for _ in range((len(heading_md) - 1) - len(counter)): | |
counter.append(0) | |
counter[-1] += 1 | |
new_h = ( | |
".".join(str(x) for x in counter) | |
+ ". " | |
+ heading_title.replace("\n", "") | |
) | |
new_h_link = "#" + new_h.replace(".", "").replace(" ", "-").lower() | |
print(f"{' ' * (len(heading_md) - 2)}- [{new_h}]({new_h_link})") |
Given the same z.md
file, create a Table of Content (TOC)
$ python3.11 md_toc.py z.md
- [1. My first heading](#1-my-first-heading)
- [1.1. subsection 1](#11-subsection-1)
- [1.1.1. going deep](#111-going-deep)
- [2. My second heading](#2-my-second-heading)
- [2.1. another subsection](#21-another-subsection)
Which renders like below
Then you can combine the 2 like below:
# My Title
## Content
- [1. My first heading](#1-my-first-heading)
- [1.1. subsection 1](#11-subsection-1)
- [1.1.1. going deep](#111-going-deep)
- [2. My second heading](#2-my-second-heading)
- [2.1. another subsection](#21-another-subsection)
## 1. My first heading
### 1.1. subsection 1
#### 1.1.1. going deep
## 2. My second heading
### 2.1. another subsection
which renders:
My Title
Content
1. My first heading
1.1. subsection 1
1.1.1. going deep
2. My second heading
2.1. another subsection
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given
z.md
Which renders like below:
My Title
1. My first heading
1.1. subsection 1
1.1.1. going deep
2. My second heading
2.1. another subsection