pull/4630/merge
Lukas Leisten 4 weeks ago committed by GitHub
commit 57047213c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -451,6 +451,13 @@ Can be used to customize the aria-label of the language toggle button in navbar.
Can be used to customize the label of the skip to content link. This link is shown when the user is navigating the site using a keyboard. Can be used to customize the label of the skip to content link. This link is shown when the user is navigating the site using a keyboard.
## skipTitleUpdate
- Type: `boolean`
- Default: `false`
Can be used to skip the default title update logic so it can be implemented on your own.
## externalLinkIcon ## externalLinkIcon
- Type: `boolean` - Type: `boolean`

@ -161,6 +161,27 @@ export default {
Refer [`socialLinks`](./default-theme-config#sociallinks). Refer [`socialLinks`](./default-theme-config#sociallinks).
## Custom Text Item Component
If you want to use the default theme but modify only the text of the items, you can override the `SidebarTextItem` inside the `enhanceApp` function. For example:
```js
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme';
import CustomSidebarTextItem from './components/CustomSidebarTextItem.vue';
export default {
extends: DefaultTheme,
enhanceApp({ app }) {
app.component('SidebarTextItem', CustomSidebarTextItem);
}
};
```
### Creating a Custom `SidebarTextItem`
To create your own `SidebarTextItem`, define a Vue component that accepts a `content` prop and renders it inside a chosen HTML tag. This allows you to fully customize how the text appears while maintaining compatibility with the default theme.
## Custom Components ## Custom Components
You can include custom components in the navigation bar by using the `component` option. The `component` key should be the Vue component name, and must be registered globally using [Theme.enhanceApp](../guide/custom-theme#theme-interface). You can include custom components in the navigation bar by using the `component` option. The `component` key should be the Vue component name, and must be registered globally using [Theme.enhanceApp](../guide/custom-theme#theme-interface).

@ -187,6 +187,27 @@ export default defineConfig({
}) })
``` ```
### Custom Local Search Box Item Component
If you want to customize the search result items in the local search page, you can override the `LocalSearchBoxItem` component similarly:
```js
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme';
import CustomLocalSearchBoxItem from './components/CustomLocalSearchBoxItem.vue';
export default {
extends: DefaultTheme,
enhanceApp({ app }) {
app.component('LocalSearchBoxItem', CustomLocalSearchBoxItem);
}
};
```
#### Creating a Custom `LocalSearchBoxItem`
To create your own `LocalSearchBoxItem`, define a Vue component that accepts a `content` prop and renders it inside a chosen HTML tag, such as a `<span>`. This allows you to control how search results are displayed while keeping the default functionality.
## Algolia Search ## Algolia Search
VitePress supports searching your docs site using [Algolia DocSearch](https://docsearch.algolia.com/docs/what-is-docsearch). Refer their getting started guide. In your `.vitepress/config.ts` you'll need to provide at least the following to make it work: VitePress supports searching your docs site using [Algolia DocSearch](https://docsearch.algolia.com/docs/what-is-docsearch). Refer their getting started guide. In your `.vitepress/config.ts` you'll need to provide at least the following to make it work:

@ -0,0 +1,13 @@
import { defineComponent, h } from 'vue'
export const LocalSearchBoxItem = defineComponent({
props: {
content: {
type: String,
required: true
}
},
render() {
return h('span', { innerHTML: this.content })
}
})

@ -0,0 +1,17 @@
import { defineComponent, h } from 'vue'
export const SidebarTextItem = defineComponent({
props: {
content: {
type: String,
required: true
},
is: {
type: [Object, String],
default: 'p'
}
},
render() {
return h(this.is, { innerHTML: this.content })
}
})

@ -56,9 +56,11 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
const frontmatterHead = (pageData && pageData.frontmatter.head) || [] const frontmatterHead = (pageData && pageData.frontmatter.head) || []
// update title and description // update title and description
const title = createTitle(siteData, pageData) if (!siteData.themeConfig.skipTitleUpdate) {
if (title !== document.title) { const title = createTitle(siteData, pageData)
document.title = title if (title !== document.title) {
document.title = title
}
} }
const description = pageDescription || siteData.description const description = pageDescription || siteData.description

@ -17,6 +17,8 @@ import { usePrefetch } from './composables/preFetch'
import { dataSymbol, initData, siteDataRef, useData } from './data' import { dataSymbol, initData, siteDataRef, useData } from './data'
import { RouterSymbol, createRouter, scrollTo, type Router } from './router' import { RouterSymbol, createRouter, scrollTo, type Router } from './router'
import { inBrowser, pathToFile } from './utils' import { inBrowser, pathToFile } from './utils'
import { SidebarTextItem } from './components/SidebarTextItem'
import { LocalSearchBoxItem } from './components/LocalSearchBoxItem'
function resolveThemeExtends(theme: typeof RawTheme): typeof RawTheme { function resolveThemeExtends(theme: typeof RawTheme): typeof RawTheme {
if (theme.extends) { if (theme.extends) {
@ -78,6 +80,8 @@ export async function createApp() {
// install global components // install global components
app.component('Content', Content) app.component('Content', Content)
app.component('ClientOnly', ClientOnly) app.component('ClientOnly', ClientOnly)
app.component('SidebarTextItem', SidebarTextItem)
app.component('LocalSearchBoxItem', LocalSearchBoxItem)
// expose $frontmatter & $params // expose $frontmatter & $params
Object.defineProperties(app.config.globalProperties, { Object.defineProperties(app.config.globalProperties, {

@ -526,11 +526,11 @@ function onMouseMove(e: MouseEvent) {
:key="index" :key="index"
class="title" class="title"
> >
<span class="text" v-html="t" /> <LocalSearchBoxItem class="text" :content="t" />
<span class="vpi-chevron-right local-search-icon" /> <span class="vpi-chevron-right local-search-icon" />
</span> </span>
<span class="title main"> <span class="title main">
<span class="text" v-html="p.title" /> <LocalSearchBoxItem class="text" :content="p.title" />
</span> </span>
</div> </div>

@ -77,9 +77,9 @@ function onCaretClick() {
:rel="item.rel" :rel="item.rel"
:target="item.target" :target="item.target"
> >
<component :is="textTag" class="text" v-html="item.text" /> <SidebarTextItem :is="textTag" class="text" :content="item.text" />
</VPLink> </VPLink>
<component v-else :is="textTag" class="text" v-html="item.text" /> <SidebarTextItem v-else :is="textTag" class="text" :content="item.text" />
<div <div
v-if="item.collapsed != null && item.items && item.items.length" v-if="item.collapsed != null && item.items && item.items.length"

@ -126,6 +126,11 @@ export namespace DefaultTheme {
*/ */
skipToContentLabel?: string skipToContentLabel?: string
/**
* @default false
*/
skipTitleUpdate?: boolean
search?: search?:
| { provider: 'local'; options?: LocalSearchOptions } | { provider: 'local'; options?: LocalSearchOptions }
| { provider: 'algolia'; options: AlgoliaSearchOptions } | { provider: 'algolia'; options: AlgoliaSearchOptions }

Loading…
Cancel
Save