config.mjs

import {defineConfig} from 'vitepress'
import {setSidebar} from "./utils/auto-sidebar.mjs";
import {viteCMSFront} from "./sider/cms-front.js"

// https://vitepress.dev/reference/site-config
// https://vitepress.dev/reference/default-theme-config
export default defineConfig({
    title: "资料库",
    description: "Mallray's description",
    lang: 'zh-CN',
    base: '/public/note', //默认站点目录
    lastUpdated: true,
    head: [["link", { rel: "icon", href: "/file/favicon.ico" }]],
    markdown: {
        lineNumbers: true
    },
    themeConfig: {
        logo: '/images/logo.svg',
        outline: [2, 6],
        outlineTitle: '章节导航',
        docFooter: {
            prev: '←上一篇',
            next: '下一篇→',
        },
        lastUpdatedText: '上次更新时间',
        //首页顶部导航
        nav: [
            {text: '首页', link: '/'},
            {
                text: '技术笔记',
                items: [
                    {text: '前端VUE3', link: '/doc/cms/front/'},
                    {text: '后台Golang', link: '/doc/cms/back/'}
                ]
            }
        ],
        //侧边框
        sidebar: {
            "/doc/cms/front": setSidebar("/doc/cms/front"),
            "/doc/cms/back": setSidebar("/doc/cms/back"),
            // "/doc/cms/front": viteCMSFront,
        },
        //主页图片导航
        socialLinks: [
            {icon: 'github', link: 'http://a.liziyu.com'}
        ],
        //页脚版权
        footer: {
            message: '',
            copyright: "liziyu.com"
        },
        // 设置搜索框的样式
        search: {
            provider: "local",
            options: {
                translations: {
                    button: {
                        buttonText: "搜索文档",
                        buttonAriaLabel: "搜索文档",
                    },
                    modal: {
                        noResultsText: "无法找到相关结果",
                        resetButtonTitle: "清除查询条件",
                        footer: {
                            selectText: "选择",
                            navigateText: "切换",
                        },
                    },
                },
            },
        },
    },
})

auto-siderbar.mjs

import path from "node:path";
import fs from "node:fs";

// 文件根目录
const DIR_PATH = path.resolve();
// 白名单,过滤不是文章的文件和文件夹
const WHITE_LIST = [
    "index.md",
    ".vitepress",
    "node_modules",
    ".idea",
    "assets",
];

// 判断是否是文件夹
const isDirectory = (path) => fs.lstatSync(path).isDirectory();

// 取差值
const intersections = (arr1, arr2) =>
    Array.from(new Set(arr1.filter((item) => !new Set(arr2).has(item))));

// 把方法导出直接使用
function getList(params, path1, pathname) {
    // 存放结果
    const res = [];
    // 开始遍历params
    for (let file in params) {
        // 拼接目录
        const dir = path.join(path1, params[file]);
        // 判断是否是文件夹
        const isDir = isDirectory(dir);
        if (isDir) {
            // 如果是文件夹,读取之后作为下一次递归参数
            const files = fs.readdirSync(dir);
            res.push({
                text: params[file],
                collapsible: true,
                items: getList(files, dir, `${pathname}/${params[file]}`),
            });
        } else {
            // 获取名字
            const name = path.basename(params[file]);
            // 排除非 md 文件
            const suffix = path.extname(params[file]);
            if (suffix !== ".md") {
                continue;
            }
            res.push({
                text: name,
                link: `${pathname}/${name}`,
            });
        }
    }
    // 对name做一下处理,把后缀删除
    res.map((item) => {
        item.text = item.text.replace(/\.md$/, "");
    });
    return res;
}

export const setSidebar = (pathname) => {
    // 获取pathname的路径
    const dirPath = path.join(DIR_PATH, pathname);
    // 读取pathname下的所有文件或者文件夹
    const files = fs.readdirSync(dirPath);
    // 过滤掉
    const items = intersections(files, WHITE_LIST);
    // getList 函数后面会讲到
    return getList(items, dirPath, pathname);
};

标签: none

添加新评论