Skip to content

Commit d815f39

Browse files
author
Stefan Fruhner
committed
Add option --skip-header
1 parent 8daa73c commit d815f39

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

gh-md-toc

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ gh_toc_load() {
5151
# <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'"
5252
gh_toc_md2html() {
5353
local gh_file_md=$1
54+
local skip_header=$2
55+
5456
URL=https://api.github.com/markdown/raw
5557

5658
if [ ! -z "$GH_TOC_TOKEN" ]; then
@@ -66,11 +68,12 @@ gh_toc_md2html() {
6668
fi
6769

6870
local gh_tmp_file_md=$gh_file_md
69-
if grep -Fxq "<!--te-->" $gh_src; then
70-
# cut everything before the toc
71-
gh_tmp_file_md=$gh_file_md~~
72-
sed '1,/<!--te-->/d' $gh_file_md > $gh_tmp_file_md
73-
71+
if [ "$skip_header" = "yes" ]; then
72+
if grep -Fxq "<!--te-->" $gh_src; then
73+
# cut everything before the toc
74+
gh_tmp_file_md=$gh_file_md~~
75+
sed '1,/<!--te-->/d' $gh_file_md > $gh_tmp_file_md
76+
fi
7477
fi
7578

7679
# echo $URL 1>&2
@@ -80,6 +83,7 @@ gh_toc_md2html() {
8083
-H "Content-Type:text/plain" \
8184
-H "$AUTHORIZATION" \
8285
"$URL")
86+
8387
rm -f $gh_file_md~~
8488

8589
if [ "$?" != "0" ]; then
@@ -115,6 +119,7 @@ gh_toc(){
115119
local need_replace=$3
116120
local no_backup=$4
117121
local no_footer=$5
122+
local skip_header=$6
118123

119124
if [ "$gh_src" = "" ]; then
120125
echo "Please, enter URL or local path for a README.md"
@@ -146,7 +151,7 @@ gh_toc(){
146151
echo
147152
fi
148153
else
149-
local rawhtml=$(gh_toc_md2html "$gh_src")
154+
local rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header")
150155
if [ "$rawhtml" == "XXNetworkErrorXX" ]; then
151156
echo "Parsing local markdown file requires access to github API"
152157
echo "Please make sure curl is installed and check your network connectivity"
@@ -256,16 +261,16 @@ gh_toc_grab() {
256261
# </h1>
257262
# became: The command <code>foo1</code></h1>
258263
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |
259-
264+
260265
# find strings that corresponds to template
261266
$grepcmd '<a.*id="user-content-[^"]*".*</h[1-6]' |
262-
267+
263268
# remove code tags
264269
sed 's/<code>//g' | sed 's/<\/code>//g' |
265270

266271
# remove g-emoji
267272
sed 's/<g-emoji[^>]*[^<]*<\/g-emoji> //g' |
268-
273+
269274
# now all rows are like:
270275
# <a id="user-content-..." href="..."><span ...></span></a> ... </h1
271276
# format result line
@@ -294,8 +299,8 @@ gh_toc_app() {
294299
echo "GitHub TOC generator ($app_name): $gh_toc_version"
295300
echo ""
296301
echo "Usage:"
297-
echo " $app_name [--insert] [--hide-footer] src [src] Create TOC for a README file (url or local path)"
298-
echo " $app_name [--no-backup] [--hide-footer] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
302+
echo " $app_name [--insert] [--hide-footer] [--skip-header] src [src] Create TOC for a README file (url or local path)"
303+
echo " $app_name [--no-backup] [--hide-footer] [--skip-header] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
299304
echo " $app_name - Create TOC for markdown from STDIN"
300305
echo " $app_name --help Show help"
301306
echo " $app_name --version Show version"
@@ -353,10 +358,16 @@ gh_toc_app() {
353358
shift
354359
fi
355360

361+
if [ "$1" = '--skip-header' ]; then
362+
skip_header="yes"
363+
shift
364+
fi
365+
366+
356367
for md in "$@"
357368
do
358369
echo ""
359-
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer"
370+
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$skip_header"
360371
done
361372

362373
echo ""

tests/tests.bats

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ load test_helper
9898
run $BATS_TEST_DIRNAME/../gh-md-toc --help
9999
assert_success
100100
assert_equal "${lines[1]}" "Usage:"
101-
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] src [src] Create TOC for a README file (url or local path)"
102-
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
101+
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--skip-header] src [src] Create TOC for a README file (url or local path)"
102+
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--skip-header] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
103103
assert_equal "${lines[4]}" " gh-md-toc - Create TOC for markdown from STDIN"
104104
assert_equal "${lines[5]}" " gh-md-toc --help Show help"
105105
assert_equal "${lines[6]}" " gh-md-toc --version Show version"
@@ -109,8 +109,8 @@ load test_helper
109109
run $BATS_TEST_DIRNAME/../gh-md-toc
110110
assert_success
111111
assert_equal "${lines[1]}" "Usage:"
112-
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] src [src] Create TOC for a README file (url or local path)"
113-
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
112+
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--skip-header] src [src] Create TOC for a README file (url or local path)"
113+
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--skip-header] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
114114
assert_equal "${lines[4]}" " gh-md-toc - Create TOC for markdown from STDIN"
115115
assert_equal "${lines[5]}" " gh-md-toc --help Show help"
116116
assert_equal "${lines[6]}" " gh-md-toc --version Show version"

0 commit comments

Comments
 (0)