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

38 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-07-01 07:03:01 +00:00
import { QuartzComponentConstructor, QuartzComponentProps } from "../types"
2023-07-23 00:27:41 +00:00
import { Fragment, jsx, jsxs } from "preact/jsx-runtime"
2023-07-01 07:03:01 +00:00
import { toJsxRuntime } from "hast-util-to-jsx-runtime"
2023-07-23 00:27:41 +00:00
import style from "../styles/listPage.scss"
2023-07-01 07:03:01 +00:00
import { PageList } from "../PageList"
import { ServerSlug, canonicalizeServer } 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/")) {
const tag = canonicalizeServer(slug.slice("tags/".length) as ServerSlug)
2023-07-23 00:27:41 +00:00
const allPagesWithTag = allFiles.filter((file) => (file.frontmatter?.tags ?? []).includes(tag))
2023-07-01 07:03:01 +00:00
const listProps = {
...props,
2023-07-23 00:27:41 +00:00
allFiles: allPagesWithTag,
2023-07-01 07:03:01 +00:00
}
// @ts-ignore
2023-07-23 00:27:41 +00:00
const content = toJsxRuntime(tree, { Fragment, jsx, jsxs, elementAttributeNameCase: "html" })
return (
<div class="popover-hint">
<article>{content}</article>
<p>{allPagesWithTag.length} items with this tag.</p>
<div>
<PageList {...listProps} />
</div>
2023-07-01 07:03:01 +00:00
</div>
2023-07-23 00:27:41 +00:00
)
2023-07-01 07:03:01 +00:00
} else {
throw new Error(`Component "TagContent" tried to render a non-tag page: ${slug}`)
2023-07-01 07:03:01 +00:00
}
}
TagContent.css = style + PageList.css
export default (() => TagContent) satisfies QuartzComponentConstructor