Repository navigation
Expand file tree
/
Copy pathexample_documents_test.go
More file actions
100 lines (89 loc) · 2.91 KB
/
Copy pathexample_documents_test.go
File metadata and controls
100 lines (89 loc) · 2.91 KB
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
package mark_test
import (
"context"
"errors"
"fmt"
"log"
"testing/fstest"
mark "github.com/kovetskiy/mark/v17"
"github.com/kovetskiy/mark/v17/confluence"
"github.com/kovetskiy/mark/v17/report"
)
// Two documents rendered in memory are published together, linking to each
// other and reading an image from an in-memory file system, with no staging
// directory on disk. Their pages are tracked by keys that stay the same from
// run to run, under a scope of their own, so a later run that no longer
// renders one of them archives its page.
func ExampleRun_documents() {
assets := fstest.MapFS{
"reference/diagram.png": {Data: []byte("\x89PNG\r\n\x1a\n")},
}
header := "<!-- Space: DOCS -->\n<!-- Parent: Reference -->\n"
err := mark.Run(context.Background(), mark.Config{
Connection: mark.ConnectionConfig{
BaseURL: "https://example.atlassian.net/wiki",
Username: "bot@example.com",
Password: "api-token",
},
Tracking: mark.TrackingConfig{
TrackPages: true,
OnOrphan: report.OnOrphanArchive,
},
DocumentScope: "api-reference",
// Every document is tried, and each failure returned.
ContinueOnError: true,
Documents: []mark.Document{
{
// Where it sits: ./groups.md and diagram.png resolve from here.
Path: "reference/users.md",
// What the manifest remembers it by, the same on every run.
Key: "reference/users.md",
// What the report names as the file to edit.
Source: "templates/users.md.tmpl",
Content: []byte(header + "<!-- Title: Users -->\n\nSee [groups](./groups.md).\n\n\n"),
FS: assets,
},
{
Path: "reference/groups.md",
Key: "reference/groups.md",
Source: "templates/groups.md.tmpl",
Content: []byte(header + "<!-- Title: Groups -->\n\nGroups hold [users](./users.md).\n"),
FS: assets,
},
},
})
// A document that failed names its Path, and its Source.
var failed *mark.FilesFailedError
if errors.As(err, &failed) {
for _, fileErr := range failed.Errors {
fmt.Printf("%s (from %s): %v\n", fileErr.File, fileErr.Source, fileErr.Err)
}
} else if err != nil {
log.Fatal(err)
}
}
// A Publisher publishes documents one at a time, keeping its browser and
// caches between them. It keeps no page manifest, so a document's Key is not
// used: its page is found by its title.
func ExamplePublisher_PublishDocument() {
ctx := context.Background()
api := confluence.NewAPI("https://example.atlassian.net/wiki", confluence.Options{
Username: "bot@example.com",
Password: "api-token",
})
publisher, err := mark.NewPublisher(ctx, api, mark.PublishConfig{})
if err != nil {
log.Fatal(err)
}
defer publisher.Close()
page, err := publisher.PublishDocument(ctx, mark.Document{
Path: "release-notes.md",
Content: []byte("<!-- Space: DOCS -->\n<!-- Title: Release notes -->\n\nWhat changed.\n"),
})
if err != nil {
log.Fatal(err)
}
if page != nil {
fmt.Println(page.Title)
}
}