Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: editLink can accept function #2058

Merged
merged 9 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/reference/default-theme-edit-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ export default {

The `pattern` option defines the URL structure for the link, and `:path` is going to be replaced with the page path.

You can also put a pure function that accepts `relativePath` as the argument and returns the URL string.

```js
export default {
themeConfig: {
editLink: {
pattern: ({ relativePath }) => {
if (relativePath.startsWith('packages/')) {
return `https://github.com/acme/monorepo/edit/main/${relativePath}`
} else {
return `https://github.com/acme/monorepo/edit/main/docs/${relativePath}`
}
}
}
}
}
```

It should not have side-effects nor access anything outside of its scope since it will be serialized and executed in the browser.

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.

```js
Expand Down
15 changes: 15 additions & 0 deletions src/client/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ export function pathToFile(path: string): string {
return pagePath
}

export function deserializeFunctions(value: any): any {
if (Array.isArray(value)) {
return value.map(deserializeFunctions)
} else if (typeof value === 'object' && value !== null) {
return Object.keys(value).reduce((acc, key) => {
acc[key] = deserializeFunctions(value[key])
return acc
}, {} as any)
} else if (typeof value === 'string' && value.startsWith('_vp-fn_')) {
return new Function(`return ${value.slice(7)}`)()
} else {
return value
}
}

export let contentUpdatedCallbacks: (() => any)[] = []

/**
Expand Down
21 changes: 14 additions & 7 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@
// so the user can do `import { useRoute, useSiteData } from 'vitepress'`

// generic types
export type { Router, Route } from './app/router.js'
export type { VitePressData } from './app/data.js'
export type { Route, Router } from './app/router.js'

// theme types
export type { Theme, EnhanceAppContext } from './app/theme.js'
export type { EnhanceAppContext, Theme } from './app/theme.js'

// shared types
export type {
PageData,
SiteData,
HeadConfig,
Header
Header,
PageData,
SiteData
} from '../../types/shared.js'

// composables
export { useData } from './app/data.js'
export { useRouter, useRoute } from './app/router.js'
export { useRoute, useRouter } from './app/router.js'

// utilities
export { inBrowser, withBase, onContentUpdated } from './app/utils.js'
export {
deserializeFunctions,
inBrowser,
onContentUpdated,
withBase
} from './app/utils.js'

// components
export { Content } from './app/components/Content.js'
7 changes: 6 additions & 1 deletion src/client/theme-default/composables/edit-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ export function useEditLink() {
return computed(() => {
const { text = 'Edit this page', pattern = '' } = theme.value.editLink || {}
const { relativePath } = page.value
const url = pattern.replace(/:path/g, relativePath)
let url: string
if (typeof pattern === 'function') {
url = pattern({ relativePath })
} else {
url = pattern.replace(/:path/g, relativePath)
}

return { url, text }
})
Expand Down
7 changes: 5 additions & 2 deletions src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { staticDataPlugin } from './plugins/staticDataPlugin'
import { webFontsPlugin } from './plugins/webFontsPlugin'
import { dynamicRoutesPlugin } from './plugins/dynamicRoutesPlugin'
import { rewritesPlugin } from './plugins/rewritesPlugin'
import { serializeFunctions } from './utils/fnSerialize.js'

declare module 'vite' {
interface UserConfig {
Expand Down Expand Up @@ -158,9 +159,11 @@ export async function createVitePressPlugin(
if (config.command === 'build') {
data = { ...siteData, head: [] }
}
return `export default JSON.parse(${JSON.stringify(
data = serializeFunctions(data)
return `import { deserializeFunctions } from 'vitepress/client'
export default deserializeFunctions(JSON.parse(${JSON.stringify(
JSON.stringify(data)
)})`
)}))`
}
},

Expand Down
14 changes: 14 additions & 0 deletions src/node/utils/fnSerialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function serializeFunctions(value: any): any {
if (Array.isArray(value)) {
return value.map(serializeFunctions)
} else if (typeof value === 'object' && value !== null) {
return Object.keys(value).reduce((acc, key) => {
acc[key] = serializeFunctions(value[key])
return acc
}, {} as any)
} else if (typeof value === 'function') {
return `_vp-fn_${value.toString()}`
} else {
return value
}
}
2 changes: 1 addition & 1 deletion types/default-theme.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export namespace DefaultTheme {
*
* @example 'https://github.com/vuejs/vitepress/edit/main/docs/:path'
*/
pattern: string
pattern: string | ((payload: { relativePath: string }) => string)

/**
* Custom text for edit link.
Expand Down