-
-
Notifications
You must be signed in to change notification settings - Fork 674
/
seo.ts
131 lines (114 loc) · 3.55 KB
/
seo.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* eslint-disable @typescript-eslint/naming-convention */
import { getDate, getAuthor } from "@mr-hope/vuepress-shared";
import { fs } from "@vuepress/utils";
import { getCover, getImages, getLocales, logger, resolveUrl } from "./utils";
import type { App, AppDir } from "@vuepress/core";
import type {
ArticleJSONLD,
ExtendPage,
SeoContent,
SeoOptions,
} from "../shared";
export const generateSeo = (
page: ExtendPage,
options: SeoOptions,
app: App
): { OGP: SeoContent; JSONLD: ArticleJSONLD | null } => {
const {
isArticle = (page): boolean =>
Boolean(page.filePathRelative && !page.frontmatter.home),
} = options;
const { base } = app.options;
const {
frontmatter: {
author: pageAuthor,
time,
date = time,
tag,
tags = tag as string[],
},
git = {},
} = page;
const { siteData } = app;
const title =
siteData.locales[page.pathLocale]?.title ||
siteData.title ||
siteData.locales["/"]?.title ||
"";
const author =
pageAuthor === false ? [] : getAuthor(pageAuthor || options.author);
const { updatedTime } = git;
const modifiedTime = updatedTime ? new Date(updatedTime).toISOString() : "";
const articleTags: string[] = Array.isArray(tags)
? tags
: typeof tag === "string"
? [tag]
: [];
const articleTitle = page.title;
const cover = getCover(page, options, app);
const images = getImages(page, options, app);
const locales = getLocales(page.lang, siteData.locales);
let publishedTime = "";
if (date instanceof Date) publishedTime = new Date(date).toISOString();
else if (date) {
const dateInfo = getDate(date);
if (dateInfo && dateInfo.value)
publishedTime = dateInfo.value.toISOString();
}
const ogImage = cover || images[0] || options.fallBackImage || "";
return {
OGP: {
"og:url": resolveUrl(options.hostname, base, page.path),
"og:site_name": title,
"og:title": articleTitle,
"og:description":
page.frontmatter.description ||
(options.autoDescription ? page.frontmatter.summary || "" : ""),
"og:type": isArticle(page) ? "article" : "website",
"og:image": ogImage,
"og:updated_time": modifiedTime,
"og:locale": page.lang,
"og:locale:alternate": locales,
...(ogImage
? {
"twitter:card": "summary_large_image",
"twitter:image:alt": articleTitle,
}
: {}),
"article:author": author[0]?.name,
"article:tag": articleTags,
"article:published_time": publishedTime,
"article:modified_time": modifiedTime,
},
JSONLD: isArticle(page)
? {
"@context": "https://schema.org",
"@type": "NewsArticle",
headline: articleTitle,
image: images.length
? images
: [cover || options.fallBackImage || ""],
datePublished: publishedTime,
dateModified: modifiedTime,
author: author.map((item) => ({ "@type": "Person", ...item })),
}
: null,
};
};
export const generateRobotsTxt = async (dir: AppDir): Promise<void> => {
logger.load("Generating robots.txt");
const publicPath = dir.public("robots.txt");
let content = fs.existsSync(publicPath)
? await fs.readFile(publicPath, { encoding: "utf8" })
: "";
if (content && !content.includes("User-agent")) {
logger.error();
logger.update("robots.txt seems invalid!");
} else {
content += "\nUser-agent:*\nDisallow:\n";
await fs.writeFile(dir.dest("robots.txt"), content, {
flag: "w",
});
logger.succeed();
}
};