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> const onContentUpdated = inject('onContentUpdated') as Ref<() => void>
onContentUpdated.value = () => { onContentUpdated.value = () => {
headers.value = getHeaders(pageOutline.value) headers.value = getHeaders(pageOutline.value, theme.value.outlineBadges)
} }
const headers = ref<MenuItem[]>([]) const headers = ref<MenuItem[]>([])

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

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

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

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

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

Loading…
Cancel
Save