-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (40 loc) · 1.11 KB
/
index.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
const os = require("node:os");
const defaultRobots = Object.freeze({
user_agent: "*",
allow: ["/", "/page/", "/archives/", "/schedule/", "/tags/"],
disallow: ["/js/", "/css/", "/images/"],
sitemaps: [""],
});
hexo.config.robots = Object.assign({}, defaultRobots, hexo.config.robots);
if (Array.isArray(hexo.config.robots.more)) {
hexo.config.robots.more.forEach((item, index, array) => {
array[index] = Object.assign({}, defaultRobots, item);
});
}
const output = (robots) => {
const { user_agent, allow, disallow, sitemaps } = robots;
return [
`User-agent: ${user_agent}`,
"",
...allow.map((item) => `Allow: ${item}`),
"",
...disallow.map((item) => `Disallow: ${item}`),
"",
...sitemaps.map((item) => `Sitemap: ${item}`),
].join(os.EOL);
};
hexo.extend.generator.register("robots", function (locals) {
const { config } = this;
const { robots } = config;
let data = output(robots);
if (robots.more) {
data +=
os.EOL +
os.EOL +
robots.more.map((item) => output(item)).join(os.EOL + os.EOL);
}
return {
path: "robots.txt",
data,
};
});