-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
46 lines (36 loc) · 933 Bytes
/
example_test.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
package mdlinks_test
import (
"errors"
"fmt"
"testing/fstest"
"github.com/artyom/mdlinks"
)
const Doc1 = `
# Document One
Text with a [broken link](non-existing-file.md).
`
const Doc2 = `
# Document Two
Example of [internal](#document-two),
and [external](../doc1.md#document-one) links.
No such [reference][1].
[1]: #invalid-ref
`
func Example() {
fs := make(fstest.MapFS) // in real scenario this will likely be os.DirFS(dir)
writeFile(fs, "doc1.md", Doc1)
writeFile(fs, "subdir/doc2.md", Doc2)
err := mdlinks.CheckFS(fs, "*.md")
var e *mdlinks.BrokenLinksError
if errors.As(err, &e) {
for _, link := range e.Links {
fmt.Println(link)
}
}
// Output:
// doc1.md: link "non-existing-file.md" points to a non-existing file
// subdir/doc2.md: link "#invalid-ref" points to a non-existing local slug
}
func writeFile(fs fstest.MapFS, name, body string) {
fs[name] = &fstest.MapFile{Data: []byte(body)}
}