Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-auto-nav-sidebar): Exclude config.route.exclude files from auto-generated sidebar #899

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/serious-clouds-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rspress/plugin-auto-nav-sidebar": patch
"@rspress/docs": patch
---

Exclude config.route.exclude files from auto-generated sidebar
10 changes: 8 additions & 2 deletions packages/document/docs/en/guide/basic/auto-nav-sidebar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ Here is a complete example using the three types above:

### No Config Usage

In some directories, you don't need to configure `_meta.json` and let the framework automatically generate the sidebar. This requires ensuring that the directory contains only documents, not subdirectories, and you have no requirements for the order of documents. For example, there is now the following document structure:
In some directories, you don't need to configure `_meta.json` and let the framework automatically generate the sidebar.

For example, there is a document structure:

```bash
docs
Expand All @@ -299,7 +301,9 @@ In the guides directory you can configure `_meta.json` as follows:
]
```

In `basic` directory, you may not configure `_meta.json`, and then the framework will automatically generate a sidebar for you, the default is sorted alphabetically according to the file name. If you want to customize the order, you can prefix the file name with a number, such as:
In `basic` directory, you may not configure `_meta.json`, and then the framework will automatically generate a sidebar for you.

By default files are sorted alphabetically. If you want to customize their orders, you can prefix the file names with a number, such as:

```bash
basic
Expand All @@ -308,6 +312,8 @@ basic
└── 3-plugin-development.md
```

> Note : The files defined in [route.exclude](/api/config/config-basic.html#routeexclude) won't appear in the auto-generated sidebar.

### Add SVG Icons Before Titles

In addition, you can add icons before the title through the `tag` confing, like this:
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-auto-nav-sidebar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"@rspress/shared": "workspace:*"
"@rspress/shared": "workspace:*",
"globby": "11.1.0"
}
}
20 changes: 18 additions & 2 deletions packages/plugin-auto-nav-sidebar/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ function processLocales(
root: string,
defaultLang: string,
defaultVersion: string,
excludedFiles?: string[],
) {
return Promise.all(
langs.map(async lang => {
Expand All @@ -168,14 +169,20 @@ function processLocales(
lang === defaultLang ? '' : `/${lang}`
}`,
);
return walk(path.join(root, version, lang), routePrefix, root);
return walk(
path.join(root, version, lang),
routePrefix,
root,
excludedFiles,
);
}),
)
: [
await walk(
path.join(root, lang),
addTrailingSlash(lang === defaultLang ? '' : `/${lang}`),
root,
excludedFiles,
),
];
return combineWalkResult(walks, versions);
Expand Down Expand Up @@ -203,6 +210,7 @@ export function pluginAutoNavSidebar(): RspressPlugin {
config.root!,
defaultLang,
defaultVersion,
config.route?.exclude,
);
config.themeConfig.locales = config.themeConfig.locales.map(
(item, index) => ({
Expand Down Expand Up @@ -230,10 +238,18 @@ export function pluginAutoNavSidebar(): RspressPlugin {
path.join(config.root!, version),
routePrefix,
config.root!,
config.route?.exclude,
);
}),
)
: [await walk(config.root!, '/', config.root!)];
: [
await walk(
config.root!,
'/',
config.root!,
config.route?.exclude,
),
];

const combined = combineWalkResult(walks, versions);

Expand Down
18 changes: 17 additions & 1 deletion packages/plugin-auto-nav-sidebar/src/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import {
} from '@rspress/shared';
import { NavMeta, SideMeta } from './type';
import { detectFilePath, extractTitleAndOverviewHeaders } from './utils';
import globby from 'globby';

export async function scanSideMeta(
workDir: string,
rootDir: string,
docsDir: string,
routePrefix: string,
excludedFiles?: string[],
) {
const addRoutePrefix = (link: string) => `${routePrefix}${link}`;
// find the `_meta.json` file
Expand All @@ -32,7 +34,18 @@ export async function scanSideMeta(
sideMeta = (await fs.readJSON(metaFile, 'utf8')) as SideMeta;
} catch (e) {
// If the `_meta.json` file doesn't exist, we will generate the sidebar config from the directory structure.
const subItems = await fs.readdir(workDir);

const includedFiles = globby.sync('*', {
cwd: workDir,
onlyFiles: false,
expandDirectories: false,
ignore: excludedFiles ?? [],
});

const subItems = [
...new Set(includedFiles.map(file => file.split('/')[0])),
];

sideMeta = (
await Promise.all(
subItems.map(async item => {
Expand Down Expand Up @@ -114,6 +127,7 @@ export async function scanSideMeta(
rootDir,
docsDir,
routePrefix,
excludedFiles,
);
const realPath = await detectFilePath(subDir);
return {
Expand Down Expand Up @@ -152,6 +166,7 @@ export async function walk(
workDir: string,
routePrefix = '/',
docsDir: string,
excludedFiles?: string[],
) {
// find the `_meta.json` file
const rootMetaFile = path.resolve(workDir, '_meta.json');
Expand Down Expand Up @@ -189,6 +204,7 @@ export async function walk(
workDir,
docsDir,
routePrefix,
excludedFiles,
);
}
return {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading