Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions gh-md-toc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ gh_toc_load() {
# <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'"
gh_toc_md2html() {
local gh_file_md=$1
local skip_header=$2

URL=https://api.github.com/markdown/raw

if [ ! -z "$GH_TOC_TOKEN" ]; then
Expand All @@ -65,14 +67,25 @@ gh_toc_md2html() {
AUTHORIZATION="Authorization: token ${TOKEN}"
fi

local gh_tmp_file_md=$gh_file_md
if [ "$skip_header" = "yes" ]; then
if grep -Fxq "<!--te-->" $gh_src; then
# cut everything before the toc
gh_tmp_file_md=$gh_file_md~~
sed '1,/<!--te-->/d' $gh_file_md > $gh_tmp_file_md
fi
fi

# echo $URL 1>&2
OUTPUT=$(curl -s \
--user-agent "$gh_user_agent" \
--data-binary @"$gh_file_md" \
--data-binary @"$gh_tmp_file_md" \
-H "Content-Type:text/plain" \
-H "$AUTHORIZATION" \
"$URL")

rm -f $gh_file_md~~

if [ "$?" != "0" ]; then
echo "XXNetworkErrorXX"
fi
Expand Down Expand Up @@ -107,6 +120,7 @@ gh_toc(){
local no_backup=$4
local no_footer=$5
local indent=$6
local skip_header=$7

if [ "$gh_src" = "" ]; then
echo "Please, enter URL or local path for a README.md"
Expand Down Expand Up @@ -138,7 +152,7 @@ gh_toc(){
echo
fi
else
local rawhtml=$(gh_toc_md2html "$gh_src")
local rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header")
if [ "$rawhtml" == "XXNetworkErrorXX" ]; then
echo "Parsing local markdown file requires access to github API"
echo "Please make sure curl is installed and check your network connectivity"
Expand Down Expand Up @@ -250,16 +264,16 @@ gh_toc_grab() {
# </h1>
# became: The command <code>foo1</code></h1>
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |

# find strings that corresponds to template
$grepcmd '<a.*id="user-content-[^"]*".*</h[1-6]' |

# remove code tags
sed 's/<code>//g' | sed 's/<\/code>//g' |

# remove g-emoji
sed 's/<g-emoji[^>]*[^<]*<\/g-emoji> //g' |

# now all rows are like:
# <a id="user-content-..." href="..."><span ...></span></a> ... </h1
# format result line
Expand Down Expand Up @@ -289,8 +303,8 @@ gh_toc_app() {
echo "GitHub TOC generator ($app_name): $gh_toc_version"
echo ""
echo "Usage:"
echo " $app_name [--insert] [--hide-footer] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
echo " $app_name [--no-backup] [--hide-footer] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
echo " $app_name [--insert] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
echo " $app_name [--no-backup] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
echo " $app_name - Create TOC for markdown from STDIN"
echo " $app_name --help Show help"
echo " $app_name --version Show version"
Expand Down Expand Up @@ -353,10 +367,16 @@ gh_toc_app() {
shift
fi

if [ "$1" = '--skip-header' ]; then
skip_header="yes"
shift
fi


for md in "$@"
do
echo ""
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent"
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header"
done

echo ""
Expand Down
32 changes: 28 additions & 4 deletions tests/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ load test_helper

}

@test "TOC for local README.md with skip headers" {
run $BATS_TEST_DIRNAME/../gh-md-toc --skip-header README.md
assert_success

assert_equal "${lines[0]}" "Table of Contents"
assert_equal "${lines[1]}" "================="
assert_equal "${lines[2]}" "* [Installation](#installation)"
assert_equal "${lines[3]}" "* [Usage](#usage)"
assert_equal "${lines[4]}" " * [STDIN](#stdin)"
assert_equal "${lines[5]}" " * [Local files](#local-files)"
assert_equal "${lines[6]}" " * [Remote files](#remote-files)"
assert_equal "${lines[7]}" " * [Multiple files](#multiple-files)"
assert_equal "${lines[8]}" " * [Combo](#combo)"
assert_equal "${lines[9]}" " * [Auto insert and update TOC](#auto-insert-and-update-toc)"
assert_equal "${lines[10]}" " * [GitHub token](#github-token)"
assert_equal "${lines[11]}" " * [TOC generation with Github Actions](#toc-generation-with-github-actions)"
assert_equal "${lines[12]}" "* [Tests](#tests)"
assert_equal "${lines[13]}" "* [Dependency](#dependency)"
assert_equal "${lines[14]}" "* [Docker](#docker)"
assert_equal "${lines[15]}" " * [Local](#local)"
assert_equal "${lines[16]}" " * [Public](#public)"
assert_equal "${lines[17]}" "<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"

}
@test "TOC for remote README.md" {
run $BATS_TEST_DIRNAME/../gh-md-toc https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md
assert_success
Expand Down Expand Up @@ -98,8 +122,8 @@ load test_helper
run $BATS_TEST_DIRNAME/../gh-md-toc --help
assert_success
assert_equal "${lines[1]}" "Usage:"
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
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)"
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"
assert_equal "${lines[4]}" " gh-md-toc - Create TOC for markdown from STDIN"
assert_equal "${lines[5]}" " gh-md-toc --help Show help"
assert_equal "${lines[6]}" " gh-md-toc --version Show version"
Expand All @@ -109,8 +133,8 @@ load test_helper
run $BATS_TEST_DIRNAME/../gh-md-toc
assert_success
assert_equal "${lines[1]}" "Usage:"
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
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)"
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"
assert_equal "${lines[4]}" " gh-md-toc - Create TOC for markdown from STDIN"
assert_equal "${lines[5]}" " gh-md-toc --help Show help"
assert_equal "${lines[6]}" " gh-md-toc --version Show version"
Expand Down