Skip to content

Commit 1056956

Browse files
committed
fixed #13
1 parent 21e1cf1 commit 1056956

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

gh-md-toc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,21 @@ gh_toc(){
105105
# It's need if TOC is generated for multiple documents.
106106
#
107107
gh_toc_grab() {
108-
echo -e "$(awk -v "gh_url=$1" '/user-content-/ {
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) ")"}' | sed 'y/+/ /; s/%/\\x/g')"
110-
}
108+
# find strings that corresponds to template
109+
grep -E --null-data --text -o \
110+
'<h[1-6]>\s*<a\s*id="user-content-[^"]*"\s*class="anchor"\s*href="[^"]*"[^>]*>\s*<span[^<*]*</span>\s*</a>.*?\s*</h' | \
111+
# replace all new lines except last one
112+
sed 's/h$/h\nendd/g' | tr '\n' ' ' | sed 's/h endd/\h\n/g' | \
113+
# trim head spaces
114+
sed 's/^\s*//' |
115+
# remove code tags
116+
sed 's/<code>//' | sed 's/<\/code>//' |
117+
# now all rows are like:
118+
# <h1> <a id="user-content-..." href="..."><span ...></span></a> ... </h
119+
# format result line
120+
echo -e "$(awk -v "gh_url=$1" '{
121+
print sprintf("%*s", substr($1, 3, 1)*2, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($0, match($0, "href=\".*\" ")+6, RLENGTH-8) ")"}' | sed 'y/+/ /; s/%/\\x/g')"
122+
}
111123

112124
#
113125
# Returns filename only from full path or url

0 commit comments

Comments
 (0)