Skip to content

Commit 599d9d7

Browse files
committed
improved anchor search, fixes #9
1 parent ebc0520 commit 599d9d7

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

gh-md-toc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
# 2. Discard rows where no substring 'user-content-' (github's markup):
1010
# awk '/user-content-/ { ...
1111
#
12-
# 3. Get header level and insert corresponding number of spaces before '*':
12+
# 3.1 Get last number in each row like ' ... </span></a>sitemap.js</h1>'.
13+
# It's a level of the current header (NF — number of fields):
14+
# substr($NF, length($NF)-1, 1)
15+
#
16+
# 3.2 Get level from 3.1 and insert corresponding number of spaces before '*':
1317
# sprintf("%*s", substr($NF, length($NF)-1, 1)*2, " ")
1418
#
1519
# 4. Find head's text and insert it inside "* [ ... ]":
1620
# substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
1721
#
1822
# 5. Find anchor and insert it inside "(...)":
19-
# substr($4, 7)
23+
# substr($0, match($0, "href=\".*\" ")+5, RLENGTH-6)
2024
#
2125

2226
gh_toc_version="0.4.0"
@@ -102,7 +106,7 @@ gh_toc(){
102106
#
103107
gh_toc_grab() {
104108
awk -v "gh_url=$1" '/user-content-/ {
105-
print sprintf("%*s", substr($NF, length($NF)-1, 1)*2, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($4, 7, length($4)-7) ")"}'
109+
print sprintf("%*s", substr($NF, length($NF)-1, 1)*2, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($0, match($0, "href=\".*\" ")+6, RLENGTH-8) ")"}'
106110
}
107111

108112
#

0 commit comments

Comments
 (0)