-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathmarkdown.ts
More file actions
23 lines (20 loc) · 867 Bytes
/
markdown.ts
File metadata and controls
23 lines (20 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Counts the number of markdown headings in the given text.
* Matches headings from level 1 to 6 (e.g. #, ##, ###, etc.).
* Code fences are stripped before matching to avoid false positives.
*/
export function countMarkdownHeadings(text: string | undefined): number {
if (!text || typeof text !== "string") return 0
// Remove fenced code blocks to avoid counting headings inside code
const withoutCodeBlocks = text.replace(/```[\s\S]*?```/g, "")
// Up to 3 leading spaces are allowed before the hashes per the markdown spec
const headingRegex = /^\s{0,3}#{1,6}\s+.+$/gm
const matches = withoutCodeBlocks.match(headingRegex)
return matches ? matches.length : 0
}
/**
* Returns true if the markdown contains at least two headings.
*/
export function hasComplexMarkdown(text: string | undefined): boolean {
return countMarkdownHeadings(text) >= 2
}