Skip to content

Instantly share code, notes, and snippets.

@chr5tphr
Last active July 18, 2022 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chr5tphr/ed54f543fdb4cef48dafe3cfb42dd274 to your computer and use it in GitHub Desktop.
Save chr5tphr/ed54f543fdb4cef48dafe3cfb42dd274 to your computer and use it in GitHub Desktop.
Prepend or replace section numbering for markdown headings
#!/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