| 1 |
import {render, readRaw} from './content'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
export async function buildDocsSidebar(folder) { |
| 12 |
const {headings} = await render(folder); |
| 13 |
const source = readRaw(`/src/content/${folder.path}`); |
| 14 |
|
| 15 |
const sections = []; |
| 16 |
let section = null; |
| 17 |
const re = /^<hr\s+data-sidebar-group="([^"]*)".*\/>$|^## (.+)$/gm; |
| 18 |
|
| 19 |
for (let m; (m = re.exec(source));) { |
| 20 |
if (m[1] != null) { |
| 21 |
section = {path: '', folder: 'docs', slug: '', url: '', frontmatter: {title: m[1]}, files: [], folders: [], allFiles: [], breadcrumbs: []}; |
| 22 |
sections.push(section); |
| 23 |
} else if (m[2] && section) { |
| 24 |
const heading = headings.find(h => h.depth === 2 && h.text === m[2].trim()); |
| 25 |
const slug = heading?.slug ?? m[2].trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, ''); |
| 26 |
section.files.push({ |
| 27 |
path: '', folder: 'docs', slug, |
| 28 |
url: `#${slug}`, |
| 29 |
frontmatter: {title: heading?.text ?? m[2].trim()}, |
| 30 |
breadcrumbs: [], |
| 31 |
}); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
const allFiles = sections.flatMap(s => s.files); |
| 36 |
return {headings, sidebar: {...folder, folders: sections, files: [], allFiles}}; |
| 37 |
} |