diff --git a/index.html b/index.html index cf6cc0d..282c276 100644 --- a/index.html +++ b/index.html @@ -705,6 +705,21 @@ container.appendChild(ul); } + function countSubcategoryArticles(children) { + let total = 0; + children.forEach(child => { + // Count articles in this child + const childCount = child.products ? child.products.length : child.articleCount; + total += childCount; + + // Recursively count articles in nested subcategories + if (child.children && child.children.length > 0) { + total += countSubcategoryArticles(child.children); + } + }); + return total; + } + function renderCategory(category) { // category might be a filtered copy or original. // If it's a copy, it has _original pointing to the real state node. @@ -764,19 +779,30 @@ const count = document.createElement('div'); count.className = 'category-count'; - // Use original counts or filtered counts? - // "remove all articles ... with no match" - // If filtered, we should probably show the count of MATCHING articles? - // User didn't specify, but it makes sense to show matching count if filtered. - // But articleCount comes from API. - // We can calculate it from products array if loaded. - const productCount = category.products ? category.products.length : category.articleCount; + // When filtering, show actual matching product count + // If this category is not a direct match (only matches due to subcategories), + // and products aren't loaded, show 0 instead of total articleCount + let productCount; + if (category.products !== null && category.products !== undefined) { + // Products are loaded, use the filtered count + productCount = category.products.length; + } else if (state.filter && !realNode._isDirectMatch) { + // Filtering is active and this is not a direct match, so show 0 + productCount = 0; + } else { + // No filter or is a direct match, use API count + productCount = category.articleCount; + } let countText = `${productCount} articles`; // Always show subcategory count if children exist (User requirement) // Use filtered children length if (category.children && category.children.length > 0) { + const subcategoryArticleCount = countSubcategoryArticles(category.children); countText += `, ${category.children.length} subcategories`; + if (subcategoryArticleCount > 0) { + countText += ` with ${subcategoryArticleCount} more articles`; + } } count.textContent = countText; info.appendChild(count);