Skip to content

Commit

Permalink
feat(theme): allow removing badge text from outline (#1825)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd authored Jan 21, 2023
1 parent 977dad0 commit 5d2fc3f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/client/theme-default/components/VPDocAsideOutline.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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[]>([])
Expand Down
18 changes: 16 additions & 2 deletions src/client/theme-default/composables/outline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
2 changes: 1 addition & 1 deletion src/shared/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"lib": ["ESNext", "DOM"]
"lib": ["esnext", "dom", "dom.iterable"]
},
"include": ["."]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"preserveValueImports": true,
"importsNotUsedAsValues": "error",
"jsx": "preserve",
"lib": ["ESNext", "DOM"]
"lib": ["esnext", "dom", "dom.iterable"]
},
"exclude": ["**/node_modules/**", "**/dist/**"]
}
7 changes: 7 additions & 0 deletions types/default-theme.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5d2fc3f

Please sign in to comment.