Skip to content

Commit

Permalink
Merge branch 'main' into theme-default-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo authored Jun 5, 2024
2 parents adf125e + 0a29075 commit 4c6f5c4
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 65 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [2.0.0-rc.35](https://github.com/vuepress/ecosystem/compare/v2.0.0-rc.34...v2.0.0-rc.35) (2024-06-05)

### Bug Fixes

- **theme-default:** fix client config file import ([e5dcba2](https://github.com/vuepress/ecosystem/commit/e5dcba2c1ad03ef3083fcfc3bcc8d0105793f270))

### Features

- **theme-default:** improve prev/next links config and auto detection ([#195](https://github.com/vuepress/ecosystem/issues/195)) ([e50f8ca](https://github.com/vuepress/ecosystem/commit/e50f8ca8a9bf1e7e39ba86bec8ca0f9270ecf432))

# [2.0.0-rc.34](https://github.com/vuepress/ecosystem/compare/v2.0.0-rc.33...v2.0.0-rc.34) (2024-06-04)

### Bug Fixes
Expand Down
5 changes: 3 additions & 2 deletions docs/themes/default/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ Frontmatter in this section will only take effect in normal pages.

### prev

- Type: `AutoLinkConfig | string`
- Type: `AutoLinkConfig | string | false`

- Details:

Expand All @@ -324,6 +324,7 @@ Frontmatter in this section will only take effect in normal pages.

- A `AutoLinkConfig` object should have a `text` field and a `link` field.
- A string should be the path to the target page file. It will be converted to a `AutoLinkConfig` object, whose `text` is the page title, and `link` is the page route path.
- Set to `false` to disable the prev link.

- Example:

Expand All @@ -349,7 +350,7 @@ prev: ../../guide/getting-started.md

### next

- Type: `AutoLinkConfig | string`
- Type: `AutoLinkConfig | string | false`

- Details:

Expand Down
8 changes: 6 additions & 2 deletions docs/themes/default/locale.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,22 @@ export default {

## prev

- Type: `string`
- Type: `string | false`

- Default: `'Prev'`

- Details:

Text for the previous page navigation button.

Set to `false` to disable the previous page navigation button.

## next

- Type: `string`
- Type: `string | false`
- Default: `'Next'`
- Details:

Text for the next page navigation button.

Set to `false` to disable the next page navigation button.
5 changes: 3 additions & 2 deletions docs/zh/themes/default/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ features:

### prev

- 类型: `AutoLinkConfig | string`
- 类型: `AutoLinkConfig | string | false`

- 详情:

Expand All @@ -324,6 +324,7 @@ features:

- `AutoLinkConfig` 对象应该有一个 `text` 字段和一个 `link` 字段。
- 字符串应为目标页面文件的路径。它将会被转换为 `AutoLinkConfig` 对象,将页面标题作为 `text` ,将页面路由路径作为 `link`
- 如果你不想显示上一个页面的链接,可以将其设置为 `false`

- 示例:

Expand All @@ -349,7 +350,7 @@ prev: ../../guide/getting-started.md

### next

- 类型: `AutoLinkConfig | string`
- 类型: `AutoLinkConfig | string | false`

- 详情:

Expand Down
8 changes: 4 additions & 4 deletions docs/zh/themes/default/locale.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,16 @@ export default {

## prev

- 类型: `string`
- 类型: `string | false`
- 默认值: `'Prev'`
- 详情:

上一页按钮的文字。
上一页按钮的文字。设置为 `false` 时,将隐藏上一页按钮。

## next

- 类型: `string`
- 类型: `string | false`
- 默认值: `'Next'`
- 详情:

下一页按钮的文字。
下一页按钮的文字。设置为 `false` 时,将隐藏下一页按钮。
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"npmClient": "pnpm",
"version": "2.0.0-rc.34",
"version": "2.0.0-rc.35",
"command": {
"version": {
"allowBranch": "main",
Expand Down
10 changes: 10 additions & 0 deletions themes/theme-default/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [2.0.0-rc.35](https://github.com/vuepress/ecosystem/compare/v2.0.0-rc.34...v2.0.0-rc.35) (2024-06-05)

### Bug Fixes

- **theme-default:** fix client config file import ([e5dcba2](https://github.com/vuepress/ecosystem/commit/e5dcba2c1ad03ef3083fcfc3bcc8d0105793f270))

### Features

- **theme-default:** improve prev/next links config and auto detection ([#195](https://github.com/vuepress/ecosystem/issues/195)) ([e50f8ca](https://github.com/vuepress/ecosystem/commit/e50f8ca8a9bf1e7e39ba86bec8ca0f9270ecf432))

# [2.0.0-rc.34](https://github.com/vuepress/ecosystem/compare/v2.0.0-rc.33...v2.0.0-rc.34) (2024-06-04)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion themes/theme-default/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vuepress/theme-default",
"version": "2.0.0-rc.34",
"version": "2.0.0-rc.35",
"description": "Default theme of VuePress",
"keywords": [
"vuepress-theme",
Expand Down
111 changes: 67 additions & 44 deletions themes/theme-default/src/client/composables/useRelatedLinks.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
import { useSidebarItems } from '@theme/useSidebarItems'
import { useThemeLocaleData } from '@theme/useThemeData'
import { computed } from 'vue'
import type { ComputedRef } from 'vue'
import { resolveRoute, usePageFrontmatter, useRoute } from 'vuepress/client'
import type { AutoLinkConfig } from 'vuepress/client'
import { isPlainObject, isString } from 'vuepress/shared'
import type { DefaultThemeNormalPageFrontmatter } from '../../shared/index.js'
import type {
AutoLinkOptions,
DefaultThemeNormalPageFrontmatter,
} from '../../shared/index.js'
import type { SidebarItem } from '../typings.js'
import { getAutoLink } from '../utils/index.js'

/**
* Resolve `prev` or `next` config from frontmatter
*/
const resolveFromFrontmatterConfig = (
conf: unknown,
current: string,
): null | false | AutoLinkConfig => {
if (conf === false) {
return null
config: unknown,
currentPath: string,
): null | false | AutoLinkOptions => {
if (config === false) {
return false
}

if (isString(conf)) {
const { notFound, meta, path } = resolveRoute<{
title?: string
}>(conf, current)

return notFound
? { text: path, link: path }
: {
text: meta.title || path,
link: path,
}
if (isString(config)) {
return getAutoLink(config, currentPath)
}

if (isPlainObject<AutoLinkConfig>(conf)) {
if (isPlainObject<AutoLinkOptions>(config)) {
return {
...conf,
link: resolveRoute(conf.link, current).path,
...config,
link: getAutoLink(config.link, currentPath).link,
}
}

return false
return null
}

/**
* Resolve `prev` or `next` config from sidebar items
*/
const resolveFromSidebarItems = (
sidebarItems: SidebarItem[],
currentPath: string,
offset: number,
): null | AutoLinkConfig => {
const index = sidebarItems.findIndex((item) => item.link === currentPath)
if (index !== -1) {
const targetItem = sidebarItems[index + offset]
): null | AutoLinkOptions => {
const linkIndex = sidebarItems.findIndex((item) => item.link === currentPath)

if (!targetItem?.link) {
return null
}
if (linkIndex !== -1) {
const targetItem = sidebarItems[linkIndex + offset]

if (!targetItem) return null

if (targetItem.link) return targetItem as AutoLinkOptions

return targetItem as AutoLinkConfig
if ('prefix' in targetItem && !resolveRoute(targetItem.prefix!).notFound)
return {
...targetItem,
link: targetItem.prefix!,
}

return null
}

for (const item of sidebarItems) {
Expand All @@ -74,16 +72,37 @@ const resolveFromSidebarItems = (
}
}

const prefixIndex = sidebarItems.findIndex(
(item) => 'prefix' in item && item.prefix === currentPath,
)

if (prefixIndex !== -1) {
const targetItem = sidebarItems[prefixIndex + offset]

if (!targetItem) return null

if (targetItem.link) return targetItem as AutoLinkOptions

if ('prefix' in targetItem && !resolveRoute(targetItem.prefix!).notFound)
return {
...targetItem,
link: targetItem.prefix!,
}

return null
}

return null
}

interface RelatedLinks {
prevLink: ComputedRef<AutoLinkConfig | null>
nextLink: ComputedRef<AutoLinkConfig | null>
prevLink: ComputedRef<AutoLinkOptions | null>
nextLink: ComputedRef<AutoLinkOptions | null>
}

export const useRelatedLinks = (): RelatedLinks => {
const frontmatter = usePageFrontmatter<DefaultThemeNormalPageFrontmatter>()
const themeLocale = useThemeLocaleData()
const sidebarItems = useSidebarItems()
const route = useRoute()

Expand All @@ -92,23 +111,27 @@ export const useRelatedLinks = (): RelatedLinks => {
frontmatter.value.prev,
route.path,
)
if (prevConfig !== false) {
return prevConfig
}

return resolveFromSidebarItems(sidebarItems.value, route.path, -1)
return prevConfig === false
? null
: prevConfig ??
(themeLocale.value.prev === false
? null
: resolveFromSidebarItems(sidebarItems.value, route.path, -1))
})

const nextLink = computed(() => {
const nextConfig = resolveFromFrontmatterConfig(
frontmatter.value.next,
route.path,
)
if (nextConfig !== false) {
return nextConfig
}

return resolveFromSidebarItems(sidebarItems.value, route.path, 1)
return nextConfig === false
? null
: nextConfig ??
(themeLocale.value.next === false
? null
: resolveFromSidebarItems(sidebarItems.value, route.path, 1))
})

return {
Expand Down
8 changes: 3 additions & 5 deletions themes/theme-default/src/client/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { setupDarkMode } from '@theme/useDarkMode'
import { useScrollPromise } from '@theme/useScrollPromise'
import { setupSidebarItems } from '@theme/useSidebarItems'
import { hasGlobalComponent } from '@vuepress/helper/client'
import { h } from 'vue'
import { defineClientConfig } from 'vuepress/client'
import type { ClientConfig } from 'vuepress/client'
import { Badge, CodeGroup, CodeGroupItem } from './components/global/index.js'
import {
setupDarkMode,
setupSidebarItems,
useScrollPromise,
} from './composables/index.js'
import Layout from './layouts/Layout.vue'
import NotFound from './layouts/NotFound.vue'

Expand Down
7 changes: 5 additions & 2 deletions themes/theme-default/src/client/utils/getAutoLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import type { AutoLinkOptions } from '../../shared'
* - Input: '/README.md'
* - Output: { text: 'Home', link: '/' }
*/
export const getAutoLink = (config: string): AutoLinkOptions => {
export const getAutoLink = (
config: string,
currentPath?: string,
): AutoLinkOptions => {
const { notFound, meta, path } = resolveRoute<{
title?: string
}>(config)
}>(config, currentPath)

return notFound
? { text: path, link: path }
Expand Down
4 changes: 2 additions & 2 deletions themes/theme-default/src/shared/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ export interface DefaultThemeLocaleData extends LocaleData {
/**
* text for prev link
*/
prev?: string
prev?: string | false

/**
* text for next link
*/
next?: string
next?: string | false
}

0 comments on commit 4c6f5c4

Please sign in to comment.