Skip to content

Instantly share code, notes, and snippets.

@RonenNess
Created January 12, 2022 18:47
Show Gist options
  • Save RonenNess/313348f2c807536961bc06c21aeb07dd to your computer and use it in GitHub Desktop.
Save RonenNess/313348f2c807536961bc06c21aeb07dd to your computer and use it in GitHub Desktop.
A very basic python script to generate ToC from markdown file. Only works for simple cases and 2 level of titles (# and ##).
def generate_link(title):
return "[" + title + "](#" + title.lower().replace(' ', '-') + ")"
print ("# Table Of Content")
with open("README.md", 'r') as infile:
for line in infile:
line = line.strip()
if line.startswith('## '):
print (" - " + generate_link(line[3:]))
elif line.startswith('# '):
print ("- " + generate_link(line[2:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment