mirror of
https://github.com/github/octodns.git
synced 2024-05-11 05:55:00 +00:00
Update the README TOC and add script that can generate it
This commit is contained in:
31
script/markdown-toc
Executable file
31
script/markdown-toc
Executable file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from re import compile
|
||||
from sys import stdin
|
||||
|
||||
splitter = compile(r'\s+')
|
||||
|
||||
in_pre = False
|
||||
headings = []
|
||||
for line in stdin:
|
||||
if line.startswith('```'):
|
||||
in_pre = not in_pre
|
||||
if in_pre or not line.startswith('#'):
|
||||
continue
|
||||
level, heading = splitter.split(line, 1)
|
||||
if 'Table of Contents' in heading:
|
||||
continue
|
||||
headings.append((len(level), heading.strip()))
|
||||
|
||||
# ignore the first one, it's more of a title
|
||||
headings.pop(0)
|
||||
|
||||
print('\n## Table of Contents\n')
|
||||
min_level = min(h[0] for h in headings)
|
||||
for heading in headings:
|
||||
level = heading[0] - min_level
|
||||
pre = ' ' * (level * 3)
|
||||
title = heading[1]
|
||||
link = title.lower().replace(' ', '-').replace('`', '')
|
||||
print(f'{pre}* [{title}](#{link})')
|
||||
print()
|
||||
Reference in New Issue
Block a user