From 91fc1357d3e3cc93c9a1d9b9c223d4ed5b1129e0 Mon Sep 17 00:00:00 2001 From: huan_kong <2564076459@qq.com> Date: Fri, 9 Feb 2024 01:43:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=20`notes`=20=E4=B8=AD=20`sidebar`=20?= =?UTF-8?q?=E4=B8=BA=20`auto`=20=E6=97=B6=20=E8=87=AA=E5=8A=A8=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/node/prepareNotesData.ts | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/plugins/plugin-notes-data/src/node/prepareNotesData.ts b/plugins/plugin-notes-data/src/node/prepareNotesData.ts index 0f1379ac..ba611ae8 100644 --- a/plugins/plugin-notes-data/src/node/prepareNotesData.ts +++ b/plugins/plugin-notes-data/src/node/prepareNotesData.ts @@ -114,11 +114,29 @@ function initSidebarByAuto( note: NotesItemOptions, pages: NotePage[], ): NotesSidebarItem[] { - pages = pages.sort((prev, next) => { - const pi = prev.relativePath.match(/\//g)?.length || 0 - const ni = next.relativePath.match(/\//g)?.length || 0 - return pi < ni ? -1 : 1 + let tempPages = pages.map(page => { + return { ...page, splitPath: page.relativePath.split('/') } }) + + const maxIndex = Math.max(...tempPages.map(page => page.splitPath.length)) + let nowIndex = 0 + + while (nowIndex < maxIndex) { + tempPages = tempPages.sort((prev, next) => { + const pi = prev.splitPath?.[nowIndex]?.match(/(\d+)\.(?=[^/]+$)/)?.[1] + const ni = next.splitPath?.[nowIndex]?.match(/(\d+)\.(?=[^/]+$)/)?.[1] + if (!pi || !ni) return 0 + return parseFloat(pi) < parseFloat(ni) ? -1 : 1 + }) + + nowIndex++ + } + + pages = tempPages.map(page => { + delete page.splitPath; + return page + }) + const RE_INDEX = ['index.md', 'README.md', 'readme.md'] const result: NotesSidebarItem[] = [] for (const page of pages) {