Skip to content

Instantly share code, notes, and snippets.

@fabiog1901
Last active December 16, 2024 19:17
Show Gist options
  • Save fabiog1901/d6a41c4416eefe49c372f11cab2b2084 to your computer and use it in GitHub Desktop.
Save fabiog1901/d6a41c4416eefe49c372f11cab2b2084 to your computer and use it in GitHub Desktop.
Set numbered headers on a markdown file
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="")
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})")
@fabiog1901
Copy link
Author

fabiog1901 commented Dec 16, 2024

Given z.md

# My Title

## My first heading

### subsection 1

#### going deep

## My second heading

### another subsection
$ python3.11 md_numbered_headers.py z.md 
# My Title

## 1. My first heading

### 1.1. subsection 1

#### 1.1.1. going deep

## 2. My second heading

### 2.1. another subsection

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

@fabiog1901
Copy link
Author

fabiog1901 commented Dec 16, 2024

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

@fabiog1901
Copy link
Author

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