Skip to content

Commit cfef5c8

Browse files
committed
1.3.1
1 parent 0084f0e commit cfef5c8

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

cmd/gh-md-toc/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
indent = kingpin.Flag("indent", "Indent space of generated list").Default("2").Int()
2424
debug = kingpin.Flag("debug", "Show debug info").Bool()
2525
ghurl = kingpin.Flag("github-url", "GitHub URL, default=https://api.github.com").String()
26+
reVersion = kingpin.Flag("re-version", "RegExp version, default=0").Default("0").String()
2627
)
2728

2829
// check if there was an error (and panic if it was)
@@ -41,7 +42,7 @@ func processPaths() {
4142

4243
for _, p := range *paths {
4344
ghdoc := ghtoc.NewGHDoc(p, absPathsInToc, *startDepth, *depth, !*noEscape, *token, *indent, *debug)
44-
ghdoc.SetGHURL(*ghurl)
45+
ghdoc.SetGHURL(*ghurl).SetReVersion(*reVersion)
4546

4647
if *serial {
4748
ch <- ghdoc.GetToc()
@@ -74,6 +75,7 @@ func processSTDIN() {
7475
check(os.WriteFile(file.Name(), bytes, 0644))
7576
check(ghtoc.NewGHDoc(file.Name(), false, *startDepth, *depth, !*noEscape, *token, *indent, *debug).
7677
SetGHURL(*ghurl).
78+
SetReVersion(*reVersion).
7779
GetToc().
7880
Print(os.Stdout))
7981
}

ghdoc.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type GHDoc struct {
4646
httpGetter httpGetter
4747
httpPoster httpPoster
4848
ghURL string
49+
reVersion string
4950
}
5051

5152
// NewGHDoc create GHDoc
@@ -64,6 +65,7 @@ func NewGHDoc(Path string, AbsPaths bool, StartDepth int, Depth int, Escape bool
6465
httpGetter: internal.HttpGet,
6566
httpPoster: internal.HttpPost,
6667
ghURL: "https://api.github.com",
68+
reVersion: "0",
6769
}
6870
}
6971

@@ -81,6 +83,12 @@ func (doc *GHDoc) SetGHURL(url string) *GHDoc {
8183
return doc
8284
}
8385

86+
// SetReVersion sets reg exp version
87+
func (doc *GHDoc) SetReVersion(v string) *GHDoc {
88+
doc.reVersion = v
89+
return doc
90+
}
91+
8492
// IsRemoteFile checks if path is for remote file or not
8593
func (doc *GHDoc) IsRemoteFile() bool {
8694
u, err := url.Parse(doc.Path)
@@ -160,16 +168,18 @@ func (doc *GHDoc) GrabToc() *GHToc {
160168
// si:
161169
// - s - let . match \n (single-line mode)
162170
// - i - case-insensitive
163-
// re := `(?si)<h(?P<num>[1-6]) id="[^"]+">\s*` +
164-
// `<a class="heading-link"\s*` +
165-
// `href="(?P<href>[^"]+)">\s*` +
166-
// `(?P<name>.*?)<span`
167-
re := `(?si)<h(?P<num>[1-6])>\s*` +
168-
`<a\s*id="user-content-[^"]*"\s*class="anchor"\s*` +
169-
`(aria-hidden="[^"]*"\s*)?` +
170-
`(tabindex="[^"]*"\s*)?` +
171-
`href="(?P<href>[^"]*)"[^>]*>\s*` +
172-
`.*?</a>(?P<name>.*?)</h`
171+
re := `(?si)<h(?P<num>[1-6]) id="[^"]+">\s*` +
172+
`<a class="heading-link"\s*` +
173+
`href="(?P<href>[^"]+)">\s*` +
174+
`(?P<name>.*?)<span`
175+
if doc.reVersion == "0" {
176+
re = `(?si)<h(?P<num>[1-6])>\s*` +
177+
`<a\s*id="user-content-[^"]*"\s*class="anchor"\s*` +
178+
`(aria-hidden="[^"]*"\s*)?` +
179+
`(tabindex="[^"]*"\s*)?` +
180+
`href="(?P<href>[^"]*)"[^>]*>\s*` +
181+
`.*?</a>(?P<name>.*?)</h`
182+
}
173183
r := regexp.MustCompile(re)
174184
listIndentation := internal.GenerateListIndentation(doc.Indent)
175185

0 commit comments

Comments
 (0)