feat: add `filePath` to `PageData` (#2140)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/2323/head
Christian Georgi 1 year ago committed by GitHub
parent d0f0012aea
commit b24acc6991
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,17 +16,17 @@ 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.
You can also put a pure function that accepts [`PageData`](./runtime-api#usedata) 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}`
pattern: ({ filePath }) => {
if (filePath.startsWith('packages/')) {
return `https://github.com/acme/monorepo/edit/main/${filePath}`
} else {
return `https://github.com/acme/monorepo/edit/main/docs/${relativePath}`
return `https://github.com/acme/monorepo/edit/main/docs/${filePath}`
}
}
}

@ -45,6 +45,7 @@ interface PageData {
titleTemplate?: string | boolean
description: string
relativePath: string
filePath: string,
headers: Header[]
frontmatter: Record<string, any>
params?: Record<string, any>

@ -6,12 +6,11 @@ export function useEditLink() {
return computed(() => {
const { text = 'Edit this page', pattern = '' } = theme.value.editLink || {}
const { relativePath } = page.value
let url: string
if (typeof pattern === 'function') {
url = pattern({ relativePath })
url = pattern(page.value)
} else {
url = pattern.replace(/:path/g, relativePath)
url = pattern.replace(/:path/g, page.value.filePath)
}
return { url, text }

@ -183,7 +183,8 @@ export async function createMarkdownToVueRenderFn(
frontmatter,
headers,
params,
relativePath
relativePath,
filePath: slash(path.relative(srcDir, fileOrig))
}
if (includeLastUpdatedData) {

@ -23,6 +23,7 @@ export const inBrowser = typeof document !== 'undefined'
export const notFoundPageData: PageData = {
relativePath: '',
filePath: '',
title: '404',
description: 'Not Found',
headers: [],

@ -1,5 +1,6 @@
import { DocSearchProps } from './docsearch.js'
import { LocalSearchTranslations } from './local-search.js'
import type { DocSearchProps } from './docsearch.js'
import type { LocalSearchTranslations } from './local-search.js'
import type { PageData } from './shared.js'
export namespace DefaultTheme {
export interface Config {
@ -215,8 +216,9 @@ export namespace DefaultTheme {
* Pattern for edit link.
*
* @example 'https://github.com/vuejs/vitepress/edit/main/docs/:path'
* @example ({ filePath }) => { ... }
*/
pattern: string | ((payload: { relativePath: string }) => string)
pattern: string | ((payload: PageData) => string)
/**
* Custom text for edit link.

1
types/shared.d.ts vendored

@ -6,6 +6,7 @@ export type Awaitable<T> = T | PromiseLike<T>
export interface PageData {
relativePath: string
filePath: string // differs from relativePath in case of path rewrites
title: string
titleTemplate?: string | boolean
description: string

Loading…
Cancel
Save