Skip to content

Commit 36f4897

Browse files
author
ChandlerVer5
committed
fix: 🐛 修复 windows 下 autoSiderBar 插件的路径生成,以及 搜索插件的链接问题
1 parent ce4722f commit 36f4897

File tree

8 files changed

+353
-178
lines changed

8 files changed

+353
-178
lines changed

.vitepress/config.ts renamed to .vitepress/config.mts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import { defineConfig } from "vitepress";
22
import { SearchPlugin } from "@ver5/vitepress-plugin-search";
33

44
import markIt from "markdown-it-mark";
5-
import { REPO_URL } from "./const";
5+
import { REPO_URL, OUT_DIR, BASE_NAME, SRC_DOC } from "./const";
66
import nav from "./nav";
7-
import { books, sidebar, SRC_DOC } from "./sidebar";
7+
import { books, sidebar } from "./sidebar";
88
import { linkPlugin } from "./plugins/linkPlugin";
99
import { rustCodePlugin } from "./plugins/rustcode/runCodePlugin";
1010
import { imageSizePlugin } from "./plugins/imagePlugin";
1111
import { alignPlugin } from "./plugins/alignPlugin";
1212

13-
const REPO_NAME_BASE = "/rust_study";
14-
const OUT_DIR = "dist";
1513

