Skip to content

Commit

Permalink
Update for a new GH layout; Add support for prev versions of layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ekalinin committed Mar 2, 2024
1 parent 5bbbc85 commit 5b1817f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/gh-md-toc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
indent = kingpin.Flag("indent", "Indent space of generated list").Default("2").Int()
debug = kingpin.Flag("debug", "Show debug info").Bool()
ghurl = kingpin.Flag("github-url", "GitHub URL, default=https://api.github.com").String()
reVersion = kingpin.Flag("re-version", "RegExp version, default=0").Default("0").String()
reVersion = kingpin.Flag("re-version", "RegExp version, default=0").Default(internal.GH_2024_03).String()
)

// check if there was an error (and panic if it was)
Expand Down
23 changes: 17 additions & 6 deletions ghdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewGHDoc(Path string, AbsPaths bool, StartDepth int, Depth int, Escape bool
httpGetter: internal.HttpGet,
httpPoster: internal.HttpPost,
ghURL: "https://api.github.com",
reVersion: "0",
reVersion: internal.GH_2024_03,
}
}

Expand Down Expand Up @@ -168,18 +168,29 @@ func (doc *GHDoc) GrabToc() *GHToc {
// si:
// - s - let . match \n (single-line mode)
// - i - case-insensitive
re := `(?si)<h(?P<num>[1-6]) id="[^"]+">\s*` +
`<a class="heading-link"\s*` +
`href="(?P<href>[^"]+)">\s*` +
`(?P<name>.*?)<span`
if doc.reVersion == "0" {
re := ""
if doc.reVersion == internal.GH_V0 {
re = `(?si)<h(?P<num>[1-6])>\s*` +
`<a\s*id="user-content-[^"]*"\s*class="anchor"\s*` +
`(aria-hidden="[^"]*"\s*)?` +
`(tabindex="[^"]*"\s*)?` +
`href="(?P<href>[^"]*)"[^>]*>\s*` +
`.*?</a>(?P<name>.*?)</h`
}
if doc.reVersion == internal.GH_2023_10 {
re = `(?si)<h(?P<num>[1-6]) id="[^"]+">\s*` +
`<a class="heading-link"\s*` +
`href="(?P<href>[^"]+)">\s*` +
`(?P<name>.*?)<span`
}
if doc.reVersion == internal.GH_2024_03 {
re = `(?si)<h(?P<num>[1-6]) class="heading-element">(?P<name>[^<]+)</h\d>` +
`<a\s*id="user-content-[^"]*"\s*` +
`class="[^"]*"\s*` +
`aria-label="[^"]*"\s*` +
`href="(?P<href>[^"]+)">`
}

r := regexp.MustCompile(re)
listIndentation := internal.GenerateListIndentation(doc.Indent)

Expand Down
7 changes: 7 additions & 0 deletions internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ const (
Version = "1.3.1"
userAgent = "github-markdown-toc.go v" + Version
)

// Versions of GH layouts
const (
GH_V0 = "0"
GH_2023_10 = "2023-10"
GH_2024_03 = "2024-03"
)

0 comments on commit 5b1817f

Please sign in to comment.