Skip to content

Commit 656b340

Browse files
Repairing setext heading support, certain uses of backticks after GitHub API format changes (#145)
* Fix parsing of HTML returned from raw API Github must have changed something on their end, the HTML seems to have changed a little. * Adding some tests for setext header support * Fixing the formatter * Putting all the tests in the same directory * Closer, but span tag doesn't work in some cases * Fixed span issue * Broke test formatting * fixing local tests - remote is busted, formatting seems very different. --------- Co-authored-by: Jon Burgess <[email protected]>
1 parent c836e5e commit 656b340

File tree

7 files changed

+538
-15
lines changed

7 files changed

+538
-15
lines changed

gh-md-toc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ gh_toc(){
218218
# $2 - number of spaces used to indent.
219219
#
220220
gh_toc_grab() {
221+
222+
href_regex="/href=\"[^\"]+?\"/"
223+
lefttext_regex="/(<a class=\"heading-link\".*?>)(.*)(<span aria-hidden)/"
224+
righttext_regex="/>.*/"
225+
221226
common_awk_script='
222227
modified_href = ""
223228
split(href, chars, "")
@@ -242,21 +247,22 @@ gh_toc_grab() {
242247
echoargs=""
243248
awkscript='{
244249
level = substr($0, 3, 1)
245-
text = substr($0, match($0, />[^<]*<span aria-hidden/)+1, RLENGTH-18)
246-
href = substr($0, match($0, "href=\"([^\"]+)?\"")+6, RLENGTH-7)
250+
lefttext = substr($0, match($0, '$lefttext_regex')+1, RLENGTH-18)
251+
text = substr(lefttext, match(lefttext, '$righttext_regex')+1)
252+
href = substr($0, match($0, '$href_regex')+6, RLENGTH-7)
247253
'"$common_awk_script"'
248254
}'
249255
else
250256
grepcmd="grep -Eo"
251257
echoargs="-e"
252258
awkscript='{
253259
level = substr($0, 3, 1)
254-
text = substr($0, match($0, />[^<]*<span aria-hidden/)+1, RLENGTH-18)
255-
href = substr($0, match($0, "href=\"[^\"]+?\"")+6, RLENGTH-7)
260+
lefttext = substr($0, match($0, '$lefttext_regex')+1, RLENGTH-18)
261+
text = substr(lefttext, match(lefttext, '$righttext_regex')+1)
262+
href = substr($0, match($0, '$href_regex')+6, RLENGTH-7)
256263
'"$common_awk_script"'
257264
}'
258265
fi
259-
href_regex='href=\"[^\"]+?\"'
260266

261267
# if closed <h[1-6]> is on the new line, then move it on the prev line
262268
# for example:
@@ -265,6 +271,9 @@ gh_toc_grab() {
265271
# became: The command <code>foo1</code></h1>
266272
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |
267273

274+
# Sometimes a line can start with <span>. Fix that.
275+
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<span/<span/g' |
276+
268277
# find strings that corresponds to template
269278
$grepcmd '<h.*id="user-content-[^"]*".*</h[1-6]' |
270279

File renamed without changes.

tests/test directory/test_filepathwithspace.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
# Title
22

33
<!--ts-->
4-
5-
- [Title](#title)
6-
- [This is test for file path with space](#this-is-test-for-file-path-with-space)
7-
8-
<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
9-
<!-- Added by: sungminyou, at: 2022년 7월 17일 일요일 16시 48분 18초 KST -->
10-
4+
* [Title](#title)
5+
* [This is test for file path with space](#this-is-test-for-file-path-with-space)
116
<!--te-->
127

138
Blabla...

tests/test directory/test_nonenglishchars.md

Lines changed: 451 additions & 0 deletions
Large diffs are not rendered by default.
File renamed without changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Title one
2+
=========
3+
4+
<!--ts-->
5+
* [Title one](#title-one)
6+
* [This is test for setext-style without formatting](#this-is-test-for-setext-style-without-formatting)
7+
* [<em>Title two</em>](#title-two)
8+
* [This is test for setext-style with formatting](#this-is-test-for-setext-style-with-formatting)
9+
* [Title three](#title-three)
10+
* [This is a regression test for atx-style](#this-is-a-regression-test-for-atx-style)
11+
* [Title four is a particularly long title because of wrapping](#title-four-is-a-particularly-long-title-because-of-wrapping)
12+
* [This is a test for long titles](#this-is-a-test-for-long-titles)
13+
<!--te-->
14+
15+
Blabla...
16+
17+
## This is test for setext-style without formatting
18+
19+
Blabla...
20+
21+
*Title `two`*
22+
=============
23+
24+
Blabla...
25+
26+
## This is test for setext-style with formatting
27+
28+
Blabla...
29+
30+
# Title `three`
31+
32+
Blabla...
33+
34+
## This is a regression test for atx-style
35+
36+
Blabla...
37+
38+
# Title four is a particularly long title because of wrapping
39+
40+
Blabla...
41+
42+
## This is a test for long titles
43+
44+
Blabla...

tests/tests.bats

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ test_help() {
154154
}
155155

156156
@test "TOC for non-english chars, #6, #10" {
157+
run $BATS_TEST_DIRNAME/../gh-md-toc tests/test\ directory/test_nonenglishchars.md
158+
assert_success
159+
160+
assert_equal "${lines[2]}" "* [命令行的艺术](#命令行的艺术)"
161+
assert_equal "${lines[3]}" " * [必读](#必读)"
162+
assert_equal "${lines[4]}" " * [基础](#基础)"
163+
assert_equal "${lines[5]}" " * [日常使用](#日常使用)"
164+
}
165+
166+
@test "TOC for non-english chars (remote load), #6, #10" {
157167
run $BATS_TEST_DIRNAME/../gh-md-toc \
158168
https://github.com/ekalinin/envirius/blob/f939d3b6882bfb6ecb28ef7b6e62862f934ba945/README.ru.md
159169
assert_success
@@ -185,7 +195,7 @@ test_help() {
185195
}
186196

187197
@test "TOC for text with backquote, #13" {
188-
run $BATS_TEST_DIRNAME/../gh-md-toc tests/test_backquote.md
198+
run $BATS_TEST_DIRNAME/../gh-md-toc tests/test\ directory/test_backquote.md
189199
assert_success
190200

191201
assert_equal "${lines[2]}" "* [The command foo1](#the-command-foo1)"
@@ -196,16 +206,30 @@ test_help() {
196206
}
197207

198208
@test "TOC for text with plus signs, #100" {
199-
run $BATS_TEST_DIRNAME/../gh-md-toc tests/test_plussign.md
209+
run $BATS_TEST_DIRNAME/../gh-md-toc tests/test\ directory/test_plussign.md
200210
assert_success
201211

202212
assert_equal "${lines[2]}" "* [C vs C++](#c-vs-c)"
203213
}
204214

205215
@test "Toc for file path with space, #136" {
206-
run $BATS_TEST_DIRNAME/../gh-md-toc --insert tests/test\ directory/test_filepathwithspace.md
216+
run $BATS_TEST_DIRNAME/../gh-md-toc --no-backup --hide-footer tests/test\ directory/test_filepathwithspace.md
207217
assert_success
208218

209219
assert_equal "${lines[2]}" "* [Title](#title)"
210220
assert_equal "${lines[3]}" " * [This is test for file path with space](#this-is-test-for-file-path-with-space)"
221+
}
222+
223+
@test "Toc for setext heading with formatting, #145" {
224+
run $BATS_TEST_DIRNAME/../gh-md-toc --no-backup --hide-footer tests/test\ directory/test_setextwithformatting.md
225+
assert_success
226+
227+
assert_equal "${lines[2]}" "* [Title one](#title-one)"
228+
assert_equal "${lines[3]}" " * [This is test for setext-style without formatting](#this-is-test-for-setext-style-without-formatting)"
229+
assert_equal "${lines[4]}" "* [<em>Title two</em>](#title-two)"
230+
assert_equal "${lines[5]}" " * [This is test for setext-style with formatting](#this-is-test-for-setext-style-with-formatting)"
231+
assert_equal "${lines[6]}" "* [Title three](#title-three)"
232+
assert_equal "${lines[7]}" " * [This is a regression test for atx-style](#this-is-a-regression-test-for-atx-style)"
233+
assert_equal "${lines[8]}" "* [Title four is a particularly long title because of wrapping](#title-four-is-a-particularly-long-title-because-of-wrapping)"
234+
assert_equal "${lines[9]}" " * [This is a test for long titles](#this-is-a-test-for-long-titles)"
211235
}

0 commit comments

Comments
 (0)