mirror of
https://github.com/jackyzha0/quartz.git
synced 2026-02-03 22:15:42 -06:00
fix: redundant code
Copilot reviews
This commit is contained in:
parent
84ff4012d1
commit
50ce65a2b4
@ -151,56 +151,51 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (opts?.includeTags) {
|
if (opts?.includeTags) {
|
||||||
|
// Optimization: Build a map of tag -> content list once (reverse index)
|
||||||
|
const tagsInput: Map<string, ContentDetails[]> = new Map()
|
||||||
|
|
||||||
|
// Iterate over the content index once to populate the reverse index
|
||||||
|
for (const [_, content] of linkIndex) {
|
||||||
|
const tags = content.tags.flatMap(getAllSegmentPrefixes)
|
||||||
|
for (const tag of new Set(tags)) {
|
||||||
|
// Use Set to avoid double counting per file
|
||||||
|
if (!tagsInput.has(tag)) {
|
||||||
|
tagsInput.set(tag, [])
|
||||||
|
}
|
||||||
|
tagsInput.get(tag)!.push(content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let sortedTags: string[] = []
|
let sortedTags: string[] = []
|
||||||
|
|
||||||
if (opts.rssTags && opts.rssTags.length > 0) {
|
if (opts.rssTags && opts.rssTags.length > 0) {
|
||||||
// Deduplicate and slugify user-provided tags
|
// Deduplicate and slugify user-provided tags
|
||||||
const userTags = new Set(opts.rssTags.map((tag) => slugTag(tag)))
|
const userTags = new Set(opts.rssTags.map((tag) => slugTag(tag)))
|
||||||
|
|
||||||
// Only include user-specified tags that actually exist in the content
|
// Filter user tags to only those that exist in the content
|
||||||
const availableTags = new Set<string>()
|
sortedTags = Array.from(userTags).filter((tag) => tagsInput.has(tag))
|
||||||
for (const [_, content] of linkIndex) {
|
|
||||||
const tags = content.tags.flatMap(getAllSegmentPrefixes)
|
|
||||||
for (const tag of tags) {
|
|
||||||
availableTags.add(tag)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sortedTags = Array.from(userTags).filter((tag) => availableTags.has(tag))
|
|
||||||
} else if ((opts.rssTagsLimit ?? 0) > 0) {
|
} else if ((opts.rssTagsLimit ?? 0) > 0) {
|
||||||
const tagCounts: Map<string, number> = new Map()
|
// Sort available tags by frequency (number of content items)
|
||||||
|
sortedTags = Array.from(tagsInput.entries())
|
||||||
// Count tag occurrences across all files in the index
|
.sort((a, b) => b[1].length - a[1].length) // Sort by frequency descending
|
||||||
for (const [_, content] of linkIndex) {
|
|
||||||
const tags = content.tags.flatMap(getAllSegmentPrefixes)
|
|
||||||
for (const tag of new Set(tags)) {
|
|
||||||
// Use Set to avoid double counting per file
|
|
||||||
tagCounts.set(tag, (tagCounts.get(tag) ?? 0) + 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sortedTags = Array.from(tagCounts.entries())
|
|
||||||
.sort((a, b) => b[1] - a[1]) // Sort by frequency descending
|
|
||||||
.slice(0, opts.rssTagsLimit)
|
.slice(0, opts.rssTagsLimit)
|
||||||
.map(([tag]) => tag)
|
.map(([tag]) => tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (sortedTags.length === 0) {
|
||||||
sortedTags.length === 0 &&
|
|
||||||
(!opts.rssTags || opts.rssTags.length === 0) &&
|
|
||||||
(opts.rssTagsLimit ?? 0) <= 0
|
|
||||||
) {
|
|
||||||
console.warn(
|
console.warn(
|
||||||
"[contentIndex] includeTags is enabled, but no tag-based RSS feeds will be generated. " +
|
"[contentIndex] includeTags is enabled, but no tag-based RSS feeds will be generated. " +
|
||||||
"Either provide non-empty `rssTags` or set `rssTagsLimit` to a positive number.",
|
"Either provide non-empty `rssTags` matching content tags or set `rssTagsLimit` to a positive number.",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const tag of sortedTags) {
|
for (const tag of sortedTags) {
|
||||||
const tagFilteredIndex = new Map(
|
const tagContent = tagsInput.get(tag)
|
||||||
Array.from(linkIndex).filter(([_, content]) => {
|
if (!tagContent) continue // Should not happen given logic above
|
||||||
const fileTags = new Set(content.tags.flatMap(getAllSegmentPrefixes))
|
|
||||||
return fileTags.has(tag)
|
// Reconstruct a map for generateRSSFeed (it expects a ContentIndexMap)
|
||||||
}),
|
// We can optimize this by making generateRSSFeed accept an array, but for now we conform to the interface
|
||||||
)
|
const tagFilteredIndex = new Map(tagContent.map((content) => [content.slug, content]))
|
||||||
|
|
||||||
yield write({
|
yield write({
|
||||||
ctx,
|
ctx,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user