quartz-research-note/quartz/components/pages/TagContent.tsx

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-07-01 07:03:01 +00:00
import { QuartzComponentConstructor, QuartzComponentProps } from "../types"
import { Fragment, jsx, jsxs } from 'preact/jsx-runtime'
import { toJsxRuntime } from "hast-util-to-jsx-runtime"
import style from '../styles/listPage.scss'
import { PageList } from "../PageList"
2023-07-13 07:19:35 +00:00
import { toServerSlug } from "../../path"
2023-07-01 07:03:01 +00:00
function TagContent(props: QuartzComponentProps) {
const { tree, fileData, allFiles } = props
const slug = fileData.slug
if (slug?.startsWith("tags/")) {
2023-07-13 07:19:35 +00:00
const tag = toServerSlug(slug.slice("tags/".length))
2023-07-01 07:03:01 +00:00
const allPagesWithTag = allFiles.filter(file => (file.frontmatter?.tags ?? []).includes(tag))
const listProps = {
...props,
allFiles: allPagesWithTag
}
// @ts-ignore
const content = toJsxRuntime(tree, { Fragment, jsx, jsxs, elementAttributeNameCase: 'html' })
return <div class="popover-hint">
2023-07-01 07:03:01 +00:00
<article>{content}</article>
<p>{allPagesWithTag.length} items with this tag.</p>
2023-07-01 07:03:01 +00:00
<div>
<PageList {...listProps} />
</div>
</div>
} else {
throw `Component "TagContent" tried to render a non-tag page: ${slug}`
}
}
TagContent.css = style + PageList.css
export default (() => TagContent) satisfies QuartzComponentConstructor