-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.js
More file actions
73 lines (73 loc) · 2.16 KB
/
plugin.js
File metadata and controls
73 lines (73 loc) · 2.16 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
const request = require("sync-request");
const fs = require("fs");
const hljs = require("highlight.js");
const urls = [];
const build_time = new Date().getTime();
exports.loadComments = function () {
return [];
return JSON.parse(request("GET", process.env.COMMENTS_READ).getBody());
};
exports.hooks = {
new_page: function (name) {
//site root exclude
if (name == "index.html") return;
if (name.endsWith("index.html"))
name = name.replace(/\/index\.html$/i, "/");
urls.push(name);
},
build_done: function () {
//xml version
const build_date = new Date();
let base = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n`;
urls.unshift("");
base += urls
.map(
(e) => `<url>
<loc>https://ngeojiajun.github.io/${e}</loc>
<lastmod>${build_date.getFullYear()}-${String(
build_date.getMonth() + 1
).padStart(2, 0)}-${String(build_date.getDate()).padStart(
2,
0
)}</lastmod>
<priority>1.0</priority>
</url>`
)
.join("\n");
base += "\n</urlset>";
fs.writeFileSync("./out/sitemap.xml", base);
},
};
//get effective commit of the tree
//since the tree always clean in CI so we can ignore the fact that the tree might be
//modifed
exports.git_commit = function () {
//This will scramble the ID when it is in development mode
if (process.env.IN_DEVELOPMENT_MODE) {
console.warn(
"Returning current time instead of the commit id to avoid caching the data during the modification"
);
return build_time;
}
//first check if the .git exists
if (!fs.existsSync(".git")) {
//the PWD is not git repo
return "0000000000000000000000000000000000000000";
}
//second get its ref
//ref can be either the commit id or the ref to branch
const ref = fs
.readFileSync(".git/HEAD", "utf8")
.split(":")
.slice(-1)[0]
.trim();
if (ref.includes("/")) {
//external reference to branch
return fs.readFileSync(`.git/${ref}`, "utf8").trim();
} else {
//checked out on a commit id
return ref;
}
};
exports.highlight = (str, lang) =>
hljs.highlight(str, { language: lang }).value;