feat(theme): allow removing badge text from outline (#1825)

pull/1833/head
Divyansh Singh 2 years ago committed by GitHub
parent 977dad0bb6
commit 5d2fc3f922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,7 +17,7 @@ const pageOutline = computed<DefaultTheme.Config['outline']>(
const onContentUpdated = inject('onContentUpdated') as Ref<() => void>
onContentUpdated.value = () => {
headers.value = getHeaders(pageOutline.value)
headers.value = getHeaders(pageOutline.value, theme.value.outlineBadges)
}
const headers = ref<MenuItem[]>([])

@ -11,16 +11,30 @@ export type MenuItem = Omit<Header, 'slug' | 'children'> & {
children?: MenuItem[]
}
export function getHeaders(pageOutline: DefaultTheme.Config['outline']) {
export function getHeaders(
pageOutline: DefaultTheme.Config['outline'],
outlineBadges: DefaultTheme.Config['outlineBadges']
) {
if (pageOutline === false) return []
let updatedHeaders: MenuItem[] = []
document
.querySelectorAll<HTMLHeadingElement>('h2, h3, h4, h5, h6')
.forEach((el) => {
if (el.textContent && el.id) {
let title = el.textContent
if (outlineBadges === false) {
const clone = el.cloneNode(true) as HTMLElement
for (const child of clone.querySelectorAll('.VPBadge')) {
child.remove()
}
title = clone.textContent || ''
}
updatedHeaders.push({
level: Number(el.tagName[1]),
title: el.innerText.replace(/\s+#\s*$/, ''),
title: title.replace(/\s+#\s*$/, ''),
link: `#${el.id}`
})
}

@ -9,7 +9,7 @@
"declaration": true,
"declarationDir": "../../dist/client-types",
"jsx": "preserve",
"lib": ["ESNext", "DOM"],
"lib": ["esnext", "dom", "dom.iterable"],
"types": ["vite/client"],
"paths": {
"vitepress": ["index.ts"],

@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"lib": ["ESNext", "DOM"]
"lib": ["esnext", "dom", "dom.iterable"]
},
"include": ["."]
}

@ -12,7 +12,7 @@
"preserveValueImports": true,
"importsNotUsedAsValues": "error",
"jsx": "preserve",
"lib": ["ESNext", "DOM"]
"lib": ["esnext", "dom", "dom.iterable"]
},
"exclude": ["**/node_modules/**", "**/dist/**"]
}

@ -22,6 +22,13 @@ export namespace DefaultTheme {
*/
outline?: Outline | Outline['level'] | false
/**
* Disable this to hide badge text from outline.
*
* @default true
*/
outlineBadges?: boolean
/**
* @deprecated
* Use `outline.label` instead.

Loading…
Cancel
Save