docs: add prepare script to generate sidebar (#11894)
This commit is contained in:
@@ -8,14 +8,27 @@ export function findMetadataTitle(content: string): string | undefined {
|
||||
return headingMatch?.groups?.title
|
||||
}
|
||||
|
||||
const HEADING_REGEX = /# (?<title>.*)/
|
||||
|
||||
export function findPageHeading(content: string): string | undefined {
|
||||
const headingMatch = HEADING_REGEX.exec(content)
|
||||
const headingMatch = /# (?<title>.*)/.exec(content)
|
||||
|
||||
return headingMatch?.groups?.title
|
||||
}
|
||||
|
||||
export function findAllPageHeadings({
|
||||
content,
|
||||
level = 1,
|
||||
}: {
|
||||
content: string
|
||||
level?: number
|
||||
}): string[] {
|
||||
const regex = new RegExp(
|
||||
`^${"#".repeat(level)}(?!#) (?<title>.*?)(?:\n|$)`,
|
||||
"gm"
|
||||
)
|
||||
const matches = [...content.matchAll(regex)]
|
||||
return matches.map((match) => match.groups?.title).filter(Boolean) as string[]
|
||||
}
|
||||
|
||||
export function findPageTitle(filePath: string): string | undefined {
|
||||
const content = readFileSync(filePath, "utf-8")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user