diff --git a/src/.vitepress/plugins/utils/file-processor.ts b/src/.vitepress/plugins/utils/file-processor.ts index d85b2c6..ee9c519 100644 --- a/src/.vitepress/plugins/utils/file-processor.ts +++ b/src/.vitepress/plugins/utils/file-processor.ts @@ -25,41 +25,29 @@ const removeDuplicates = (links: SectionLinkListItem[]) => { const hasChanges = ( existingLinks: SectionLinkListItem[], - mergedLinks: SectionLinkListItem[], - newLinks: SectionLinkListItem[] + mergedLinks: SectionLinkListItem[] ) => { - const existingLinksSet = new Set( + if (existingLinks.length !== mergedLinks.length) return true + + const existingSet = new Set( existingLinks - .map(link => link.link) - .filter((link): link is string => Boolean(link)) - .map(normalizeLink) - ) - - const hasNewLinks = newLinks.some( - link => link.link && !existingLinksSet.has(normalizeLink(link.link)) + .filter(link => link.link) + .map(link => `${normalizeLink(link.link!)}|${link.title}`) ) - if (hasNewLinks) return true - - const sidebarLinksSet = new Set( - newLinks - .map(link => link.link) - .filter((link): link is string => Boolean(link)) - .map(normalizeLink) + const mergedSet = new Set( + mergedLinks + .filter(link => link.link) + .map(link => `${normalizeLink(link.link!)}|${link.title}`) ) - const existingSidebarLinks = existingLinks - .filter(link => link.link && sidebarLinksSet.has(normalizeLink(link.link))) - .map(link => normalizeLink(link.link!)) - .join('|') - - const mergedSidebarLinks = mergedLinks - .slice(0, newLinks.length) - .filter(link => link.link && sidebarLinksSet.has(normalizeLink(link.link))) - .map(link => normalizeLink(link.link!)) - .join('|') + if (existingSet.size !== mergedSet.size) return true - return existingSidebarLinks !== mergedSidebarLinks && mergedLinks.length === existingLinks.length + for (const item of existingSet) { + if (!mergedSet.has(item)) return true + } + + return false } export const processIndexFile = ( @@ -76,22 +64,19 @@ export const processIndexFile = ( const relativePath = relative(srcDir, filePath).replace(/\\/g, '/') const normalizedPath = relativePath.startsWith('/') ? relativePath : `/${relativePath}` - const rawExistingLinks: SectionLinkListItem[] = Array.isArray(frontmatter.section_links) - ? [...frontmatter.section_links] - : [] - - const existingLinks = removeDuplicates(rawExistingLinks) + const existingLinks = removeDuplicates( + Array.isArray(frontmatter.section_links) ? [...frontmatter.section_links] : [] + ) const newLinks = extractTopLevelLinks(sidebarItems, normalizedPath, srcDir) if (!newLinks.length) return false const mergedLinks = removeDuplicates(mergeSectionLinks(existingLinks, newLinks)) - if (!hasChanges(existingLinks, mergedLinks, newLinks)) return false + if (!hasChanges(existingLinks, mergedLinks)) return false frontmatter.section_links = mergedLinks - const updatedContent = stringifyFrontmatter(frontmatter, content) - writeFileSync(filePath, updatedContent, 'utf-8') + writeFileSync(filePath, stringifyFrontmatter(frontmatter, content), 'utf-8') return true } catch { return false @@ -112,22 +97,19 @@ export const processPageWithItems = ( const relativePath = relative(srcDir, filePath).replace(/\\/g, '/') const normalizedPath = relativePath.startsWith('/') ? relativePath : `/${relativePath}` - const rawExistingLinks: SectionLinkListItem[] = Array.isArray(frontmatter.section_links) - ? [...frontmatter.section_links] - : [] - - const existingLinks = removeDuplicates(rawExistingLinks) + const existingLinks = removeDuplicates( + Array.isArray(frontmatter.section_links) ? [...frontmatter.section_links] : [] + ) const newLinks = extractItemsForPage(sidebarItems, normalizedPath, srcDir) - if (!newLinks.length) return false + const mergedLinks = newLinks.length > 0 + ? removeDuplicates(mergeSectionLinks(existingLinks, newLinks)) + : [] - const mergedLinks = removeDuplicates(mergeSectionLinks(existingLinks, newLinks)) - - if (!hasChanges(existingLinks, mergedLinks, newLinks)) return false + if (!hasChanges(existingLinks, mergedLinks)) return false frontmatter.section_links = mergedLinks - const updatedContent = stringifyFrontmatter(frontmatter, content) - writeFileSync(filePath, updatedContent, 'utf-8') + writeFileSync(filePath, stringifyFrontmatter(frontmatter, content), 'utf-8') return true } catch { return false diff --git a/src/.vitepress/plugins/utils/frontmatter.ts b/src/.vitepress/plugins/utils/frontmatter.ts index 6084ad9..b3502ee 100644 --- a/src/.vitepress/plugins/utils/frontmatter.ts +++ b/src/.vitepress/plugins/utils/frontmatter.ts @@ -11,7 +11,11 @@ export const parseFrontmatter = (content: string) => { const [, frontmatterText] = frontmatterMatch const frontmatterEnd = frontmatterMatch[0].length - const cleanContent = content.slice(frontmatterEnd).replace(/^---[\s\S]*?---\s*\n*/g, '').trim() + let cleanContent = content.slice(frontmatterEnd) + cleanContent = cleanContent.replace(/^---[\s\S]*?---\s*\n*/g, '').trim() + if (!cleanContent) { + cleanContent = content.slice(frontmatterEnd).replace(/^---[\s\S]*?---\s*\n*/g, '').trim() + } const frontmatter: Frontmatter = {} const lines = frontmatterText.split('\n') diff --git a/src/.vitepress/plugins/utils/links.ts b/src/.vitepress/plugins/utils/links.ts index 62bdf32..d5a45e4 100644 --- a/src/.vitepress/plugins/utils/links.ts +++ b/src/.vitepress/plugins/utils/links.ts @@ -37,16 +37,6 @@ export const mergeSectionLinks = ( } } - for (const existingLink of existingLinks) { - if (existingLink.link) { - const normalizedLink = normalizeLink(existingLink.link) - if (!processedLinks.has(normalizedLink)) { - processedLinks.add(normalizedLink) - result.push({ ...existingLink }) - } - } - } - return result } @@ -62,7 +52,7 @@ export const extractTopLevelLinks = ( if ('excludeFromIndex' in item && item.excludeFromIndex) { continue } - + if (item.link) { const normalizedItemPath = normalizeLink(item.link) if (normalizedItemPath === normalizedCurrentPath) { @@ -97,7 +87,7 @@ const findItemsRecursive = ( if ('excludeFromIndex' in subItem && subItem.excludeFromIndex) { continue } - + if (subItem.link) { links.push({ title: subItem.text || '',