Skip to content

Commit

Permalink
feat: add filePath to PageData (#2140)
Browse files Browse the repository at this point in the history
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
  • Loading branch information
chgeo and brc-dd committed Apr 30, 2023
1 parent d0f0012 commit b24acc6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 12 deletions.
10 changes: 5 additions & 5 deletions docs/reference/default-theme-edit-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
5 changes: 2 additions & 3 deletions src/client/theme-default/composables/edit-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ export async function createMarkdownToVueRenderFn(
frontmatter,
headers,
params,
relativePath
relativePath,
filePath: slash(path.relative(srcDir, fileOrig))
}

if (includeLastUpdatedData) {
Expand Down
1 change: 1 addition & 0 deletions src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const inBrowser = typeof document !== 'undefined'

export const notFoundPageData: PageData = {
relativePath: '',
filePath: '',
title: '404',
description: 'Not Found',
headers: [],
Expand Down
8 changes: 5 additions & 3 deletions types/default-theme.d.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b24acc6

Please sign in to comment.