Files
nuxt__ui/docs/server/utils/extractSections.ts
renovate[bot] 7e359508f8 chore(deps): update all non-major dependencies (#6592)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
2026-06-15 11:01:33 +02:00

62 lines
1.6 KiB
TypeScript

/**
* Extract specific h2 sections from markdown content.
* Always includes the title (h1) and description blockquote.
*/
export function extractSections(markdown: string, sectionTitles: string[]): string {
const lines = markdown.split('\n')
const result: string[] = []
// Normalize section titles for matching
const normalizedTitles = sectionTitles.map(t => t.toLowerCase().trim())
// Always include title (h1) and description (first blockquote)
let inHeader = true
for (const line of lines) {
if (inHeader) {
if (line.startsWith('## ')) {
break
}
result.push(line)
// Stop after the description blockquote
if (line.startsWith('>') && result.length > 1) {
result.push('')
inHeader = false
}
continue
}
break
}
// Find and extract requested sections
let currentSection: string | null = null
let sectionContent: string[] = []
for (const line of lines) {
// Check for h2 heading
if (line.startsWith('## ')) {
// Save previous section if it was requested
if (currentSection && normalizedTitles.includes(currentSection.toLowerCase())) {
result.push(...sectionContent)
result.push('')
}
// Start new section
currentSection = line.replace('## ', '').trim()
sectionContent = [line]
continue
}
// Add line to current section
if (currentSection) {
sectionContent.push(line)
}
}
// Don't forget the last section
if (currentSection && normalizedTitles.includes(currentSection.toLowerCase())) {
result.push(...sectionContent)
}
return result.join('\n').trim()
}