Skip to content

Commit

Permalink
feat: scan the md file in path
Browse files Browse the repository at this point in the history
  • Loading branch information
QC2168 committed Dec 17, 2024
1 parent 3770168 commit 77c4e3c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default defineConfig({
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ | ------- |
| ignoreList | ignore specified folder | `string[]` | `true` |
| path | create scan path for the sidebar | `string` | `/docs` |
| scanRootMdFiles | scan the md file in path | `boolean` | `true` |
| ignoreIndexItem | ignore the page sidebar with only index.md | `boolean` | `false` |
| collapsed | By adding collapsed option to the sidebar group, it shows a toggle button to hide/show each section,For specific usage, please refer to VitePress | `boolean` | `false` |
| deletePrefix | deletes the md file prefix | `string` | |
Expand Down
28 changes: 18 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ let option: SidebarPluginOptionType;

function createSideBarItems(
targetPath: string,
...reset: string[]
path: string[],
recursive = true
): DefaultTheme.SidebarItem[] {
const {
ignoreIndexItem,
Expand All @@ -21,25 +22,25 @@ function createSideBarItems(
beforeCreateSideBarItems,
ignoreList = [],
titleFromFile = false,
titleFromFileByYaml = false
titleFromFileByYaml = false,
} = option;
const rawNode = readdirSync(join(targetPath, ...reset));
const rawNode = readdirSync(join(targetPath, ...path));
const node = beforeCreateSideBarItems?.(rawNode) ?? rawNode;
const currentDir = join(targetPath, ...reset);
const currentDir = join(targetPath, ...path);
if (ignoreIndexItem && node.length === 1 && node[0] === 'index.md') {
return [];
}
const result: DefaultTheme.SidebarItem[] = [];

const exec = extractTitleFn({ titleFromFile, titleFromFileByYaml });
for (const fname of node) {
if (statSync(join(targetPath, ...reset, fname)).isDirectory()) {
if (recursive && statSync(join(targetPath, ...path, fname)).isDirectory()) {
if (ignoreList.some(item => item === fname || (item instanceof RegExp && item.test(fname)))) {
continue;
}
// is directory
// ignore cur node if items length is 0
const items = createSideBarItems(join(targetPath), ...reset, fname);
const items = createSideBarItems(join(targetPath), [...path, fname]);
// replace directory name, if yes
let text = fname;
// get the title in index.md file
Expand Down Expand Up @@ -94,7 +95,7 @@ function createSideBarItems(
}
const item: DefaultTheme.SidebarItem = {
text,
link: '/' + [...reset, `${fileName}.html`].join('/')
link: '/' + [...path.filter(Boolean), `${fileName}.html`].join('/')
};
result.push(item);
}
Expand All @@ -104,11 +105,12 @@ function createSideBarItems(

function createSideBarGroups(
targetPath: string,
folder: string
folder: string,
recursive = true
): DefaultTheme.SidebarItem[] {
return [
{
items: createSideBarItems(targetPath, folder)
items: createSideBarItems(targetPath, [folder], recursive)
}
];
}
Expand All @@ -117,14 +119,20 @@ function createSidebarMulti(path: string): DefaultTheme.SidebarMulti {
const {
ignoreList = [],
ignoreIndexItem = false,
sideBarResolved
sideBarResolved,
scanRootMdFiles = true
} = option;
const il = [...DEFAULT_IGNORE_FOLDER, ...ignoreList];
const data: DefaultTheme.SidebarMulti = {};
const node = readdirSync(path).filter(
(n) => statSync(join(path, n)).isDirectory() && !il.includes(n)
);

// all md in root
if (scanRootMdFiles) {
data['/'] = createSideBarGroups(path, '', false);
}

for (const k of node) {
data[`/${k}/`] = createSideBarGroups(path, k);
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface SidebarPluginOptionType {
// 是否从文件获取sidebar标题,默认是否
titleFromFile?: boolean
titleFromFileByYaml?: boolean
scanRootMdFiles?: boolean
sideBarResolved?: (data: DefaultTheme.SidebarMulti) => DefaultTheme.SidebarMulti
sideBarItemsResolved?: (data: DefaultTheme.SidebarItem[]) => DefaultTheme.SidebarItem[]
beforeCreateSideBarItems?: (data: string[]) => string[]
Expand Down

0 comments on commit 77c4e3c

Please sign in to comment.