import { readFileSync, readdirSync } from 'node:fs' import { resolve } from 'node:path' import { defineConfig, type DefaultTheme } from 'vitepress' import { withMermaid } from 'vitepress-plugin-mermaid' import './theme-config.d.ts' import { repoVersionLabel } from './version' const root = resolve(import.meta.dirname, '..') const sections: { dir: string; label: string }[] = [ { dir: '01-getting-started', label: '快速开始' }, { dir: '02-architecture', label: '架构' }, { dir: '03-features', label: '功能模块' }, { dir: '04-api', label: 'API 参考' }, { dir: '05-clients', label: '客户端' }, { dir: '06-development', label: '开发指南' }, ] /** 侧边栏条目文字:取正文一级标题,去掉冗余前后缀 */ function itemText(dir: string, file: string): string { const raw = readFileSync(resolve(root, dir, file), 'utf-8') const heading = raw.match(/^#\s+(.+)$/m)?.[1] ?? file.replace(/\.md$/, '') return heading .replace(/^API 参考[::]\s*/, '') .replace(/\s*[((][^(())]*[))]\s*/g, ' ') .replace(/\s{2,}/g, ' ') .trim() } function itemsOf(dir: string): DefaultTheme.SidebarItem[] { return readdirSync(resolve(root, dir)) .filter((f) => f.endsWith('.md')) .sort() .map((f) => ({ text: itemText(dir, f), link: `/${dir}/${f.replace(/\.md$/, '')}`, })) } const sidebar: DefaultTheme.SidebarItem[] = sections.map((s) => ({ text: s.label, collapsed: false, items: itemsOf(s.dir), })) /** 本地搜索默认按空白分词,中文整段会被当作一个词,这里退化为字粒度切分 */ function tokenize(text: string): string[] { const tokens: string[] = [] for (const part of text.split(/[\s\n\r#%*,=/:;?[\]{}()&+\-!'"$·、,。:;?!()【】《》…—]+/)) { if (!part) continue if (/[\u4e00-\u9fa5]/.test(part)) { tokens.push(part, ...part.split('')) } else { tokens.push(part) } } return tokens } const repo = 'https://github.com/Tencent/WeKnora' const site = 'https://weknora.weixin.qq.com' export default withMermaid( defineConfig({ title: 'WeKnora', titleTemplate: ':title · WeKnora 文档', description: 'WeKnora(维娜拉)官方文档:部署、配置、功能说明、API 参考与二次开发', lang: 'zh-CN', base: '/docs/', cleanUrls: true, lastUpdated: true, srcExclude: ['README.md'], metaChunk: true, head: [ ['link', { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' }], ['meta', { name: 'theme-color', content: '#101f38' }], ['meta', { property: 'og:type', content: 'website' }], ['meta', { property: 'og:title', content: 'WeKnora 文档' }], [ 'meta', { property: 'og:description', content: '把 PDF、Word、网页与飞书 / Notion / 语雀的资料收进知识库,做成答案带出处的问答系统', }, ], ], markdown: { theme: { light: 'github-light', dark: 'github-dark' }, lineNumbers: false, toc: { level: [2, 3] }, config(md) { // 表格外面包一层滚动容器。默认主题把