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. 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 ```js
export default { export default {
themeConfig: { themeConfig: {
editLink: { editLink: {
pattern: ({ relativePath }) => { pattern: ({ filePath }) => {
if (relativePath.startsWith('packages/')) { if (filePath.startsWith('packages/')) {
return `https://github.com/acme/monorepo/edit/main/${relativePath}` return `https://github.com/acme/monorepo/edit/main/${filePath}`
} else { } 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 titleTemplate?: string | boolean
description: string description: string
relativePath: string relativePath: string
filePath: string,
headers: Header[] headers: Header[]
frontmatter: Record<string, any> frontmatter: Record<string, any>
params?: Record<string, any> params?: Record<string, any>

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

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

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

@ -1,5 +1,6 @@
import { DocSearchProps } from './docsearch.js' import type { DocSearchProps } from './docsearch.js'
import { LocalSearchTranslations } from './local-search.js' import type { LocalSearchTranslations } from './local-search.js'
import type { PageData } from './shared.js'
export namespace DefaultTheme { export namespace DefaultTheme {
export interface Config { export interface Config {
@ -215,8 +216,9 @@ export namespace DefaultTheme {
* Pattern for edit link. * Pattern for edit link.
* *
* @example 'https://github.com/vuejs/vitepress/edit/main/docs/:path' * @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. * 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 { export interface PageData {
relativePath: string relativePath: string
filePath: string // differs from relativePath in case of path rewrites
title: string title: string
titleTemplate?: string | boolean titleTemplate?: string | boolean
description: string description: string

Loading…
Cancel
Save