Skip to content

Commit 8436472

Browse files
authoredOct 12, 2024··
feat(experimental): support passing function for rewrites (#4274)
1 parent 92b92ae commit 8436472

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed
 

‎docs/en/guide/routing.md

+29-15
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,24 @@ You can customize the mapping between the source directory structure and the gen
156156

157157
```
158158
.
159-
─ packages
160-
├─ pkg-a
161-
│ └─ src
162-
├─ pkg-a-code.ts
163-
└─ pkg-a-docs.md
164-
└─ pkg-b
165-
└─ src
166-
├─ pkg-b-code.ts
167-
└─ pkg-b-docs.md
159+
─ packages
160+
├─ pkg-a
161+
│ └─ src
162+
│ ├─ foo.md
163+
│ └─ index.md
164+
└─ pkg-b
165+
└─ src
166+
├─ bar.md
167+
└─ index.md
168168
```
169169

170170
And you want the VitePress pages to be generated like this:
171171

172172
```
173-
packages/pkg-a/src/pkg-a-docs.md --> /pkg-a/index.html
174-
packages/pkg-b/src/pkg-b-docs.md --> /pkg-b/index.html
173+
packages/pkg-a/src/index.md --> /pkg-a/index.html
174+
packages/pkg-a/src/foo.md --> /pkg-a/foo.html
175+
packages/pkg-b/src/index.md --> /pkg-b/index.html
176+
packages/pkg-b/src/bar.md --> /pkg-b/bar.html
175177
```
176178

177179
You can achieve this by configuring the [`rewrites`](../reference/site-config#rewrites) option like this:
@@ -180,8 +182,10 @@ You can achieve this by configuring the [`rewrites`](../reference/site-config#re
180182
// .vitepress/config.js
181183
export default {
182184
rewrites: {
183-
'packages/pkg-a/src/pkg-a-docs.md': 'pkg-a/index.md',
184-
'packages/pkg-b/src/pkg-b-docs.md': 'pkg-b/index.md'
185+
'packages/pkg-a/src/index.md': 'pkg-a/index.md',
186+
'packages/pkg-a/src/foo.md': 'pkg-a/foo.md',
187+
'packages/pkg-b/src/index.md': 'pkg-b/index.md',
188+
'packages/pkg-b/src/bar.md': 'pkg-b/bar.md'
185189
}
186190
}
187191
```
@@ -191,12 +195,22 @@ The `rewrites` option also supports dynamic route parameters. In the above examp
191195
```ts
192196
export default {
193197
rewrites: {
194-
'packages/:pkg/src/(.*)': ':pkg/index.md'
198+
'packages/:pkg/src/:slug*': ':pkg/:slug*'
195199
}
196200
}
197201
```
198202

199-
The rewrite paths are compiled using the `path-to-regexp` package - consult [its documentation](https://github.com/pillarjs/path-to-regexp#parameters) for more advanced syntax.
203+
The rewrite paths are compiled using the `path-to-regexp` package - consult [its documentation](https://github.com/pillarjs/path-to-regexp/tree/6.x#parameters) for more advanced syntax.
204+
205+
`rewrites` can also be a function that receives the original path and returns the new path:
206+
207+
```ts
208+
export default {
209+
rewrites(id) {
210+
return id.replace(/^packages\/([^/]+)\/src\//, '$1/')
211+
}
212+
}
213+
```
200214

201215
::: warning Relative Links with Rewrites
202216

‎src/node/plugins/rewritesPlugin.ts

+26-13
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,35 @@ export function resolveRewrites(
66
pages: string[],
77
userRewrites: UserConfig['rewrites']
88
) {
9-
const rewriteRules = Object.entries(userRewrites || {}).map(([from, to]) => ({
10-
toPath: compile(`/${to}`, { validate: false }),
11-
matchUrl: match(from.startsWith('^') ? new RegExp(from) : from)
12-
}))
13-
149
const pageToRewrite: Record<string, string> = {}
1510
const rewriteToPage: Record<string, string> = {}
16-
if (rewriteRules.length) {
11+
12+
if (typeof userRewrites === 'function') {
1713
for (const page of pages) {
18-
for (const { matchUrl, toPath } of rewriteRules) {
19-
const res = matchUrl(page)
20-
if (res) {
21-
const dest = toPath(res.params).slice(1)
22-
pageToRewrite[page] = dest
23-
rewriteToPage[dest] = page
24-
break
14+
const dest = userRewrites(page)
15+
if (dest && dest !== page) {
16+
pageToRewrite[page] = dest
17+
rewriteToPage[dest] = page
18+
}
19+
}
20+
} else if (typeof userRewrites === 'object') {
21+
const rewriteRules = Object.entries(userRewrites || {}).map(
22+
([from, to]) => ({
23+
toPath: compile(`/${to}`, { validate: false }),
24+
matchUrl: match(from.startsWith('^') ? new RegExp(from) : from)
25+
})
26+
)
27+
28+
if (rewriteRules.length) {
29+
for (const page of pages) {
30+
for (const { matchUrl, toPath } of rewriteRules) {
31+
const res = matchUrl(page)
32+
if (res) {
33+
const dest = toPath(res.params).slice(1)
34+
pageToRewrite[page] = dest
35+
rewriteToPage[dest] = page
36+
break
37+
}
2538
}
2639
}
2740
}

‎src/node/siteConfig.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export interface UserConfig<ThemeConfig = any>
162162
*
163163
* source -> destination
164164
*/
165-
rewrites?: Record<string, string>
165+
rewrites?: Record<string, string> | ((id: string) => string)
166166

167167
/**
168168
* @experimental

0 commit comments

Comments
 (0)
Please sign in to comment.