jooy2 / vitepress-sidebar

🔌 VitePress Sidebar is a plugin for VitePress that automatically configures and manages the sidebar of your page with simple settings.
https://vitepress-sidebar.cdget.com
MIT License
144 stars 7 forks source link

When rewrites are enabled, the plugin fails #146

Closed ghost closed 2 weeks ago

ghost commented 5 months ago

When rewrites are enabled in the config.ts file, the sidebar is not automatically generated

// vitepress config.ts 
import { defineConfig } from "vitepress";
import { generateSidebar } from "vitepress-sidebar";

export default defineConfig({
  rewrites: {
    "demo/test.md": "page/example.md",
  },
  themeConfig: {
    nav: [
      { text: "dmeo", link: "/demo/test" },
      { text: "javascript", link: "/javascript/basic" },
    ],

    sidebar: generateSidebar([
      {
        documentRootPath: "docs",
        scanStartPath: "demo",
        resolvePath: "/demo/",
      },
      {
        documentRootPath: "docs",
        scanStartPath: "javascript",
        resolvePath: "/javascript/",
      },
    ]),
  },
});
├─.vitepress
│  │  config.mts
│  │  
│  └─theme
│          index.ts
│          style.css
│          
├─demo
│      test.md
│      
└─javascript
        basic.md

When I click the demo button in nav, there is no sidebar on the left side of the page

2024-04-07_04-08-07

jooy2 commented 5 months ago

Hello, thank you for using VitePress Sidebar.

Could you please change the resolvePath in your settings to page, which is the URL after the rewrite, and see if the issue persists?

before:

generateSidebar([
      {
        documentRootPath: "docs",
        scanStartPath: "demo",
        resolvePath: "/demo/",
      },
      {
        documentRootPath: "docs",
        scanStartPath: "javascript",
        resolvePath: "/javascript/",
      },
    ])

after:

generateSidebar([
      {
        documentRootPath: "docs",
        scanStartPath: "demo",
        resolvePath: "/page/",
      },
      {
        documentRootPath: "docs",
        scanStartPath: "javascript",
        resolvePath: "/javascript/",
      },
    ])

Regards,

ghost commented 5 months ago

Still wrong. When rewrites are not enabled, Click this button to display the demo/index.md file, and the sidebar of the left side is jump to normal

before

On this basis, open rewrites. Add the following in the config.ts file

rewrites: {
  "demo/test.md": "page/example.md",
},

Click the demo-index button to display the index.md file under the demo folder correctly. Click on the page button on the left to jump correctly to demo/page.html, and correctly display the content of the page.md file and the left side Sidebar. However, clicking the test button will jump to page/example.html. Although the content of the test.md file is correctly displayed, there is no sidebar on the left.

Click the demo-test button, the same is true.

1712465648222

According to the way you said, after modifying resolvepath to page, it is still wrong

satche commented 3 weeks ago

Hi! First, well done for this plugin @jooy2. It's amazing!

I have a similar issue that could be related to this problem.

I defined a rewrites rule to rename files with same name as folder into index.md

  rewrites: {
    '^(?<rest>.*(?=\\/))?\\/?(?<dir>.+?)\\/\\2\\.md$': ':rest*/:dir/index.md',
  },

It works well, but somehow mess with the Next/Prev links.

Here is a stackblitz that reproduces the problem: https://stackblitz.com/edit/vite-epbzzd?file=docs%2F.vitepress%2Fconfig.ts

What I noticed is everytime we get on an index file, navigation only propose the top-file of the sidebar as next link.

I thought it was the rewrites rules, but even if I apply page by page, it's still the same:

  rewrites: {
    'menu/menu.md': 'menu/index.md',
    'menu/fruits/fruits.md': 'menu/fruits/index.md',
    'menu/vegetables/vegetables.md': 'menu/vegetables/index.md',
  },

Let me know if you need more infos!

jooy2 commented 2 weeks ago

Hello! Thank you for your patience. I recently released a version that addresses the original issue and additional issues you left in the comments.

Here's the solution to @ghost's case:

  1. Upgrade the version of vitepress-sidebar to 1.25.0.
  2. Modify the following options:
        ...
        scanStartPath: 'demo',
        resolvePath: '/page/',
        basePath: '/demo/',
        ...

Specifying this option will satisfy the rewrite rule above, 'demo/test.md': 'page/example.md', to display the menu. However, this will display the menu, but the previous/next links will not be organized correctly.

To configure the previous/next links correctly, modify the rewrite option as follows:

  rewrites: {
    'demo/:page': 'page/:page'
  },

And modify the vitepress-sidebar' options to:

    ...
    scanStartPath: 'demo',
    resolvePath: '/page/',
    ...

For more information, see the following articles: https://vitepress-sidebar.jooy2.com/advanced-usage/multiple-sidebars-how-to

And @satche's case has been fixed with the PR you sent me.

If the issue persists, please open a new issue and I'll move this issue to complete.

Thanks!