fix-edit-link-title

This commit is contained in:
Rusovich Violetta
2025-12-26 16:08:55 +03:00
parent abdc7bd70e
commit a9cafb629c
3 changed files with 36 additions and 60 deletions
+29 -47
View File
@@ -25,41 +25,29 @@ const removeDuplicates = (links: SectionLinkListItem[]) => {
const hasChanges = ( const hasChanges = (
existingLinks: SectionLinkListItem[], existingLinks: SectionLinkListItem[],
mergedLinks: SectionLinkListItem[], mergedLinks: SectionLinkListItem[]
newLinks: SectionLinkListItem[]
) => { ) => {
const existingLinksSet = new Set( if (existingLinks.length !== mergedLinks.length) return true
const existingSet = new Set(
existingLinks existingLinks
.map(link => link.link) .filter(link => link.link)
.filter((link): link is string => Boolean(link)) .map(link => `${normalizeLink(link.link!)}|${link.title}`)
.map(normalizeLink)
)
const hasNewLinks = newLinks.some(
link => link.link && !existingLinksSet.has(normalizeLink(link.link))
) )
if (hasNewLinks) return true const mergedSet = new Set(
mergedLinks
const sidebarLinksSet = new Set( .filter(link => link.link)
newLinks .map(link => `${normalizeLink(link.link!)}|${link.title}`)
.map(link => link.link)
.filter((link): link is string => Boolean(link))
.map(normalizeLink)
) )
const existingSidebarLinks = existingLinks if (existingSet.size !== mergedSet.size) return true
.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('|')
return existingSidebarLinks !== mergedSidebarLinks && mergedLinks.length === existingLinks.length for (const item of existingSet) {
if (!mergedSet.has(item)) return true
}
return false
} }
export const processIndexFile = ( export const processIndexFile = (
@@ -76,22 +64,19 @@ export const processIndexFile = (
const relativePath = relative(srcDir, filePath).replace(/\\/g, '/') const relativePath = relative(srcDir, filePath).replace(/\\/g, '/')
const normalizedPath = relativePath.startsWith('/') ? relativePath : `/${relativePath}` const normalizedPath = relativePath.startsWith('/') ? relativePath : `/${relativePath}`
const rawExistingLinks: SectionLinkListItem[] = Array.isArray(frontmatter.section_links) const existingLinks = removeDuplicates(
? [...frontmatter.section_links] Array.isArray(frontmatter.section_links) ? [...frontmatter.section_links] : []
: [] )
const existingLinks = removeDuplicates(rawExistingLinks)
const newLinks = extractTopLevelLinks(sidebarItems, normalizedPath, srcDir) const newLinks = extractTopLevelLinks(sidebarItems, normalizedPath, srcDir)
if (!newLinks.length) return false if (!newLinks.length) return false
const mergedLinks = 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 frontmatter.section_links = mergedLinks
const updatedContent = stringifyFrontmatter(frontmatter, content) writeFileSync(filePath, stringifyFrontmatter(frontmatter, content), 'utf-8')
writeFileSync(filePath, updatedContent, 'utf-8')
return true return true
} catch { } catch {
return false return false
@@ -112,22 +97,19 @@ export const processPageWithItems = (
const relativePath = relative(srcDir, filePath).replace(/\\/g, '/') const relativePath = relative(srcDir, filePath).replace(/\\/g, '/')
const normalizedPath = relativePath.startsWith('/') ? relativePath : `/${relativePath}` const normalizedPath = relativePath.startsWith('/') ? relativePath : `/${relativePath}`
const rawExistingLinks: SectionLinkListItem[] = Array.isArray(frontmatter.section_links) const existingLinks = removeDuplicates(
? [...frontmatter.section_links] Array.isArray(frontmatter.section_links) ? [...frontmatter.section_links] : []
: [] )
const existingLinks = removeDuplicates(rawExistingLinks)
const newLinks = extractItemsForPage(sidebarItems, normalizedPath, srcDir) 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)) return false
if (!hasChanges(existingLinks, mergedLinks, newLinks)) return false
frontmatter.section_links = mergedLinks frontmatter.section_links = mergedLinks
const updatedContent = stringifyFrontmatter(frontmatter, content) writeFileSync(filePath, stringifyFrontmatter(frontmatter, content), 'utf-8')
writeFileSync(filePath, updatedContent, 'utf-8')
return true return true
} catch { } catch {
return false return false
+5 -1
View File
@@ -11,7 +11,11 @@ export const parseFrontmatter = (content: string) => {
const [, frontmatterText] = frontmatterMatch const [, frontmatterText] = frontmatterMatch
const frontmatterEnd = frontmatterMatch[0].length 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 frontmatter: Frontmatter = {}
const lines = frontmatterText.split('\n') const lines = frontmatterText.split('\n')
+2 -12
View File
@@ -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 return result
} }
@@ -62,7 +52,7 @@ export const extractTopLevelLinks = (
if ('excludeFromIndex' in item && item.excludeFromIndex) { if ('excludeFromIndex' in item && item.excludeFromIndex) {
continue continue
} }
if (item.link) { if (item.link) {
const normalizedItemPath = normalizeLink(item.link) const normalizedItemPath = normalizeLink(item.link)
if (normalizedItemPath === normalizedCurrentPath) { if (normalizedItemPath === normalizedCurrentPath) {
@@ -97,7 +87,7 @@ const findItemsRecursive = (
if ('excludeFromIndex' in subItem && subItem.excludeFromIndex) { if ('excludeFromIndex' in subItem && subItem.excludeFromIndex) {
continue continue
} }
if (subItem.link) { if (subItem.link) {
links.push({ links.push({
title: subItem.text || '', title: subItem.text || '',