From bcfa99fe2232b0ad31d16dcc3b8f8174e8bd38e3 Mon Sep 17 00:00:00 2001 From: vintro Date: Fri, 13 Dec 2024 23:48:47 -0800 Subject: [PATCH] fix tag filtering --- quartz/components/PageList.tsx | 7 +-- quartz/components/pages/FolderContent.tsx | 55 +++++++++++++++++------ 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/quartz/components/PageList.tsx b/quartz/components/PageList.tsx index 28e35d8b9..b91b05db3 100644 --- a/quartz/components/PageList.tsx +++ b/quartz/components/PageList.tsx @@ -40,9 +40,10 @@ export const PageList: QuartzComponent = ({ cfg, fileData, allFiles, limit }: Pr {list.map((page) => { const title = page.frontmatter?.title const tags = page.frontmatter?.tags ?? [] + const tagsStr = JSON.stringify(tags) return ( -
  • +
  • {page.dates && (

    @@ -68,8 +69,4 @@ PageList.css = ` .section h3 { margin: 0; } - -.section > .tags { - margin: 0; -} ` diff --git a/quartz/components/pages/FolderContent.tsx b/quartz/components/pages/FolderContent.tsx index ab3338b36..b5dadd667 100644 --- a/quartz/components/pages/FolderContent.tsx +++ b/quartz/components/pages/FolderContent.tsx @@ -56,30 +56,47 @@ export default ((opts?: Partial) => { // Add client-side filtering script const filterScript = ` - document.addEventListener('DOMContentLoaded', () => { + function initializeTagFiltering() { const selectedTags = new Set() const cards = document.querySelectorAll('.section-li') const countEl = document.querySelector('.folder-count') - const originalCount = ${allPagesInFolder.length} - + const tagButtons = document.querySelectorAll('.tag-filter-btn') + + // Debug logging + console.log('Found ' + cards.length + ' cards') + console.log('Found ' + tagButtons.length + ' tag buttons') + function updateVisibility() { let visibleCount = 0 cards.forEach(card => { - const tags = JSON.parse(card.dataset.tags || '[]') - const shouldShow = selectedTags.size === 0 || - tags.some(tag => selectedTags.has(tag)) - card.style.display = shouldShow ? '' : 'none' - if (shouldShow) visibleCount++ + try { + const tagsAttr = card.getAttribute('data-tags') + console.log('Card tags:', tagsAttr) + const tags = JSON.parse(tagsAttr || '[]') + const shouldShow = selectedTags.size === 0 || + tags.some(tag => selectedTags.has(tag)) + card.style.display = shouldShow ? '' : 'none' + if (shouldShow) visibleCount++ + } catch (e) { + console.error('Error processing card:', e) + } }) + if (countEl) { - countEl.textContent = ${JSON.stringify(i18n(cfg.locale).pages.folderContent.itemsUnderFolder({count: 0}))} - .replace('0', visibleCount.toString()) + const countText = ${JSON.stringify(i18n(cfg.locale).pages.folderContent.itemsUnderFolder({count: 0}))} + countEl.textContent = countText.replace('0', visibleCount.toString()) } + + // Debug logging + console.log('Selected tags:', Array.from(selectedTags)) + console.log('Visible count:', visibleCount) } - document.querySelectorAll('.tag-filter-btn').forEach(btn => { + tagButtons.forEach(btn => { btn.addEventListener('click', () => { - const tag = btn.dataset.tag + const tag = btn.getAttribute('data-tag') + console.log('Clicked tag:', tag) + if (selectedTags.has(tag)) { selectedTags.delete(tag) btn.classList.remove('selected') @@ -90,7 +107,18 @@ export default ((opts?: Partial) => { updateVisibility() }) }) - }) + } + + // Try to initialize immediately + initializeTagFiltering() + + // Also try after a short delay to ensure DOM is ready + setTimeout(initializeTagFiltering, 100) + + // Also initialize on DOMContentLoaded if it hasn't fired yet + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', initializeTagFiltering) + } ` return ( @@ -104,6 +132,7 @@ export default ((opts?: Partial) => {