Skip to content

Commit b7d58f5

Browse files
committed
fix code generation due to goldmark's changes to Node.Text
Close #458 Upstream context is yuin/goldmark#471
1 parent 9f88c86 commit b7d58f5

File tree

4 files changed

+55
-24
lines changed

4 files changed

+55
-24
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/mattn/go-colorable v0.1.13
99
github.com/mattn/go-runewidth v0.0.16
1010
github.com/robfig/cron/v3 v3.0.1
11-
github.com/yuin/goldmark v1.7.4
11+
github.com/yuin/goldmark v1.7.8
1212
golang.org/x/sync v0.8.0
1313
golang.org/x/sys v0.26.0
1414
gopkg.in/yaml.v3 v3.0.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
1414
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
1515
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
1616
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
17-
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
18-
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
17+
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
18+
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
1919
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
2020
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
2121
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

scripts/generate-availability/main.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ const theURL = "https://raw.githubusercontent.com/github/docs/main/content/actio
2626
var dbg = log.New(io.Discard, "", log.LstdFlags)
2727
var reReplaceholder = regexp.MustCompile("{%[^%]+%}")
2828

29+
// `ast.Walk` doesn't work for `TableCell`'s children.
30+
func buildTextOf(n ast.Node, src []byte, b *strings.Builder) {
31+
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
32+
if t, ok := c.(*ast.Text); ok {
33+
b.Write(t.Value(src))
34+
} else {
35+
buildTextOf(c, src, b)
36+
}
37+
}
38+
}
39+
40+
// `Node.Text` method was deprecated. This is alternative to it.
41+
// https://github.com/yuin/goldmark/issues/471
42+
func textOf(n ast.Node, src []byte) string {
43+
var b strings.Builder
44+
buildTextOf(n, src, &b)
45+
return b.String()
46+
}
47+
2948
type switchCase struct {
3049
ctx []string
3150
sp []string
@@ -62,7 +81,7 @@ func parseContextAvailabilityTable(src []byte) (*extast.Table, bool) {
6281
n := root.FirstChild()
6382

6483
for ; n != nil; n = n.NextSibling() {
65-
if h, ok := n.(*ast.Heading); ok && h.Level == 3 && bytes.Equal(h.Text(src), []byte("Context availability")) {
84+
if h, ok := n.(*ast.Heading); ok && h.Level == 3 && textOf(h, src) == "Context availability" {
6685
n = n.NextSibling()
6786
break
6887
}
@@ -84,7 +103,7 @@ func cells(n *extast.TableRow, src []byte) []string {
84103
t := []string{}
85104
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
86105
if tc, ok := c.(*extast.TableCell); ok {
87-
t = append(t, string(tc.Text(src)))
106+
t = append(t, textOf(tc, src))
88107
}
89108
}
90109
return t
@@ -162,7 +181,7 @@ func WorkflowKeyAvailability(key string) ([]string, []string) {
162181

163182
key := cs[0]
164183
if key == "" {
165-
dbg.Printf("Skip %q due to empty key\n", r.Text(src))
184+
dbg.Printf("Skip %q due to empty key\n", textOf(r, src))
166185
continue
167186
}
168187
ctx := split(cs[1])

scripts/generate-webhook-events/main.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,36 @@ const theURL = "https://raw.githubusercontent.com/github/docs/main/content/actio
2222

2323
var dbg = log.New(io.Discard, "", log.LstdFlags)
2424

25-
func getFirstLinkText(n ast.Node, src []byte) (string, bool) {
26-
var link ast.Node
25+
// `Node.Text` method was deprecated. This is alternative to it.
26+
// https://github.com/yuin/goldmark/issues/471
27+
func textOf(n ast.Node, src []byte) string {
28+
var b strings.Builder
29+
2730
ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
2831
if !entering {
2932
return ast.WalkStop, nil
3033
}
34+
if t, ok := n.(*ast.Text); ok {
35+
b.Write(t.Value(src))
36+
}
37+
return ast.WalkContinue, nil
38+
})
39+
40+
return b.String()
41+
}
3142

32-
if n.Kind() != ast.KindLink {
33-
return ast.WalkContinue, nil
43+
func getFirstLinkText(n ast.Node, src []byte) (string, bool) {
44+
var link *ast.Link
45+
ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
46+
if !entering {
47+
return ast.WalkStop, nil
3448
}
3549

36-
link = n // Found
37-
return ast.WalkStop, nil
50+
if l, ok := n.(*ast.Link); ok {
51+
link = l
52+
return ast.WalkStop, nil
53+
}
54+
return ast.WalkContinue, nil
3855
})
3956

4057
if link == nil {
@@ -43,28 +60,23 @@ func getFirstLinkText(n ast.Node, src []byte) (string, bool) {
4360

4461
// Note: All text pieces must be collected. For example the text "pull_request" is pieces of
4562
// "pull_" and "request" since an underscore is delimiter of italic/bold text.
46-
var b strings.Builder
47-
for c := link.FirstChild(); c != nil; c = c.NextSibling() {
48-
b.Write(c.Text(src))
49-
}
50-
51-
return b.String(), true
63+
return textOf(link, src), true
5264
}
5365

5466
func collectCodeSpans(n ast.Node, src []byte) []string {
5567
spans := []string{}
5668
ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
5769
kind := n.Kind()
5870
if entering && kind == ast.KindCodeSpan {
59-
spans = append(spans, string(n.Text(src)))
71+
spans = append(spans, textOf(n, src))
6072
}
6173
return ast.WalkContinue, nil
6274
})
6375
return spans
6476
}
6577

6678
func getWebhookTypes(table ast.Node, src []byte) ([]string, bool, error) {
67-
dbg.Printf("Table: %s", table.Text(src))
79+
dbg.Println("Table:", textOf(table, src))
6880

6981
sawHeader := false
7082
for c := table.FirstChild(); c != nil; c = c.NextSibling() {
@@ -74,7 +86,7 @@ func getWebhookTypes(table ast.Node, src []byte) ([]string, bool, error) {
7486
sawHeader = true
7587

7688
cell := c.FirstChild()
77-
if string(cell.Text(src)) != "Webhook event payload" {
89+
if textOf(cell, src) != "Webhook event payload" {
7890
dbg.Println(" Skip this table because it is not for Webhook event payload")
7991
return nil, false, nil
8092
}
@@ -95,7 +107,7 @@ func getWebhookTypes(table ast.Node, src []byte) ([]string, bool, error) {
95107
cell := c.FirstChild()
96108
name, ok := getFirstLinkText(cell, src)
97109
if !ok {
98-
return nil, false, fmt.Errorf("\"Webhook event payload\" table was found, but first cell did not contain hook name: %q", cell.Text(src))
110+
return nil, false, fmt.Errorf("\"Webhook event payload\" table was found, but first cell did not contain hook name: %q", textOf(cell, src))
99111
}
100112

101113
// Second cell
@@ -138,15 +150,15 @@ Toplevel:
138150
k := n.Kind()
139151
if !sawAbout {
140152
// When '## About events that trigger workflows'
141-
if h, ok := n.(*ast.Heading); ok && h.Level == 2 && string(h.Text(src)) == "About events that trigger workflows" {
153+
if h, ok := n.(*ast.Heading); ok && h.Level == 2 && textOf(h, src) == "About events that trigger workflows" {
142154
sawAbout = true
143155
dbg.Println("Found \"About events that trigger workflows\" heading")
144156
}
145157
continue
146158
}
147159

148160
if h, ok := n.(*ast.Heading); ok && h.Level == 2 {
149-
currentHook = string(h.Text(src))
161+
currentHook = textOf(h, src)
150162
dbg.Printf("Found new hook %q\n", currentHook)
151163
continue
152164
}

0 commit comments

Comments
 (0)