Commit 192708d 1 parent 90e924a commit 192708d Copy full SHA for 192708d
File tree 7 files changed +75
-11
lines changed
theme-default/composables
7 files changed +75
-11
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,26 @@ export default {
14
14
15
15
The ` pattern ` option defines the URL structure for the link, and ` :path ` is going to be replaced with the page path.
16
16
17
+ You can also put a pure function that accepts ` relativePath ` as the argument and returns the URL string.
18
+
19
+ ``` js
20
+ export default {
21
+ themeConfig: {
22
+ editLink: {
23
+ pattern : ({ relativePath }) => {
24
+ if (relativePath .startsWith (' packages/' )) {
25
+ return ` https://github.com/acme/monorepo/edit/main/${ relativePath} `
26
+ } else {
27
+ return ` https://github.com/acme/monorepo/edit/main/docs/${ relativePath} `
28
+ }
29
+ }
30
+ }
31
+ }
32
+ }
33
+ ```
34
+
35
+ It should not have side-effects nor access anything outside of its scope since it will be serialized and executed in the browser.
36
+
17
37
By default, this will add the link text "Edit this page" at the bottom of the doc page. You may customize this text by defining the ` text ` option.
18
38
19
39
``` js
Original file line number Diff line number Diff line change @@ -58,6 +58,21 @@ export function pathToFile(path: string): string {
58
58
return pagePath
59
59
}
60
60
61
+ export function deserializeFunctions ( value : any ) : any {
62
+ if ( Array . isArray ( value ) ) {
63
+ return value . map ( deserializeFunctions )
64
+ } else if ( typeof value === 'object' && value !== null ) {
65
+ return Object . keys ( value ) . reduce ( ( acc , key ) => {
66
+ acc [ key ] = deserializeFunctions ( value [ key ] )
67
+ return acc
68
+ } , { } as any )
69
+ } else if ( typeof value === 'string' && value . startsWith ( '_vp-fn_' ) ) {
70
+ return new Function ( `return ${ value . slice ( 7 ) } ` ) ( )
71
+ } else {
72
+ return value
73
+ }
74
+ }
75
+
61
76
export let contentUpdatedCallbacks : ( ( ) => any ) [ ] = [ ]
62
77
63
78
/**
Original file line number Diff line number Diff line change 2
2
// so the user can do `import { useRoute, useSiteData } from 'vitepress'`
3
3
4
4
// generic types
5
- export type { Router , Route } from './app/router.js'
6
5
export type { VitePressData } from './app/data.js'
6
+ export type { Route , Router } from './app/router.js'
7
+
7
8
// theme types
8
- export type { Theme , EnhanceAppContext } from './app/theme.js'
9
+ export type { EnhanceAppContext , Theme } from './app/theme.js'
10
+
9
11
// shared types
10
12
export type {
11
- PageData ,
12
- SiteData ,
13
13
HeadConfig ,
14
- Header
14
+ Header ,
15
+ PageData ,
16
+ SiteData
15
17
} from '../../types/shared.js'
16
18
17
19
// composables
18
20
export { useData } from './app/data.js'
19
- export { useRouter , useRoute } from './app/router.js'
21
+ export { useRoute , useRouter } from './app/router.js'
20
22
21
23
// utilities
22
- export { inBrowser , withBase , onContentUpdated } from './app/utils.js'
24
+ export {
25
+ deserializeFunctions ,
26
+ inBrowser ,
27
+ onContentUpdated ,
28
+ withBase
29
+ } from './app/utils.js'
23
30
24
31
// components
25
32
export { Content } from './app/components/Content.js'
Original file line number Diff line number Diff line change @@ -7,7 +7,12 @@ export function useEditLink() {
7
7
return computed ( ( ) => {
8
8
const { text = 'Edit this page' , pattern = '' } = theme . value . editLink || { }
9
9
const { relativePath } = page . value
10
- const url = pattern . replace ( / : p a t h / g, relativePath )
10
+ let url : string
11
+ if ( typeof pattern === 'function' ) {
12
+ url = pattern ( { relativePath } )
13
+ } else {
14
+ url = pattern . replace ( / : p a t h / g, relativePath )
15
+ }
11
16
12
17
return { url, text }
13
18
} )
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ import { staticDataPlugin } from './plugins/staticDataPlugin'
22
22
import { webFontsPlugin } from './plugins/webFontsPlugin'
23
23
import { dynamicRoutesPlugin } from './plugins/dynamicRoutesPlugin'
24
24
import { rewritesPlugin } from './plugins/rewritesPlugin'
25
+ import { serializeFunctions } from './utils/fnSerialize.js'
25
26
26
27
declare module 'vite' {
27
28
interface UserConfig {
@@ -158,9 +159,11 @@ export async function createVitePressPlugin(
158
159
if ( config . command === 'build' ) {
159
160
data = { ...siteData , head : [ ] }
160
161
}
161
- return `export default JSON.parse(${ JSON . stringify (
162
+ data = serializeFunctions ( data )
163
+ return `import { deserializeFunctions } from 'vitepress/client'
164
+ export default deserializeFunctions(JSON.parse(${ JSON . stringify (
162
165
JSON . stringify ( data )
163
- ) } )`
166
+ ) } )) `
164
167
}
165
168
} ,
166
169
Original file line number Diff line number Diff line change
1
+ export function serializeFunctions ( value : any ) : any {
2
+ if ( Array . isArray ( value ) ) {
3
+ return value . map ( serializeFunctions )
4
+ } else if ( typeof value === 'object' && value !== null ) {
5
+ return Object . keys ( value ) . reduce ( ( acc , key ) => {
6
+ acc [ key ] = serializeFunctions ( value [ key ] )
7
+ return acc
8
+ } , { } as any )
9
+ } else if ( typeof value === 'function' ) {
10
+ return `_vp-fn_${ value . toString ( ) } `
11
+ } else {
12
+ return value
13
+ }
14
+ }
Original file line number Diff line number Diff line change @@ -209,7 +209,7 @@ export namespace DefaultTheme {
209
209
*
210
210
* @example 'https://github.com/vuejs/vitepress/edit/main/docs/:path'
211
211
*/
212
- pattern : string
212
+ pattern : string | ( ( payload : { relativePath : string } ) => string )
213
213
214
214
/**
215
215
* Custom text for edit link.
You can’t perform that action at this time.
0 commit comments