Skip to content

Commit 27b0f5a

Browse files
authored
Merge pull request #82 from robin-cruk/use-authorization-header
use Authorization header rather than query parameter
2 parents 9b0f537 + 56f7c59 commit 27b0f5a

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

gh-md-toc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# substr($0, match($0, "href=\"[^\"]+?\" ")+6, RLENGTH-8)
2424
#
2525

26-
gh_toc_version="0.6.1"
26+
gh_toc_version="0.6.2"
2727

2828
gh_user_agent="gh-md-toc v$gh_toc_version"
2929

@@ -52,17 +52,23 @@ gh_toc_load() {
5252
gh_toc_md2html() {
5353
local gh_file_md=$1
5454
URL=https://api.github.com/markdown/raw
55+
5556
if [ ! -z "$GH_TOC_TOKEN" ]; then
5657
TOKEN=$GH_TOC_TOKEN
5758
else
58-
TOKEN="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
59+
TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
60+
if [ -f "$TOKEN_FILE" ]; then
61+
TOKEN="$(cat $TOKEN_FILE)"
62+
fi
5963
fi
60-
if [ -f "$TOKEN" ]; then
61-
AUTH_HEADER=("-H" "Authorization: token $(cat $TOKEN)")
64+
if [ ! -z "${TOKEN}" ]; then
65+
AUTHORIZATION="--header \"Authorization: token ${TOKEN}\""
6266
fi
67+
6368
# echo $URL 1>&2
6469
OUTPUT="$(curl -s --user-agent "$gh_user_agent" \
65-
--data-binary @"$gh_file_md" -H "Content-Type:text/plain" "${AUTH_HEADER[@]}" \
70+
--data-binary @"$gh_file_md" -H "Content-Type:text/plain" \
71+
${AUTHORIZATION} \
6672
$URL)"
6773

6874
if [ "$?" != "0" ]; then
@@ -136,8 +142,8 @@ gh_toc(){
136142
if [ "$rawhtml" == "XXRateLimitXX" ]; then
137143
echo "Parsing local markdown file requires access to github API"
138144
echo "Error: You exceeded the hourly limit. See: https://developer.github.com/v3/#rate-limiting"
139-
TOKEN="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
140-
echo "or place github auth token here: $TOKEN"
145+
TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
146+
echo "or place GitHub auth token here: ${TOKEN_FILE}"
141147
exit 1
142148
fi
143149
local toc=`echo "$rawhtml" | gh_toc_grab "$gh_src_copy"`

tests/tests.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ load test_helper
110110
@test "--version" {
111111
run $BATS_TEST_DIRNAME/../gh-md-toc --version
112112
assert_success
113-
assert_equal "${lines[0]}" "0.6.1"
113+
assert_equal "${lines[0]}" "0.6.2"
114114
}
115115

116116
@test "TOC for non-english chars, #6, #10" {

0 commit comments

Comments
 (0)