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
+28 -46
View File
@@ -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)
.filter(link => link.link)
.map(link => `${normalizeLink(link.link!)}|${link.title}`)
)
const hasNewLinks = newLinks.some(
link => link.link && !existingLinksSet.has(normalizeLink(link.link))
const mergedSet = new Set(
mergedLinks
.filter(link => link.link)
.map(link => `${normalizeLink(link.link!)}|${link.title}`)
)
if (hasNewLinks) return true
if (existingSet.size !== mergedSet.size) return true
const sidebarLinksSet = new Set(
newLinks
.map(link => link.link)
.filter((link): link is string => Boolean(link))
.map(normalizeLink)
)
for (const item of existingSet) {
if (!mergedSet.has(item)) return true
}
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('|')
return existingSidebarLinks !== mergedSidebarLinks && mergedLinks.length === existingLinks.length
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
+5 -1
View File
@@ -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')
-10
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
}