fix-edit-link-title
This commit is contained in:
@@ -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(
|
const mergedSet = new Set(
|
||||||
link => link.link && !existingLinksSet.has(normalizeLink(link.link))
|
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(
|
for (const item of existingSet) {
|
||||||
newLinks
|
if (!mergedSet.has(item)) return true
|
||||||
.map(link => link.link)
|
}
|
||||||
.filter((link): link is string => Boolean(link))
|
|
||||||
.map(normalizeLink)
|
|
||||||
)
|
|
||||||
|
|
||||||
const existingSidebarLinks = existingLinks
|
return false
|
||||||
.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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
@@ -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')
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user