-
Notifications
You must be signed in to change notification settings - Fork 6
/
yuque.js
112 lines (97 loc) · 2.4 KB
/
yuque.js
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
const SDK = require("@yuque/sdk");
const fs = require("fs");
const { isDate, parseDate, formatDate, formatDate2, formatDate3 } = require("./util");
const namespace = "xiaoyu2er/xwlb";
const args = process.argv.slice(2);
const token = args[0];
const client = new SDK({
token,
// other options
});
function getDocUrl(doc) {
if (doc) {
return `https://www.yuque.com/xiaoyu2er/xwlb/${doc.slug}`;
}
return "";
}
async function getDocs() {
try {
const result = await client.docs.list({ namespace });
return [null, result];
} catch (e) {
return [e];
}
}
async function getDoc(date) {
const id = formatDate(date);
try {
const result = await client.docs.get({ namespace, slug: id, data: { raw: 1 } });
return [null, result];
} catch (e) {
return [e];
}
}
async function createDoc(date) {
const [err, doc] = await getDoc(date);
if (doc) {
return [err, doc];
}
if (err) {
if (err.status !== 404) {
return [err, doc];
}
}
var id = formatDate(date);
var text = fs.readFileSync(`./news/${id}.md`).toString().replace(/^.+\n/, "");
try {
const result = await client.docs.create({
namespace,
data: {
title: `新闻联播 ${id}`,
slug: `${id}`,
public: 1,
format: "markdown",
body: text,
},
});
return [null, result];
} catch (e) {
return [e];
}
// result = await client.docs.delete({ namespace: "xiaoyu2er/ehuk28", id: 48568695 });
// console.log(result);
}
async function main() {
var files = fs.readdirSync("./news").map((f) => f.replace(".md", ""));
for (const file of files) {
const [err, result] = await createDoc(file);
console.log(file, err, getDocUrl(result));
}
}
if (!module.parent) {
// this is the main module
// getDocs().then(([err, result]) => {
// if (err) {
// console.log(err);
// } else {
// console.log(result.map((doc) => getDocUrl(doc)).join("\n"));
// }
// });
// getDoc("2021-07-07").then(([err, doc]) => {
// if (err) {
// console.log(err.status);
// } else {
// console.log(getDocUrl(doc));
// }
// });
let date = args[1] ? new Date(args[1]) : new Date();
createDoc(date).then(([err, result]) => {
if (err) {
console.log(err);
} else {
console.log(date, getDocUrl(result));
}
});
} else {
// we were require()d from somewhere else
}