-
-
Notifications
You must be signed in to change notification settings - Fork 673
/
injectHead.ts
112 lines (98 loc) · 3.03 KB
/
injectHead.ts
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
import { getFilename } from "./options";
import { resolveUrl } from "./utils";
import type { App, HeadConfig } from "@vuepress/core";
import type { ResolvedFeedOptionsMap } from "./options";
export const injectLinkstoHead = (
app: App,
options: ResolvedFeedOptionsMap
): void => {
const { base } = app.options;
const { siteData } = app;
const localePaths = Object.keys(options);
// there is only one language, so we append it to siteData
if (localePaths.length === 1) {
const { atomOutputFilename, jsonOutputFilename, rssOutputFilename } =
getFilename(options["/"]);
const getHeadItem = (
name: string,
fileName: string,
type: string
): HeadConfig => {
return [
"link",
{
rel: "alternate",
type,
href: resolveUrl(options["/"].hostname, base, fileName),
title: `${
siteData.title || siteData.locales["/"]?.title || ""
} ${name} Feed`,
},
];
};
// ensure head exists
if (!siteData.head) siteData.head = [];
// add atom link
if (options.atom)
siteData.head.push(
getHeadItem("Atom", atomOutputFilename, "application/atom+xml")
);
// add json link
if (options.json)
siteData.head.push(
getHeadItem("JSON", jsonOutputFilename, "application/json")
);
// add rss link
if (options.rss)
siteData.head.push(
getHeadItem("RSS", rssOutputFilename, "application/rss+xml")
);
}
// there are mutiple languages, so we should append to page
else
app.pages.forEach((page) => {
const { pathLocale } = page;
const localeOptions = options[pathLocale];
if (localePaths.includes(pathLocale)) {
const { atomOutputFilename, jsonOutputFilename, rssOutputFilename } =
getFilename(localeOptions, pathLocale);
const getHeadItem = (
name: string,
fileName: string,
type: string
): HeadConfig => {
return [
"link",
{
rel: "alternate",
type,
href: resolveUrl(localeOptions.hostname, base, fileName),
title: `${
siteData.locales[pathLocale]?.title ||
siteData.title ||
siteData.locales["/"]?.title ||
""
} ${name} Feed`,
},
];
};
// ensure head exists
if (!page.frontmatter.head) page.frontmatter.head = [];
// add atom link
if (localeOptions.atom)
page.frontmatter.head.push(
getHeadItem("Atom", atomOutputFilename, "application/atom+xml")
);
// add json link
if (localeOptions.json)
page.frontmatter.head.push(
getHeadItem("JSON", jsonOutputFilename, "application/json")
);
// add rss link
if (localeOptions.rss)
page.frontmatter.head.push(
getHeadItem("RSS", rssOutputFilename, "application/rss+xml")
);
}
});
};