Skip to content

Commit 352d7ae

Browse files
authored
Merge pull request #126 from firebirdberlin/issue-125-skip-headers
Skip headers if end marker is present
2 parents 5659f2a + 69e880a commit 352d7ae

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

gh-md-toc

Lines changed: 28 additions & 8 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
@@ -65,14 +67,25 @@ gh_toc_md2html() {
6567
AUTHORIZATION="Authorization: token ${TOKEN}"
6668
fi
6769

70+
local gh_tmp_file_md=$gh_file_md
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
77+
fi
78+
6879
# echo $URL 1>&2
6980
OUTPUT=$(curl -s \
7081
--user-agent "$gh_user_agent" \
71-
--data-binary @"$gh_file_md" \
82+
--data-binary @"$gh_tmp_file_md" \
7283
-H "Content-Type:text/plain" \
7384
-H "$AUTHORIZATION" \
7485
"$URL")
7586

87+
rm -f $gh_file_md~~
88+
7689
if [ "$?" != "0" ]; then
7790
echo "XXNetworkErrorXX"
7891
fi
@@ -107,6 +120,7 @@ gh_toc(){
107120
local no_backup=$4
108121
local no_footer=$5
109122
local indent=$6
123+
local skip_header=$7
110124

111125
if [ "$gh_src" = "" ]; then
112126
echo "Please, enter URL or local path for a README.md"
@@ -138,7 +152,7 @@ gh_toc(){
138152
echo
139153
fi
140154
else
141-
local rawhtml=$(gh_toc_md2html "$gh_src")
155+
local rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header")
142156
if [ "$rawhtml" == "XXNetworkErrorXX" ]; then
143157
echo "Parsing local markdown file requires access to github API"
144158
echo "Please make sure curl is installed and check your network connectivity"
@@ -250,16 +264,16 @@ gh_toc_grab() {
250264
# </h1>
251265
# became: The command <code>foo1</code></h1>
252266
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |
253-
267+
254268
# find strings that corresponds to template
255269
$grepcmd '<a.*id="user-content-[^"]*".*</h[1-6]' |
256-
270+
257271
# remove code tags
258272
sed 's/<code>//g' | sed 's/<\/code>//g' |
259273

260274
# remove g-emoji
261275
sed 's/<g-emoji[^>]*[^<]*<\/g-emoji> //g' |
262-
276+
263277
# now all rows are like:
264278
# <a id="user-content-..." href="..."><span ...></span></a> ... </h1
265279
# format result line
@@ -289,8 +303,8 @@ gh_toc_app() {
289303
echo "GitHub TOC generator ($app_name): $gh_toc_version"
290304
echo ""
291305
echo "Usage:"
292-
echo " $app_name [--insert] [--hide-footer] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
293-
echo " $app_name [--no-backup] [--hide-footer] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
306+
echo " $app_name [--insert] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
307+
echo " $app_name [--no-backup] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
294308
echo " $app_name - Create TOC for markdown from STDIN"
295309
echo " $app_name --help Show help"
296310
echo " $app_name --version Show version"
@@ -353,10 +367,16 @@ gh_toc_app() {
353367
shift
354368
fi
355369

370+
if [ "$1" = '--skip-header' ]; then
371+
skip_header="yes"
372+
shift
373+
fi
374+
375+
356376
for md in "$@"
357377
do
358378
echo ""
359-
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent"
379+
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header"
360380
done
361381

362382
echo ""

tests/tests.bats

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ load test_helper
3030

3131
}
3232

33+
@test "TOC for local README.md with skip headers" {
34+
run $BATS_TEST_DIRNAME/../gh-md-toc --skip-header README.md
35+
assert_success
36+
37+
assert_equal "${lines[0]}" "Table of Contents"
38+
assert_equal "${lines[1]}" "================="
39+
assert_equal "${lines[2]}" "* [Installation](#installation)"
40+
assert_equal "${lines[3]}" "* [Usage](#usage)"
41+
assert_equal "${lines[4]}" " * [STDIN](#stdin)"
42+
assert_equal "${lines[5]}" " * [Local files](#local-files)"
43+
assert_equal "${lines[6]}" " * [Remote files](#remote-files)"
44+
assert_equal "${lines[7]}" " * [Multiple files](#multiple-files)"
45+
assert_equal "${lines[8]}" " * [Combo](#combo)"
46+
assert_equal "${lines[9]}" " * [Auto insert and update TOC](#auto-insert-and-update-toc)"
47+
assert_equal "${lines[10]}" " * [GitHub token](#github-token)"
48+
assert_equal "${lines[11]}" " * [TOC generation with Github Actions](#toc-generation-with-github-actions)"
49+
assert_equal "${lines[12]}" "* [Tests](#tests)"
50+
assert_equal "${lines[13]}" "* [Dependency](#dependency)"
51+
assert_equal "${lines[14]}" "* [Docker](#docker)"
52+
assert_equal "${lines[15]}" " * [Local](#local)"
53+
assert_equal "${lines[16]}" " * [Public](#public)"
54+
assert_equal "${lines[17]}" "<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
55+
56+
}
3357
@test "TOC for remote README.md" {
3458
run $BATS_TEST_DIRNAME/../gh-md-toc https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md
3559
assert_success
@@ -98,8 +122,8 @@ load test_helper
98122
run $BATS_TEST_DIRNAME/../gh-md-toc --help
99123
assert_success
100124
assert_equal "${lines[1]}" "Usage:"
101-
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
102-
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
125+
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
126+
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
103127
assert_equal "${lines[4]}" " gh-md-toc - Create TOC for markdown from STDIN"
104128
assert_equal "${lines[5]}" " gh-md-toc --help Show help"
105129
assert_equal "${lines[6]}" " gh-md-toc --version Show version"
@@ -109,8 +133,8 @@ load test_helper
109133
run $BATS_TEST_DIRNAME/../gh-md-toc
110134
assert_success
111135
assert_equal "${lines[1]}" "Usage:"
112-
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
113-
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
136+
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
137+
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
114138
assert_equal "${lines[4]}" " gh-md-toc - Create TOC for markdown from STDIN"
115139
assert_equal "${lines[5]}" " gh-md-toc --help Show help"
116140
assert_equal "${lines[6]}" " gh-md-toc --version Show version"

0 commit comments

Comments
 (0)