1614
// https://vitepress.dev/reference/site-config
1715
export default defineConfig({
@@ -24,7 +22,7 @@ export default defineConfig({
2422
],
2523
srcDir: SRC_DOC,
2624
outDir: OUT_DIR,
27-
base: REPO_NAME_BASE,
25+
base: BASE_NAME,
2826
vite: {
2927
plugins: [
3028
SearchPlugin({
@@ -34,10 +32,10 @@ export default defineConfig({
3432
allow: [],
3533
ignore: [],
3634
tokenize: "forward",
35+
separator: ' ', // fix search result link problems
3736
}),
3837
],
3938
},
40-
4139
lastUpdated: true,
4240
cleanUrls: false,
4341
markdown: {

.vitepress/const.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
// 定义常量
1+
/** 定义常量 */
2+
3+
export const BASE_NAME = "/rust_study";
4+
export const OUT_DIR = "dist";
5+
export const SRC_DOC = "docs";
6+
27
export const REPO_URL = 'https://github.com/ChandlerVer5/rust_study/'

.vitepress/plugins/autoSidebar.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
//
44
import merge from 'lodash/merge';
55
import sortBy from 'lodash/sortBy';
6-
import remove from 'lodash/remove';
76
import { globbySync } from '@cjs-exporter/globby';
8-
import { sep, basename, join } from 'node:path';
7+
import { basename, resolve, join } from 'node:path';
98

109
type Sidebar = SidebarGroup[] | SidebarItem;
1110

11+
const sep = '/';
12+
1213
interface SidebarItem {
1314
text: string
1415
link: string
@@ -58,24 +59,23 @@ const getDirName = (path: string) => {
5859
};
5960

6061
// Load all MD files in a specified directory
61-
const getChildren = function(parentPath: string, options: Options) {
62+
const getChildren = function (parentPath: string, options: Options) {
6263
const { indexLink, sortBy: sortFn, ignoreMDFiles, hierarchy } = options
63-
const pattern = '/**/*.md';
64-
const files = globbySync(join(parentPath, pattern)).map((path) => {
64+
const pattern = join('**', '*.md');
65+
const files = globbySync(join(parentPath, pattern).replace(/\\/g, sep)).map((path) => {
6566
// fix parentPath relative dir
66-
const newPath = path.slice((new RegExp(`.*?${sep}`)).exec(path)![0].length, -3);
67+
const newPath = path.slice(((/.*?\//).exec(path))?.[0].length, -3);
6768

6869
// ignore some files
69-
if (ignoreMDFiles?.length && ignoreMDFiles.some((ifile) => newPath.endsWith(ifile))) {
70-
return 0;
70+
if (ignoreMDFiles?.length && ignoreMDFiles.some((f) => newPath.endsWith(f))) {
71+
return { path: '' };
7172
}
72-
if (hierarchy && indexLink === basename(newPath)) return 0;
73+
if (hierarchy && indexLink === basename(newPath)) return { path: '' };
7374
return { path: newPath };
74-
});
75+
}).filter(file => !!file.path);
7576

76-
remove(files, file => file === 0);
7777
// Return the ordered list of files, sort by Options.sortBy or 'path'
78-
return sortBy(files, sortFn ? (f) => sortFn(f!.path) : ['path']).map(file => file?.path || '');
78+
return sortBy(files, sortFn ? (f) => sortFn(f.path) : ['path']).map(file => file.path || '');
7979
};
8080

8181
/**
@@ -84,7 +84,7 @@ const getChildren = function(parentPath: string, options: Options) {
8484
ss.dev/reference/default-theme-sidebar#the-basics
8585
*/
8686
function normalize(path: string) {
87-
return sep + path + '.md'
87+
return (sep + path + '.md')
8888
}
8989

9090
// Return sidebar config for given baseDir.

.vitepress/plugins/linkPlugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type MarkdownIt from 'markdown-it'
22
import { normalizeLink } from './utils'
3+
import { BASE_NAME } from '../const'
34

45
export interface Options {
56
books: any[]
67
}
78

8-
99
const link_rules = {
1010
"RFC 文档|RFC 项目": "https://rust-lang.github.io/rfcs/",
1111
"RFC 1216": 'https://rust-lang.github.io/rfcs/1216-bang-type.html',
@@ -18,8 +18,9 @@ const getRegRules = (text: string) => [
1818
]
1919

2020
function toLinkUrl(raw: string, rule: string, link: string) {
21-
return raw.replace(new RegExp(rule, 'g'), (matchText) => `<a href=${normalizeLink(link)}>${matchText}</a>`)
21+
return raw.replace(new RegExp(rule, 'g'), (matchText) => `<a href=${normalizeLink(BASE_NAME + link)}>${matchText}</a>`)
2222
}
23+
2324
/**
2425
* 将对应的部分、章节进行链接,方便快速跳转。
2526
*/
@@ -42,6 +43,7 @@ export function linkPlugin(md: MarkdownIt, options: Options) {
4243
})
4344
}
4445

46+
// 自定义规则匹配
4547
Object.keys(link_rules).forEach((ruleKey) => {
4648
rawText = toLinkUrl(rawText, ruleKey, link_rules[ruleKey])
4749
})

.vitepress/sidebar.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { join } from 'node:path'
22
import { getSideBar } from './plugins/autoSidebar'
3-
4-
export const SRC_DOC = "docs";
3+
import { SRC_DOC } from './const'
54

65
export const notes = getSideBar(join(SRC_DOC, 'notes'), {
76
hierarchy: false,
@@ -14,7 +13,7 @@ export const codes = getSideBar(join(SRC_DOC, 'codes'), {
1413
})
1514

1615
export const books = getSideBar(SRC_DOC, {
17-
ignoreDirs: ['notes', 'codes'],
16+
ignoreDirs: ['notes', 'codes', 'images', 'public'],
1817
indexLink: 'index',
1918
ignoreMDFiles: ['index'],
2019
sortBy: (path) => {

docs/第 5 章 trait/5.7 trait 别名.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
跟 type alias 类似的,trait 也可以起别名(trait alias)。假如在某些场景下,我们有一个比较复杂的 trait:
44

5-
---
6-
75
```rust
86
pub trait Service {
97
type Request;
@@ -14,16 +12,10 @@ pub trait Service {
1412
}
1513
```
1614

17-
---
18-
1915
每次使用这个 trait 的时候都需要携带一堆的关联类型参数。为了避免这样的麻烦,在已经确定了关联类型的场景下,我们可以为它取一个别名,比如:
2016

21-
---
22-
2317
```rust
2418
trait HttpService = Service<Request = http::Request,
2519
Response = http::Response,
2620
Error = http::Error>;
2721
```
28-
29-
---

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"markdown-it-mark": "^3.0.1",
2323
"sass": "^1.62.1",
2424
"tslib": "^2.5.2",
25-
"vitepress": "1.0.0-beta.1",
26-
"vitepress-plugin-search": "1.0.4-alpha.20",
25+
"vite": "^5.2.6",
26+
"vitepress": "^1.0.1",
2727
"vue": "^3.3.4"
2828
}
29-
}
29+
}

0 commit comments

Comments
 (0)