vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
11.47k stars 1.86k forks source link

auto sidebar mode #1297

Open didoee opened 1 year ago

didoee commented 1 year ago

Is your feature request related to a problem? Please describe.

建议新增自动提取md标题作为sidebar

Describe the solution you'd like

建议新增自动提取md标题作为sidebar

Describe alternatives you've considered

No response

Additional context

No response

Validations

Charles7c commented 1 year ago

任务优先级估计不会太高,可参考 我的配置

mingyuLi97 commented 1 year ago

很希望有这个功能!在这之前,我的做法是:

通过 脚本 维护一个 json 文件,在 config.ts 中读取以达到自动生成

Jimmylxue commented 1 year ago

这个自己写个脚本就好了其实

lizhen789 commented 1 year ago

顺序什么的,好像也不好弄

QC2168 commented 1 year ago

import * as fs from 'node:fs';
import { join } from 'node:path';

function getTitle(content: string): string | null {
  let mdTitle = content.match(/(?<=title:\s).+/gm);
  if (mdTitle === null) return 'mdTitle';
  return mdTitle[1];
}

function getTag(content: string, index?: number): string | string[] | null {
  let tags = content.match(/(?<=tags:\s\[).+?(?=\])/gm);
  if (tags === null) return tags;
  if (!index || index > tags.length) {
    return tags;
  }
  return tags[0].split(',')[index];
}

export function scanMdToCreateSidebarGroup(
  scope: string,
  path: string,
  pure = false,
  suffix: null | string
) {
  const result: any = [];
  const temp: any = [];
  let allFile = fs.readdirSync(path);
  for (const name of allFile) {
    let info = fs.statSync(join(path, name));
    if (info.isDirectory()) {
      result.push({
        text: name,
        items: scanMdToCreateSidebarGroup(join(scope, name)
          .replace('\\', '/'), join(path, name), true, suffix),
      });
    } else if (info.isFile() && /\.md$/.test(name)) {
      temp.push({
        text: name.slice(0, -3),
        link: suffix ? `/${scope}/${name.replace(/\.md/, suffix)}` : `/${scope}/${name}`,
      });
    }
  }
  return pure ? temp : result;
}

source code

QC2168 commented 1 year ago

我写了个侧边栏生成插件vite-plugin-vitepress-auto-sidebar,欢迎使用~

mingyuLi97 commented 1 year ago

很希望有这个功能!在这之前,我的做法是:

通过 脚本 维护一个 json 文件,在 config.ts 中读取以达到自动生成

更新:现在可以使用这个插件vitepress-plugin-auto-sidebar 来解决自动的问题 支持:

  1. 自动成 sidebar 配置,自动取文章标题
  2. 当我们修改文件时,可以自动同刷新页面,更新 sidebar
  3. 可以对生成的配置做二次操作(修改名称、排序等
digisomni commented 8 months ago

I think this should be an official "plugin" as it's the most common sidebar use of any standard user. My org uses Vue for all of our projects, however, we opt for Docusaurus purely for the out-of-the-box featureset (official means less risk of breakage happening under our feet!)

e.g. https://docusaurus.io/docs/sidebar/autogenerated

jooy2 commented 8 months ago

You can also consider the following solutions:

https://github.com/jooy2/vitepress-sidebar

It supports a lot of customization, alignment, frontmatter, and multiple sidebars.

cruvie commented 6 months ago

really needed

QC2168 commented 6 months ago

really needed

You can use it to help you vite-plugin-vitepress-auto-sidebar

liuliangsir commented 4 months ago

@cruvie You can auto sidebar with vitepress-auto-configure-nav-sidebar.

lsne commented 1 month ago

+1

lsne commented 1 month ago

希望可以实现 Hugo 中 book 主题的效果。 在 .md 文档中写 frontmatter 控制侧边栏标题位置,顺序以及隐藏

比如: --- title: 快速入门 # 在侧边栏显示的标题 weight: 1000 # 在本目录层级的权重值, 侧边栏该层的页面顺序按该值大小排序 bookCollapseSection: false # 是否折叠子层级的侧边栏目录 ---

Jackiexiao commented 3 days ago

非常非常推荐用这个插件, 我发现比另一个插件好用的多!

Really recommand this plugin! it's the best! https://github.com/jooy2/vitepress-sidebar?tab=readme-ov-file