Skip to content

Commit

Permalink
refactor: migrate to goldmark (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon authored Feb 9, 2023
1 parent 2a521e9 commit da8faf4
Show file tree
Hide file tree
Showing 25 changed files with 752 additions and 778 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist/
.idea
40 changes: 27 additions & 13 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"strings"
"sync"

"github.com/russross/blackfriday/v2"
"github.com/sourcegraph/docsite/markdown"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"

"github.com/sourcegraph/docsite/markdown"
)

// Check checks the site content for common problems (such as broken links).
Expand Down Expand Up @@ -84,34 +86,46 @@ type contentPageCheckData struct {

func (s *Site) checkContentPage(page *contentPageCheckData) (problems []string) {
// Find invalid links.
ast := markdown.NewParser(markdown.NewBfRenderer()).Parse(page.Data)
ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
if entering && (node.Type == blackfriday.Link || node.Type == blackfriday.Image) {
u, err := url.Parse(string(node.LinkData.Destination))
doc := markdown.New(markdown.Options{}).Parser().Parse(text.NewReader(page.Data))
err := ast.Walk(doc, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering && (node.Kind() == ast.KindLink || node.Kind() == ast.KindImage) {
var dest string
switch n := node.(type) {
case *ast.Link:
dest = string(n.Destination)
case *ast.Image:
dest = string(n.Destination)
default:
panic("unreachable")
}

u, err := url.Parse(dest)
if err != nil {
problems = append(problems, fmt.Sprintf("invalid URL %q", node.LinkData.Destination))
return blackfriday.GoToNext
problems = append(problems, fmt.Sprintf("invalid URL %q", dest))
return ast.WalkContinue, nil
}

isPathOnly := u.Scheme == "" && u.Host == ""

// Reject absolute paths because they will break when browsing the docs on
// GitHub/Sourcegraph in the repository, or if the root path ever changes.
if isPathOnly && strings.HasPrefix(u.Path, "/") {
problems = append(problems, fmt.Sprintf("must use relative, not absolute, link to %s", node.LinkData.Destination))
problems = append(problems, fmt.Sprintf("must use relative, not absolute, link to %s", dest))
}

if node.Type == blackfriday.Link {
if node.Kind() == ast.KindLink {
// Require that relative paths link to the actual .md file, i.e not the "foo" folder in the case of
// of "foo/index.md", so that browsing docs on the file system works.
if isPathOnly && u.Path != "" && filepath.Ext(u.Path) == "" {
problems = append(problems, fmt.Sprintf("must link to .md file, not %s", u.Path))
}
}
}

return blackfriday.GoToNext
return ast.WalkContinue, nil
})
if err != nil {
problems = append(problems, fmt.Sprintf("find invalid links: %v", err))
}

// Find broken links.
handler := s.Handler()
Expand All @@ -132,7 +146,7 @@ func (s *Site) checkContentPage(page *contentPageCheckData) (problems []string)
return
}
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
if rr.Code != http.StatusOK && rr.Code != http.StatusMovedPermanently {
problems = append(problems, fmt.Sprintf("broken link to %s", urlStr))
}
},
Expand Down
6 changes: 4 additions & 2 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (

"github.com/mozillazg/go-slugify"
"github.com/pkg/errors"
"github.com/sourcegraph/docsite/markdown"
"github.com/sourcegraph/go-jsonschema/jsonschema"
"github.com/sourcegraph/jsonschemadoc"

"github.com/sourcegraph/docsite/markdown"
)

// createMarkdownFuncs creates the standard set of Markdown functions expected by documentation
Expand Down Expand Up @@ -81,6 +82,7 @@ func createMarkdownFuncs(site *Site) markdown.FuncMap {
<code>{{.Title}}</code>
</h2>
<div class="json-schema-doc pre-wrap">
{{.Schema}}
</div>`

Expand All @@ -99,7 +101,7 @@ func createMarkdownFuncs(site *Site) markdown.FuncMap {
return "", err
}

doc, err := markdown.Run(ctx, []byte(output.String()), markdown.Options{})
doc, err := markdown.Run(output.Bytes(), markdown.Options{})
if err != nil {
return "", err
}
Expand Down
21 changes: 11 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
module github.com/sourcegraph/docsite

require (
github.com/Depado/bfchroma v1.2.0
github.com/alecthomas/chroma v0.8.1
github.com/alecthomas/colour v0.1.0 // indirect
github.com/dlclark/regexp2 v1.2.1 // indirect
github.com/alecthomas/chroma v0.10.0
github.com/mozillazg/go-slugify v0.2.0
github.com/mozillazg/go-unidecode v0.1.1 // indirect
github.com/pkg/errors v0.9.1
github.com/russross/blackfriday/v2 v2.0.1
github.com/shurcooL/sanitized_anchor_name v1.0.0
github.com/sourcegraph/go-jsonschema v0.0.0-20191016093751-6a4f2b621f5d
github.com/sourcegraph/jsonschemadoc v0.0.0-20190214000648-1850b818f08c
github.com/stretchr/testify v1.4.0 // indirect
github.com/yuin/goldmark v1.5.4
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914
golang.org/x/tools v0.0.0-20191122071640-df8e87c2cec0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.7
gopkg.in/yaml.v2 v2.3.0
)

require github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
require (
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/mozillazg/go-unidecode v0.1.1 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.17
go 1.18
54 changes: 16 additions & 38 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
github.com/Depado/bfchroma v1.2.0 h1:NyYPFVhWvq8S2ts6Ok4kwXVE3TEO5fof+9ZOKbBJQUo=
github.com/Depado/bfchroma v1.2.0/go.mod h1:U3RJUYwWVJrZRaJQyfS+wuxBApSTR/BC37PhAI+Ydps=
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
github.com/alecthomas/chroma v0.6.0/go.mod h1:MmozekIi2rfQSzDcdEZ2BoJ9Pxs/7uc2Y4Boh+hIeZo=
github.com/alecthomas/chroma v0.8.1 h1:ym20sbvyC6RXz45u4qDglcgr8E313oPROshcuCHqiEE=
github.com/alecthomas/chroma v0.8.1/go.mod h1:sko8vR34/90zvl5QdcUdvzL3J8NKjAUx9va9jPuFNoM=
github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
github.com/alecthomas/colour v0.1.0 h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk=
github.com/alecthomas/colour v0.1.0/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
github.com/alecthomas/kong v0.2.4/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1 h1:GDQdwm/gAcJcLAKQQZGOJ4knlw+7rfEQQcmwTbt4p5E=
github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.2.1 h1:Ff/S0snjr1oZHUNOkvA/gP6KUaMg5vDDl3Qnhjnwgm8=
github.com/dlclark/regexp2 v1.2.1/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mozillazg/go-slugify v0.2.0 h1:SIhqDlnJWZH8OdiTmQgeXR28AOnypmAXPeOTcG7b9lk=
github.com/mozillazg/go-slugify v0.2.0/go.mod h1:z7dPH74PZf2ZPFkyxx+zjPD8CNzRJNa1CGacv0gg8Ns=
github.com/mozillazg/go-unidecode v0.1.1 h1:uiRy1s4TUqLbcROUrnCN/V85Jlli2AmDF6EeAXOeMHE=
Expand All @@ -41,8 +21,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sourcegraph/go-jsonschema v0.0.0-20190205151546-7939fa138765/go.mod h1:6DfNy4BLIggAeittTJ8o9z/6d1ly+YujBTSnv03i7Bk=
Expand All @@ -51,28 +29,28 @@ github.com/sourcegraph/go-jsonschema v0.0.0-20191016093751-6a4f2b621f5d/go.mod h
github.com/sourcegraph/jsonschemadoc v0.0.0-20190214000648-1850b818f08c h1:MXlcJZ1VL5nNGkCj6ZTT71P4pImPkeG2lvzcJYzGvU4=
github.com/sourcegraph/jsonschemadoc v0.0.0-20190214000648-1850b818f08c/go.mod h1:ovHiFoMDwf4nf7ynAc7lIhD4w0nc/6tO27DtVzqYrTQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.5/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg=
github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 h1:yHfZyN55+5dp1wG7wDKv8HQ044moxkyGq12KFFMFDxg=
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594/go.mod h1:U9ihbh+1ZN7fR5Se3daSPoz1CGF9IYtSvWwVQtnzGHU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914 h1:MlY3mEfbnWGmUi4rtHOtNnnnN4UJRGSyLPx+DXA5Sq4=
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 h1:opSr2sbRXk5X5/givKrrKj9HXxFpW2sdCiP8MJSKLQY=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191122071640-df8e87c2cec0 h1:CWlTyMUD9qhx663mgsnpfHQPG6sI9uwY4aWgJvojriU=
golang.org/x/tools v0.0.0-20191122071640-df8e87c2cec0/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *Site) Handler() http.Handler {
}

// Content page found.
data.Content, err = s.newContentPage(r.Context(), filePath, fileData, contentVersion)
data.Content, err = s.newContentPage(filePath, fileData, contentVersion)
}
if err != nil {
// Content page not found.
Expand Down
10 changes: 5 additions & 5 deletions internal/search/excerpt.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package search

import (
"strings"
"bytes"
)

func excerpt(text string, start, end, maxChars int) string {
func excerpt(text []byte, start, end, maxChars int) []byte {
origStart := start
origEnd := end

Expand All @@ -20,20 +20,20 @@ func excerpt(text string, start, end, maxChars int) string {

const breakChars = ".\n"

if index := strings.IndexAny(text[start:origStart], breakChars); index != -1 {
if index := bytes.IndexAny(text[start:origStart], breakChars); index != -1 {
start += index + 1
end += index
if end > len(text) {
end = len(text)
}
}

if index := strings.LastIndexAny(text[origEnd:end], breakChars); index != -1 {
if index := bytes.LastIndexAny(text[origEnd:end], breakChars); index != -1 {
end = origEnd + index + 1
if end > len(text) {
end = len(text)
}
}

return strings.TrimSpace(text[start:end])
return bytes.TrimSpace(text[start:end])
}
4 changes: 2 additions & 2 deletions internal/search/excerpt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestExcerpt(t *testing.T) {
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := excerpt(test.text, test.start, test.end, test.maxChars)
if got != test.want {
got := excerpt([]byte(test.text), test.start, test.end, test.maxChars)
if string(got) != test.want {
t.Errorf("got %q, want %q", got, test.want)
}
})
Expand Down
2 changes: 1 addition & 1 deletion internal/search/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Document struct {
ID DocID // the document ID
Title string // the document title
URL string // the document URL
Data string // the text content
Data []byte // the text content
}

// Index is a search index.
Expand Down
3 changes: 1 addition & 2 deletions internal/search/index/search.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package index

import (
"context"
"sort"

"github.com/sourcegraph/docsite/internal/search/query"
Expand All @@ -20,7 +19,7 @@ type DocumentResult struct {
}

// Search performs a search against the index.
func (i *Index) Search(ctx context.Context, query query.Query) (*Result, error) {
func (i *Index) Search(query query.Query) (*Result, error) {
var documentResults []DocumentResult
for _, doc := range i.index {
if query.Match(doc.URL, doc.Data) {
Expand Down
8 changes: 4 additions & 4 deletions internal/search/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func Parse(queryStr string) Query {
}

// Match reports whether the path or text contains at least 1 match of the query.
func (q Query) Match(pathStr, text string) bool {
func (q Query) Match(pathStr string, text []byte) bool {
name := path.Base(pathStr)

for _, token := range q.tokens {
if token.pattern.MatchString(name) {
return true
}
if token.pattern.MatchString(text) {
if token.pattern.Match(text) {
return true
}
}
Expand All @@ -51,7 +51,7 @@ func (q Query) Match(pathStr, text string) bool {
const maxMatchesPerDoc = 50

// Score scores the query match against the path and text.
func (q Query) Score(pathStr, text string) float64 {
func (q Query) Score(pathStr string, text []byte) float64 {
name := path.Base(pathStr)

tokensInName := 0
Expand All @@ -61,7 +61,7 @@ func (q Query) Score(pathStr, text string) float64 {
if token.pattern.MatchString(name) {
tokensInName++
}
count := len(token.pattern.FindAllStringIndex(text, maxMatchesPerDoc))
count := len(token.pattern.FindAllIndex(text, maxMatchesPerDoc))
if count > 0 {
tokensMatching++
}
Expand Down
7 changes: 3 additions & 4 deletions internal/search/search.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package search

import (
"context"

"github.com/pkg/errors"

"github.com/sourcegraph/docsite/internal/search/index"
"github.com/sourcegraph/docsite/internal/search/query"
)
Expand All @@ -20,8 +19,8 @@ type DocumentResult struct {
SectionResults []SectionResult
}

func Search(ctx context.Context, query query.Query, index *index.Index) (*Result, error) {
result0, err := index.Search(ctx, query)
func Search(query query.Query, index *index.Index) (*Result, error) {
result0, err := index.Search(query)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit da8faf4

Please sign in to comment.