-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(theme-default): fix setupHeaders, close #326
- Loading branch information
1 parent
aab502f
commit f80d7e2
Showing
4 changed files
with
59 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useThemeLocaleData } from '@theme/useThemeData' | ||
import type { MenuItem } from '@vuepress/helper/client' | ||
import { getHeaders } from '@vuepress/helper/client' | ||
import { watchImmediate } from '@vueuse/core' | ||
import type { InjectionKey, Ref } from 'vue' | ||
import { computed, inject, onMounted, provide, ref } from 'vue' | ||
import { usePageFrontmatter, useRoutePath } from 'vuepress/client' | ||
import type { DefaultThemeNormalPageFrontmatter } from '../../shared/index.js' | ||
|
||
export type HeadersRef = Ref<MenuItem[]> | ||
|
||
export const headersSymbol: InjectionKey<HeadersRef> = Symbol('headers') | ||
|
||
/** | ||
* Inject headers | ||
*/ | ||
export const useHeaders = (): HeadersRef => { | ||
const headers = inject(headersSymbol) | ||
|
||
if (!headers) { | ||
throw new Error('useHeaders() is called without provider.') | ||
} | ||
return headers | ||
} | ||
|
||
export const setupHeaders = (): void => { | ||
const headersRef: HeadersRef = ref([]) | ||
|
||
const routePath = useRoutePath() | ||
const themeLocale = useThemeLocaleData() | ||
const frontmatter = usePageFrontmatter<DefaultThemeNormalPageFrontmatter>() | ||
const levels = computed( | ||
() => frontmatter.value.sidebarDepth ?? themeLocale.value.sidebarDepth ?? 2, | ||
) | ||
|
||
const updateHeaders = (): void => { | ||
if (levels.value <= 0) { | ||
headersRef.value = [] | ||
return | ||
} | ||
|
||
headersRef.value = getHeaders({ | ||
levels: [2, levels.value + 1], | ||
ignore: ['.vp-badge'], | ||
}) | ||
} | ||
|
||
provide(headersSymbol, headersRef) | ||
|
||
onMounted(() => { | ||
watchImmediate([levels, routePath], updateHeaders) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters