@techjiang/vuepress-plugin-jsonld 0.0.0-stage → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.cjs ADDED
@@ -0,0 +1,341 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ default: () => index_default,
24
+ jsonldPlugin: () => jsonldPlugin
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var PLUGIN_NAME = "vuepress-plugin-jsonld";
28
+ var warned = /* @__PURE__ */ new Set();
29
+ function warnOnce(file, message) {
30
+ const key = `${file}#${message}`;
31
+ if (warned.has(key)) return;
32
+ warned.add(key);
33
+ console.warn(`[${PLUGIN_NAME}] \u8B66\u544A\uFF1A${message}
34
+ [${PLUGIN_NAME}] \u6587\u4EF6\uFF1A${file}`);
35
+ }
36
+ function isHttpsUrl(value) {
37
+ return /^https:\/\/[^\s]+$/i.test(value);
38
+ }
39
+ function trimSlash(value) {
40
+ return value.replace(/\/+$/, "");
41
+ }
42
+ function toIsoDate(raw, file, field) {
43
+ if (raw === void 0 || raw === null || raw === "") return null;
44
+ const date = raw instanceof Date ? raw : new Date(raw);
45
+ if (Number.isNaN(date.getTime())) {
46
+ warnOnce(
47
+ file,
48
+ `\u65E5\u671F\u5B57\u6BB5\u300C${field}\u300D\u7684\u503C "${String(raw)}" \u683C\u5F0F\u975E\u6CD5\uFF0C\u65E0\u6CD5\u89E3\u6790\u4E3A\u65E5\u671F\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u5C5E\u6027\uFF1B\u8BF7\u4F7F\u7528 ISO 8601 \u683C\u5F0F\uFF08\u5982 2024-01-01T08:00:00+08:00\uFF09`
49
+ );
50
+ return null;
51
+ }
52
+ return date.toISOString();
53
+ }
54
+ function resolveImage(raw, base, hostname, file) {
55
+ const value = (raw ?? "").trim();
56
+ if (value === "") return null;
57
+ if (/^https?:\/\//i.test(value)) {
58
+ if (!/^https:\/\//i.test(value)) {
59
+ warnOnce(
60
+ file,
61
+ `\u56FE\u7247\u5730\u5740 "${value}" \u4E0D\u662F HTTPS \u534F\u8BAE\uFF0Cschema.org \u5EFA\u8BAE\uFF08Google \u8981\u6C42\uFF09\u4F7F\u7528 https:// \u7EDD\u5BF9 URL`
62
+ );
63
+ }
64
+ return value;
65
+ }
66
+ if (!hostname) {
67
+ warnOnce(
68
+ file,
69
+ `\u56FE\u7247\u5730\u5740 "${value}" \u662F\u76F8\u5BF9\u8DEF\u5F84\uFF0C\u4F46\u63D2\u4EF6\u672A\u914D\u7F6E hostname\uFF0C\u65E0\u6CD5\u8865\u5168\u4E3A HTTPS \u7EDD\u5BF9 URL\uFF0C\u5DF2\u8DF3\u8FC7 image \u5C5E\u6027\uFF1B\u8BF7\u5728\u63D2\u4EF6\u914D\u7F6E\u4E2D\u6DFB\u52A0 hostname\uFF08\u5982 'https://example.com'\uFF09\uFF0C\u6216\u76F4\u63A5\u4F7F\u7528 https:// \u5F00\u5934\u7684\u7EDD\u5BF9 URL`
70
+ );
71
+ return null;
72
+ }
73
+ return `${hostname}${trimSlash(base)}/${value.replace(/^\/+/, "")}`;
74
+ }
75
+ function toAbsoluteUrl(path, base, hostname) {
76
+ if (!hostname) return path;
77
+ const cleanPath = path.startsWith(base) ? path : `${trimSlash(base)}${path}`;
78
+ return `${hostname}${cleanPath.startsWith("/") ? cleanPath : `/${cleanPath}`}`;
79
+ }
80
+ function normalizeAuthorItem(raw, file, field) {
81
+ if (typeof raw === "string") {
82
+ return raw === "" ? null : { "@type": "Person", name: raw };
83
+ }
84
+ if (typeof raw === "object" && raw !== null && typeof raw.name === "string" && raw.name !== "") {
85
+ const node = { "@type": raw["@type"] ?? "Person", name: raw.name };
86
+ if (raw.url) node.url = raw.url;
87
+ return node;
88
+ }
89
+ warnOnce(file, `\u5B57\u6BB5\u300C${field}\u300D\u5B58\u5728\u65E0\u6CD5\u8BC6\u522B\u7684\u4F5C\u8005\u6761\u76EE\uFF08\u5E94\u4E3A\u5B57\u7B26\u4E32\u6216\u542B name \u7684\u5BF9\u8C61\uFF09\uFF0C\u8BE5\u6761\u76EE\u5DF2\u8DF3\u8FC7`);
90
+ return null;
91
+ }
92
+ function normalizeAuthors(raw, file, field) {
93
+ if (raw === void 0 || raw === null || raw === "") return null;
94
+ const list = Array.isArray(raw) ? raw : [raw];
95
+ const nodes = list.map((item) => normalizeAuthorItem(item, file, field)).filter((item) => item !== null);
96
+ if (nodes.length === 0) return null;
97
+ return nodes.length === 1 ? nodes[0] : nodes;
98
+ }
99
+ function assignIfDefined(node, key, value) {
100
+ if (value === void 0 || value === null || value === "") return;
101
+ node[key] = value;
102
+ }
103
+ function isHomePage(page, base) {
104
+ return page.frontmatter.home === true || page.path === base || page.path === "/";
105
+ }
106
+ function buildBreadcrumb(page, app, pages, base, hostname) {
107
+ if (typeof page.path !== "string" || page.path === "") return null;
108
+ const dirPath = page.path.slice(0, page.path.lastIndexOf("/") + 1);
109
+ const relativeDir = dirPath.startsWith(base) ? dirPath.slice(base.length) : dirPath;
110
+ const segments = relativeDir.split("/").filter(Boolean);
111
+ if (segments.length === 0) return null;
112
+ const siteName = app.options.title || page.title;
113
+ if (!siteName) return null;
114
+ const items = [
115
+ {
116
+ "@type": "ListItem",
117
+ position: 1,
118
+ name: siteName,
119
+ item: toAbsoluteUrl(base, base, hostname)
120
+ }
121
+ ];
122
+ let acc = "";
123
+ for (const segment of segments) {
124
+ acc += `${segment}/`;
125
+ const parentPath = `/${acc}`;
126
+ const parent = pages.find(
127
+ (p) => p.path === parentPath || p.path === `${base}${acc}` || p.path === `${parentPath}index.html` || p.path === `${base}${acc}index.html`
128
+ );
129
+ if (!parent) continue;
130
+ items.push({
131
+ "@type": "ListItem",
132
+ position: items.length + 1,
133
+ name: parent.title || segment,
134
+ item: toAbsoluteUrl(parent.path, base, hostname)
135
+ });
136
+ }
137
+ items.push({
138
+ "@type": "ListItem",
139
+ position: items.length + 1,
140
+ name: page.title || page.path,
141
+ item: toAbsoluteUrl(page.path, base, hostname)
142
+ });
143
+ return {
144
+ "@context": "https://schema.org",
145
+ "@type": "BreadcrumbList",
146
+ itemListElement: items
147
+ };
148
+ }
149
+ function buildFaqPage(page, file) {
150
+ const faq = page.frontmatter.faq;
151
+ if (!Array.isArray(faq) || faq.length === 0) return null;
152
+ const mainEntity = faq.map((entry) => {
153
+ const question = entry.question ?? entry.q;
154
+ const answer = entry.answer ?? entry.a;
155
+ if (!question || !answer) {
156
+ warnOnce(
157
+ file,
158
+ `frontmatter.faq \u4E2D\u5B58\u5728\u7F3A\u5C11 question \u6216 answer \u7684\u6761\u76EE\uFF08JSON.stringify: ${JSON.stringify(entry)}\uFF09\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u6761\u76EE`
159
+ );
160
+ return null;
161
+ }
162
+ return {
163
+ "@type": "Question",
164
+ name: question,
165
+ acceptedAnswer: { "@type": "Answer", text: answer }
166
+ };
167
+ }).filter((item) => item !== null);
168
+ if (mainEntity.length === 0) return null;
169
+ return {
170
+ "@context": "https://schema.org",
171
+ "@type": "FAQPage",
172
+ mainEntity
173
+ };
174
+ }
175
+ function buildHomepageSchemas(page, app, base, hostname) {
176
+ const file = page.filePath ?? page.path;
177
+ const siteName = page.title || app.options.title;
178
+ const siteDescription = page.frontmatter.description || app.options.description;
179
+ if (!siteName) {
180
+ warnOnce(file, "\u9996\u9875\u7F3A\u5C11\u7AD9\u70B9\u540D\u79F0\uFF08frontmatter.title \u6216\u7AD9\u70B9 config.title\uFF09\uFF0C\u65E0\u6CD5\u751F\u6210 WebSite schema");
181
+ }
182
+ const website = {
183
+ "@context": "https://schema.org",
184
+ "@type": "WebSite",
185
+ name: siteName || "Untitled Site",
186
+ url: toAbsoluteUrl(base, base, hostname)
187
+ };
188
+ assignIfDefined(website, "description", siteDescription);
189
+ const schemas = [website];
190
+ if (siteName) {
191
+ const organization = {
192
+ "@context": "https://schema.org",
193
+ "@type": "Organization",
194
+ name: siteName,
195
+ url: toAbsoluteUrl(base, base, hostname)
196
+ };
197
+ const logo = page.frontmatter.logo;
198
+ if (typeof logo === "string") {
199
+ assignIfDefined(
200
+ organization,
201
+ "logo",
202
+ resolveImage(logo, base, hostname, file)
203
+ );
204
+ }
205
+ schemas.push(organization);
206
+ }
207
+ return schemas;
208
+ }
209
+ function buildArticleSchema(page, file, base, hostname, options) {
210
+ const fm = page.frontmatter;
211
+ if (!page.title) {
212
+ warnOnce(file, "\u7F3A\u5C11\u5FC5\u586B\u5B57\u6BB5 title\uFF08\u5BF9\u5E94 schema.org \u7684 headline\uFF09\uFF0C\u5DF2\u8DF3\u8FC7\u8BE5\u9875\u9762\u7684\u6587\u7AE0 schema");
213
+ return null;
214
+ }
215
+ const schemaType = options.type || "TechArticle";
216
+ const schema = {
217
+ "@context": "https://schema.org",
218
+ "@type": schemaType,
219
+ headline: page.title,
220
+ // 页面唯一标识,利于搜索引擎去重
221
+ mainEntityOfPage: {
222
+ "@type": "WebPage",
223
+ "@id": toAbsoluteUrl(page.path, base, hostname)
224
+ }
225
+ };
226
+ assignIfDefined(schema, "description", fm.description);
227
+ assignIfDefined(
228
+ schema,
229
+ "datePublished",
230
+ // frontmatter.date 优先;缺失时兜底使用 VuePress 从 git 提取的页面创建日期
231
+ // (无 git 环境时 page.date 为 '0000-00-00' 占位值,需过滤掉)
232
+ toIsoDate(fm.date, file, "date") ?? toIsoDate(
233
+ typeof page.date === "string" && page.date !== "" && page.date !== "0000-00-00" ? page.date : null,
234
+ file,
235
+ "page.date"
236
+ )
237
+ );
238
+ assignIfDefined(
239
+ schema,
240
+ "dateModified",
241
+ toIsoDate(fm.dateModified, file, "dateModified") ?? toIsoDate(fm.date, file, "date")
242
+ );
243
+ assignIfDefined(
244
+ schema,
245
+ "author",
246
+ normalizeAuthors(fm.author, file, "author") ?? normalizeAuthors(options.author, file, "options.author")
247
+ );
248
+ assignIfDefined(
249
+ schema,
250
+ "image",
251
+ resolveImage(fm.image ?? options.image, base, hostname, file)
252
+ );
253
+ const keywords = resolveKeywords(fm.tags);
254
+ if (keywords) schema.keywords = keywords;
255
+ return schema;
256
+ }
257
+ function resolveKeywords(raw) {
258
+ if (raw === void 0 || raw === null || raw === "") return null;
259
+ const list = Array.isArray(raw) ? raw : typeof raw === "string" ? raw.split(",") : [];
260
+ const cleaned = list.map((item) => typeof item === "string" ? item.trim() : "").filter((item) => item !== "");
261
+ return cleaned.length > 0 ? cleaned.join(", ") : null;
262
+ }
263
+ function pushJsonldHead(page, schema) {
264
+ page.frontmatter ??= {};
265
+ const head = page.frontmatter.head ??= [];
266
+ const payload = JSON.stringify(schema);
267
+ const duplicated = head.some(
268
+ (tag) => tag[0] === "script" && tag[1]?.type === "application/ld+json" && tag[2] === payload
269
+ );
270
+ if (!duplicated) {
271
+ head.push(["script", { type: "application/ld+json" }, payload]);
272
+ }
273
+ }
274
+ function jsonldPlugin(options = {}) {
275
+ if (options.hostname !== void 0 && !isHttpsUrl(trimSlash(options.hostname))) {
276
+ console.warn(
277
+ `[${PLUGIN_NAME}] \u8B66\u544A\uFF1A\u914D\u7F6E\u9879 hostname \u5E94\u4E3A https:// \u5F00\u5934\u7684\u4E0D\u5E26\u8DEF\u5F84\u57DF\u540D\uFF08\u5982 'https://example.com'\uFF09\uFF0C\u5F53\u524D\u503C\u4E3A "${options.hostname}"\uFF0C\u76F8\u5BF9\u8DEF\u5F84\u56FE\u7247\u5C06\u65E0\u6CD5\u6B63\u786E\u8865\u5168`
278
+ );
279
+ }
280
+ if (options.type !== void 0 && (typeof options.type !== "string" || options.type === "")) {
281
+ console.warn(
282
+ `[${PLUGIN_NAME}] \u8B66\u544A\uFF1A\u914D\u7F6E\u9879 type \u5E94\u4E3A\u975E\u7A7A\u5B57\u7B26\u4E32\u7684 schema.org \u7C7B\u578B\u540D\uFF08\u5982 'Article'\uFF09\uFF0C\u5F53\u524D\u503C\u5DF2\u5FFD\u7565\uFF0C\u5C06\u4F7F\u7528\u9ED8\u8BA4\u7684 TechArticle`
283
+ );
284
+ }
285
+ return {
286
+ name: PLUGIN_NAME,
287
+ /**
288
+ * extendsPage 在每个页面编译时逐页调用。
289
+ * 在这里往 page.frontmatter.head 追加 <script type="application/ld+json">,
290
+ * VuePress SSR 会把它渲染进首屏 HTML,无需任何客户端脚本。
291
+ *
292
+ * 注意:此阶段 app.pages 尚未收集完成,所以依赖全站页面的
293
+ * BreadcrumbList 延后到 onPrepared 钩子注入。
294
+ */
295
+ extendsPage(page, app) {
296
+ page.frontmatter ??= {};
297
+ const file = page.filePath ?? page.path;
298
+ if (page.frontmatter.jsonld === false) return;
299
+ const base = app.options.base ?? "/";
300
+ const hostname = options.hostname ? trimSlash(options.hostname) : "";
301
+ const schemas = [];
302
+ if (isHomePage(page, base)) {
303
+ schemas.push(...buildHomepageSchemas(page, app, base, hostname));
304
+ } else {
305
+ const article = buildArticleSchema(page, file, base, hostname, options);
306
+ if (article) schemas.push(article);
307
+ }
308
+ const faqPage = buildFaqPage(page, file);
309
+ if (faqPage) schemas.push(faqPage);
310
+ if (typeof options.additionalSchemas === "function") {
311
+ schemas.push(...options.additionalSchemas(page, app));
312
+ } else if (Array.isArray(options.additionalSchemas)) {
313
+ schemas.push(...options.additionalSchemas);
314
+ }
315
+ for (const schema of schemas) {
316
+ pushJsonldHead(page, schema);
317
+ }
318
+ },
319
+ /**
320
+ * onPrepared 在全部页面收集完成后调用一次。
321
+ * 此时 app.pages 可用,可以为存在目录层级的页面生成 BreadcrumbList。
322
+ */
323
+ onPrepared(app) {
324
+ const base = app.options.base ?? "/";
325
+ const hostname = options.hostname ? trimSlash(options.hostname) : "";
326
+ const pages = app.pages ?? [];
327
+ for (const page of pages) {
328
+ if (page.frontmatter?.jsonld === false) continue;
329
+ const breadcrumb = buildBreadcrumb(page, app, pages, base, hostname);
330
+ if (!breadcrumb) continue;
331
+ pushJsonldHead(page, breadcrumb);
332
+ }
333
+ }
334
+ };
335
+ }
336
+ var index_default = jsonldPlugin;
337
+ // Annotate the CommonJS export names for ESM import in node:
338
+ 0 && (module.exports = {
339
+ jsonldPlugin
340
+ });
341
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2026 科技酱 (TechSauce)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * vuepress-plugin-jsonld\n *\n * VuePress 2 插件:构建时(SSR 阶段)向每个页面的 head 注入符合\n * schema.org 规范的 JSON-LD 结构化数据。\n *\n * 作者:科技酱 (TechSauce) https://docs.asoe.cn\n * 核心行为:\n * 1. 通过 extendsPage 钩子读取页面 frontmatter:\n * title / description / date / dateModified / author / image / tags\n * 2. 自动判断 schema 类型:\n * - 首页(frontmatter.home === true 或站点根路径)→ WebSite + Organization\n * - 普通文章 → TechArticle(可通过 options.type 覆盖)\n * - 存在父级目录页(与主题侧边栏一致的层级结构)→ 额外生成 BreadcrumbList\n * - frontmatter.faq → 额外生成 FAQPage\n * 3. 日期自动转为 ISO 8601;image 自动补全为基于站点 base 的 HTTPS 绝对 URL;\n * 缺失字段直接跳过该属性,绝不输出空值。\n * 4. 通过页面 head 配置注入 <script type=\"application/ld+json\"> —— 这是 SSR\n * 静态标签,首屏 HTML 直接携带,不依赖任何客户端动态插入;\n * 相同内容自动去重,不会因钩子多次触发而重复注入。\n * 5. 构建时校验:图片非绝对 HTTPS、日期非法、必填字段缺失时输出清晰的\n * 中文警告,并指出具体文件路径。\n */\nimport type { App, Page, Plugin } from 'vuepress/core'\nimport type {\n AuthorItem,\n AuthorInput,\n JsonldFrontmatter,\n JsonldPage,\n JsonldPluginOptions,\n SchemaNode,\n} from './types'\n\nexport type {\n AuthorInput,\n JsonldFrontmatter,\n JsonldPluginOptions,\n SchemaNode,\n} from './types'\n\n/** 插件名,也是所有警告的日志前缀 */\nconst PLUGIN_NAME = 'vuepress-plugin-jsonld'\n\n/** 已警告过的「文件 + 消息」集合,避免构建时同一问题重复刷屏 */\nconst warned = new Set<string>()\n\n/** 输出中文警告并附带具体文件路径(同一文件的同一问题只警告一次) */\nfunction warnOnce(file: string, message: string): void {\n const key = `${file}#${message}`\n if (warned.has(key)) return\n warned.add(key)\n console.warn(`[${PLUGIN_NAME}] 警告:${message}\\n[${PLUGIN_NAME}] 文件:${file}`)\n}\n\n/** 是否为 https:// 开头的绝对 URL */\nfunction isHttpsUrl(value: string): boolean {\n return /^https:\\/\\/[^\\s]+$/i.test(value)\n}\n\n/** 去掉末尾斜杠 */\nfunction trimSlash(value: string): string {\n return value.replace(/\\/+$/, '')\n}\n\n/**\n * 把任意日期输入(Date / ISO 字符串 / 时间戳)转为 ISO 8601 字符串。\n * 输入为空返回 null(上层直接跳过该属性);非法格式打印警告。\n */\nfunction toIsoDate(raw: unknown, file: string, field: string): string | null {\n if (raw === undefined || raw === null || raw === '') return null\n const date = raw instanceof Date ? raw : new Date(raw as string | number)\n if (Number.isNaN(date.getTime())) {\n warnOnce(\n file,\n `日期字段「${field}」的值 \"${String(raw)}\" 格式非法,无法解析为日期,已跳过该属性;请使用 ISO 8601 格式(如 2024-01-01T08:00:00+08:00)`,\n )\n return null\n }\n return date.toISOString()\n}\n\n/**\n * 图片地址解析:\n * - 已是 https:// 绝对 URL → 原样使用(http:// 触发警告,仍使用但不安全)\n * - 以 / 开头的站点内路径 → hostname + base + 路径 补全为绝对 URL\n * - 其他相对路径 → 同上按站点内路径处理\n * - 未配置 hostname 时打印警告并返回 null(跳过 image 属性)\n */\nfunction resolveImage(\n raw: string | undefined,\n base: string,\n hostname: string,\n file: string,\n): string | null {\n const value = (raw ?? '').trim()\n if (value === '') return null\n\n if (/^https?:\\/\\//i.test(value)) {\n if (!/^https:\\/\\//i.test(value)) {\n warnOnce(\n file,\n `图片地址 \"${value}\" 不是 HTTPS 协议,schema.org 建议(Google 要求)使用 https:// 绝对 URL`,\n )\n }\n return value\n }\n\n if (!hostname) {\n warnOnce(\n file,\n `图片地址 \"${value}\" 是相对路径,但插件未配置 hostname,无法补全为 HTTPS 绝对 URL,已跳过 image 属性;请在插件配置中添加 hostname(如 'https://example.com'),或直接使用 https:// 开头的绝对 URL`,\n )\n return null\n }\n\n // 站点内路径统一为:hostname + base + 去掉开头斜杠的路径\n return `${hostname}${trimSlash(base)}/${value.replace(/^\\/+/, '')}`\n}\n\n/** 把站点内路径补全为绝对 URL;没有 hostname 时保留原路径 */\nfunction toAbsoluteUrl(path: string, base: string, hostname: string): string {\n if (!hostname) return path\n const cleanPath = path.startsWith(base) ? path : `${trimSlash(base)}${path}`\n return `${hostname}${cleanPath.startsWith('/') ? cleanPath : `/${cleanPath}`}`\n}\n\n/** 规范化单个作者条目为 schema.org 节点;无法识别时警告并返回 null */\nfunction normalizeAuthorItem(raw: AuthorItem, file: string, field: string): SchemaNode | null {\n // 字符串:'张三' → Person 节点\n if (typeof raw === 'string') {\n return raw === '' ? null : { '@type': 'Person', name: raw }\n }\n // 对象:{ name, url?, '@type'? }\n if (typeof raw === 'object' && raw !== null && typeof raw.name === 'string' && raw.name !== '') {\n const node: SchemaNode = { '@type': raw['@type'] ?? 'Person', name: raw.name }\n if (raw.url) node.url = raw.url\n return node\n }\n warnOnce(file, `字段「${field}」存在无法识别的作者条目(应为字符串或含 name 的对象),该条目已跳过`)\n return null\n}\n\n/**\n * 规范化作者:支持单条或数组输入,统一为 schema.org 节点。\n * - 单条 → 输出单个 Person / Organization 对象\n * - 数组 → 输出节点数组(schema.org 支持 author 为数组)\n * 全部无法识别时返回 null(上层跳过 author 属性)\n */\nfunction normalizeAuthors(\n raw: AuthorInput | undefined,\n file: string,\n field: string,\n): SchemaNode | SchemaNode[] | null {\n if (raw === undefined || raw === null || raw === '') return null\n\n const list = Array.isArray(raw) ? raw : [raw]\n const nodes = list\n .map((item) => normalizeAuthorItem(item, file, field))\n .filter((item): item is SchemaNode => item !== null)\n\n if (nodes.length === 0) return null\n return nodes.length === 1 ? nodes[0] : nodes\n}\n\n/** 把「存在才写入」的属性合并进 schema,缺失字段直接跳过,不输出空值 */\nfunction assignIfDefined(node: SchemaNode, key: string, value: unknown): void {\n if (value === undefined || value === null || value === '') return\n node[key] = value\n}\n\n/** 判断是否为站点首页 */\nfunction isHomePage(page: JsonldPage, base: string): boolean {\n return page.frontmatter.home === true || page.path === base || page.path === '/'\n}\n\n/**\n * 判断页面是否存在侧边栏式目录层级(即处于某个父级目录之下),\n * 并基于父级目录的索引页生成 BreadcrumbList。\n *\n * 约定:每个目录层级通过其 index 页面(如 /guide/ → /guide/index.html)命名,\n * 这与 VuePress 主题默认侧边栏的目录结构一致。\n */\nfunction buildBreadcrumb(\n page: JsonldPage,\n app: App,\n pages: Page[],\n base: string,\n hostname: string,\n): SchemaNode | null {\n // 路径异常(空字符串等)直接跳过\n if (typeof page.path !== 'string' || page.path === '') return null\n // 取页面路径的目录部分,如 /guide/setup.html → /guide/\n // 注意:不同 VuePress 版本中 page.path 可能带或不带 base 前缀,这里做兼容\n const dirPath = page.path.slice(0, page.path.lastIndexOf('/') + 1)\n const relativeDir = dirPath.startsWith(base) ? dirPath.slice(base.length) : dirPath\n const segments = relativeDir.split('/').filter(Boolean)\n // 无层级(根下直接一层且无父目录页)不生成\n if (segments.length === 0) return null\n\n const siteName = app.options.title || page.title\n if (!siteName) return null\n\n const items: SchemaNode[] = [\n {\n '@type': 'ListItem',\n position: 1,\n name: siteName,\n item: toAbsoluteUrl(base, base, hostname),\n },\n ]\n\n // 逐级匹配父级目录页,匹配到的才加入面包屑\n // 兼容三种目录索引页路径形态:/guide/、/blog/guide/(带 base)、/guide/index.html\n let acc = ''\n for (const segment of segments) {\n acc += `${segment}/`\n const parentPath = `/${acc}`\n const parent = pages.find(\n (p) =>\n p.path === parentPath ||\n p.path === `${base}${acc}` ||\n p.path === `${parentPath}index.html` ||\n p.path === `${base}${acc}index.html`,\n )\n if (!parent) continue\n items.push({\n '@type': 'ListItem',\n position: items.length + 1,\n name: parent.title || segment,\n item: toAbsoluteUrl(parent.path, base, hostname),\n })\n }\n\n // 当前页面本身\n items.push({\n '@type': 'ListItem',\n position: items.length + 1,\n name: page.title || page.path,\n item: toAbsoluteUrl(page.path, base, hostname),\n })\n\n return {\n '@context': 'https://schema.org',\n '@type': 'BreadcrumbList',\n itemListElement: items,\n }\n}\n\n/** 基于 frontmatter.faq 生成 FAQPage schema */\nfunction buildFaqPage(page: JsonldPage, file: string): SchemaNode | null {\n const faq = page.frontmatter.faq\n if (!Array.isArray(faq) || faq.length === 0) return null\n\n const mainEntity = faq\n .map((entry): SchemaNode | null => {\n // 兼容 question/q 与 answer/a 两种字段命名\n const question = entry.question ?? entry.q\n const answer = entry.answer ?? entry.a\n if (!question || !answer) {\n warnOnce(\n file,\n `frontmatter.faq 中存在缺少 question 或 answer 的条目(JSON.stringify: ${JSON.stringify(entry)}),已跳过该条目`,\n )\n return null\n }\n return {\n '@type': 'Question',\n name: question,\n acceptedAnswer: { '@type': 'Answer', text: answer },\n }\n })\n .filter((item): item is SchemaNode => item !== null)\n\n if (mainEntity.length === 0) return null\n\n return {\n '@context': 'https://schema.org',\n '@type': 'FAQPage',\n mainEntity,\n }\n}\n\n/** 生成首页的 WebSite + Organization schema */\nfunction buildHomepageSchemas(\n page: JsonldPage,\n app: App,\n base: string,\n hostname: string,\n): SchemaNode[] {\n const file = page.filePath ?? page.path\n const siteName = page.title || app.options.title\n const siteDescription = page.frontmatter.description || app.options.description\n\n // WebSite 必填字段:name\n if (!siteName) {\n warnOnce(file, '首页缺少站点名称(frontmatter.title 或站点 config.title),无法生成 WebSite schema')\n }\n\n const website: SchemaNode = {\n '@context': 'https://schema.org',\n '@type': 'WebSite',\n name: siteName || 'Untitled Site',\n url: toAbsoluteUrl(base, base, hostname),\n }\n assignIfDefined(website, 'description', siteDescription)\n\n // Organization:有站点名才生成,避免输出空壳\n const schemas: SchemaNode[] = [website]\n if (siteName) {\n const organization: SchemaNode = {\n '@context': 'https://schema.org',\n '@type': 'Organization',\n name: siteName,\n url: toAbsoluteUrl(base, base, hostname),\n }\n // 首页若声明了 logo(frontmatter.logo),输出为 Organization.logo\n const logo = page.frontmatter.logo\n if (typeof logo === 'string') {\n assignIfDefined(\n organization,\n 'logo',\n resolveImage(logo, base, hostname, file),\n )\n }\n schemas.push(organization)\n }\n\n return schemas\n}\n\n/** 生成文章页的 TechArticle(或 options.type 覆盖的类型)schema */\nfunction buildArticleSchema(\n page: JsonldPage,\n file: string,\n base: string,\n hostname: string,\n options: JsonldPluginOptions,\n): SchemaNode | null {\n const fm = page.frontmatter\n\n // 必填字段校验:headline(页面标题)\n if (!page.title) {\n warnOnce(file, '缺少必填字段 title(对应 schema.org 的 headline),已跳过该页面的文章 schema')\n return null\n }\n\n const schemaType = options.type || 'TechArticle'\n const schema: SchemaNode = {\n '@context': 'https://schema.org',\n '@type': schemaType,\n headline: page.title,\n // 页面唯一标识,利于搜索引擎去重\n mainEntityOfPage: {\n '@type': 'WebPage',\n '@id': toAbsoluteUrl(page.path, base, hostname),\n },\n }\n\n // 存在才写入的属性:缺失字段直接跳过\n assignIfDefined(schema, 'description', fm.description)\n assignIfDefined(\n schema,\n 'datePublished',\n // frontmatter.date 优先;缺失时兜底使用 VuePress 从 git 提取的页面创建日期\n // (无 git 环境时 page.date 为 '0000-00-00' 占位值,需过滤掉)\n toIsoDate(fm.date, file, 'date') ??\n toIsoDate(\n typeof page.date === 'string' && page.date !== '' && page.date !== '0000-00-00'\n ? page.date\n : null,\n file,\n 'page.date',\n ),\n )\n assignIfDefined(\n schema,\n 'dateModified',\n toIsoDate(fm.dateModified, file, 'dateModified') ??\n toIsoDate(fm.date, file, 'date'),\n )\n assignIfDefined(\n schema,\n 'author',\n normalizeAuthors(fm.author, file, 'author') ??\n normalizeAuthors(options.author, file, 'options.author'),\n )\n assignIfDefined(\n schema,\n 'image',\n resolveImage(fm.image ?? options.image, base, hostname, file),\n )\n // tags → keywords(逗号分隔,符合 schema.org Text 规范)\n const keywords = resolveKeywords(fm.tags)\n if (keywords) schema.keywords = keywords\n\n return schema\n}\n\n/** tags → keywords:支持字符串数组或逗号分隔字符串;无效输入返回 null */\nfunction resolveKeywords(raw: JsonldFrontmatter['tags']): string | null {\n if (raw === undefined || raw === null || raw === '') return null\n const list = Array.isArray(raw)\n ? raw\n : typeof raw === 'string'\n ? raw.split(',')\n : []\n const cleaned = list\n .map((item) => (typeof item === 'string' ? item.trim() : ''))\n .filter((item) => item !== '')\n return cleaned.length > 0 ? cleaned.join(', ') : null\n}\n\n/**\n * 向页面 head 配置注入 ld+json 标签。\n * 相同内容自动去重,避免热更新 / 钩子多次触发导致的重复注入;\n * 用户自己在 frontmatter.head 中声明的 ld+json 不受影响。\n */\nfunction pushJsonldHead(page: JsonldPage, schema: SchemaNode): void {\n page.frontmatter ??= {}\n const head = (page.frontmatter.head ??= [])\n const payload = JSON.stringify(schema)\n const duplicated = head.some(\n (tag) =>\n tag[0] === 'script' &&\n (tag[1] as { type?: string } | undefined)?.type === 'application/ld+json' &&\n tag[2] === payload,\n )\n if (!duplicated) {\n head.push(['script', { type: 'application/ld+json' }, payload])\n }\n}\n\n/** 插件工厂:返回 VuePress 2 插件对象 */\nexport function jsonldPlugin(options: JsonldPluginOptions = {}): Plugin {\n // 配置项预校验:在插件注册阶段尽早发现配置错误\n if (options.hostname !== undefined && !isHttpsUrl(trimSlash(options.hostname))) {\n console.warn(\n `[${PLUGIN_NAME}] 警告:配置项 hostname 应为 https:// 开头的不带路径域名(如 'https://example.com'),当前值为 \"${options.hostname}\",相对路径图片将无法正确补全`,\n )\n }\n if (options.type !== undefined && (typeof options.type !== 'string' || options.type === '')) {\n console.warn(\n `[${PLUGIN_NAME}] 警告:配置项 type 应为非空字符串的 schema.org 类型名(如 'Article'),当前值已忽略,将使用默认的 TechArticle`,\n )\n }\n\n return {\n name: PLUGIN_NAME,\n\n /**\n * extendsPage 在每个页面编译时逐页调用。\n * 在这里往 page.frontmatter.head 追加 <script type=\"application/ld+json\">,\n * VuePress SSR 会把它渲染进首屏 HTML,无需任何客户端脚本。\n *\n * 注意:此阶段 app.pages 尚未收集完成,所以依赖全站页面的\n * BreadcrumbList 延后到 onPrepared 钩子注入。\n */\n extendsPage(page, app) {\n // frontmatter 极端情况下可能为空(虚拟页面等),兜底为空对象\n page.frontmatter ??= {}\n const file = page.filePath ?? page.path\n\n // 允许页面通过 frontmatter.jsonld: false 完全关闭注入\n if (page.frontmatter.jsonld === false) return\n\n const base = app.options.base ?? '/'\n const hostname = options.hostname ? trimSlash(options.hostname) : ''\n\n const schemas: SchemaNode[] = []\n\n // 1) 首页 → WebSite + Organization;2) 其他页面 → 文章 schema\n if (isHomePage(page, base)) {\n schemas.push(...buildHomepageSchemas(page, app, base, hostname))\n } else {\n const article = buildArticleSchema(page, file, base, hostname, options)\n if (article) schemas.push(article)\n }\n\n // 2) 识别到 FAQ 结构 → 额外生成 FAQPage\n const faqPage = buildFaqPage(page, file)\n if (faqPage) schemas.push(faqPage)\n\n // 3) 追加 additionalSchemas(静态数组或动态函数)\n if (typeof options.additionalSchemas === 'function') {\n schemas.push(...options.additionalSchemas(page, app))\n } else if (Array.isArray(options.additionalSchemas)) {\n schemas.push(...options.additionalSchemas)\n }\n\n // 注入 head:通过 frontmatter.head(VuePress 的 head 配置机制)追加\n // <script type=\"application/ld+json\">{...}</script>,\n // SSR 渲染时会静态写入首屏 HTML,不依赖客户端动态插入\n for (const schema of schemas) {\n pushJsonldHead(page, schema)\n }\n },\n\n /**\n * onPrepared 在全部页面收集完成后调用一次。\n * 此时 app.pages 可用,可以为存在目录层级的页面生成 BreadcrumbList。\n */\n onPrepared(app) {\n const base = app.options.base ?? '/'\n const hostname = options.hostname ? trimSlash(options.hostname) : ''\n const pages = app.pages ?? []\n\n for (const page of pages) {\n // 尊重页面级开关(可选链防御 frontmatter 缺失)\n if (page.frontmatter?.jsonld === false) continue\n\n const breadcrumb = buildBreadcrumb(page, app, pages, base, hostname)\n if (!breadcrumb) continue\n\n pushJsonldHead(page, breadcrumb)\n }\n },\n }\n}\n\nexport default jsonldPlugin\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAyDA,IAAM,cAAc;AAGpB,IAAM,SAAS,oBAAI,IAAY;AAG/B,SAAS,SAAS,MAAc,SAAuB;AACrD,QAAM,MAAM,GAAG,IAAI,IAAI,OAAO;AAC9B,MAAI,OAAO,IAAI,GAAG,EAAG;AACrB,SAAO,IAAI,GAAG;AACd,UAAQ,KAAK,IAAI,WAAW,uBAAQ,OAAO;AAAA,GAAM,WAAW,uBAAQ,IAAI,EAAE;AAC5E;AAGA,SAAS,WAAW,OAAwB;AAC1C,SAAO,sBAAsB,KAAK,KAAK;AACzC;AAGA,SAAS,UAAU,OAAuB;AACxC,SAAO,MAAM,QAAQ,QAAQ,EAAE;AACjC;AAMA,SAAS,UAAU,KAAc,MAAc,OAA8B;AAC3E,MAAI,QAAQ,UAAa,QAAQ,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S,SAAQ,KAAK,OAAO;AAAA,MACnC;AAGA,YAAM,UAAU,aAAa,MAAM,IAAI;AACvC,UAAI,QAAS,SAAQ,KAAK,OAAO;AAGjC,UAAI,OAAO,QAAQ,sBAAsB,YAAY;AACnD,gBAAQ,KAAK,GAAG,QAAQ,kBAAkB,MAAM,GAAG,CAAC;AAAA,MACtD,WAAW,MAAM,QAAQ,QAAQ,iBAAiB,GAAG;AACnD,gBAAQ,KAAK,GAAG,QAAQ,iBAAiB;AAAA,MAC3C;AAKA,iBAAW,UAAU,SAAS;AAC5B,uBAAe,MAAM,MAAM;AAAA,MAC7B;AAAA,IACF;AAAA;AAAA;AAAA;AAAA;AAAA,IAMA,WAAW,KAAK;AACd,YAAM,OAAO,IAAI,QAAQ,QAAQ;AACjC,YAAM,WAAW,QAAQ,WAAW,UAAU,QAAQ,QAAQ,IAAI;AAClE,YAAM,QAAQ,IAAI,SAAS,CAAC;AAE5B,iBAAW,QAAQ,OAAO;AAExB,YAAI,KAAK,aAAa,WAAW,MAAO;AAExC,cAAM,aAAa,gBAAgB,MAAM,KAAK,OAAO,MAAM,QAAQ;AACnE,YAAI,CAAC,WAAY;AAEjB,uBAAe,MAAM,UAAU;AAAA,MACjC;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAO,gBAAQ;","names":[]}
@@ -0,0 +1,134 @@
1
+ import { Page, App, Plugin } from 'vuepress/core';
2
+
3
+ /**
4
+ * Copyright 2026 科技酱 (TechSauce)
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ /**
19
+ * vuepress-plugin-jsonld 类型声明
20
+ *
21
+ * 插件配置项与内部使用的 schema.org 节点类型定义。
22
+ */
23
+
24
+ /** 单个作者条目:字符串或 schema.org Person / Organization 对象 */
25
+ type AuthorItem = string | {
26
+ '@type'?: 'Person' | 'Organization';
27
+ name: string;
28
+ url?: string;
29
+ };
30
+ /** 作者输入:兼容字符串 / 对象 / 数组(多作者),统一解析为 schema.org 节点 */
31
+ type AuthorInput = AuthorItem | AuthorItem[];
32
+ /** schema.org JSON-LD 节点(宽松结构,允许任意 schema.org 属性) */
33
+ type SchemaNode = Record<string, unknown>;
34
+ /** 页面 frontmatter 中可识别的 JSON-LD 相关字段 */
35
+ interface JsonldFrontmatter {
36
+ title?: string;
37
+ description?: string;
38
+ /** 发布日期,接受 Date / ISO 字符串 / 时间戳 */
39
+ date?: unknown;
40
+ /** 修改日期,接受 Date / ISO 字符串 / 时间戳 */
41
+ dateModified?: unknown;
42
+ /** 作者,字符串、对象或数组(多作者) */
43
+ author?: AuthorInput;
44
+ /** 封面图,站点内相对路径(以 / 开头)或 https 绝对 URL */
45
+ image?: string;
46
+ /** 标签列表,输出为 keywords;也接受逗号分隔字符串 */
47
+ tags?: string[] | string;
48
+ /**
49
+ * FAQ 结构:存在时额外生成 FAQPage schema
50
+ * question / q 与 answer / a 均可识别
51
+ */
52
+ faq?: {
53
+ question?: string;
54
+ q?: string;
55
+ answer?: string;
56
+ a?: string;
57
+ }[];
58
+ /** 设为 false 可完全关闭该页面的 JSON-LD 注入 */
59
+ jsonld?: boolean;
60
+ [key: string]: unknown;
61
+ }
62
+ /** 插件配置项 */
63
+ interface JsonldPluginOptions {
64
+ /**
65
+ * 手动覆盖普通文章页的 schema 类型(默认 'TechArticle')。
66
+ * 例如 'Article'、'BlogPosting'、'HowTo' 等 schema.org 类型名。
67
+ */
68
+ type?: string;
69
+ /**
70
+ * 默认作者:页面 frontmatter.author 缺失时使用。
71
+ * 字符串或 schema.org Person / Organization 对象。
72
+ */
73
+ author?: AuthorInput;
74
+ /**
75
+ * 默认图片:页面 frontmatter.image 缺失时使用。
76
+ * 相对路径会基于站点 base 与 hostname 自动补全为 HTTPS 绝对 URL。
77
+ */
78
+ image?: string;
79
+ /**
80
+ * 站点域名(不带末尾斜杠),如 'https://example.com'。
81
+ * 用于把相对路径的图片 / 面包屑 URL 补全为绝对 URL;
82
+ * 未配置时,frontmatter 中的相对路径图片会触发中文警告。
83
+ */
84
+ hostname?: string;
85
+ /**
86
+ * 额外的 JSON-LD schema,会原样追加到每个页面的 head 中。
87
+ * 支持静态数组,或根据页面动态返回数组的函数。
88
+ */
89
+ additionalSchemas?: SchemaNode[] | ((page: Page, app: App) => SchemaNode[]);
90
+ }
91
+
92
+ /**
93
+ * Copyright 2026 科技酱 (TechSauce)
94
+ *
95
+ * Licensed under the Apache License, Version 2.0 (the "License");
96
+ * you may not use this file except in compliance with the License.
97
+ * You may obtain a copy of the License at
98
+ *
99
+ * http://www.apache.org/licenses/LICENSE-2.0
100
+ *
101
+ * Unless required by applicable law or agreed to in writing, software
102
+ * distributed under the License is distributed on an "AS IS" BASIS,
103
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
104
+ * See the License for the specific language governing permissions and
105
+ * limitations under the License.
106
+ */
107
+ /**
108
+ * vuepress-plugin-jsonld
109
+ *
110
+ * VuePress 2 插件:构建时(SSR 阶段)向每个页面的 head 注入符合
111
+ * schema.org 规范的 JSON-LD 结构化数据。
112
+ *
113
+ * 作者:科技酱 (TechSauce) https://docs.asoe.cn
114
+ * 核心行为:
115
+ * 1. 通过 extendsPage 钩子读取页面 frontmatter:
116
+ * title / description / date / dateModified / author / image / tags
117
+ * 2. 自动判断 schema 类型:
118
+ * - 首页(frontmatter.home === true 或站点根路径)→ WebSite + Organization
119
+ * - 普通文章 → TechArticle(可通过 options.type 覆盖)
120
+ * - 存在父级目录页(与主题侧边栏一致的层级结构)→ 额外生成 BreadcrumbList
121
+ * - frontmatter.faq → 额外生成 FAQPage
122
+ * 3. 日期自动转为 ISO 8601;image 自动补全为基于站点 base 的 HTTPS 绝对 URL;
123
+ * 缺失字段直接跳过该属性,绝不输出空值。
124
+ * 4. 通过页面 head 配置注入 <script type="application/ld+json"> —— 这是 SSR
125
+ * 静态标签,首屏 HTML 直接携带,不依赖任何客户端动态插入;
126
+ * 相同内容自动去重,不会因钩子多次触发而重复注入。
127
+ * 5. 构建时校验:图片非绝对 HTTPS、日期非法、必填字段缺失时输出清晰的
128
+ * 中文警告,并指出具体文件路径。
129
+ */
130
+
131
+ /** 插件工厂:返回 VuePress 2 插件对象 */
132
+ declare function jsonldPlugin(options?: JsonldPluginOptions): Plugin;
133
+
134
+ export { type AuthorInput, type JsonldFrontmatter, type JsonldPluginOptions, type SchemaNode, jsonldPlugin as default, jsonldPlugin };
package/lib/index.d.ts ADDED
@@ -0,0 +1,134 @@
1
+ import { Page, App, Plugin } from 'vuepress/core';
2
+
3
+ /**
4
+ * Copyright 2026 科技酱 (TechSauce)
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ /**
19
+ * vuepress-plugin-jsonld 类型声明
20
+ *
21
+ * 插件配置项与内部使用的 schema.org 节点类型定义。
22
+ */
23
+
24
+ /** 单个作者条目:字符串或 schema.org Person / Organization 对象 */
25
+ type AuthorItem = string | {
26
+ '@type'?: 'Person' | 'Organization';
27
+ name: string;
28
+ url?: string;
29
+ };
30
+ /** 作者输入:兼容字符串 / 对象 / 数组(多作者),统一解析为 schema.org 节点 */
31
+ type AuthorInput = AuthorItem | AuthorItem[];
32
+ /** schema.org JSON-LD 节点(宽松结构,允许任意 schema.org 属性) */
33
+ type SchemaNode = Record<string, unknown>;
34
+ /** 页面 frontmatter 中可识别的 JSON-LD 相关字段 */
35
+ interface JsonldFrontmatter {
36
+ title?: string;
37
+ description?: string;
38
+ /** 发布日期,接受 Date / ISO 字符串 / 时间戳 */
39
+ date?: unknown;
40
+ /** 修改日期,接受 Date / ISO 字符串 / 时间戳 */
41
+ dateModified?: unknown;
42
+ /** 作者,字符串、对象或数组(多作者) */
43
+ author?: AuthorInput;
44
+ /** 封面图,站点内相对路径(以 / 开头)或 https 绝对 URL */
45
+ image?: string;
46
+ /** 标签列表,输出为 keywords;也接受逗号分隔字符串 */
47
+ tags?: string[] | string;
48
+ /**
49
+ * FAQ 结构:存在时额外生成 FAQPage schema
50
+ * question / q 与 answer / a 均可识别
51
+ */
52
+ faq?: {
53
+ question?: string;
54
+ q?: string;
55
+ answer?: string;
56
+ a?: string;
57
+ }[];
58
+ /** 设为 false 可完全关闭该页面的 JSON-LD 注入 */
59
+ jsonld?: boolean;
60
+ [key: string]: unknown;
61
+ }
62
+ /** 插件配置项 */
63
+ interface JsonldPluginOptions {
64
+ /**
65
+ * 手动覆盖普通文章页的 schema 类型(默认 'TechArticle')。
66
+ * 例如 'Article'、'BlogPosting'、'HowTo' 等 schema.org 类型名。
67
+ */
68
+ type?: string;
69
+ /**
70
+ * 默认作者:页面 frontmatter.author 缺失时使用。
71
+ * 字符串或 schema.org Person / Organization 对象。
72
+ */
73
+ author?: AuthorInput;
74
+ /**
75
+ * 默认图片:页面 frontmatter.image 缺失时使用。
76
+ * 相对路径会基于站点 base 与 hostname 自动补全为 HTTPS 绝对 URL。
77
+ */
78
+ image?: string;
79
+ /**
80
+ * 站点域名(不带末尾斜杠),如 'https://example.com'。
81
+ * 用于把相对路径的图片 / 面包屑 URL 补全为绝对 URL;
82
+ * 未配置时,frontmatter 中的相对路径图片会触发中文警告。
83
+ */
84
+ hostname?: string;
85
+ /**
86
+ * 额外的 JSON-LD schema,会原样追加到每个页面的 head 中。
87
+ * 支持静态数组,或根据页面动态返回数组的函数。
88
+ */
89
+ additionalSchemas?: SchemaNode[] | ((page: Page, app: App) => SchemaNode[]);
90
+ }
91
+
92
+ /**
93
+ * Copyright 2026 科技酱 (TechSauce)
94
+ *
95
+ * Licensed under the Apache License, Version 2.0 (the "License");
96
+ * you may not use this file except in compliance with the License.
97
+ * You may obtain a copy of the License at
98
+ *
99
+ * http://www.apache.org/licenses/LICENSE-2.0
100
+ *
101
+ * Unless required by applicable law or agreed to in writing, software
102
+ * distributed under the License is distributed on an "AS IS" BASIS,
103
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
104
+ * See the License for the specific language governing permissions and
105
+ * limitations under the License.
106
+ */
107
+ /**
108
+ * vuepress-plugin-jsonld
109
+ *
110
+ * VuePress 2 插件:构建时(SSR 阶段)向每个页面的 head 注入符合
111
+ * schema.org 规范的 JSON-LD 结构化数据。
112
+ *
113
+ * 作者:科技酱 (TechSauce) https://docs.asoe.cn
114
+ * 核心行为:
115
+ * 1. 通过 extendsPage 钩子读取页面 frontmatter:
116
+ * title / description / date / dateModified / author / image / tags
117
+ * 2. 自动判断 schema 类型:
118
+ * - 首页(frontmatter.home === true 或站点根路径)→ WebSite + Organization
119
+ * - 普通文章 → TechArticle(可通过 options.type 覆盖)
120
+ * - 存在父级目录页(与主题侧边栏一致的层级结构)→ 额外生成 BreadcrumbList
121
+ * - frontmatter.faq → 额外生成 FAQPage
122
+ * 3. 日期自动转为 ISO 8601;image 自动补全为基于站点 base 的 HTTPS 绝对 URL;
123
+ * 缺失字段直接跳过该属性,绝不输出空值。
124
+ * 4. 通过页面 head 配置注入 <script type="application/ld+json"> —— 这是 SSR
125
+ * 静态标签,首屏 HTML 直接携带,不依赖任何客户端动态插入;
126
+ * 相同内容自动去重,不会因钩子多次触发而重复注入。
127
+ * 5. 构建时校验:图片非绝对 HTTPS、日期非法、必填字段缺失时输出清晰的
128
+ * 中文警告,并指出具体文件路径。
129
+ */
130
+
131
+ /** 插件工厂:返回 VuePress 2 插件对象 */
132
+ declare function jsonldPlugin(options?: JsonldPluginOptions): Plugin;
133
+
134
+ export { type AuthorInput, type JsonldFrontmatter, type JsonldPluginOptions, type SchemaNode, jsonldPlugin as default, jsonldPlugin };