mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-20 11:24:05 -06:00
fix tag filtering
This commit is contained in:
parent
f9b1856da8
commit
bcfa99fe22
@ -40,9 +40,10 @@ export const PageList: QuartzComponent = ({ cfg, fileData, allFiles, limit }: Pr
|
|||||||
{list.map((page) => {
|
{list.map((page) => {
|
||||||
const title = page.frontmatter?.title
|
const title = page.frontmatter?.title
|
||||||
const tags = page.frontmatter?.tags ?? []
|
const tags = page.frontmatter?.tags ?? []
|
||||||
|
const tagsStr = JSON.stringify(tags)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li class="section-li" data-tags={JSON.stringify(tags)}>
|
<li class="section-li" data-tags={tagsStr}>
|
||||||
<div class="section">
|
<div class="section">
|
||||||
{page.dates && (
|
{page.dates && (
|
||||||
<p class="meta">
|
<p class="meta">
|
||||||
@ -68,8 +69,4 @@ PageList.css = `
|
|||||||
.section h3 {
|
.section h3 {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.section > .tags {
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
`
|
`
|
||||||
|
|||||||
@ -56,30 +56,47 @@ export default ((opts?: Partial<FolderContentOptions>) => {
|
|||||||
|
|
||||||
// Add client-side filtering script
|
// Add client-side filtering script
|
||||||
const filterScript = `
|
const filterScript = `
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
function initializeTagFiltering() {
|
||||||
const selectedTags = new Set()
|
const selectedTags = new Set()
|
||||||
const cards = document.querySelectorAll('.section-li')
|
const cards = document.querySelectorAll('.section-li')
|
||||||
const countEl = document.querySelector('.folder-count')
|
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() {
|
function updateVisibility() {
|
||||||
let visibleCount = 0
|
let visibleCount = 0
|
||||||
cards.forEach(card => {
|
cards.forEach(card => {
|
||||||
const tags = JSON.parse(card.dataset.tags || '[]')
|
try {
|
||||||
const shouldShow = selectedTags.size === 0 ||
|
const tagsAttr = card.getAttribute('data-tags')
|
||||||
tags.some(tag => selectedTags.has(tag))
|
console.log('Card tags:', tagsAttr)
|
||||||
card.style.display = shouldShow ? '' : 'none'
|
const tags = JSON.parse(tagsAttr || '[]')
|
||||||
if (shouldShow) visibleCount++
|
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) {
|
if (countEl) {
|
||||||
countEl.textContent = ${JSON.stringify(i18n(cfg.locale).pages.folderContent.itemsUnderFolder({count: 0}))}
|
const countText = ${JSON.stringify(i18n(cfg.locale).pages.folderContent.itemsUnderFolder({count: 0}))}
|
||||||
.replace('0', visibleCount.toString())
|
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', () => {
|
btn.addEventListener('click', () => {
|
||||||
const tag = btn.dataset.tag
|
const tag = btn.getAttribute('data-tag')
|
||||||
|
console.log('Clicked tag:', tag)
|
||||||
|
|
||||||
if (selectedTags.has(tag)) {
|
if (selectedTags.has(tag)) {
|
||||||
selectedTags.delete(tag)
|
selectedTags.delete(tag)
|
||||||
btn.classList.remove('selected')
|
btn.classList.remove('selected')
|
||||||
@ -90,7 +107,18 @@ export default ((opts?: Partial<FolderContentOptions>) => {
|
|||||||
updateVisibility()
|
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 (
|
return (
|
||||||
@ -104,6 +132,7 @@ export default ((opts?: Partial<FolderContentOptions>) => {
|
|||||||
<button
|
<button
|
||||||
data-tag={tag}
|
data-tag={tag}
|
||||||
class="tag-filter-btn"
|
class="tag-filter-btn"
|
||||||
|
type="button"
|
||||||
>
|
>
|
||||||
{tag}
|
{tag}
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user