-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (96 loc) · 2.62 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/wujiyu115/yuqueg"
)
type FrontMatter struct {
Title string `json:"title"`
Slug string `json:"slug"`
Description interface{} `json:"description"`
Date time.Time `json:"date"`
Tags []string `json:"tags"`
IsCJKLanguage bool
}
type config struct {
NameSpace []string `toml:"namespace"`
ImageMirror string `toml:"image_mirror"`
Dir string `toml:"dir"`
}
type Yuque2mdConfig struct {
YuQue config `toml:"yuque2md"`
}
func sinkToFile(namespace string, slug string, doc yuqueg.DocDetail, config Yuque2mdConfig) error {
filePath := config.YuQue.Dir + "/" + namespace + "/" + slug + ".md"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println("文件打开失败", err)
return err
}
//及时关闭file句柄
defer file.Close()
//写入文件时,使用带缓存的 *Writer
write := bufio.NewWriter(file)
body := doc.Data.Body
// body = strings.ReplaceAll(body, "<br />", "\n")
// re3, _ := regexp.Compile(`<a.+><\/a>`)
// body = re3.ReplaceAllString(body, "")
frontMatter := FrontMatter{
Title: doc.Data.Title,
Slug: doc.Data.Slug,
Description: doc.Data.CustomDescription,
Date: doc.Data.FirstPublishedAt,
Tags: []string{doc.Data.Book.Name},
IsCJKLanguage: true,
}
jsonData, err := json.Marshal(frontMatter)
if err != nil {
fmt.Println("json转换失败", err)
return err
}
body = string(jsonData) + "\n\n" + body
body = strings.ReplaceAll(body, "https://cdn.nlark.com", config.YuQue.ImageMirror)
write.WriteString(body)
//Flush将缓存的文件真正写入到文件中
write.Flush()
return nil
}
func downloadNameSpace(namespace string, token string, config Yuque2mdConfig) error {
l := yuqueg.L
yu := yuqueg.NewService(token)
d, err := yu.Repo.GetToc(namespace)
if err != nil {
l.Info(err)
return err
}
os.MkdirAll(config.YuQue.Dir+"/"+namespace, os.ModePerm)
for i := 0; i < len(d.Data); i++ {
current := d.Data[i]
doc, err := yu.Doc.Get(namespace, current.Slug, &yuqueg.DocGet{Raw: 1})
if err != nil {
panic(err)
}
sinkToFile(namespace, current.Slug, doc, config)
l.Info("空间:", namespace, " 文章:", current.Title, " 已完成")
}
return nil
}
func main() {
var (
yq Yuque2mdConfig
)
token := os.Getenv("YUQUE_TOKEN")
_, err := toml.DecodeFile("config.toml", &yq)
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(yq.YuQue.NameSpace); i++ {
downloadNameSpace(yq.YuQue.NameSpace[i], token, yq)
}
}