Skip to content

Commit 5b1817f

Browse files
committed
Update for a new GH layout; Add support for prev versions of layout
1 parent 5bbbc85 commit 5b1817f

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

cmd/gh-md-toc/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +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()
26+
reVersion = kingpin.Flag("re-version", "RegExp version, default=0").Default(internal.GH_2024_03).String()
2727
)
2828

2929
// check if there was an error (and panic if it was)

ghdoc.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewGHDoc(Path string, AbsPaths bool, StartDepth int, Depth int, Escape bool
6565
httpGetter: internal.HttpGet,
6666
httpPoster: internal.HttpPost,
6767
ghURL: "https://api.github.com",
68-
reVersion: "0",
68+
reVersion: internal.GH_2024_03,
6969
}
7070
}
7171

@@ -168,18 +168,29 @@ func (doc *GHDoc) GrabToc() *GHToc {
168168
// si:
169169
// - s - let . match \n (single-line mode)
170170
// - i - case-insensitive
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" {
171+
re := ""
172+
if doc.reVersion == internal.GH_V0 {
176173
re = `(?si)<h(?P<num>[1-6])>\s*` +
177174
`<a\s*id="user-content-[^"]*"\s*class="anchor"\s*` +
178175
`(aria-hidden="[^"]*"\s*)?` +
179176
`(tabindex="[^"]*"\s*)?` +
180177
`href="(?P<href>[^"]*)"[^>]*>\s*` +
181178
`.*?</a>(?P<name>.*?)</h`
182179
}
180+
if doc.reVersion == internal.GH_2023_10 {
181+
re = `(?si)<h(?P<num>[1-6]) id="[^"]+">\s*` +
182+
`<a class="heading-link"\s*` +
183+
`href="(?P<href>[^"]+)">\s*` +
184+
`(?P<name>.*?)<span`
185+
}
186+
if doc.reVersion == internal.GH_2024_03 {
187+
re = `(?si)<h(?P<num>[1-6]) class="heading-element">(?P<name>[^<]+)</h\d>` +
188+
`<a\s*id="user-content-[^"]*"\s*` +
189+
`class="[^"]*"\s*` +
190+
`aria-label="[^"]*"\s*` +
191+
`href="(?P<href>[^"]+)">`
192+
}
193+
183194
r := regexp.MustCompile(re)
184195
listIndentation := internal.GenerateListIndentation(doc.Indent)
185196

internal/version.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ const (
55
Version = "1.3.1"
66
userAgent = "github-markdown-toc.go v" + Version
77
)
8+
9+
// Versions of GH layouts
10+
const (
11+
GH_V0 = "0"
12+
GH_2023_10 = "2023-10"
13+
GH_2024_03 = "2024-03"
14+
)

0 commit comments

Comments
 (0)