@alauda/doom 0.6.4 → 0.6.5

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.
@@ -2,6 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Link, renderInlineMarkdown, useSidebarData } from '@rspress/core/theme';
3
3
  import { isEqualPath, normalizeHrefInRuntime as normalizeHref, usePageData, withBase, } from '@rspress/runtime';
4
4
  import { Fragment, useMemo } from 'react';
5
+ import { findItemByRoutePath } from './_utils.js';
5
6
  import classes from '../../../styles/overview.module.scss';
6
7
  // The sidebar data include two types: sidebar item and sidebar group.
7
8
  // In overpage page, we select all the related sidebar groups and show the groups in the page.
@@ -23,24 +24,13 @@ export function Overview(props) {
23
24
  }
24
25
  return '';
25
26
  };
26
- const findItemByRoutePath = (items, routePath, originalItems) => {
27
- for (const item of items) {
28
- if ('link' in item && withBase(item.link) === routePath) {
29
- return [item];
30
- }
31
- if ('items' in item) {
32
- return findItemByRoutePath(item.items, routePath, originalItems);
33
- }
34
- }
35
- return originalItems;
36
- };
37
27
  const { pages } = siteData;
38
28
  const overviewModules = pages.filter((page) => subFilter(page.routePath));
39
29
  let { items: overviewSidebarGroups } = useSidebarData();
40
30
  if (overviewSidebarGroups[0] &&
41
31
  'link' in overviewSidebarGroups[0] &&
42
32
  overviewSidebarGroups[0]?.link !== routePath) {
43
- overviewSidebarGroups = findItemByRoutePath(overviewSidebarGroups, routePath, overviewSidebarGroups);
33
+ overviewSidebarGroups = findItemByRoutePath(overviewSidebarGroups, routePath);
44
34
  }
45
35
  function normalizeSidebarItem(item, sidebarGroup, frontmatter) {
46
36
  if ('dividerType' in item) {
@@ -0,0 +1,7 @@
1
+ import type { NormalizedSidebarGroup, SidebarDivider, SidebarItem, SidebarSectionHeader } from '@rspress/shared';
2
+ export declare const isSidebarDivider: (item: NormalizedSidebarGroup | SidebarItem | SidebarDivider | SidebarSectionHeader) => item is SidebarDivider;
3
+ /**
4
+ * @zh_CN 如果 index 在 sidebar items 中, 则返回所有平级 item, 如果 index 在 dir 上, 则返回 items
5
+ * @example
6
+ */
7
+ export declare function findItemByRoutePath(items: (SidebarItem | NormalizedSidebarGroup | SidebarDivider)[], routePath: string): (SidebarItem | NormalizedSidebarGroup)[];
@@ -0,0 +1,60 @@
1
+ import { withBase } from '@rspress/core/runtime';
2
+ function removeIndex(link) {
3
+ if (link.endsWith('/index')) {
4
+ return link.slice(0, -5);
5
+ }
6
+ return link;
7
+ }
8
+ export const isSidebarDivider = (item) => {
9
+ return 'dividerType' in item;
10
+ };
11
+ /**
12
+ * @zh_CN 如果 index 在 sidebar items 中, 则返回所有平级 item, 如果 index 在 dir 上, 则返回 items
13
+ * @example
14
+ */
15
+ export function findItemByRoutePath(items, routePath) {
16
+ function isRoutePathMatch(item) {
17
+ if (isSidebarDivider(item)) {
18
+ return false;
19
+ }
20
+ const withBaseUrl = withBase(item.link);
21
+ const removeIndexUrl = removeIndex(withBaseUrl);
22
+ const removeBackSlashedRoutePath = routePath.replace(/\/$/, '');
23
+ return (
24
+ // FIXME: 😅 we should refactor all the path logic, /index.html / or no /, l
25
+ withBaseUrl === routePath ||
26
+ removeIndexUrl === routePath ||
27
+ withBaseUrl === removeBackSlashedRoutePath ||
28
+ removeIndexUrl === removeBackSlashedRoutePath);
29
+ }
30
+ const matchRoutePathItemIndex = items.findIndex((item) => {
31
+ return isRoutePathMatch(item);
32
+ });
33
+ if (matchRoutePathItemIndex === -1) {
34
+ return items
35
+ .map((item) => {
36
+ if (!('items' in item)) {
37
+ return [];
38
+ }
39
+ return findItemByRoutePath(item.items, routePath);
40
+ })
41
+ .flat();
42
+ }
43
+ const matchRoutePathItem = items[matchRoutePathItemIndex];
44
+ const isArray = (i) => Array.isArray(i) && i.length >= 1;
45
+ // 1. if isDir(item) return item.items
46
+ if ('items' in matchRoutePathItem && isArray(matchRoutePathItem.items)) {
47
+ // 2. if isDir(item) && item.items is all files, style is different
48
+ if (matchRoutePathItem.items.every((item) => !('items' in item))) {
49
+ return [matchRoutePathItem];
50
+ }
51
+ return matchRoutePathItem.items.filter((item) => !isSidebarDivider(item));
52
+ }
53
+ // 3. if matchRoutePathItem is a item, return other items in same level (/api/index.md is in the child sidebar)
54
+ const result = [...items];
55
+ if (!('items' in matchRoutePathItem)) {
56
+ result.splice(matchRoutePathItemIndex, 1);
57
+ }
58
+ const res = result.filter((item) => !isSidebarDivider(item));
59
+ return res;
60
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {