vitepress-allyouneed 0.3.6 → 0.3.7

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 CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  本项目遵循 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/);版本号遵循 [SemVer](https://semver.org/lang/zh-CN/)。
4
4
 
5
+ ## [0.3.6] - 2026-05-20
6
+
7
+ 针对用户反馈"Perspectives 有时不加载 / sidebar 有时错"的间歇性问题做了系统排查,5 个真 bug。
8
+
9
+ ### Fixed
10
+ - **Perspectives 在 `locales.root.themeConfig` 配置下消失**:VitePress 1.6 渲染时浅 merge `themeConfig` 与 `siteData.locales[k]?.themeConfig` —— locale 整个**替换**顶层 nav/sidebar。wrapper 之前 `if (lang === 'root') continue` 跳过 root,导致用户用标准 i18n 写法 `locales: { root: { themeConfig: { nav: [...] }}}` 时,顶层 Perspectives 被 root 的(未注入)nav 覆盖。现在所有 locale(含 root)都过 inject。
11
+ - **dev 模式下新增 / 删除文件后 sidebar/nav 不更新**:wrapper 只在 config time 跑一次 scan + 生成 sidebar,后续 hot-update 只刷新 Vite 插件的 index(用于 wikilink / asset / vault-data.json)。结构变化静默吞掉。现在 `handleHotUpdate` 检测到 `.md` 文件 add / remove 时 `console.warn` 提示"重启 dev 服务器才能反映 sidebar/nav 变化"。
12
+ - **空 `themeConfig.sidebar = {}` 或部分 per-path 配置下 Perspectives 在根路径消失**:`injectViewsSidebar` 老逻辑 `for (Object.keys(sidebar))` 在空 object 上跑 0 次,Perspectives 没塞任何路径;用户在 `/` 或非匹配路径访问时看不到。现在显式给 `/` key 兜底一份。
13
+ - **`themeConfig.nav` 是 function 时被替换为 `[Perspectives]`**:VitePress nav 支持 function(动态 / locale-aware)。老 `Array.isArray(fn) ? [...fn] : []` 把 function 错判成"无 nav",推 Perspectives 后用单元素数组覆盖用户函数。现在 `typeof === 'function'` 时 wrap 一层,运行时调用用户 fn + 追加 Perspectives;用户 fn 抛错时容错只返 Perspectives 并 warn。
14
+ - **perspective fallback sidebar 含标题为 `/` 的死项**:`buildPerspectivesFallbackSidebar` 过滤 `p !== base`,但 base 是 `/sub/` 时永远不等于 `/` → `/` 进 topPaths → seg 为空 → 渲染为 `text: '/'` 的死项。现在显式排除 `'/'` + 双保险 `if (!seg) continue`。
15
+
16
+ ### Tests
17
+ - 新增 `tests/v036-perspective-locale.test.ts`,覆盖以上 5 个 bug。
18
+
5
19
  ## [0.3.5] - 2026-05-20
6
20
 
7
21
  零配置导航更顺手:不写 `index.md` 也能从导航 / sidebar / 用户手写 wikilink 进到一个文件夹;默认 index 模板换成"文件管理器风格"(子文件夹在上、文件在下)。
package/dist/index.cjs CHANGED
@@ -2424,15 +2424,28 @@ function viteAllYouNeed(userOptions = {}) {
2424
2424
  },
2425
2425
  handleHotUpdate(ctx) {
2426
2426
  if (!index) return;
2427
+ const isMd = /\.(md|markdown)$/i.test(ctx.file);
2428
+ const wasIndexed = isMd && index.files.has(ctx.file);
2429
+ let structuralChange = null;
2427
2430
  try {
2428
2431
  const stat = import_node_fs7.default.statSync(ctx.file);
2429
2432
  if (stat.isFile()) {
2430
2433
  updateFile(index, ctx.file, resolved);
2434
+ const isNowIndexed = index.files.has(ctx.file);
2435
+ if (!wasIndexed && isNowIndexed) structuralChange = "add";
2431
2436
  } else if (!import_node_fs7.default.existsSync(ctx.file)) {
2432
2437
  removeFile(index, ctx.file, resolved);
2438
+ if (wasIndexed) structuralChange = "remove";
2433
2439
  }
2434
2440
  } catch {
2435
2441
  removeFile(index, ctx.file, resolved);
2442
+ if (wasIndexed) structuralChange = "remove";
2443
+ }
2444
+ if (structuralChange) {
2445
+ const rel = toPosix(ctx.file).replace(toPosix(resolved.srcDir) + "/", "");
2446
+ console.warn(
2447
+ `vitepress-allyouneed: \u68C0\u6D4B\u5230\u7ED3\u6784\u53D8\u5316(${structuralChange === "add" ? "\u65B0\u589E" : "\u5220\u9664"} ${rel}),sidebar/nav \u5DF2\u88AB\u70D8\u7119\u5230 VitePress \u914D\u7F6E\u4E2D,**\u91CD\u542F dev \u670D\u52A1\u5668**\u624D\u80FD\u53CD\u6620;wikilink/asset \u5DF2\u5B9E\u65F6\u5237\u65B0\u3002`
2448
+ );
2436
2449
  }
2437
2450
  if (resolved.modules.views) {
2438
2451
  try {
@@ -2546,6 +2559,11 @@ function injectViewsSidebar(sidebar, options) {
2546
2559
  arr.push(group);
2547
2560
  }
2548
2561
  }
2562
+ if (!sidebar["/"]) {
2563
+ sidebar["/"] = [group];
2564
+ } else if (!sidebar["/"].some((it) => it.text === group.text)) {
2565
+ sidebar["/"].push(group);
2566
+ }
2549
2567
  }
2550
2568
  const prefix = options.views.urlPrefix ? options.views.urlPrefix.replace(/^\/+|\/+$/g, "") : "";
2551
2569
  if (prefix) {
@@ -2567,10 +2585,11 @@ function buildPerspectivesFallbackSidebar(allSidebars, group, base, viewsPrefix)
2567
2585
  const out = [{ text: "Home", link: "/" }];
2568
2586
  const persSuffix = `/${viewsPrefix}/`;
2569
2587
  const topPaths = Object.keys(allSidebars).filter(
2570
- (p) => p !== base && !p.endsWith(persSuffix)
2588
+ (p) => p !== "/" && p !== base && !p.endsWith(persSuffix)
2571
2589
  );
2572
2590
  for (const p of topPaths) {
2573
2591
  const seg = p.replace(/^\/|\/$/g, "").split("/").filter(Boolean).pop() ?? p;
2592
+ if (!seg) continue;
2574
2593
  const text = seg.charAt(0).toUpperCase() + seg.slice(1);
2575
2594
  const b = base.endsWith("/") ? base : base + "/";
2576
2595
  const stripped = b !== "/" && p.startsWith(b) ? "/" + p.slice(b.length) : p;
@@ -2612,6 +2631,26 @@ function injectViewsNav(nav, options) {
2612
2631
  link: it.link
2613
2632
  }))
2614
2633
  };
2634
+ if (typeof nav === "function") {
2635
+ const userFn = nav;
2636
+ return () => {
2637
+ let arr2;
2638
+ try {
2639
+ const r = userFn();
2640
+ arr2 = Array.isArray(r) ? [...r] : [];
2641
+ } catch (e) {
2642
+ console.warn(
2643
+ "vitepress-allyouneed: themeConfig.nav \u51FD\u6570\u6267\u884C\u5931\u8D25,\u4EC5\u8FD4\u56DE Perspectives \u4E0B\u62C9\u3002",
2644
+ e instanceof Error ? e.message : String(e)
2645
+ );
2646
+ arr2 = [];
2647
+ }
2648
+ if (!arr2.some((it) => it && it.text === navItem.text)) {
2649
+ arr2.push(navItem);
2650
+ }
2651
+ return arr2;
2652
+ };
2653
+ }
2615
2654
  const arr = Array.isArray(nav) ? [...nav] : [];
2616
2655
  if (!arr.some((it) => it.text === navItem.text)) {
2617
2656
  arr.push(navItem);
@@ -3675,7 +3714,6 @@ function defineConfigWithAllYouNeed(config, pluginOptions = {}) {
3675
3714
  const localesForViews = config.locales;
3676
3715
  if (localesForViews) {
3677
3716
  for (const lang of Object.keys(localesForViews)) {
3678
- if (lang === "root") continue;
3679
3717
  const lc = localesForViews[lang];
3680
3718
  if (!lc.themeConfig) lc.themeConfig = {};
3681
3719
  if (lc.themeConfig.sidebar !== void 0) {