vitepress-allyouneed 0.1.0
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/CHANGELOG.md +61 -0
- package/LICENSE +25 -0
- package/README.md +157 -0
- package/dist/index.cjs +1489 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +95 -0
- package/dist/index.d.ts +95 -0
- package/dist/index.js +1441 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown-it.cjs +800 -0
- package/dist/markdown-it.cjs.map +1 -0
- package/dist/markdown-it.d.cts +34 -0
- package/dist/markdown-it.d.ts +34 -0
- package/dist/markdown-it.js +763 -0
- package/dist/markdown-it.js.map +1 -0
- package/dist/types-CapUIPbW.d.cts +262 -0
- package/dist/types-CapUIPbW.d.ts +262 -0
- package/dist/vite.cjs +783 -0
- package/dist/vite.cjs.map +1 -0
- package/dist/vite.d.cts +30 -0
- package/dist/vite.d.ts +30 -0
- package/dist/vite.js +748 -0
- package/dist/vite.js.map +1 -0
- package/dist/vitepress.cjs +1468 -0
- package/dist/vitepress.cjs.map +1 -0
- package/dist/vitepress.d.cts +36 -0
- package/dist/vitepress.d.ts +36 -0
- package/dist/vitepress.js +1431 -0
- package/dist/vitepress.js.map +1 -0
- package/package.json +98 -0
- package/style.css +159 -0
package/dist/vite.js
ADDED
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
// src/vite.ts
|
|
2
|
+
import fs5 from "fs";
|
|
3
|
+
import nodePath6 from "path";
|
|
4
|
+
|
|
5
|
+
// src/core/slugify.ts
|
|
6
|
+
import { slugify as mditVueSlugify } from "@mdit-vue/shared";
|
|
7
|
+
function defaultSlugify(text) {
|
|
8
|
+
return mditVueSlugify(text);
|
|
9
|
+
}
|
|
10
|
+
var CUSTOM_ID_RE = /\s*\{#([^}\s]+)\}\s*$/;
|
|
11
|
+
function extractCustomId(headingText) {
|
|
12
|
+
const m = headingText.match(CUSTOM_ID_RE);
|
|
13
|
+
if (!m) return { text: headingText, customId: void 0 };
|
|
14
|
+
return {
|
|
15
|
+
text: headingText.replace(CUSTOM_ID_RE, ""),
|
|
16
|
+
customId: m[1]
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// src/core/config-bridge.ts
|
|
21
|
+
var DEFAULT_ASSET_EXTENSIONS = [
|
|
22
|
+
// 位图
|
|
23
|
+
"bmp",
|
|
24
|
+
"gif",
|
|
25
|
+
"jpeg",
|
|
26
|
+
"jpg",
|
|
27
|
+
"png",
|
|
28
|
+
"svg",
|
|
29
|
+
"webp",
|
|
30
|
+
"avif",
|
|
31
|
+
"ico",
|
|
32
|
+
// 视频
|
|
33
|
+
"mp4",
|
|
34
|
+
"webm",
|
|
35
|
+
"mov",
|
|
36
|
+
"m4v",
|
|
37
|
+
// 音频
|
|
38
|
+
"mp3",
|
|
39
|
+
"wav",
|
|
40
|
+
"ogg",
|
|
41
|
+
"m4a",
|
|
42
|
+
"flac",
|
|
43
|
+
// 文档
|
|
44
|
+
"pdf",
|
|
45
|
+
// Obsidian 专属
|
|
46
|
+
"canvas",
|
|
47
|
+
"excalidraw"
|
|
48
|
+
];
|
|
49
|
+
var DEFAULT_IMAGE_EXTENSIONS = [
|
|
50
|
+
"bmp",
|
|
51
|
+
"gif",
|
|
52
|
+
"jpeg",
|
|
53
|
+
"jpg",
|
|
54
|
+
"png",
|
|
55
|
+
"svg",
|
|
56
|
+
"webp",
|
|
57
|
+
"avif",
|
|
58
|
+
"ico"
|
|
59
|
+
];
|
|
60
|
+
function resolveOptions(user = {}, ctx = {}) {
|
|
61
|
+
const srcDir = user.srcDir ?? ctx.srcDir ?? process.cwd();
|
|
62
|
+
let base = user.base ?? ctx.base ?? "/";
|
|
63
|
+
if (!base.startsWith("/")) base = "/" + base;
|
|
64
|
+
if (!base.endsWith("/")) base = base + "/";
|
|
65
|
+
const cleanUrls = user.cleanUrls ?? ctx.cleanUrls ?? false;
|
|
66
|
+
const slugify = user.slugify ?? ctx.externalSlugify ?? defaultSlugify;
|
|
67
|
+
const wikilinksUser = user.wikilinks ?? {};
|
|
68
|
+
const embedsUser = user.embeds ?? {};
|
|
69
|
+
const scanUser = user.scan ?? {};
|
|
70
|
+
const assetsUser = user.assets ?? {};
|
|
71
|
+
const modulesUser = user.modules ?? {};
|
|
72
|
+
const wikilinksHtmlAttrs = wikilinksUser.htmlAttributes ?? {};
|
|
73
|
+
const embedsHtmlAttrs = embedsUser.htmlAttributes ?? {};
|
|
74
|
+
return {
|
|
75
|
+
srcDir,
|
|
76
|
+
base,
|
|
77
|
+
cleanUrls,
|
|
78
|
+
caseSensitive: user.caseSensitive ?? false,
|
|
79
|
+
deadLink: user.deadLink ?? "warn",
|
|
80
|
+
onConflict: user.onConflict ?? "shortest",
|
|
81
|
+
onAliasConflict: user.onAliasConflict ?? "first",
|
|
82
|
+
scan: {
|
|
83
|
+
include: scanUser.include ?? ["**/*.md", "**/*.markdown"],
|
|
84
|
+
exclude: scanUser.exclude ?? [],
|
|
85
|
+
followSymlinks: scanUser.followSymlinks ?? false,
|
|
86
|
+
respectGitignore: scanUser.respectGitignore ?? true,
|
|
87
|
+
assetExtensions: scanUser.assetExtensions ?? DEFAULT_ASSET_EXTENSIONS
|
|
88
|
+
},
|
|
89
|
+
assets: {
|
|
90
|
+
mode: assetsUser.mode ?? "auto",
|
|
91
|
+
preserveAssetPaths: assetsUser.preserveAssetPaths ?? false,
|
|
92
|
+
outputDir: assetsUser.outputDir ?? "_assets"
|
|
93
|
+
},
|
|
94
|
+
wikilinks: {
|
|
95
|
+
postProcessLinkTarget: wikilinksUser.postProcessLinkTarget ?? ((t) => t.trim()),
|
|
96
|
+
postProcessLinkLabel: wikilinksUser.postProcessLinkLabel ?? ((l) => l.trim()),
|
|
97
|
+
allowLinkLabelFormatting: wikilinksUser.allowLinkLabelFormatting ?? false,
|
|
98
|
+
linkText: wikilinksUser.linkText ?? "basename",
|
|
99
|
+
htmlAttributes: wikilinksHtmlAttrs
|
|
100
|
+
},
|
|
101
|
+
embeds: {
|
|
102
|
+
imageFileExt: embedsUser.imageFileExt ?? DEFAULT_IMAGE_EXTENSIONS,
|
|
103
|
+
defaultAltText: embedsUser.defaultAltText ?? false,
|
|
104
|
+
postProcessImageTarget: embedsUser.postProcessImageTarget ?? ((t) => t.trim()),
|
|
105
|
+
postProcessAltText: embedsUser.postProcessAltText ?? ((a) => a.trim()),
|
|
106
|
+
uriSuffix: embedsUser.uriSuffix ?? "",
|
|
107
|
+
transclusionMaxDepth: embedsUser.transclusionMaxDepth ?? 8,
|
|
108
|
+
htmlAttributes: embedsHtmlAttrs
|
|
109
|
+
},
|
|
110
|
+
modules: {
|
|
111
|
+
wikilinks: modulesUser.wikilinks ?? true,
|
|
112
|
+
embeds: modulesUser.embeds ?? true
|
|
113
|
+
},
|
|
114
|
+
slugify
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/core/vault/index.ts
|
|
119
|
+
import fs3 from "fs";
|
|
120
|
+
import nodePath4 from "path";
|
|
121
|
+
|
|
122
|
+
// src/utils/path.ts
|
|
123
|
+
import nodePath from "path";
|
|
124
|
+
function toPosix(p) {
|
|
125
|
+
return p.replace(/\\/g, "/");
|
|
126
|
+
}
|
|
127
|
+
function relative(from, to) {
|
|
128
|
+
return toPosix(nodePath.relative(from, to));
|
|
129
|
+
}
|
|
130
|
+
function basename(p, stripExt = false) {
|
|
131
|
+
const idx = p.lastIndexOf("/");
|
|
132
|
+
const file = idx === -1 ? p : p.slice(idx + 1);
|
|
133
|
+
if (!stripExt) return file;
|
|
134
|
+
const dot = file.lastIndexOf(".");
|
|
135
|
+
return dot <= 0 ? file : file.slice(0, dot);
|
|
136
|
+
}
|
|
137
|
+
function extname(p) {
|
|
138
|
+
const file = basename(p);
|
|
139
|
+
const dot = file.lastIndexOf(".");
|
|
140
|
+
if (dot <= 0) return "";
|
|
141
|
+
return file.slice(dot + 1).toLowerCase();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// src/utils/url.ts
|
|
145
|
+
function encodePath(s) {
|
|
146
|
+
return encodeURI(s);
|
|
147
|
+
}
|
|
148
|
+
function buildUrl(base, pathSegments, anchor) {
|
|
149
|
+
let normBase = base || "/";
|
|
150
|
+
if (!normBase.startsWith("/")) normBase = "/" + normBase;
|
|
151
|
+
if (!normBase.endsWith("/")) normBase = normBase + "/";
|
|
152
|
+
const joined = pathSegments.map((s) => s.replace(/^\/+|\/+$/g, "")).filter(Boolean).join("/");
|
|
153
|
+
let url = normBase + joined;
|
|
154
|
+
url = url.replace(/\/{2,}/g, "/");
|
|
155
|
+
url = encodePath(url);
|
|
156
|
+
if (anchor) {
|
|
157
|
+
url += "#" + encodeURIComponent(anchor).replace(/%2F/g, "/");
|
|
158
|
+
}
|
|
159
|
+
return url;
|
|
160
|
+
}
|
|
161
|
+
function applyCleanUrls(path, cleanUrls) {
|
|
162
|
+
if (cleanUrls) return path;
|
|
163
|
+
if (/\.html$/i.test(path)) return path;
|
|
164
|
+
if (path.endsWith("/")) return path + "index.html";
|
|
165
|
+
return path + ".html";
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// src/core/vault/scan.ts
|
|
169
|
+
import fs from "fs";
|
|
170
|
+
import nodePath2 from "path";
|
|
171
|
+
function walk(srcDir, isIgnored, followSymlinks) {
|
|
172
|
+
const out = [];
|
|
173
|
+
const seenInodes = /* @__PURE__ */ new Set();
|
|
174
|
+
function visit(dir) {
|
|
175
|
+
let entries;
|
|
176
|
+
try {
|
|
177
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
178
|
+
} catch {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
for (const ent of entries) {
|
|
182
|
+
const full = nodePath2.join(dir, ent.name);
|
|
183
|
+
const posix = toPosix(full);
|
|
184
|
+
if (isIgnored(posix)) continue;
|
|
185
|
+
let isDir = ent.isDirectory();
|
|
186
|
+
let isFile = ent.isFile();
|
|
187
|
+
if (ent.isSymbolicLink()) {
|
|
188
|
+
if (!followSymlinks) continue;
|
|
189
|
+
try {
|
|
190
|
+
const stat = fs.statSync(full);
|
|
191
|
+
isDir = stat.isDirectory();
|
|
192
|
+
isFile = stat.isFile();
|
|
193
|
+
} catch {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (isDir) {
|
|
198
|
+
if (followSymlinks) {
|
|
199
|
+
try {
|
|
200
|
+
const stat = fs.statSync(full);
|
|
201
|
+
const key = `${stat.dev}:${stat.ino}`;
|
|
202
|
+
if (seenInodes.has(key)) continue;
|
|
203
|
+
seenInodes.add(key);
|
|
204
|
+
} catch {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
visit(full);
|
|
209
|
+
} else if (isFile) {
|
|
210
|
+
try {
|
|
211
|
+
const stat = fs.statSync(full);
|
|
212
|
+
out.push({
|
|
213
|
+
absolutePath: posix,
|
|
214
|
+
size: stat.size,
|
|
215
|
+
mtime: stat.mtimeMs,
|
|
216
|
+
extension: extname(posix)
|
|
217
|
+
});
|
|
218
|
+
} catch {
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
visit(srcDir);
|
|
224
|
+
return out;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// src/core/vault/ignore.ts
|
|
228
|
+
import fs2 from "fs";
|
|
229
|
+
import nodePath3 from "path";
|
|
230
|
+
import picomatch from "picomatch";
|
|
231
|
+
var HARD_IGNORE_DIRS = /* @__PURE__ */ new Set([
|
|
232
|
+
"node_modules",
|
|
233
|
+
".git",
|
|
234
|
+
".svn",
|
|
235
|
+
".hg",
|
|
236
|
+
".obsidian",
|
|
237
|
+
".trash",
|
|
238
|
+
".vitepress",
|
|
239
|
+
".next",
|
|
240
|
+
".nuxt",
|
|
241
|
+
".cache",
|
|
242
|
+
".idea",
|
|
243
|
+
".vscode",
|
|
244
|
+
"dist",
|
|
245
|
+
"build"
|
|
246
|
+
]);
|
|
247
|
+
function buildIgnorer(srcDir, userExclude, respectGitignore) {
|
|
248
|
+
const patterns = [...userExclude];
|
|
249
|
+
if (respectGitignore) {
|
|
250
|
+
const gitignore = nodePath3.join(srcDir, ".gitignore");
|
|
251
|
+
try {
|
|
252
|
+
const content = fs2.readFileSync(gitignore, "utf8");
|
|
253
|
+
for (const line of content.split(/\r?\n/)) {
|
|
254
|
+
const trimmed = line.trim();
|
|
255
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
256
|
+
patterns.push(trimmed.endsWith("/") ? trimmed + "**" : trimmed);
|
|
257
|
+
}
|
|
258
|
+
} catch {
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const matchers = patterns.map(
|
|
262
|
+
(p) => picomatch(p, { dot: true, nocase: false })
|
|
263
|
+
);
|
|
264
|
+
return (absPath) => {
|
|
265
|
+
const rel = toPosix(relative(srcDir, absPath));
|
|
266
|
+
if (!rel || rel.startsWith("..")) return true;
|
|
267
|
+
for (const seg of rel.split("/")) {
|
|
268
|
+
if (HARD_IGNORE_DIRS.has(seg)) return true;
|
|
269
|
+
}
|
|
270
|
+
for (const m of matchers) {
|
|
271
|
+
if (m(rel)) return true;
|
|
272
|
+
}
|
|
273
|
+
return false;
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// src/core/vault/frontmatter.ts
|
|
278
|
+
import matter from "gray-matter";
|
|
279
|
+
function parseFrontmatter(raw) {
|
|
280
|
+
try {
|
|
281
|
+
const { data, content } = matter(raw);
|
|
282
|
+
return { data: data ?? {}, content };
|
|
283
|
+
} catch (err) {
|
|
284
|
+
return {
|
|
285
|
+
data: {},
|
|
286
|
+
content: raw,
|
|
287
|
+
error: err instanceof Error ? err.message : String(err)
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function normalizeAliases(raw) {
|
|
292
|
+
if (raw == null) return [];
|
|
293
|
+
if (typeof raw === "string") return raw.trim() ? [raw.trim()] : [];
|
|
294
|
+
if (Array.isArray(raw)) {
|
|
295
|
+
return raw.filter((v) => typeof v === "string").map((s) => s.trim()).filter(Boolean);
|
|
296
|
+
}
|
|
297
|
+
return [];
|
|
298
|
+
}
|
|
299
|
+
function normalizeTags(raw) {
|
|
300
|
+
if (raw == null) return [];
|
|
301
|
+
if (typeof raw === "string") {
|
|
302
|
+
return raw.split(/[,\s]+/).map((s) => s.trim().replace(/^#/, "")).filter(Boolean);
|
|
303
|
+
}
|
|
304
|
+
if (Array.isArray(raw)) {
|
|
305
|
+
return raw.filter((v) => typeof v === "string").map((s) => s.trim().replace(/^#/, "")).filter(Boolean);
|
|
306
|
+
}
|
|
307
|
+
return [];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// src/core/vault/headings.ts
|
|
311
|
+
var HEADING_RE = /^(#{1,6})\s+(.+?)\s*$/;
|
|
312
|
+
var FENCE_RE = /^(`{3,}|~{3,})/;
|
|
313
|
+
function collectHeadings(content, slugify) {
|
|
314
|
+
const lines = content.split(/\r?\n/);
|
|
315
|
+
const out = [];
|
|
316
|
+
let inFence = false;
|
|
317
|
+
let fenceMarker = "";
|
|
318
|
+
for (let i = 0; i < lines.length; i++) {
|
|
319
|
+
const line = lines[i];
|
|
320
|
+
const fenceMatch = line.match(FENCE_RE);
|
|
321
|
+
if (fenceMatch) {
|
|
322
|
+
if (!inFence) {
|
|
323
|
+
inFence = true;
|
|
324
|
+
fenceMarker = fenceMatch[1];
|
|
325
|
+
} else if (line.startsWith(fenceMarker)) {
|
|
326
|
+
inFence = false;
|
|
327
|
+
fenceMarker = "";
|
|
328
|
+
}
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (inFence) continue;
|
|
332
|
+
const m = line.match(HEADING_RE);
|
|
333
|
+
if (!m) continue;
|
|
334
|
+
const level = m[1].length;
|
|
335
|
+
const rawText = m[2];
|
|
336
|
+
const { text, customId } = extractCustomId(rawText);
|
|
337
|
+
const slug = customId ?? slugify(text);
|
|
338
|
+
out.push({ level, text, slug, line: i });
|
|
339
|
+
}
|
|
340
|
+
return out;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// src/core/vault/index.ts
|
|
344
|
+
var MD_EXTENSIONS = /* @__PURE__ */ new Set(["md", "markdown"]);
|
|
345
|
+
function createEmptyIndex(srcDir = "", base = "/", cleanUrls = false) {
|
|
346
|
+
return {
|
|
347
|
+
files: /* @__PURE__ */ new Map(),
|
|
348
|
+
assets: /* @__PURE__ */ new Map(),
|
|
349
|
+
byBasename: /* @__PURE__ */ new Map(),
|
|
350
|
+
byBasenameLower: /* @__PURE__ */ new Map(),
|
|
351
|
+
byAlias: /* @__PURE__ */ new Map(),
|
|
352
|
+
byRelativePath: /* @__PURE__ */ new Map(),
|
|
353
|
+
byUrl: /* @__PURE__ */ new Map(),
|
|
354
|
+
assetsByBasename: /* @__PURE__ */ new Map(),
|
|
355
|
+
assetsByBasenameLower: /* @__PURE__ */ new Map(),
|
|
356
|
+
assetsByRelativePath: /* @__PURE__ */ new Map(),
|
|
357
|
+
tags: /* @__PURE__ */ new Map(),
|
|
358
|
+
backlinks: /* @__PURE__ */ new Map(),
|
|
359
|
+
headings: /* @__PURE__ */ new Map(),
|
|
360
|
+
srcDir,
|
|
361
|
+
base,
|
|
362
|
+
cleanUrls,
|
|
363
|
+
scannedAt: Date.now(),
|
|
364
|
+
warnings: []
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function scanVault(options) {
|
|
368
|
+
const srcDir = toPosix(nodePath4.resolve(options.srcDir));
|
|
369
|
+
const index = createEmptyIndex(srcDir, options.base, options.cleanUrls);
|
|
370
|
+
const isIgnored = buildIgnorer(
|
|
371
|
+
srcDir,
|
|
372
|
+
options.scan.exclude,
|
|
373
|
+
options.scan.respectGitignore
|
|
374
|
+
);
|
|
375
|
+
const assetExtSet = new Set(
|
|
376
|
+
options.scan.assetExtensions.map((e) => e.toLowerCase())
|
|
377
|
+
);
|
|
378
|
+
const entries = walk(srcDir, isIgnored, options.scan.followSymlinks);
|
|
379
|
+
for (const ent of entries) {
|
|
380
|
+
const ext = ent.extension;
|
|
381
|
+
if (MD_EXTENSIONS.has(ext)) {
|
|
382
|
+
ingestMarkdown(index, ent.absolutePath, ent.size, ent.mtime, options);
|
|
383
|
+
} else if (assetExtSet.has(ext)) {
|
|
384
|
+
ingestAsset(index, ent.absolutePath, ent.size, ent.mtime, ext);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
index.scannedAt = Date.now();
|
|
388
|
+
return index;
|
|
389
|
+
}
|
|
390
|
+
function ingestMarkdown(index, absPath, size, mtime, options) {
|
|
391
|
+
let raw;
|
|
392
|
+
try {
|
|
393
|
+
raw = fs3.readFileSync(absPath, "utf8");
|
|
394
|
+
} catch (err) {
|
|
395
|
+
index.warnings.push({
|
|
396
|
+
kind: "unreadable-file",
|
|
397
|
+
message: `\u65E0\u6CD5\u8BFB\u53D6\u6587\u4EF6: ${absPath} (${err instanceof Error ? err.message : String(err)})`,
|
|
398
|
+
affected: [absPath]
|
|
399
|
+
});
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
const { data, content, error } = parseFrontmatter(raw);
|
|
403
|
+
if (error) {
|
|
404
|
+
index.warnings.push({
|
|
405
|
+
kind: "invalid-frontmatter",
|
|
406
|
+
message: `frontmatter \u89E3\u6790\u5931\u8D25 (${absPath}): ${error}`,
|
|
407
|
+
affected: [absPath]
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
const aliases = normalizeAliases(data.aliases);
|
|
411
|
+
const tags = normalizeTags(data.tags);
|
|
412
|
+
const headings = collectHeadings(content, options.slugify);
|
|
413
|
+
const rel = relative(index.srcDir, absPath);
|
|
414
|
+
const base = basename(absPath, true);
|
|
415
|
+
const ext = extname(absPath);
|
|
416
|
+
const url = computeUrl(rel, options);
|
|
417
|
+
const entry = {
|
|
418
|
+
absolutePath: absPath,
|
|
419
|
+
relativePath: rel,
|
|
420
|
+
basename: base,
|
|
421
|
+
extension: ext,
|
|
422
|
+
url,
|
|
423
|
+
frontmatter: data,
|
|
424
|
+
aliases,
|
|
425
|
+
tags,
|
|
426
|
+
headings,
|
|
427
|
+
mtime,
|
|
428
|
+
size,
|
|
429
|
+
content
|
|
430
|
+
};
|
|
431
|
+
registerFileEntry(index, entry, options);
|
|
432
|
+
}
|
|
433
|
+
function ingestAsset(index, absPath, size, mtime, ext) {
|
|
434
|
+
const rel = relative(index.srcDir, absPath);
|
|
435
|
+
const base = basename(absPath);
|
|
436
|
+
const entry = {
|
|
437
|
+
absolutePath: absPath,
|
|
438
|
+
relativePath: rel,
|
|
439
|
+
basename: base,
|
|
440
|
+
extension: ext,
|
|
441
|
+
mtime,
|
|
442
|
+
size,
|
|
443
|
+
referencedBy: /* @__PURE__ */ new Set()
|
|
444
|
+
};
|
|
445
|
+
index.assets.set(absPath, entry);
|
|
446
|
+
index.assetsByRelativePath.set(rel, entry);
|
|
447
|
+
pushToArrayMap(index.assetsByBasename, base, entry);
|
|
448
|
+
pushToArrayMap(index.assetsByBasenameLower, base.toLowerCase(), entry);
|
|
449
|
+
}
|
|
450
|
+
function registerFileEntry(index, entry, options) {
|
|
451
|
+
index.files.set(entry.absolutePath, entry);
|
|
452
|
+
index.byRelativePath.set(entry.relativePath, entry);
|
|
453
|
+
const existingAtUrl = index.byUrl.get(entry.url);
|
|
454
|
+
if (existingAtUrl && existingAtUrl.absolutePath !== entry.absolutePath) {
|
|
455
|
+
index.warnings.push({
|
|
456
|
+
kind: "unknown",
|
|
457
|
+
message: `URL \u51B2\u7A81:\u6587\u4EF6 "${entry.relativePath}" \u548C "${existingAtUrl.relativePath}" \u90FD\u8DEF\u7531\u5230 "${entry.url}"\u3002VitePress \u4F1A\u8BA9\u5176\u4E2D\u4E00\u4E2A 404\u3002\u5EFA\u8BAE\u5728 .vitepress/config \u52A0 srcExclude: ['${entry.relativePath}'](\u6216\u53E6\u4E00\u4E2A)\u3002`,
|
|
458
|
+
affected: [existingAtUrl.absolutePath, entry.absolutePath]
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
index.byUrl.set(entry.url, entry);
|
|
462
|
+
index.headings.set(entry.absolutePath, entry.headings);
|
|
463
|
+
pushToArrayMap(index.byBasename, entry.basename, entry);
|
|
464
|
+
pushToArrayMap(index.byBasenameLower, entry.basename.toLowerCase(), entry);
|
|
465
|
+
for (const alias of entry.aliases) {
|
|
466
|
+
const key = options.caseSensitive ? alias : alias.toLowerCase();
|
|
467
|
+
if (index.byAlias.has(key)) {
|
|
468
|
+
index.warnings.push({
|
|
469
|
+
kind: "duplicate-alias",
|
|
470
|
+
message: `alias "${alias}" \u540C\u65F6\u88AB\u591A\u4E2A\u6587\u4EF6\u58F0\u660E,\u6309 onAliasConflict='${options.onAliasConflict}' \u5904\u7406`,
|
|
471
|
+
affected: [index.byAlias.get(key).absolutePath, entry.absolutePath]
|
|
472
|
+
});
|
|
473
|
+
if (options.onAliasConflict === "first") continue;
|
|
474
|
+
}
|
|
475
|
+
index.byAlias.set(key, entry);
|
|
476
|
+
}
|
|
477
|
+
for (const tag of entry.tags) {
|
|
478
|
+
pushToArrayMap(index.tags, tag, entry);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
function computeUrl(rel, options) {
|
|
482
|
+
const noExt = rel.replace(/\.(md|markdown)$/i, "");
|
|
483
|
+
const isIndex = /(^|\/)(index|README)$/i.test(noExt);
|
|
484
|
+
const pathPart = isIndex ? noExt.replace(/(^|\/)(index|README)$/i, "$1") : noExt;
|
|
485
|
+
const segments = pathPart.split("/").filter(Boolean);
|
|
486
|
+
if (segments.length === 0) {
|
|
487
|
+
return options.base;
|
|
488
|
+
}
|
|
489
|
+
const last = segments[segments.length - 1];
|
|
490
|
+
if (!isIndex) {
|
|
491
|
+
segments[segments.length - 1] = applyCleanUrls(last, options.cleanUrls);
|
|
492
|
+
} else if (!options.cleanUrls) {
|
|
493
|
+
segments.push("index.html");
|
|
494
|
+
}
|
|
495
|
+
return buildUrl(options.base, segments);
|
|
496
|
+
}
|
|
497
|
+
function pushToArrayMap(m, k, v) {
|
|
498
|
+
const arr = m.get(k);
|
|
499
|
+
if (arr) arr.push(v);
|
|
500
|
+
else m.set(k, [v]);
|
|
501
|
+
}
|
|
502
|
+
function updateFile(index, absPath, options) {
|
|
503
|
+
const posix = toPosix(absPath);
|
|
504
|
+
removeFile(index, posix, options);
|
|
505
|
+
let stat;
|
|
506
|
+
try {
|
|
507
|
+
stat = fs3.statSync(posix);
|
|
508
|
+
} catch {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const ext = extname(posix);
|
|
512
|
+
if (MD_EXTENSIONS.has(ext)) {
|
|
513
|
+
ingestMarkdown(index, posix, stat.size, stat.mtimeMs, options);
|
|
514
|
+
} else if (new Set(options.scan.assetExtensions.map((e) => e.toLowerCase())).has(ext)) {
|
|
515
|
+
ingestAsset(index, posix, stat.size, stat.mtimeMs, ext);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
function removeFile(index, absPath, options) {
|
|
519
|
+
const posix = toPosix(absPath);
|
|
520
|
+
const file = index.files.get(posix);
|
|
521
|
+
if (file) {
|
|
522
|
+
index.files.delete(posix);
|
|
523
|
+
index.byRelativePath.delete(file.relativePath);
|
|
524
|
+
index.byUrl.delete(file.url);
|
|
525
|
+
index.headings.delete(posix);
|
|
526
|
+
removeFromArrayMap(index.byBasename, file.basename, file);
|
|
527
|
+
removeFromArrayMap(
|
|
528
|
+
index.byBasenameLower,
|
|
529
|
+
file.basename.toLowerCase(),
|
|
530
|
+
file
|
|
531
|
+
);
|
|
532
|
+
for (const alias of file.aliases) {
|
|
533
|
+
const key = options.caseSensitive ? alias : alias.toLowerCase();
|
|
534
|
+
if (index.byAlias.get(key) === file) index.byAlias.delete(key);
|
|
535
|
+
}
|
|
536
|
+
for (const tag of file.tags) {
|
|
537
|
+
removeFromArrayMap(index.tags, tag, file);
|
|
538
|
+
}
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
const asset = index.assets.get(posix);
|
|
542
|
+
if (asset) {
|
|
543
|
+
index.assets.delete(posix);
|
|
544
|
+
index.assetsByRelativePath.delete(asset.relativePath);
|
|
545
|
+
removeFromArrayMap(index.assetsByBasename, asset.basename, asset);
|
|
546
|
+
removeFromArrayMap(
|
|
547
|
+
index.assetsByBasenameLower,
|
|
548
|
+
asset.basename.toLowerCase(),
|
|
549
|
+
asset
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
function removeFromArrayMap(m, k, v) {
|
|
554
|
+
const arr = m.get(k);
|
|
555
|
+
if (!arr) return;
|
|
556
|
+
const idx = arr.indexOf(v);
|
|
557
|
+
if (idx >= 0) arr.splice(idx, 1);
|
|
558
|
+
if (arr.length === 0) m.delete(k);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// src/core/asset-pipeline/dev-middleware.ts
|
|
562
|
+
import fs4 from "fs";
|
|
563
|
+
import nodePath5 from "path";
|
|
564
|
+
function createDevMiddleware(index, options) {
|
|
565
|
+
return (req, res, next) => {
|
|
566
|
+
if (!req.url) return next();
|
|
567
|
+
const cleanPath = req.url.split("?")[0].split("#")[0];
|
|
568
|
+
let inSiteUrl = cleanPath;
|
|
569
|
+
if (options.base !== "/" && cleanPath.startsWith(options.base)) {
|
|
570
|
+
inSiteUrl = "/" + cleanPath.slice(options.base.length);
|
|
571
|
+
}
|
|
572
|
+
let decoded;
|
|
573
|
+
try {
|
|
574
|
+
decoded = decodeURIComponent(inSiteUrl);
|
|
575
|
+
} catch {
|
|
576
|
+
return next();
|
|
577
|
+
}
|
|
578
|
+
if (!/\.[a-zA-Z0-9]+$/.test(decoded)) return next();
|
|
579
|
+
const relCandidate = decoded.replace(/^\/+/, "");
|
|
580
|
+
let asset = index.assetsByRelativePath.get(relCandidate);
|
|
581
|
+
if (!asset) {
|
|
582
|
+
const bn = basename(decoded);
|
|
583
|
+
const map = options.caseSensitive ? index.assetsByBasename : index.assetsByBasenameLower;
|
|
584
|
+
const key = options.caseSensitive ? bn : bn.toLowerCase();
|
|
585
|
+
const candidates = map.get(key);
|
|
586
|
+
if (candidates && candidates.length > 0) asset = candidates[0];
|
|
587
|
+
}
|
|
588
|
+
if (!asset) return next();
|
|
589
|
+
let stat;
|
|
590
|
+
try {
|
|
591
|
+
stat = fs4.statSync(asset.absolutePath);
|
|
592
|
+
} catch {
|
|
593
|
+
return next();
|
|
594
|
+
}
|
|
595
|
+
res.statusCode = 200;
|
|
596
|
+
res.setHeader("Content-Type", guessMime(asset.extension));
|
|
597
|
+
res.setHeader("Content-Length", String(stat.size));
|
|
598
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
599
|
+
fs4.createReadStream(asset.absolutePath).on("error", () => next()).pipe(res);
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
var MIME = {
|
|
603
|
+
png: "image/png",
|
|
604
|
+
jpg: "image/jpeg",
|
|
605
|
+
jpeg: "image/jpeg",
|
|
606
|
+
gif: "image/gif",
|
|
607
|
+
webp: "image/webp",
|
|
608
|
+
svg: "image/svg+xml",
|
|
609
|
+
bmp: "image/bmp",
|
|
610
|
+
avif: "image/avif",
|
|
611
|
+
ico: "image/x-icon",
|
|
612
|
+
mp4: "video/mp4",
|
|
613
|
+
webm: "video/webm",
|
|
614
|
+
mov: "video/quicktime",
|
|
615
|
+
m4v: "video/x-m4v",
|
|
616
|
+
mp3: "audio/mpeg",
|
|
617
|
+
wav: "audio/wav",
|
|
618
|
+
ogg: "audio/ogg",
|
|
619
|
+
m4a: "audio/mp4",
|
|
620
|
+
flac: "audio/flac",
|
|
621
|
+
pdf: "application/pdf",
|
|
622
|
+
canvas: "application/json",
|
|
623
|
+
excalidraw: "application/json"
|
|
624
|
+
};
|
|
625
|
+
function guessMime(ext) {
|
|
626
|
+
return MIME[ext.toLowerCase()] ?? "application/octet-stream";
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// src/core/asset-pipeline/build-emit.ts
|
|
630
|
+
var ASSET_PLACEHOLDER_PREFIX = "/__ayn_asset__/";
|
|
631
|
+
|
|
632
|
+
// src/vite.ts
|
|
633
|
+
function stripQueryAndHash(id) {
|
|
634
|
+
return id.split("?")[0].split("#")[0];
|
|
635
|
+
}
|
|
636
|
+
function viteAllYouNeed(userOptions = {}) {
|
|
637
|
+
let resolved;
|
|
638
|
+
let index;
|
|
639
|
+
let viteConfig;
|
|
640
|
+
const plugin = {
|
|
641
|
+
name: "vitepress-allyouneed",
|
|
642
|
+
/**
|
|
643
|
+
* 在 Vite 配置定型前扩 server.fs.allow,让我们的 vault srcDir 可被 Vite
|
|
644
|
+
* 服务(即便 srcDir 不在项目根下)。
|
|
645
|
+
*/
|
|
646
|
+
config(_userViteConfig, _envCtx) {
|
|
647
|
+
const srcDirOpt = userOptions.srcDir;
|
|
648
|
+
if (!srcDirOpt) return void 0;
|
|
649
|
+
const abs = toPosix(nodePath6.resolve(srcDirOpt));
|
|
650
|
+
return {
|
|
651
|
+
server: {
|
|
652
|
+
fs: {
|
|
653
|
+
allow: [abs]
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
},
|
|
658
|
+
configResolved(cfg) {
|
|
659
|
+
viteConfig = cfg;
|
|
660
|
+
resolved = resolveOptions(userOptions, {
|
|
661
|
+
srcDir: userOptions.srcDir ?? cfg.root,
|
|
662
|
+
base: userOptions.base ?? cfg.base,
|
|
663
|
+
cleanUrls: userOptions.cleanUrls
|
|
664
|
+
});
|
|
665
|
+
try {
|
|
666
|
+
index = scanVault(resolved);
|
|
667
|
+
if (index.warnings.length > 0) {
|
|
668
|
+
const top = index.warnings.slice(0, 10);
|
|
669
|
+
for (const w of top) {
|
|
670
|
+
cfg.logger.warn(`[vitepress-allyouneed] ${w.message}`);
|
|
671
|
+
}
|
|
672
|
+
if (index.warnings.length > top.length) {
|
|
673
|
+
cfg.logger.warn(
|
|
674
|
+
`[vitepress-allyouneed] (...\u8FD8\u6709 ${index.warnings.length - top.length} \u6761\u544A\u8B66)`
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
} catch (err) {
|
|
679
|
+
cfg.logger.error(
|
|
680
|
+
`[vitepress-allyouneed] vault \u626B\u63CF\u5931\u8D25: ${err instanceof Error ? err.message : String(err)}`
|
|
681
|
+
);
|
|
682
|
+
index = void 0;
|
|
683
|
+
}
|
|
684
|
+
},
|
|
685
|
+
configureServer(server) {
|
|
686
|
+
if (!index) return;
|
|
687
|
+
const mw = createDevMiddleware(index, resolved);
|
|
688
|
+
return () => {
|
|
689
|
+
server.middlewares.use(mw);
|
|
690
|
+
};
|
|
691
|
+
},
|
|
692
|
+
handleHotUpdate(ctx) {
|
|
693
|
+
if (!index) return;
|
|
694
|
+
try {
|
|
695
|
+
const stat = fs5.statSync(ctx.file);
|
|
696
|
+
if (stat.isFile()) {
|
|
697
|
+
updateFile(index, ctx.file, resolved);
|
|
698
|
+
} else if (!fs5.existsSync(ctx.file)) {
|
|
699
|
+
removeFile(index, ctx.file, resolved);
|
|
700
|
+
}
|
|
701
|
+
} catch {
|
|
702
|
+
removeFile(index, ctx.file, resolved);
|
|
703
|
+
}
|
|
704
|
+
},
|
|
705
|
+
/**
|
|
706
|
+
* 拦截占位符 URL,**返回真实绝对文件路径**(POSIX 风格)。
|
|
707
|
+
*
|
|
708
|
+
* Vite 拿到文件路径会:
|
|
709
|
+
* - dev:按文件系统服务,id 经 transform 后变成 `export default '<url>'`
|
|
710
|
+
* - build:emit asset、Rollup 自动加 hash,导出最终 URL
|
|
711
|
+
*/
|
|
712
|
+
resolveId(id) {
|
|
713
|
+
if (!index) return null;
|
|
714
|
+
const stripped = stripQueryAndHash(id);
|
|
715
|
+
const ph = ASSET_PLACEHOLDER_PREFIX;
|
|
716
|
+
const phIdx = stripped.indexOf(ph);
|
|
717
|
+
if (phIdx < 0) return null;
|
|
718
|
+
const encoded = stripped.slice(phIdx + ph.length);
|
|
719
|
+
let relPath;
|
|
720
|
+
try {
|
|
721
|
+
relPath = decodeURI(encoded);
|
|
722
|
+
} catch {
|
|
723
|
+
return null;
|
|
724
|
+
}
|
|
725
|
+
const asset = index.assetsByRelativePath.get(relPath);
|
|
726
|
+
if (!asset) return null;
|
|
727
|
+
const query = id.slice(stripped.length);
|
|
728
|
+
return asset.absolutePath + query;
|
|
729
|
+
},
|
|
730
|
+
/**
|
|
731
|
+
* 给 vitepress.ts wrapper 用。
|
|
732
|
+
*/
|
|
733
|
+
__getOptions() {
|
|
734
|
+
return resolved;
|
|
735
|
+
},
|
|
736
|
+
__getIndex() {
|
|
737
|
+
return index;
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
void viteConfig;
|
|
741
|
+
return plugin;
|
|
742
|
+
}
|
|
743
|
+
var vite_default = viteAllYouNeed;
|
|
744
|
+
export {
|
|
745
|
+
vite_default as default,
|
|
746
|
+
viteAllYouNeed
|
|
747
|
+
};
|
|
748
|
+
//# sourceMappingURL=vite.js.map
|