|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import type { DefaultTheme } from 'vitepress' |
| 4 | + |
| 5 | +const ROOT_DIR = path.resolve(__dirname, '../..') |
| 6 | +const MARKDOWN_FENCE_RE = /^(~~~|```)/ |
| 7 | +const HEADING_RE = /^(#{2,3})\s+(.+)$/ |
| 8 | +const CUSTOM_ANCHOR_RE = /\s*{#([^}]+)}\s*$/ |
| 9 | + |
| 10 | +const sanitizeHeadingText = (raw: string): { text: string; slugHint?: string } => { |
| 11 | + const anchorMatch = raw.match(CUSTOM_ANCHOR_RE) |
| 12 | + const slugHint = anchorMatch?.[1] |
| 13 | + const withoutAnchor = anchorMatch ? raw.replace(CUSTOM_ANCHOR_RE, '') : raw |
| 14 | + |
| 15 | + const text = withoutAnchor |
| 16 | + .replace(/!\[[^\]]*]\([^)]*\)/g, '') |
| 17 | + .replace(/\[([^\]]+)]\([^)]*\)/g, '$1') |
| 18 | + .replace(/`([^`]+)`/g, '$1') |
| 19 | + .replace(/\*\*([^*]+)\*\*/g, '$1') |
| 20 | + .replace(/\*([^*]+)\*/g, '$1') |
| 21 | + .replace(/~~([^~]+)~~/g, '$1') |
| 22 | + .replace(/<[^>]+>/g, '') |
| 23 | + .replace(/</g, '<') |
| 24 | + .replace(/>/g, '>') |
| 25 | + .trim() |
| 26 | + |
| 27 | + return { text, slugHint } |
| 28 | +} |
| 29 | + |
| 30 | +const slugifyHeading = (text: string, slugHint?: string): string => { |
| 31 | + if (slugHint?.trim()) { |
| 32 | + return slugHint.trim() |
| 33 | + } |
| 34 | + |
| 35 | + const normalized = text |
| 36 | + .trim() |
| 37 | + .toLowerCase() |
| 38 | + .replace(/[^\p{Letter}\p{Number}\u4e00-\u9fff\-\s]/gu, '') |
| 39 | + .replace(/\s+/g, '-') |
| 40 | + |
| 41 | + return normalized || encodeURIComponent(text.trim()).replace(/%/g, '').toLowerCase() |
| 42 | +} |
| 43 | + |
| 44 | +const buildMarkdownFilePath = (pageLink: string): string => { |
| 45 | + const normalized = pageLink.replace(/^\/+/, '') |
| 46 | + if (!normalized) { |
| 47 | + return path.join(ROOT_DIR, 'index.md') |
| 48 | + } |
| 49 | + const isEndWithSlash = normalized.endsWith('/') |
| 50 | + return isEndWithSlash |
| 51 | + ? path.join(ROOT_DIR, normalized, 'index.md') |
| 52 | + : path.join(ROOT_DIR, `${normalized}.md`) |
| 53 | +} |
| 54 | + |
| 55 | +const resolvePageLink = (pageLink: string): string => |
| 56 | + pageLink.startsWith('/') ? pageLink : `/${pageLink}` |
| 57 | + |
| 58 | +export const getNestedSidebarItems = (pageLink: string): DefaultTheme.SidebarItem[] => { |
| 59 | + const markdownFilePath = buildMarkdownFilePath(pageLink) |
| 60 | + |
| 61 | + if (!fs.existsSync(markdownFilePath)) { |
| 62 | + console.warn(`[getNestedSidebarItems] markdown file not found: ${markdownFilePath}`) |
| 63 | + return [] |
| 64 | + } |
| 65 | + |
| 66 | + const fileContent = fs.readFileSync(markdownFilePath, 'utf-8') |
| 67 | + const lines = fileContent.split(/\r?\n/) |
| 68 | + |
| 69 | + const items: DefaultTheme.SidebarItem[] = [] |
| 70 | + let currentGroup: DefaultTheme.SidebarItem | null = null |
| 71 | + let insideFence = false |
| 72 | + |
| 73 | + for (const line of lines) { |
| 74 | + const trimmed = line.trim() |
| 75 | + |
| 76 | + if (MARKDOWN_FENCE_RE.test(trimmed)) { |
| 77 | + insideFence = !insideFence |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + if (insideFence) continue |
| 82 | + |
| 83 | + const headingMatch = trimmed.match(HEADING_RE) |
| 84 | + if (!headingMatch) continue |
| 85 | + |
| 86 | + const level = headingMatch[1].length |
| 87 | + if (level !== 2 && level !== 3) continue |
| 88 | + |
| 89 | + const { text, slugHint } = sanitizeHeadingText(headingMatch[2]) |
| 90 | + if (!text) continue |
| 91 | + |
| 92 | + const slug = slugifyHeading(text, slugHint) |
| 93 | + const link = `${resolvePageLink(pageLink)}#${slug}` |
| 94 | + |
| 95 | + if (level === 2) { |
| 96 | + const groupItem: DefaultTheme.SidebarItem = { |
| 97 | + text, |
| 98 | + link, |
| 99 | + collapsed: true |
| 100 | + } |
| 101 | + currentGroup = groupItem |
| 102 | + items.push(groupItem) |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + if (!currentGroup) { |
| 107 | + currentGroup = { |
| 108 | + text, |
| 109 | + link, |
| 110 | + collapsed: true, |
| 111 | + items: [] |
| 112 | + } |
| 113 | + items.push(currentGroup) |
| 114 | + } |
| 115 | + |
| 116 | + if (!currentGroup.items) { |
| 117 | + currentGroup.items = [] |
| 118 | + } |
| 119 | + |
| 120 | + currentGroup.items.push({ text, link }) |
| 121 | + } |
| 122 | + |
| 123 | + return items.map(item => { |
| 124 | + if (item.items && item.items.length === 0) { |
| 125 | + const { items: _empty, ...rest } = item |
| 126 | + return rest |
| 127 | + } |
| 128 | + return item |
| 129 | + }) |
| 130 | +} |
0 commit comments