diff --git a/gh-md-toc b/gh-md-toc index 4749673..40eeeaa 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -51,6 +51,8 @@ gh_toc_load() { #

Hello world github/linguist#1 cool, and #1!

'" 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 @@ -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 "" $gh_src; then + # cut everything before the toc + gh_tmp_file_md=$gh_file_md~~ + sed '1,//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 @@ -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" @@ -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" @@ -250,16 +264,16 @@ gh_toc_grab() { # # became: The command foo1 sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' | - + # find strings that corresponds to template $grepcmd '//g' | sed 's/<\/code>//g' | # remove g-emoji sed 's/]*[^<]*<\/g-emoji> //g' | - + # now all rows are like: # ... / 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 / placeholders" echo " $app_name - Create TOC for markdown from STDIN" echo " $app_name --help Show help" echo " $app_name --version Show version" @@ -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 "" diff --git a/tests/tests.bats b/tests/tests.bats index e9dcec9..d87687e 100755 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -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]}" "" + +} @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 @@ -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 / 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 / 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" @@ -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 / 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 / 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"