Last active
July 18, 2022 13:14
-
-
Save chr5tphr/ed54f543fdb4cef48dafe3cfb42dd274 to your computer and use it in GitHub Desktop.
Prepend or replace section numbering for markdown headings
This file contains 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
#!/usr/bin/env -S gawk -f | |
$1~/^##+$/ && $0!~/Table of Contents$/{ | |
if (l > length($1)) for (k in num) if (k > length($1)) delete num[k] | |
l = length($1) | |
num[l] += 1 | |
r = substr($0, l + 2) | |
p = ""; for (i in num) p = p num[i] "." | |
if (length(num) > 1) p = substr(p, 0, length(p) - 1) | |
gsub("^[0-9.]+ ", "", r) | |
print $1, p, r | |
next | |
} | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment