-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_find_all_test.go
57 lines (43 loc) · 1.13 KB
/
example_find_all_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
47
48
49
50
51
52
53
54
55
56
57
package larkdown_test
import (
"fmt"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/text"
"go.abhg.dev/goldmark/hashtag"
"github.com/will-wow/larkdown"
"github.com/will-wow/larkdown/match"
)
var findAllMarkdown = `
# My Recipe
Here's a long story about making dinner.
## Tags
#dinner #chicken
## Ingredients
- Chicken
- Vegetables
- Salt
- Pepper
`
func ExampleFindAll() {
source := []byte(findAllMarkdown)
// Preprocess the markdown into goldmark AST
md := goldmark.New(
// Parse hashtags to they can be matched against.
goldmark.WithExtensions(
&hashtag.Extender{Variant: hashtag.ObsidianVariant},
),
)
doc := md.Parser().Parse(text.NewReader(source))
tagsQuery := []match.Node{
match.Branch{Level: 2, Name: []byte("Tags")},
}
// Find all Tags under the tags header, and decode their contents into strings.
tags, err := larkdown.FindAll(doc, source, tagsQuery, match.Tag{}, larkdown.DecodeTag)
if err != nil {
// This will not return an error if there are no tags, only if something else went wrong.
panic(fmt.Errorf("error finding tags: %w", err))
}
fmt.Println(tags)
// Output:
// [dinner chicken]
}