-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg.go
387 lines (365 loc) · 10.3 KB
/
pkg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Package mdlinks provides functions to verify cross document links in a set
// of markdown files.
package mdlinks
import (
"bytes"
"fmt"
"io/fs"
"net/url"
"path"
"strings"
"unicode"
"unicode/utf8"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
)
// Checker allows checks customization.
//
// Usage example:
//
// c := &mdlinks.Checker{
// Matcher: func(s string) (bool, error) { return path.Ext(s) == ".md", nil },
// }
// err := c.CheckFS(os.DirFS(dir))
type Checker struct {
// Matcher takes /-separated paths when CheckFS method traverses filesystem
// (see documentation on fs.WalkDirFunc, its first argument). If Matcher
// returns a non-nil error, CheckFS stops and returns this error. If
// Matcher returns true, file is considered an utf-8 markdown document and
// is processed.
Matcher func(path string) (bool, error)
}
// CheckFS walks file system fsys looking for files using the Matcher function.
// It parses matched files as markdown, looks for local urls (urls that don't
// have schema and domain), and reports if it finds any urls pointing to
// non-existing files.
//
// If error returned is a *BrokenLinksError, it describes found files with
// broken links.
func (c *Checker) CheckFS(fsys fs.FS) error {
if c == nil {
panic("mdlinks: CheckFS called on a nil Checker")
}
if c.Matcher == nil {
panic("mdlinks: CheckFS called with a nil Checker.Matcher")
}
exists := func(p string) bool {
f, err := fsys.Open(p)
if err != nil {
return false
}
defer f.Close()
return true
}
// track processed files to make sure each one is processed only once, even
// if we need to get back to it at a later time to get its header ids. Keys
// are full fsys paths.
seen := make(map[string]*docDetails)
getFileMeta := func(p string) (*docDetails, error) {
docMeta, ok := seen[p]
if ok {
return docMeta, nil
}
b, err := fs.ReadFile(fsys, p)
if err != nil {
return nil, err
}
if !utf8.Valid(b) {
return nil, fmt.Errorf("%s is not a valid utf8 file", p)
}
if docMeta, err = extractDocDetails(b); err != nil {
return nil, err
}
seen[p] = docMeta
return docMeta, nil
}
var brokenLinks []BrokenLink
fn := func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && d.Name() == ".git" {
return fs.SkipDir
}
if d.IsDir() {
return nil
}
switch ok, err := c.Matcher(p); {
case err != nil:
return err
case !ok:
return nil
}
docMeta, err := getFileMeta(p)
if err != nil {
return err
}
for _, s := range docMeta.links {
var srel string // fs.FS relative path that link points to
if s.Path != "" && s.Path[0] == '/' { // e.g. “/abc”
srel = s.Path[1:]
} else if s.Path != "" { // e.g. “abc” or “../abc”
srel = path.Join(strings.TrimSuffix(p, d.Name()), s.Path)
}
// path is non-empty
if srel != "" && !exists(srel) {
brokenLinks = append(brokenLinks, BrokenLink{File: p, Link: s})
continue
}
// path is empty, and fragment is non-empty (internal link)
if s.Path == "" && s.Fragment != "" { // internal link
if _, ok := docMeta.anchors[s.Fragment]; !ok {
brokenLinks = append(brokenLinks, BrokenLink{File: p, Link: s, kind: kindBrokenInternalAnchor})
continue
}
}
if srel == "" || s.Fragment == "" {
continue
}
if ok, _ := c.Matcher(srel); !ok {
continue
}
// path is non-empty, fragment is non-empty, path points to the markdown file
meta2, err := getFileMeta(srel)
if err != nil {
return err
}
if _, ok := meta2.anchors[s.Fragment]; !ok {
brokenLinks = append(brokenLinks, BrokenLink{
File: p,
Link: s,
kind: kindBrokenExternalAnchor,
})
}
}
return nil
}
if err := fs.WalkDir(fsys, ".", fn); err != nil {
return err
}
if len(brokenLinks) != 0 {
return &BrokenLinksError{Links: brokenLinks}
}
return nil
}
// CheckFS walks file system fsys looking for files with their base names
// matching pattern pat (e.g. “*.md”). It parses such files as markdown, looks
// for local urls (urls that don't have schema and domain), and reports if it
// finds any urls pointing to non-existing files.
//
// If error returned is a *BrokenLinksError, it describes found files with
// broken links.
func CheckFS(fsys fs.FS, pat string) error {
if _, err := path.Match(pat, "xxx"); err != nil { // report bad pattern early
return err
}
c := &Checker{
Matcher: func(s string) (bool, error) { return path.Match(pat, path.Base(s)) },
}
return c.CheckFS(fsys)
}
type docDetails struct {
links []LinkInfo // non-external links
anchors map[string]struct{} // header slugs
}
func extractDocDetails(body []byte) (*docDetails, error) {
// nodeContext returns numbers of the first and the last lines of the link
// context: block element that contains it, usually paragraph
nodeContext := func(n ast.Node) (int, int) {
// only block type nodes have usable Lines() method, so if node is not
// a block type, find its first block ancestor
for n.Type() != ast.TypeBlock {
if n.Type() == ast.TypeDocument {
return 0, 0
}
if n = n.Parent(); n == nil {
return 0, 0
}
}
lines := n.Lines()
if lines == nil || lines.Len() == 0 {
return 0, 0
}
start := lines.At(0).Start
stop := lines.At(lines.Len() - 1).Stop
if stop == 0 || start == stop {
return 0, 0
}
startLine := 1 + bytes.Count(body[:start], []byte{'\n'})
endLine := startLine + bytes.Count(body[start:stop], []byte{'\n'})
return startLine, endLine
}
var localLinks []LinkInfo
var anchors map[string]struct{}
// localLink parses s and returns *url.URL only if the link is local
// (schema-less and domain-less link)
localLink := func(s string) *url.URL {
if s == "" {
return nil
}
u, err := url.Parse(s)
if err != nil || u.Scheme != "" || u.Host != "" {
return nil
}
if u.Path == "" && u.Fragment == "" {
return nil
}
return u
}
fn := func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
var u *url.URL
var raw string // link target as seen in the document body
switch n.Kind() {
case ast.KindHeading:
if n, ok := n.(*ast.Heading); ok {
if name := slugify(nodeText(n, body)); name != "" {
if anchors == nil {
anchors = make(map[string]struct{})
}
for i := 0; i < 100; i++ {
var cand string
if i == 0 {
cand = name
} else {
cand = fmt.Sprintf("%s-%d", name, i)
}
if _, ok := anchors[cand]; !ok {
anchors[cand] = struct{}{}
break
}
}
}
}
case ast.KindAutoLink:
if l, ok := n.(*ast.AutoLink); ok && l.AutoLinkType == ast.AutoLinkURL {
raw = string(l.URL(body))
u = localLink(raw)
}
case ast.KindLink:
if l, ok := n.(*ast.Link); ok {
raw = string(l.Destination)
u = localLink(raw)
}
case ast.KindImage:
if l, ok := n.(*ast.Image); ok {
raw = string(l.Destination)
u = localLink(raw)
}
}
if u != nil && raw != "" {
l1, l2 := nodeContext(n)
localLinks = append(localLinks, LinkInfo{
Raw: raw,
Path: u.Path,
Fragment: u.Fragment,
LineStart: l1,
LineEnd: l2,
})
}
return ast.WalkContinue, nil
}
node := mdparser.Parse(text.NewReader(body))
if err := ast.Walk(node, fn); err != nil {
return nil, err
}
return &docDetails{anchors: anchors, links: localLinks}, nil
}
// BrokenLinksError is an error type returned by this package functions to
// report found broken links.
//
// Usage example:
//
// err := mdlinks.CheckFS(os.DirFS(dir), "*.md")
// var e *mdlinks.BrokenLinksError
// if errors.As(err, &e) {
// for _, link := range e.Links {
// log.Println(link)
// }
// }
type BrokenLinksError struct {
Links []BrokenLink
}
func (e *BrokenLinksError) Error() string { return "broken links found" }
// BrokenLink describes broken markdown link and the file it belongs to.
type BrokenLink struct {
File string // file path, relative to directory/filesystem scanned; uses '/' as a separator
Link LinkInfo
kind violationKind
}
func (b BrokenLink) String() string {
switch b.kind {
case kindBrokenInternalAnchor:
return fmt.Sprintf("%s: link %q points to a non-existing local slug", b.File, b.Link.Raw)
case kindBrokenExternalAnchor:
return fmt.Sprintf("%s: link %q points to a non-existing slug", b.File, b.Link.Raw)
}
return fmt.Sprintf("%s: link %q points to a non-existing file", b.File, b.Link.Raw)
}
func (b BrokenLink) Reason() string { return b.kind.String() }
type violationKind byte
const (
kindFileNotExists = iota
kindBrokenInternalAnchor
kindBrokenExternalAnchor
)
func (v violationKind) String() string {
switch v {
case kindBrokenInternalAnchor:
return "link points to a non-existing local slug"
case kindBrokenExternalAnchor:
return "link points to a non-existing slug"
}
return "link points to a non-existing file"
}
// LinkInfo describes markdown link
type LinkInfo struct {
Raw string // as seen in the source, usually “some/path#fragment”
Path string // only the path part of the link
Fragment string // only the fragment part of the link, without '#'
LineStart int // number of the first line of the context (usually paragraph)
LineEnd int // number of the last line of the context (usually paragraph)
}
var mdparser = parser.NewParser(
parser.WithBlockParsers(parser.DefaultBlockParsers()...),
parser.WithInlineParsers(parser.DefaultInlineParsers()...),
parser.WithParagraphTransformers(parser.DefaultParagraphTransformers()...),
)
// nodeText walks node and extracts plain text from it and its descendants,
// effectively removing all markdown syntax
func nodeText(node ast.Node, src []byte) string {
var b strings.Builder
fn := func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
switch n.Kind() {
case ast.KindText:
if t, ok := n.(*ast.Text); ok {
b.Write(t.Text(src))
}
}
return ast.WalkContinue, nil
}
if err := ast.Walk(node, fn); err != nil {
return ""
}
return b.String()
}
func slugify(text string) string {
f := func(r rune) rune {
switch {
case unicode.IsLetter(r) || unicode.IsNumber(r):
return unicode.ToLower(r)
case unicode.IsSpace(r):
return '-'
case r == '-' || r == '_':
return r
}
return -1
}
return strings.Map(f, text)
}