Skip to content

Commit

Permalink
chore(helper): tweaks getHeadersFromDom
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jan 17, 2025
1 parent 27445a0 commit 7c5daab
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions tools/helper/src/client/utils/getHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { PageHeader } from 'vuepress/shared'

const DEFAULT_HEADER_SELECTOR = [...new Array<undefined>(6)]
.map((_, i) => `[vp-content] h${i + 1}`)
.join(',')

export type HeaderLevels = number | 'deep' | false | [number, number]

export type HeaderItem = Omit<PageHeader, 'children'> & {
Expand Down Expand Up @@ -67,8 +71,8 @@ const serializeHeader = (h: Element, ignore: string[] = []): string => {
}

export const getHeadersFromDom = (
selector: string,
ignore: string[],
selector = DEFAULT_HEADER_SELECTOR,
ignore: string[] = [],
): HeaderItem[] =>
Array.from(document.querySelectorAll(selector))
.filter((el) => el.id && el.hasChildNodes())
Expand Down Expand Up @@ -117,9 +121,7 @@ export interface GetHeadersOptions {
* Get headers of current page.
*/
export const getHeaders = ({
selector = [...new Array<undefined>(6)]
.map((_, i) => `[vp-content] h${i + 1}`)
.join(','),
selector = DEFAULT_HEADER_SELECTOR,
levels = 2,
ignore = [],
}: GetHeadersOptions = {}): HeaderItem[] =>
Expand Down

2 comments on commit 7c5daab

@stfsy
Copy link

@stfsy stfsy commented on 7c5daab Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mister-Hope out of curiosity. What's the advantage of reading the headers from the DOM, when there's also utils like usePageData that store the headers of the current page

@Mister-Hope
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are planning to remove header data from page data, just like vitepress.

Please sign in to comment.