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
|
@@ -0,0 +1,763 @@
|
|
|
1
|
+
// src/core/slugify.ts
|
|
2
|
+
import { slugify as mditVueSlugify } from "@mdit-vue/shared";
|
|
3
|
+
function defaultSlugify(text) {
|
|
4
|
+
return mditVueSlugify(text);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// src/core/config-bridge.ts
|
|
8
|
+
var DEFAULT_ASSET_EXTENSIONS = [
|
|
9
|
+
// 位图
|
|
10
|
+
"bmp",
|
|
11
|
+
"gif",
|
|
12
|
+
"jpeg",
|
|
13
|
+
"jpg",
|
|
14
|
+
"png",
|
|
15
|
+
"svg",
|
|
16
|
+
"webp",
|
|
17
|
+
"avif",
|
|
18
|
+
"ico",
|
|
19
|
+
// 视频
|
|
20
|
+
"mp4",
|
|
21
|
+
"webm",
|
|
22
|
+
"mov",
|
|
23
|
+
"m4v",
|
|
24
|
+
// 音频
|
|
25
|
+
"mp3",
|
|
26
|
+
"wav",
|
|
27
|
+
"ogg",
|
|
28
|
+
"m4a",
|
|
29
|
+
"flac",
|
|
30
|
+
// 文档
|
|
31
|
+
"pdf",
|
|
32
|
+
// Obsidian 专属
|
|
33
|
+
"canvas",
|
|
34
|
+
"excalidraw"
|
|
35
|
+
];
|
|
36
|
+
var DEFAULT_IMAGE_EXTENSIONS = [
|
|
37
|
+
"bmp",
|
|
38
|
+
"gif",
|
|
39
|
+
"jpeg",
|
|
40
|
+
"jpg",
|
|
41
|
+
"png",
|
|
42
|
+
"svg",
|
|
43
|
+
"webp",
|
|
44
|
+
"avif",
|
|
45
|
+
"ico"
|
|
46
|
+
];
|
|
47
|
+
function resolveOptions(user = {}, ctx = {}) {
|
|
48
|
+
const srcDir = user.srcDir ?? ctx.srcDir ?? process.cwd();
|
|
49
|
+
let base = user.base ?? ctx.base ?? "/";
|
|
50
|
+
if (!base.startsWith("/")) base = "/" + base;
|
|
51
|
+
if (!base.endsWith("/")) base = base + "/";
|
|
52
|
+
const cleanUrls = user.cleanUrls ?? ctx.cleanUrls ?? false;
|
|
53
|
+
const slugify = user.slugify ?? ctx.externalSlugify ?? defaultSlugify;
|
|
54
|
+
const wikilinksUser = user.wikilinks ?? {};
|
|
55
|
+
const embedsUser = user.embeds ?? {};
|
|
56
|
+
const scanUser = user.scan ?? {};
|
|
57
|
+
const assetsUser = user.assets ?? {};
|
|
58
|
+
const modulesUser = user.modules ?? {};
|
|
59
|
+
const wikilinksHtmlAttrs = wikilinksUser.htmlAttributes ?? {};
|
|
60
|
+
const embedsHtmlAttrs = embedsUser.htmlAttributes ?? {};
|
|
61
|
+
return {
|
|
62
|
+
srcDir,
|
|
63
|
+
base,
|
|
64
|
+
cleanUrls,
|
|
65
|
+
caseSensitive: user.caseSensitive ?? false,
|
|
66
|
+
deadLink: user.deadLink ?? "warn",
|
|
67
|
+
onConflict: user.onConflict ?? "shortest",
|
|
68
|
+
onAliasConflict: user.onAliasConflict ?? "first",
|
|
69
|
+
scan: {
|
|
70
|
+
include: scanUser.include ?? ["**/*.md", "**/*.markdown"],
|
|
71
|
+
exclude: scanUser.exclude ?? [],
|
|
72
|
+
followSymlinks: scanUser.followSymlinks ?? false,
|
|
73
|
+
respectGitignore: scanUser.respectGitignore ?? true,
|
|
74
|
+
assetExtensions: scanUser.assetExtensions ?? DEFAULT_ASSET_EXTENSIONS
|
|
75
|
+
},
|
|
76
|
+
assets: {
|
|
77
|
+
mode: assetsUser.mode ?? "auto",
|
|
78
|
+
preserveAssetPaths: assetsUser.preserveAssetPaths ?? false,
|
|
79
|
+
outputDir: assetsUser.outputDir ?? "_assets"
|
|
80
|
+
},
|
|
81
|
+
wikilinks: {
|
|
82
|
+
postProcessLinkTarget: wikilinksUser.postProcessLinkTarget ?? ((t) => t.trim()),
|
|
83
|
+
postProcessLinkLabel: wikilinksUser.postProcessLinkLabel ?? ((l) => l.trim()),
|
|
84
|
+
allowLinkLabelFormatting: wikilinksUser.allowLinkLabelFormatting ?? false,
|
|
85
|
+
linkText: wikilinksUser.linkText ?? "basename",
|
|
86
|
+
htmlAttributes: wikilinksHtmlAttrs
|
|
87
|
+
},
|
|
88
|
+
embeds: {
|
|
89
|
+
imageFileExt: embedsUser.imageFileExt ?? DEFAULT_IMAGE_EXTENSIONS,
|
|
90
|
+
defaultAltText: embedsUser.defaultAltText ?? false,
|
|
91
|
+
postProcessImageTarget: embedsUser.postProcessImageTarget ?? ((t) => t.trim()),
|
|
92
|
+
postProcessAltText: embedsUser.postProcessAltText ?? ((a) => a.trim()),
|
|
93
|
+
uriSuffix: embedsUser.uriSuffix ?? "",
|
|
94
|
+
transclusionMaxDepth: embedsUser.transclusionMaxDepth ?? 8,
|
|
95
|
+
htmlAttributes: embedsHtmlAttrs
|
|
96
|
+
},
|
|
97
|
+
modules: {
|
|
98
|
+
wikilinks: modulesUser.wikilinks ?? true,
|
|
99
|
+
embeds: modulesUser.embeds ?? true
|
|
100
|
+
},
|
|
101
|
+
slugify
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/core/vault/index.ts
|
|
106
|
+
import fs3 from "fs";
|
|
107
|
+
import nodePath4 from "path";
|
|
108
|
+
|
|
109
|
+
// src/utils/path.ts
|
|
110
|
+
import nodePath from "path";
|
|
111
|
+
function toPosix(p) {
|
|
112
|
+
return p.replace(/\\/g, "/");
|
|
113
|
+
}
|
|
114
|
+
function stripMarkdownExt(target) {
|
|
115
|
+
return target.replace(/\.(md|markdown)$/i, "");
|
|
116
|
+
}
|
|
117
|
+
function basename(p, stripExt = false) {
|
|
118
|
+
const idx = p.lastIndexOf("/");
|
|
119
|
+
const file = idx === -1 ? p : p.slice(idx + 1);
|
|
120
|
+
if (!stripExt) return file;
|
|
121
|
+
const dot = file.lastIndexOf(".");
|
|
122
|
+
return dot <= 0 ? file : file.slice(0, dot);
|
|
123
|
+
}
|
|
124
|
+
function splitPath(p) {
|
|
125
|
+
return toPosix(p).split("/").filter(Boolean);
|
|
126
|
+
}
|
|
127
|
+
function pathDepth(relPath) {
|
|
128
|
+
return splitPath(relPath).length;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/core/vault/scan.ts
|
|
132
|
+
import fs from "fs";
|
|
133
|
+
import nodePath2 from "path";
|
|
134
|
+
|
|
135
|
+
// src/core/vault/ignore.ts
|
|
136
|
+
import fs2 from "fs";
|
|
137
|
+
import nodePath3 from "path";
|
|
138
|
+
import picomatch from "picomatch";
|
|
139
|
+
|
|
140
|
+
// src/core/vault/frontmatter.ts
|
|
141
|
+
import matter from "gray-matter";
|
|
142
|
+
|
|
143
|
+
// src/core/vault/index.ts
|
|
144
|
+
function sortByShortestPath(items) {
|
|
145
|
+
return [...items].sort((a, b) => {
|
|
146
|
+
const da = pathDepth(a.relativePath);
|
|
147
|
+
const db = pathDepth(b.relativePath);
|
|
148
|
+
if (da !== db) return da - db;
|
|
149
|
+
return a.relativePath.localeCompare(b.relativePath);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/core/resolver.ts
|
|
154
|
+
function resolveWikilink(rawTarget, index, options, kind = "page") {
|
|
155
|
+
let target = toPosix(rawTarget).trim();
|
|
156
|
+
const hashIdx = target.indexOf("#");
|
|
157
|
+
let headingPart = "";
|
|
158
|
+
if (hashIdx >= 0) {
|
|
159
|
+
headingPart = target.slice(hashIdx + 1).trim();
|
|
160
|
+
target = target.slice(0, hashIdx).trim();
|
|
161
|
+
}
|
|
162
|
+
target = stripMarkdownExt(target);
|
|
163
|
+
const entry = lookupEntry(target, index, options);
|
|
164
|
+
if (!entry) {
|
|
165
|
+
return {
|
|
166
|
+
url: buildDeadUrl(rawTarget, options),
|
|
167
|
+
defaultLabel: defaultLabel(target, headingPart, void 0, options),
|
|
168
|
+
isDead: true,
|
|
169
|
+
hasUnmatchedAnchor: false,
|
|
170
|
+
kind
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
let url = entry.url;
|
|
174
|
+
let hasUnmatchedAnchor = false;
|
|
175
|
+
if (headingPart) {
|
|
176
|
+
const heading = entry.headings.find(
|
|
177
|
+
(h) => h.text === headingPart || h.slug === headingPart || h.slug === options.slugify(headingPart)
|
|
178
|
+
);
|
|
179
|
+
if (heading) {
|
|
180
|
+
url = entry.url + "#" + heading.slug;
|
|
181
|
+
} else {
|
|
182
|
+
hasUnmatchedAnchor = true;
|
|
183
|
+
url = entry.url + "#" + encodeURIComponent(headingPart);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
url,
|
|
188
|
+
defaultLabel: defaultLabel(target, headingPart, entry, options),
|
|
189
|
+
isDead: false,
|
|
190
|
+
hasUnmatchedAnchor,
|
|
191
|
+
target: entry,
|
|
192
|
+
kind
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function lookupEntry(target, index, options) {
|
|
196
|
+
if (!target) return void 0;
|
|
197
|
+
if (target.includes("/")) {
|
|
198
|
+
const variants = [
|
|
199
|
+
target,
|
|
200
|
+
target + ".md",
|
|
201
|
+
target + ".markdown",
|
|
202
|
+
target + "/index.md",
|
|
203
|
+
target + "/index.markdown"
|
|
204
|
+
];
|
|
205
|
+
for (const v of variants) {
|
|
206
|
+
const e = index.byRelativePath.get(v);
|
|
207
|
+
if (e) return e;
|
|
208
|
+
}
|
|
209
|
+
return void 0;
|
|
210
|
+
}
|
|
211
|
+
const aliasKey = options.caseSensitive ? target : target.toLowerCase();
|
|
212
|
+
const aliased = index.byAlias.get(aliasKey);
|
|
213
|
+
if (aliased) return aliased;
|
|
214
|
+
const bnMap = options.caseSensitive ? index.byBasename : index.byBasenameLower;
|
|
215
|
+
const bnKey = options.caseSensitive ? target : target.toLowerCase();
|
|
216
|
+
const candidates = bnMap.get(bnKey);
|
|
217
|
+
if (!candidates || candidates.length === 0) return void 0;
|
|
218
|
+
if (candidates.length === 1) return candidates[0];
|
|
219
|
+
switch (options.onConflict) {
|
|
220
|
+
case "shortest": {
|
|
221
|
+
const sorted = sortByShortestPath(candidates);
|
|
222
|
+
return sorted[0];
|
|
223
|
+
}
|
|
224
|
+
case "first":
|
|
225
|
+
return candidates[0];
|
|
226
|
+
case "error":
|
|
227
|
+
return void 0;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function buildDeadUrl(rawTarget, options) {
|
|
231
|
+
const safe = encodeURIComponent(stripMarkdownExt(rawTarget).split("#")[0]);
|
|
232
|
+
return options.base + safe;
|
|
233
|
+
}
|
|
234
|
+
function defaultLabel(target, headingPart, entry, options) {
|
|
235
|
+
const lt = options.wikilinks.linkText;
|
|
236
|
+
let base;
|
|
237
|
+
if (typeof lt === "function") {
|
|
238
|
+
if (entry) {
|
|
239
|
+
base = lt(entry, target);
|
|
240
|
+
} else {
|
|
241
|
+
base = basename(target);
|
|
242
|
+
}
|
|
243
|
+
} else if (lt === "fullPath") {
|
|
244
|
+
base = entry ? entry.relativePath.replace(/\.(md|markdown)$/i, "") : target;
|
|
245
|
+
} else {
|
|
246
|
+
base = entry ? entry.basename : basename(target);
|
|
247
|
+
}
|
|
248
|
+
if (headingPart) {
|
|
249
|
+
return `${base} > ${headingPart}`;
|
|
250
|
+
}
|
|
251
|
+
return base;
|
|
252
|
+
}
|
|
253
|
+
function resolveAsset(rawTarget, index, options) {
|
|
254
|
+
const target = toPosix(rawTarget).trim();
|
|
255
|
+
if (target.includes("/")) {
|
|
256
|
+
return {
|
|
257
|
+
asset: index.assetsByRelativePath.get(target),
|
|
258
|
+
rawBasename: basename(target)
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
const bn = options.caseSensitive ? target : target.toLowerCase();
|
|
262
|
+
const map = options.caseSensitive ? index.assetsByBasename : index.assetsByBasenameLower;
|
|
263
|
+
const candidates = map.get(bn);
|
|
264
|
+
if (!candidates || candidates.length === 0) {
|
|
265
|
+
return { asset: void 0, rawBasename: target };
|
|
266
|
+
}
|
|
267
|
+
if (candidates.length === 1) {
|
|
268
|
+
return { asset: candidates[0], rawBasename: target };
|
|
269
|
+
}
|
|
270
|
+
switch (options.onConflict) {
|
|
271
|
+
case "shortest": {
|
|
272
|
+
const sorted = sortByShortestPath(candidates);
|
|
273
|
+
return { asset: sorted[0], rawBasename: target };
|
|
274
|
+
}
|
|
275
|
+
case "first":
|
|
276
|
+
return { asset: candidates[0], rawBasename: target };
|
|
277
|
+
case "error":
|
|
278
|
+
return { asset: void 0, rawBasename: target };
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// src/modules/wikilinks/render.ts
|
|
283
|
+
function renderPageLink(state, result, label, env) {
|
|
284
|
+
const open = state.push("link_open", "a", 1);
|
|
285
|
+
const classes = ["wikilink"];
|
|
286
|
+
if (result.hasUnmatchedAnchor) classes.push("wikilink--unmatched-anchor");
|
|
287
|
+
const baseAttrs = {
|
|
288
|
+
href: result.url,
|
|
289
|
+
class: classes.join(" "),
|
|
290
|
+
"data-wikilink-target": result.target ? result.target.relativePath : ""
|
|
291
|
+
};
|
|
292
|
+
const extra = resolveExtraAttrs(env.options.wikilinks.htmlAttributes, {
|
|
293
|
+
originalHref: result.url,
|
|
294
|
+
label,
|
|
295
|
+
target: result.target,
|
|
296
|
+
isDead: result.isDead,
|
|
297
|
+
hasUnmatchedAnchor: result.hasUnmatchedAnchor
|
|
298
|
+
});
|
|
299
|
+
applyAttrs(open, baseAttrs, extra);
|
|
300
|
+
emitLabel(state, label, env);
|
|
301
|
+
state.push("link_close", "a", -1);
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
function renderDeadLink(state, url, label, rawTarget, env) {
|
|
305
|
+
const open = state.push("link_open", "a", 1);
|
|
306
|
+
const baseAttrs = {
|
|
307
|
+
href: url,
|
|
308
|
+
class: "wikilink wikilink--dead",
|
|
309
|
+
"data-wikilink-target": rawTarget,
|
|
310
|
+
title: `\u6B7B\u94FE:\u627E\u4E0D\u5230 [[${rawTarget}]]`
|
|
311
|
+
};
|
|
312
|
+
const extra = resolveExtraAttrs(env.options.wikilinks.htmlAttributes, {
|
|
313
|
+
originalHref: url,
|
|
314
|
+
label,
|
|
315
|
+
target: void 0,
|
|
316
|
+
isDead: true,
|
|
317
|
+
hasUnmatchedAnchor: false
|
|
318
|
+
});
|
|
319
|
+
applyAttrs(open, baseAttrs, extra);
|
|
320
|
+
emitLabel(state, label, env);
|
|
321
|
+
state.push("link_close", "a", -1);
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
function emitLabel(state, label, env) {
|
|
325
|
+
if (env.options.wikilinks.allowLinkLabelFormatting) {
|
|
326
|
+
const depth = env._labelDepth ?? 0;
|
|
327
|
+
if (depth < 3) {
|
|
328
|
+
;
|
|
329
|
+
env._labelDepth = depth + 1;
|
|
330
|
+
const md = state.md;
|
|
331
|
+
const html = md.renderInline(label, env);
|
|
332
|
+
const token = state.push("html_inline", "", 0);
|
|
333
|
+
token.content = html;
|
|
334
|
+
env._labelDepth = depth;
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const t = state.push("text", "", 0);
|
|
339
|
+
t.content = label;
|
|
340
|
+
}
|
|
341
|
+
function resolveExtraAttrs(htmlAttrs, ctx) {
|
|
342
|
+
if (typeof htmlAttrs === "function") return htmlAttrs(ctx);
|
|
343
|
+
return htmlAttrs ?? {};
|
|
344
|
+
}
|
|
345
|
+
function applyAttrs(token, base, extra) {
|
|
346
|
+
const merged = { ...base };
|
|
347
|
+
for (const [k, v] of Object.entries(extra)) {
|
|
348
|
+
if (k === "class" && merged.class) {
|
|
349
|
+
merged.class = merged.class + " " + v;
|
|
350
|
+
} else {
|
|
351
|
+
merged[k] = v;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
for (const [k, v] of Object.entries(merged)) {
|
|
355
|
+
token.attrSet(k, v);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// src/utils/escape.ts
|
|
360
|
+
var HTML_ESCAPE_MAP = {
|
|
361
|
+
"&": "&",
|
|
362
|
+
"<": "<",
|
|
363
|
+
">": ">",
|
|
364
|
+
'"': """,
|
|
365
|
+
"'": "'"
|
|
366
|
+
};
|
|
367
|
+
var HTML_ESCAPE_RE = /[&<>"']/g;
|
|
368
|
+
function escapeHtml(s) {
|
|
369
|
+
return s.replace(HTML_ESCAPE_RE, (c) => HTML_ESCAPE_MAP[c]);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// src/core/asset-pipeline/build-emit.ts
|
|
373
|
+
var ASSET_PLACEHOLDER_PREFIX = "/__ayn_asset__/";
|
|
374
|
+
function buildPlaceholderUrl(asset, options) {
|
|
375
|
+
const id = encodeURI(asset.relativePath);
|
|
376
|
+
return options.base + ASSET_PLACEHOLDER_PREFIX.slice(1) + id;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// src/modules/embeds/image.ts
|
|
380
|
+
function renderImageHtml(rawTarget, aliasParts, env) {
|
|
381
|
+
const { index, options } = env;
|
|
382
|
+
const { altText, dim } = parseAltAndDim(aliasParts);
|
|
383
|
+
const processedTarget = options.embeds.postProcessImageTarget(rawTarget);
|
|
384
|
+
const { asset } = resolveAsset(processedTarget, index, options);
|
|
385
|
+
let src;
|
|
386
|
+
if (asset) {
|
|
387
|
+
asset.referencedBy.add(env.currentPath ?? "<unknown>");
|
|
388
|
+
env.referencedAssets?.add(asset);
|
|
389
|
+
src = buildPlaceholderUrl(asset, options) + options.embeds.uriSuffix;
|
|
390
|
+
} else {
|
|
391
|
+
src = options.base + encodeURIComponent(basename(processedTarget)) + options.embeds.uriSuffix;
|
|
392
|
+
}
|
|
393
|
+
const finalAlt = determineAlt(altText, processedTarget, options);
|
|
394
|
+
const attrs = {
|
|
395
|
+
src,
|
|
396
|
+
alt: finalAlt ?? ""
|
|
397
|
+
};
|
|
398
|
+
if (dim.width !== void 0) attrs.width = String(dim.width);
|
|
399
|
+
if (dim.height !== void 0) attrs.height = String(dim.height);
|
|
400
|
+
const extra = resolveExtra(options.embeds.htmlAttributes, {
|
|
401
|
+
originalHref: src,
|
|
402
|
+
altText: finalAlt,
|
|
403
|
+
dimensions: dim.raw,
|
|
404
|
+
embedType: "image"
|
|
405
|
+
});
|
|
406
|
+
for (const [k, v] of Object.entries(extra)) {
|
|
407
|
+
if (k === "class" && attrs.class) {
|
|
408
|
+
attrs.class = attrs.class + " " + v;
|
|
409
|
+
} else {
|
|
410
|
+
attrs[k] = v;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return "<img " + Object.entries(attrs).map(([k, v]) => `${escapeAttrName(k)}="${escapeHtml(v)}"`).join(" ") + " />";
|
|
414
|
+
}
|
|
415
|
+
function handleImageEmbed(state, rawTarget, aliasParts, env) {
|
|
416
|
+
const html = renderImageHtml(rawTarget, aliasParts, env);
|
|
417
|
+
const token = state.push("html_inline", "", 0);
|
|
418
|
+
token.content = html;
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
function parseAltAndDim(parts) {
|
|
422
|
+
if (parts.length === 0) return { altText: "", dim: { raw: "" } };
|
|
423
|
+
const last = parts[parts.length - 1];
|
|
424
|
+
const parsedLast = tryParseDimension(last);
|
|
425
|
+
if (parsedLast) {
|
|
426
|
+
const alt = parts.slice(0, -1).join("|").trim();
|
|
427
|
+
return { altText: alt, dim: { ...parsedLast, raw: last } };
|
|
428
|
+
}
|
|
429
|
+
return { altText: parts.join("|").trim(), dim: { raw: "" } };
|
|
430
|
+
}
|
|
431
|
+
function tryParseDimension(s) {
|
|
432
|
+
const trimmed = s.trim().toLowerCase();
|
|
433
|
+
if (!trimmed) return void 0;
|
|
434
|
+
if (trimmed.includes("x")) {
|
|
435
|
+
const [w, h] = trimmed.split("x");
|
|
436
|
+
const wOk = w === "" || /^\d+$/.test(w);
|
|
437
|
+
const hOk = h === "" || /^\d+$/.test(h);
|
|
438
|
+
if (!wOk || !hOk) return void 0;
|
|
439
|
+
if (w === "" && h === "") return void 0;
|
|
440
|
+
return {
|
|
441
|
+
width: w === "" ? void 0 : Number(w),
|
|
442
|
+
height: h === "" ? void 0 : Number(h)
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
if (/^\d+$/.test(trimmed)) {
|
|
446
|
+
return { width: Number(trimmed) };
|
|
447
|
+
}
|
|
448
|
+
return void 0;
|
|
449
|
+
}
|
|
450
|
+
function determineAlt(rawAlt, target, options) {
|
|
451
|
+
if (rawAlt && rawAlt !== "") {
|
|
452
|
+
return options.embeds.postProcessAltText(rawAlt);
|
|
453
|
+
}
|
|
454
|
+
const def = options.embeds.defaultAltText;
|
|
455
|
+
if (def === false) return void 0;
|
|
456
|
+
if (def === true) {
|
|
457
|
+
const bn = basename(target);
|
|
458
|
+
const dot = bn.lastIndexOf(".");
|
|
459
|
+
const noExt = dot > 0 ? bn.slice(0, dot) : bn;
|
|
460
|
+
return options.embeds.postProcessAltText(noExt);
|
|
461
|
+
}
|
|
462
|
+
if (typeof def === "string") {
|
|
463
|
+
return def === "" ? "" : options.embeds.postProcessAltText(def);
|
|
464
|
+
}
|
|
465
|
+
return options.embeds.postProcessAltText(basename(target));
|
|
466
|
+
}
|
|
467
|
+
function resolveExtra(attrs, ctx) {
|
|
468
|
+
if (typeof attrs === "function") return attrs(ctx);
|
|
469
|
+
return attrs ?? {};
|
|
470
|
+
}
|
|
471
|
+
function escapeAttrName(k) {
|
|
472
|
+
return k.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// src/modules/embeds/transclusion.ts
|
|
476
|
+
function renderTransclusionHtml(md, rawTarget, aliasParts, env) {
|
|
477
|
+
const { index, options } = env;
|
|
478
|
+
const result = resolveWikilink(rawTarget, index, options, "transclusion");
|
|
479
|
+
if (result.isDead || !result.target) {
|
|
480
|
+
return `<div class="transclusion transclusion--dead" data-target="${escapeHtml(
|
|
481
|
+
rawTarget
|
|
482
|
+
)}">\u26A0\uFE0F \u627E\u4E0D\u5230\u7B14\u8BB0 <code>${escapeHtml(rawTarget)}</code></div>`;
|
|
483
|
+
}
|
|
484
|
+
const target = result.target;
|
|
485
|
+
const arr = index.backlinks.get(target.absolutePath) ?? [];
|
|
486
|
+
if (env.currentPath) {
|
|
487
|
+
arr.push({
|
|
488
|
+
fromPath: env.currentPath,
|
|
489
|
+
fromUrl: index.files.get(env.currentPath)?.url ?? "",
|
|
490
|
+
context: "",
|
|
491
|
+
isEmbed: true,
|
|
492
|
+
line: -1
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
index.backlinks.set(target.absolutePath, arr);
|
|
496
|
+
const stack = env.transclusionStack ?? [];
|
|
497
|
+
if (stack.includes(target.absolutePath)) {
|
|
498
|
+
return `<div class="transclusion transclusion--cycle" data-target="${escapeHtml(
|
|
499
|
+
rawTarget
|
|
500
|
+
)}">\u26A0\uFE0F \u5FAA\u73AF\u5F15\u7528:<code>${escapeHtml(
|
|
501
|
+
rawTarget
|
|
502
|
+
)}</code> \u5DF2\u5728 transclusion \u94FE\u4E0A</div>`;
|
|
503
|
+
}
|
|
504
|
+
const depth = env.transclusionDepth ?? 0;
|
|
505
|
+
if (depth >= options.embeds.transclusionMaxDepth) {
|
|
506
|
+
return `<div class="transclusion transclusion--too-deep" data-target="${escapeHtml(
|
|
507
|
+
rawTarget
|
|
508
|
+
)}">\u26A0\uFE0F transclusion \u5D4C\u5957\u8FC7\u6DF1(> ${options.embeds.transclusionMaxDepth})</div>`;
|
|
509
|
+
}
|
|
510
|
+
const headingPart = extractHeading(rawTarget);
|
|
511
|
+
const fragment = headingPart ? sliceByHeading(target, headingPart, options.slugify) : target.content;
|
|
512
|
+
if (fragment == null) {
|
|
513
|
+
return `<div class="transclusion transclusion--unmatched-anchor" data-target="${escapeHtml(
|
|
514
|
+
rawTarget
|
|
515
|
+
)}">\u26A0\uFE0F \u627E\u4E0D\u5230\u7AE0\u8282 <code>#${escapeHtml(
|
|
516
|
+
headingPart
|
|
517
|
+
)}</code></div>`;
|
|
518
|
+
}
|
|
519
|
+
const cacheKey = `${target.absolutePath}::${headingPart ?? ""}`;
|
|
520
|
+
const cache = getCache(env);
|
|
521
|
+
let inner;
|
|
522
|
+
const cached = cache.get(cacheKey);
|
|
523
|
+
if (cached) {
|
|
524
|
+
inner = cached.html;
|
|
525
|
+
} else {
|
|
526
|
+
const childEnv = {
|
|
527
|
+
index: env.index,
|
|
528
|
+
options: env.options,
|
|
529
|
+
currentPath: target.absolutePath,
|
|
530
|
+
transclusionStack: [...stack, target.absolutePath],
|
|
531
|
+
transclusionDepth: depth + 1,
|
|
532
|
+
// 跨递归共享:asset 引用集合(build 时要 emit 所有被引用 asset)
|
|
533
|
+
referencedAssets: env.referencedAssets
|
|
534
|
+
};
|
|
535
|
+
childEnv._transclusionCache = env._transclusionCache;
|
|
536
|
+
inner = md.render(fragment, childEnv);
|
|
537
|
+
cache.set(cacheKey, { html: inner });
|
|
538
|
+
}
|
|
539
|
+
const sourceUrl = headingPart ? `${target.url}#${escapeHtml(options.slugify(headingPart))}` : target.url;
|
|
540
|
+
const aliasData = aliasParts.length ? ` data-caption="${escapeHtml(aliasParts.join("|"))}"` : "";
|
|
541
|
+
return `<div class="transclusion" data-source="${escapeHtml(
|
|
542
|
+
target.relativePath
|
|
543
|
+
)}" data-source-url="${escapeHtml(sourceUrl)}"${aliasData}>${inner}</div>`;
|
|
544
|
+
}
|
|
545
|
+
function handleTransclusion(state, rawTarget, aliasParts, env) {
|
|
546
|
+
if (env.options.deadLink !== "silent") {
|
|
547
|
+
console.warn(
|
|
548
|
+
`vitepress-allyouneed: ![[${rawTarget}]] \u5728\u6BB5\u843D\u4E2D\u65E0\u6CD5 transclude(\u4F1A\u4EA7\u751F\u4E0D\u5408\u6CD5 HTML),\u5DF2\u964D\u7EA7\u4E3A\u94FE\u63A5\u3002\u8BF7\u5355\u72EC\u653E\u4E00\u884C\u3002`
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
const { index, options } = env;
|
|
552
|
+
const result = resolveWikilink(rawTarget, index, options, "page");
|
|
553
|
+
const url = result.url;
|
|
554
|
+
const label = aliasParts.length ? aliasParts.join("|").trim() : result.defaultLabel;
|
|
555
|
+
const html = `<a class="wikilink wikilink--inline-transclusion-degraded" href="${escapeHtml(url)}" data-wikilink-target="${escapeHtml(rawTarget)}" title="\u884C\u5185 transclusion \u5DF2\u964D\u7EA7,\u89C1\u63A7\u5236\u53F0">${escapeHtml(label)}</a>`;
|
|
556
|
+
const token = state.push("html_inline", "", 0);
|
|
557
|
+
token.content = html;
|
|
558
|
+
return true;
|
|
559
|
+
}
|
|
560
|
+
function extractHeading(raw) {
|
|
561
|
+
const hashIdx = raw.indexOf("#");
|
|
562
|
+
if (hashIdx < 0) return "";
|
|
563
|
+
return raw.slice(hashIdx + 1).trim();
|
|
564
|
+
}
|
|
565
|
+
function sliceByHeading(target, headingPart, slugify) {
|
|
566
|
+
const matched = target.headings.find(
|
|
567
|
+
(h) => h.text === headingPart || h.slug === headingPart || h.slug === slugify(headingPart)
|
|
568
|
+
);
|
|
569
|
+
if (!matched) return void 0;
|
|
570
|
+
const lines = target.content.split(/\r?\n/);
|
|
571
|
+
const startLine = matched.line + 1;
|
|
572
|
+
let endLine = lines.length;
|
|
573
|
+
for (let i = startLine; i < lines.length; i++) {
|
|
574
|
+
const l = lines[i];
|
|
575
|
+
const m = l.match(/^(#{1,6})\s+/);
|
|
576
|
+
if (m && m[1].length <= matched.level) {
|
|
577
|
+
endLine = i;
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return lines.slice(startLine, endLine).join("\n").trim();
|
|
582
|
+
}
|
|
583
|
+
function getCache(env) {
|
|
584
|
+
const e = env;
|
|
585
|
+
if (!e._transclusionCache) e._transclusionCache = /* @__PURE__ */ new Map();
|
|
586
|
+
return e._transclusionCache;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// src/modules/wikilinks/rule.ts
|
|
590
|
+
function makeWikilinkRule(scope) {
|
|
591
|
+
return function wikilinkRule(state, silent) {
|
|
592
|
+
const src = state.src;
|
|
593
|
+
const start = state.pos;
|
|
594
|
+
const max = state.posMax;
|
|
595
|
+
let isEmbed = false;
|
|
596
|
+
let inner;
|
|
597
|
+
if (src.charCodeAt(start) === 33 && src.charCodeAt(start + 1) === 91 && src.charCodeAt(start + 2) === 91) {
|
|
598
|
+
if (scope === "wikilinks-only") return false;
|
|
599
|
+
isEmbed = true;
|
|
600
|
+
} else if (src.charCodeAt(start) === 91 && src.charCodeAt(start + 1) === 91) {
|
|
601
|
+
if (scope === "embeds-only") return false;
|
|
602
|
+
} else {
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
|
+
const innerStart = start + (isEmbed ? 3 : 2);
|
|
606
|
+
const closeIdx = src.indexOf("]]", innerStart);
|
|
607
|
+
if (closeIdx < 0 || closeIdx >= max) return false;
|
|
608
|
+
inner = src.slice(innerStart, closeIdx);
|
|
609
|
+
if (inner.includes("\n")) return false;
|
|
610
|
+
if (!inner.trim()) return false;
|
|
611
|
+
if (silent) return true;
|
|
612
|
+
const env = state.env;
|
|
613
|
+
if (!env || !env.index || !env.options) {
|
|
614
|
+
return false;
|
|
615
|
+
}
|
|
616
|
+
state.pos = closeIdx + 2;
|
|
617
|
+
const parts = inner.split("|").map((p) => p.trim());
|
|
618
|
+
const rawTarget = parts[0];
|
|
619
|
+
const aliasParts = parts.slice(1);
|
|
620
|
+
if (isEmbed) {
|
|
621
|
+
const ext = extractExt(rawTarget);
|
|
622
|
+
const isImage = ext && env.options.embeds.imageFileExt.includes(ext.toLowerCase());
|
|
623
|
+
if (isImage) {
|
|
624
|
+
return handleImageEmbed(state, rawTarget, aliasParts, env);
|
|
625
|
+
}
|
|
626
|
+
return handleTransclusion(state, rawTarget, aliasParts, env);
|
|
627
|
+
}
|
|
628
|
+
return emitPageLink(state, rawTarget, aliasParts, env);
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
function extractExt(target) {
|
|
632
|
+
const cleaned = target.split("#")[0];
|
|
633
|
+
const dot = cleaned.lastIndexOf(".");
|
|
634
|
+
if (dot <= 0) return "";
|
|
635
|
+
return cleaned.slice(dot + 1).toLowerCase();
|
|
636
|
+
}
|
|
637
|
+
function emitPageLink(state, rawTarget, aliasParts, env) {
|
|
638
|
+
const { index, options } = env;
|
|
639
|
+
const userAlias = aliasParts.length > 0 ? aliasParts.join("|").trim() : "";
|
|
640
|
+
const processed = options.wikilinks.postProcessLinkTarget(rawTarget);
|
|
641
|
+
const result = resolveWikilink(processed, index, options, "page");
|
|
642
|
+
const label = userAlias ? options.wikilinks.postProcessLinkLabel(userAlias) : result.defaultLabel;
|
|
643
|
+
registerBacklink(env, result.target?.absolutePath, false);
|
|
644
|
+
if (result.isDead) {
|
|
645
|
+
handleDeadLink(env, rawTarget);
|
|
646
|
+
return renderDeadLink(state, result.url, label, rawTarget, env);
|
|
647
|
+
}
|
|
648
|
+
return renderPageLink(state, result, label, env);
|
|
649
|
+
}
|
|
650
|
+
function registerBacklink(env, targetPath, isEmbed) {
|
|
651
|
+
if (!targetPath || !env.currentPath) return;
|
|
652
|
+
const arr = env.index.backlinks.get(targetPath) ?? [];
|
|
653
|
+
arr.push({
|
|
654
|
+
fromPath: env.currentPath,
|
|
655
|
+
fromUrl: env.index.files.get(env.currentPath)?.url ?? "",
|
|
656
|
+
context: "",
|
|
657
|
+
isEmbed,
|
|
658
|
+
line: -1
|
|
659
|
+
});
|
|
660
|
+
env.index.backlinks.set(targetPath, arr);
|
|
661
|
+
}
|
|
662
|
+
function handleDeadLink(env, rawTarget) {
|
|
663
|
+
const { options } = env;
|
|
664
|
+
const msg = `vitepress-allyouneed: \u6B7B\u94FE [[${rawTarget}]]${env.currentPath ? ` (in ${env.currentPath})` : ""}`;
|
|
665
|
+
if (options.deadLink === "silent") return;
|
|
666
|
+
if (options.deadLink === "warn") {
|
|
667
|
+
console.warn(msg);
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
env.index.warnings.push({
|
|
671
|
+
kind: "unknown",
|
|
672
|
+
message: msg,
|
|
673
|
+
affected: env.currentPath ? [env.currentPath] : []
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// src/modules/wikilinks/index.ts
|
|
678
|
+
function registerWikilinks(md, scope) {
|
|
679
|
+
md.inline.ruler.before(
|
|
680
|
+
"link",
|
|
681
|
+
"allyouneed_wikilinks",
|
|
682
|
+
makeWikilinkRule(scope)
|
|
683
|
+
);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// src/modules/embeds/block-rule.ts
|
|
687
|
+
var LINE_RE = /^!\[\[([^\n\]]+)\]\]\s*$/;
|
|
688
|
+
function registerEmbedBlockRule(md) {
|
|
689
|
+
md.block.ruler.before(
|
|
690
|
+
"paragraph",
|
|
691
|
+
"allyouneed_embed_block",
|
|
692
|
+
makeRule(md),
|
|
693
|
+
{ alt: ["paragraph"] }
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
function makeRule(md) {
|
|
697
|
+
return function embedBlockRule(state, startLine, _endLine, silent) {
|
|
698
|
+
const start = state.bMarks[startLine] + state.tShift[startLine];
|
|
699
|
+
const max = state.eMarks[startLine];
|
|
700
|
+
const lineText = state.src.slice(start, max);
|
|
701
|
+
const m = lineText.match(LINE_RE);
|
|
702
|
+
if (!m) return false;
|
|
703
|
+
if (silent) return true;
|
|
704
|
+
const env = state.env;
|
|
705
|
+
if (!env || !env.index || !env.options) return false;
|
|
706
|
+
const inner = m[1];
|
|
707
|
+
const parts = inner.split("|").map((p) => p.trim());
|
|
708
|
+
const rawTarget = parts[0];
|
|
709
|
+
const aliasParts = parts.slice(1);
|
|
710
|
+
const ext = extractExt2(rawTarget);
|
|
711
|
+
const isImage = !!ext && env.options.embeds.imageFileExt.includes(ext.toLowerCase());
|
|
712
|
+
let html;
|
|
713
|
+
if (isImage) {
|
|
714
|
+
html = renderImageHtml(rawTarget, aliasParts, env);
|
|
715
|
+
} else {
|
|
716
|
+
html = renderTransclusionHtml(md, rawTarget, aliasParts, env);
|
|
717
|
+
}
|
|
718
|
+
const token = state.push("html_block", "", 0);
|
|
719
|
+
token.content = html + "\n";
|
|
720
|
+
token.map = [startLine, startLine + 1];
|
|
721
|
+
state.line = startLine + 1;
|
|
722
|
+
return true;
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
function extractExt2(target) {
|
|
726
|
+
const cleaned = target.split("#")[0];
|
|
727
|
+
const dot = cleaned.lastIndexOf(".");
|
|
728
|
+
if (dot <= 0) return "";
|
|
729
|
+
return cleaned.slice(dot + 1).toLowerCase();
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// src/modules/embeds/index.ts
|
|
733
|
+
function registerEmbedsOnly(md) {
|
|
734
|
+
md.inline.ruler.before(
|
|
735
|
+
"link",
|
|
736
|
+
"allyouneed_embeds",
|
|
737
|
+
makeWikilinkRule("embeds-only")
|
|
738
|
+
);
|
|
739
|
+
registerEmbedBlockRule(md);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// src/markdown-it.ts
|
|
743
|
+
function allYouNeedMarkdownIt(md, options) {
|
|
744
|
+
const resolved = options && isResolved(options) ? options : resolveOptions(options);
|
|
745
|
+
const { wikilinks: wlOn, embeds: emOn } = resolved.modules;
|
|
746
|
+
if (wlOn && emOn) {
|
|
747
|
+
registerWikilinks(md, "both");
|
|
748
|
+
registerEmbedBlockRule(md);
|
|
749
|
+
} else if (wlOn) {
|
|
750
|
+
registerWikilinks(md, "wikilinks-only");
|
|
751
|
+
} else if (emOn) {
|
|
752
|
+
registerEmbedsOnly(md);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
function isResolved(o) {
|
|
756
|
+
return typeof o.slugify === "function" && typeof o.srcDir === "string" && typeof o.modules === "object" && o.modules !== null;
|
|
757
|
+
}
|
|
758
|
+
var markdown_it_default = allYouNeedMarkdownIt;
|
|
759
|
+
export {
|
|
760
|
+
allYouNeedMarkdownIt,
|
|
761
|
+
markdown_it_default as default
|
|
762
|
+
};
|
|
763
|
+
//# sourceMappingURL=markdown-it.js.map
|