fix-edit-link-title
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user