From 5f8c59ff93f67727ef5335a5b752d345b36fb4a0 Mon Sep 17 00:00:00 2001 From: Tyneor Date: Tue, 2 Apr 2024 16:29:27 +0200 Subject: [PATCH 1/5] fix(#848, plugin-auto-nav-sidebar) exclude config.route.exclude files from auto-generated sidebar --- packages/plugin-auto-nav-sidebar/src/index.ts | 20 +++++++++++++++++-- packages/plugin-auto-nav-sidebar/src/walk.ts | 16 ++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/plugin-auto-nav-sidebar/src/index.ts b/packages/plugin-auto-nav-sidebar/src/index.ts index 1ae459f7f..1d0422b7d 100644 --- a/packages/plugin-auto-nav-sidebar/src/index.ts +++ b/packages/plugin-auto-nav-sidebar/src/index.ts @@ -157,6 +157,7 @@ function processLocales( root: string, defaultLang: string, defaultVersion: string, + excludedFiles?: string[], ) { return Promise.all( langs.map(async lang => { @@ -168,7 +169,12 @@ function processLocales( lang === defaultLang ? '' : `/${lang}` }`, ); - return walk(path.join(root, version, lang), routePrefix, root); + return walk( + path.join(root, version, lang), + routePrefix, + root, + excludedFiles, + ); }), ) : [ @@ -176,6 +182,7 @@ function processLocales( path.join(root, lang), addTrailingSlash(lang === defaultLang ? '' : `/${lang}`), root, + excludedFiles, ), ]; return combineWalkResult(walks, versions); @@ -203,6 +210,7 @@ export function pluginAutoNavSidebar(): RspressPlugin { config.root!, defaultLang, defaultVersion, + config.route?.exclude, ); config.themeConfig.locales = config.themeConfig.locales.map( (item, index) => ({ @@ -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); diff --git a/packages/plugin-auto-nav-sidebar/src/walk.ts b/packages/plugin-auto-nav-sidebar/src/walk.ts index 7b0769d86..c7eee15a9 100644 --- a/packages/plugin-auto-nav-sidebar/src/walk.ts +++ b/packages/plugin-auto-nav-sidebar/src/walk.ts @@ -13,12 +13,14 @@ import { } from '@rspress/shared'; import { NavMeta, SideMeta } from './type'; import { detectFilePath, extractH1Title } from './utils'; +import globby from '../../core/compiled/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 @@ -32,7 +34,16 @@ 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, + ignore: [...excludedFiles], + }); + const subItems = [ + ...new Set(includedFiles.map(file => file.split('/')[0])), + ]; + sideMeta = ( await Promise.all( subItems.map(async item => { @@ -106,6 +117,7 @@ export async function scanSideMeta( rootDir, docsDir, routePrefix, + excludedFiles, ); const realPath = await detectFilePath(subDir); return { @@ -144,6 +156,7 @@ export async function walk( workDir: string, routePrefix = '/', docsDir: string, + excludedFiles?: string[], ) { // find the `_meta.json` file const rootMetaFile = path.resolve(workDir, '_meta.json'); @@ -181,6 +194,7 @@ export async function walk( workDir, docsDir, routePrefix, + excludedFiles, ); } return { From 4b9442fc1a88ef2260a79e0637e09fc7235c4a53 Mon Sep 17 00:00:00 2001 From: Tyneor Date: Tue, 2 Apr 2024 16:30:30 +0200 Subject: [PATCH 2/5] changeset --- .changeset/little-pumpkins-fly.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/little-pumpkins-fly.md diff --git a/.changeset/little-pumpkins-fly.md b/.changeset/little-pumpkins-fly.md new file mode 100644 index 000000000..c94a52068 --- /dev/null +++ b/.changeset/little-pumpkins-fly.md @@ -0,0 +1,5 @@ +--- +"@rspress/plugin-auto-nav-sidebar": minor +--- + +Exclude config.route.exclude files from auto-generated sidebar From 3e878047f5696586843ca9b192dca9ec3be3dfbb Mon Sep 17 00:00:00 2001 From: Tyneor Date: Tue, 2 Apr 2024 17:18:03 +0200 Subject: [PATCH 3/5] fix(#848, plugin-auto-nav-sidebar, doc) added documentation, fixed globby import, fixed empty folder appearing in sidebars, fixed changeset --- .../{little-pumpkins-fly.md => serious-clouds-tap.md} | 3 ++- .../document/docs/en/guide/basic/auto-nav-sidebar.mdx | 10 ++++++++-- packages/plugin-auto-nav-sidebar/package.json | 3 ++- packages/plugin-auto-nav-sidebar/src/walk.ts | 3 +-- pnpm-lock.yaml | 3 +++ 5 files changed, 16 insertions(+), 6 deletions(-) rename .changeset/{little-pumpkins-fly.md => serious-clouds-tap.md} (52%) diff --git a/.changeset/little-pumpkins-fly.md b/.changeset/serious-clouds-tap.md similarity index 52% rename from .changeset/little-pumpkins-fly.md rename to .changeset/serious-clouds-tap.md index c94a52068..8171bf0c3 100644 --- a/.changeset/little-pumpkins-fly.md +++ b/.changeset/serious-clouds-tap.md @@ -1,5 +1,6 @@ --- -"@rspress/plugin-auto-nav-sidebar": minor +"@rspress/plugin-auto-nav-sidebar": patch +"@rspress/docs": patch --- Exclude config.route.exclude files from auto-generated sidebar diff --git a/packages/document/docs/en/guide/basic/auto-nav-sidebar.mdx b/packages/document/docs/en/guide/basic/auto-nav-sidebar.mdx index fc89b574a..d1da33cf5 100644 --- a/packages/document/docs/en/guide/basic/auto-nav-sidebar.mdx +++ b/packages/document/docs/en/guide/basic/auto-nav-sidebar.mdx @@ -266,7 +266,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 @@ -293,7 +295,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 @@ -302,6 +306,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: diff --git a/packages/plugin-auto-nav-sidebar/package.json b/packages/plugin-auto-nav-sidebar/package.json index f2de78c14..720de90ff 100644 --- a/packages/plugin-auto-nav-sidebar/package.json +++ b/packages/plugin-auto-nav-sidebar/package.json @@ -57,6 +57,7 @@ "registry": "https://registry.npmjs.org/" }, "dependencies": { - "@rspress/shared": "workspace:*" + "@rspress/shared": "workspace:*", + "globby": "11.1.0" } } diff --git a/packages/plugin-auto-nav-sidebar/src/walk.ts b/packages/plugin-auto-nav-sidebar/src/walk.ts index c7eee15a9..8cfd6e907 100644 --- a/packages/plugin-auto-nav-sidebar/src/walk.ts +++ b/packages/plugin-auto-nav-sidebar/src/walk.ts @@ -13,7 +13,7 @@ import { } from '@rspress/shared'; import { NavMeta, SideMeta } from './type'; import { detectFilePath, extractH1Title } from './utils'; -import globby from '../../core/compiled/globby'; +import globby from 'globby'; export async function scanSideMeta( workDir: string, @@ -37,7 +37,6 @@ export async function scanSideMeta( const includedFiles = globby.sync('**', { cwd: workDir, - onlyFiles: false, ignore: [...excludedFiles], }); const subItems = [ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f0789e427..c76918473 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -727,6 +727,9 @@ importers: '@rspress/shared': specifier: workspace:* version: link:../shared + globby: + specifier: 11.1.0 + version: link:../core/compiled/globby devDependencies: '@modern-js/tsconfig': specifier: 2.48.4 From 327be23f92e4842d9a48a22a7fc690cfde82473f Mon Sep 17 00:00:00 2001 From: Etienne <35744626+Tyneor@users.noreply.github.com> Date: Wed, 3 Apr 2024 10:31:34 +0200 Subject: [PATCH 4/5] fix(#848, plugin-auto-nav-sidebar) catch undefined excludedFiles --- packages/plugin-auto-nav-sidebar/src/walk.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugin-auto-nav-sidebar/src/walk.ts b/packages/plugin-auto-nav-sidebar/src/walk.ts index 8cfd6e907..e33aa108f 100644 --- a/packages/plugin-auto-nav-sidebar/src/walk.ts +++ b/packages/plugin-auto-nav-sidebar/src/walk.ts @@ -20,7 +20,7 @@ export async function scanSideMeta( rootDir: string, docsDir: string, routePrefix: string, - excludedFiles: string[] = [], + excludedFiles?: string[], ) { const addRoutePrefix = (link: string) => `${routePrefix}${link}`; // find the `_meta.json` file @@ -37,7 +37,7 @@ export async function scanSideMeta( const includedFiles = globby.sync('**', { cwd: workDir, - ignore: [...excludedFiles], + ignore: excludedFiles ?? [], }); const subItems = [ ...new Set(includedFiles.map(file => file.split('/')[0])), From d081634c423878b5676d0d964e247a92a3c53192 Mon Sep 17 00:00:00 2001 From: Tyneor Date: Tue, 16 Apr 2024 19:05:39 +0200 Subject: [PATCH 5/5] only exclude on directory direct level --- packages/plugin-auto-nav-sidebar/src/walk.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/plugin-auto-nav-sidebar/src/walk.ts b/packages/plugin-auto-nav-sidebar/src/walk.ts index e33aa108f..1eb5d5e52 100644 --- a/packages/plugin-auto-nav-sidebar/src/walk.ts +++ b/packages/plugin-auto-nav-sidebar/src/walk.ts @@ -35,10 +35,13 @@ export async function scanSideMeta( } catch (e) { // If the `_meta.json` file doesn't exist, we will generate the sidebar config from the directory structure. - const includedFiles = globby.sync('**', { + const includedFiles = globby.sync('*', { cwd: workDir, + onlyFiles: false, + expandDirectories: false, ignore: excludedFiles ?? [], }); + const subItems = [ ...new Set(includedFiles.map(file => file.split('/')[0])), ];