Skip to content

Commit d6502dd

Browse files
committed
search support other packages
1 parent fcf7392 commit d6502dd

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

pkg/godoc/search.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ type SearchResult struct {
1414
}
1515

1616
type SearchPackageInfo struct {
17-
Name string
18-
Path string
19-
Synopsis string
20-
GoDocUrl string
17+
Name string
18+
Path string
19+
Synopsis string
20+
GoDocUrl string
21+
OtherPackages []string `json:"other_packages_in_this_module,omitempty"`
2122
}
2223

2324
func Search(query string) (*SearchResult, error) {
@@ -74,12 +75,17 @@ func extractPackageInfo(selection *goquery.Selection) (*SearchPackageInfo, error
7475
if err != nil {
7576
return nil, err
7677
}
78+
otherPackages, err := extractOtherPackages(selection)
79+
if err != nil {
80+
return nil, err
81+
}
7782

7883
return &SearchPackageInfo{
79-
Name: name,
80-
Path: path,
81-
Synopsis: synopsis,
82-
GoDocUrl: baseURL() + url,
84+
Name: name,
85+
Path: path,
86+
Synopsis: synopsis,
87+
GoDocUrl: baseURL() + url,
88+
OtherPackages: otherPackages,
8389
}, nil
8490
}
8591

@@ -124,6 +130,31 @@ func extractPackageGoDocUrl(selection *goquery.Selection) (string, error) {
124130
return goDocUrl, nil
125131
}
126132

133+
func extractOtherPackages(s *goquery.Selection) ([]string, error) {
134+
var otherPackages []string
135+
var moduleName string
136+
tmp := s.Find("div.SearchSnippet-sub.go-textSubtle").
137+
Find("strong").Text()
138+
tmp = strings.TrimPrefix(tmp, "Other packages in module")
139+
tmp = strings.Trim(tmp, ":")
140+
moduleName = strings.TrimSpace(tmp)
141+
if moduleName == "" {
142+
return nil, nil
143+
}
144+
145+
s.Find("a.go-Chip.go-Chip--subtle").
146+
Each(func(i int, s *goquery.Selection) {
147+
p := s.Text()
148+
p = strings.TrimSpace(p)
149+
if p == "" {
150+
return
151+
}
152+
otherPackages = append(otherPackages, moduleName+"/"+p)
153+
})
154+
155+
return otherPackages, nil
156+
}
157+
127158
func getDoc(query string) (*goquery.Document, error) {
128159
p, e := html.Parse(strings.NewReader(query))
129160
if e != nil {

0 commit comments

Comments
 (0)