notes/quartz/components/pages/FolderContent.tsx

110 lines
3.5 KiB
TypeScript
Raw Normal View History

import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "../types"
2023-07-01 00:03:01 -07:00
import path from "path"
2023-07-22 17:27:41 -07:00
import style from "../styles/listPage.scss"
import { byDateAndAlphabetical, PageList, SortFn } from "../PageList"
import { stripSlashes, simplifySlug, joinSegments, FullSlug } from "../../util/path"
import { Root } from "hast"
import { htmlToJsx } from "../../util/jsx"
import { i18n } from "../../i18n"
import { QuartzPluginData } from "../../plugins/vfile"
chore(deps): bump the production-dependencies group with 8 updates (#1788) * chore(deps): bump the production-dependencies group with 8 updates Bumps the production-dependencies group with 8 updates: | Package | From | To | | --- | --- | --- | | [hast-util-to-html](https://github.com/syntax-tree/hast-util-to-html) | `9.0.4` | `9.0.5` | | [hast-util-to-jsx-runtime](https://github.com/syntax-tree/hast-util-to-jsx-runtime) | `2.3.2` | `2.3.4` | | [preact](https://github.com/preactjs/preact) | `10.26.0` | `10.26.2` | | [rehype-mathjax](https://github.com/remarkjs/remark-math) | `7.0.0` | `7.1.0` | | [ws](https://github.com/websockets/ws) | `8.18.0` | `8.18.1` | | [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) | `22.13.4` | `22.13.5` | | [prettier](https://github.com/prettier/prettier) | `3.5.1` | `3.5.2` | | [tsx](https://github.com/privatenumber/tsx) | `4.19.2` | `4.19.3` | Updates `hast-util-to-html` from 9.0.4 to 9.0.5 - [Release notes](https://github.com/syntax-tree/hast-util-to-html/releases) - [Commits](https://github.com/syntax-tree/hast-util-to-html/compare/9.0.4...9.0.5) Updates `hast-util-to-jsx-runtime` from 2.3.2 to 2.3.4 - [Release notes](https://github.com/syntax-tree/hast-util-to-jsx-runtime/releases) - [Commits](https://github.com/syntax-tree/hast-util-to-jsx-runtime/compare/2.3.2...2.3.4) Updates `preact` from 10.26.0 to 10.26.2 - [Release notes](https://github.com/preactjs/preact/releases) - [Commits](https://github.com/preactjs/preact/compare/10.26.0...10.26.2) Updates `rehype-mathjax` from 7.0.0 to 7.1.0 - [Release notes](https://github.com/remarkjs/remark-math/releases) - [Commits](https://github.com/remarkjs/remark-math/compare/rehype-mathjax@7.0.0...rehype-mathjax@7.1.0) Updates `ws` from 8.18.0 to 8.18.1 - [Release notes](https://github.com/websockets/ws/releases) - [Commits](https://github.com/websockets/ws/compare/8.18.0...8.18.1) Updates `@types/node` from 22.13.4 to 22.13.5 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) Updates `prettier` from 3.5.1 to 3.5.2 - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/3.5.1...3.5.2) Updates `tsx` from 4.19.2 to 4.19.3 - [Release notes](https://github.com/privatenumber/tsx/releases) - [Changelog](https://github.com/privatenumber/tsx/blob/master/release.config.cjs) - [Commits](https://github.com/privatenumber/tsx/compare/v4.19.2...v4.19.3) --- updated-dependencies: - dependency-name: hast-util-to-html dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: hast-util-to-jsx-runtime dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: preact dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: rehype-mathjax dependency-type: direct:production update-type: version-update:semver-minor dependency-group: production-dependencies - dependency-name: ws dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: "@types/node" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: prettier dependency-type: direct:development update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: tsx dependency-type: direct:development update-type: version-update:semver-patch dependency-group: production-dependencies ... Signed-off-by: dependabot[bot] <support@github.com> * chore: update type error Signed-off-by: Aaron Pham <contact@aarnphm.xyz> --------- Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Aaron Pham <contact@aarnphm.xyz> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Aaron Pham <contact@aarnphm.xyz>
2025-02-25 16:52:52 -05:00
import { ComponentChildren } from "preact"
2023-07-01 00:03:01 -07:00
interface FolderContentOptions {
/**
* Whether to display number of folders
*/
showFolderCount: boolean
showSubfolders: boolean
sort?: SortFn
}
2023-07-22 17:27:41 -07:00
const defaultOptions: FolderContentOptions = {
showFolderCount: true,
showSubfolders: true,
2023-07-01 00:03:01 -07:00
}
export default ((opts?: Partial<FolderContentOptions>) => {
const options: FolderContentOptions = { ...defaultOptions, ...opts }
const FolderContent: QuartzComponent = (props: QuartzComponentProps) => {
const { tree, fileData, allFiles, cfg } = props
const folderSlug = stripSlashes(simplifySlug(fileData.slug!))
const folderParts = folderSlug.split(path.posix.sep)
const allPagesInFolder: QuartzPluginData[] = []
const allPagesInSubfolders: Map<FullSlug, QuartzPluginData[]> = new Map()
allFiles.forEach((file) => {
const fileSlug = stripSlashes(simplifySlug(file.slug!))
const prefixed = fileSlug.startsWith(folderSlug) && fileSlug !== folderSlug
const fileParts = fileSlug.split(path.posix.sep)
const isDirectChild = fileParts.length === folderParts.length + 1
if (!prefixed) {
return
}
if (isDirectChild) {
allPagesInFolder.push(file)
} else if (options.showSubfolders) {
const subfolderSlug = joinSegments(
...fileParts.slice(0, folderParts.length + 1),
) as FullSlug
const pagesInFolder = allPagesInSubfolders.get(subfolderSlug) || []
allPagesInSubfolders.set(subfolderSlug, [...pagesInFolder, file])
}
})
allPagesInSubfolders.forEach((files, subfolderSlug) => {
const hasIndex = allPagesInFolder.some(
(file) => subfolderSlug === stripSlashes(simplifySlug(file.slug!)),
)
if (!hasIndex) {
const subfolderDates = files.sort(byDateAndAlphabetical(cfg))[0].dates
const subfolderTitle = subfolderSlug.split(path.posix.sep).at(-1)!
allPagesInFolder.push({
slug: subfolderSlug,
dates: subfolderDates,
frontmatter: { title: subfolderTitle, tags: ["folder"] },
})
}
})
const cssClasses: string[] = fileData.frontmatter?.cssclasses ?? []
const classes = cssClasses.join(" ")
const listProps = {
...props,
sort: options.sort,
allFiles: allPagesInFolder,
}
chore(deps): bump the production-dependencies group with 8 updates (#1788) * chore(deps): bump the production-dependencies group with 8 updates Bumps the production-dependencies group with 8 updates: | Package | From | To | | --- | --- | --- | | [hast-util-to-html](https://github.com/syntax-tree/hast-util-to-html) | `9.0.4` | `9.0.5` | | [hast-util-to-jsx-runtime](https://github.com/syntax-tree/hast-util-to-jsx-runtime) | `2.3.2` | `2.3.4` | | [preact](https://github.com/preactjs/preact) | `10.26.0` | `10.26.2` | | [rehype-mathjax](https://github.com/remarkjs/remark-math) | `7.0.0` | `7.1.0` | | [ws](https://github.com/websockets/ws) | `8.18.0` | `8.18.1` | | [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) | `22.13.4` | `22.13.5` | | [prettier](https://github.com/prettier/prettier) | `3.5.1` | `3.5.2` | | [tsx](https://github.com/privatenumber/tsx) | `4.19.2` | `4.19.3` | Updates `hast-util-to-html` from 9.0.4 to 9.0.5 - [Release notes](https://github.com/syntax-tree/hast-util-to-html/releases) - [Commits](https://github.com/syntax-tree/hast-util-to-html/compare/9.0.4...9.0.5) Updates `hast-util-to-jsx-runtime` from 2.3.2 to 2.3.4 - [Release notes](https://github.com/syntax-tree/hast-util-to-jsx-runtime/releases) - [Commits](https://github.com/syntax-tree/hast-util-to-jsx-runtime/compare/2.3.2...2.3.4) Updates `preact` from 10.26.0 to 10.26.2 - [Release notes](https://github.com/preactjs/preact/releases) - [Commits](https://github.com/preactjs/preact/compare/10.26.0...10.26.2) Updates `rehype-mathjax` from 7.0.0 to 7.1.0 - [Release notes](https://github.com/remarkjs/remark-math/releases) - [Commits](https://github.com/remarkjs/remark-math/compare/rehype-mathjax@7.0.0...rehype-mathjax@7.1.0) Updates `ws` from 8.18.0 to 8.18.1 - [Release notes](https://github.com/websockets/ws/releases) - [Commits](https://github.com/websockets/ws/compare/8.18.0...8.18.1) Updates `@types/node` from 22.13.4 to 22.13.5 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) Updates `prettier` from 3.5.1 to 3.5.2 - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/3.5.1...3.5.2) Updates `tsx` from 4.19.2 to 4.19.3 - [Release notes](https://github.com/privatenumber/tsx/releases) - [Changelog](https://github.com/privatenumber/tsx/blob/master/release.config.cjs) - [Commits](https://github.com/privatenumber/tsx/compare/v4.19.2...v4.19.3) --- updated-dependencies: - dependency-name: hast-util-to-html dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: hast-util-to-jsx-runtime dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: preact dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: rehype-mathjax dependency-type: direct:production update-type: version-update:semver-minor dependency-group: production-dependencies - dependency-name: ws dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: "@types/node" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: prettier dependency-type: direct:development update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: tsx dependency-type: direct:development update-type: version-update:semver-patch dependency-group: production-dependencies ... Signed-off-by: dependabot[bot] <support@github.com> * chore: update type error Signed-off-by: Aaron Pham <contact@aarnphm.xyz> --------- Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Aaron Pham <contact@aarnphm.xyz> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Aaron Pham <contact@aarnphm.xyz>
2025-02-25 16:52:52 -05:00
const content = (
(tree as Root).children.length === 0
? fileData.description
: htmlToJsx(fileData.filePath!, tree)
chore(deps): bump the production-dependencies group with 8 updates (#1788) * chore(deps): bump the production-dependencies group with 8 updates Bumps the production-dependencies group with 8 updates: | Package | From | To | | --- | --- | --- | | [hast-util-to-html](https://github.com/syntax-tree/hast-util-to-html) | `9.0.4` | `9.0.5` | | [hast-util-to-jsx-runtime](https://github.com/syntax-tree/hast-util-to-jsx-runtime) | `2.3.2` | `2.3.4` | | [preact](https://github.com/preactjs/preact) | `10.26.0` | `10.26.2` | | [rehype-mathjax](https://github.com/remarkjs/remark-math) | `7.0.0` | `7.1.0` | | [ws](https://github.com/websockets/ws) | `8.18.0` | `8.18.1` | | [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) | `22.13.4` | `22.13.5` | | [prettier](https://github.com/prettier/prettier) | `3.5.1` | `3.5.2` | | [tsx](https://github.com/privatenumber/tsx) | `4.19.2` | `4.19.3` | Updates `hast-util-to-html` from 9.0.4 to 9.0.5 - [Release notes](https://github.com/syntax-tree/hast-util-to-html/releases) - [Commits](https://github.com/syntax-tree/hast-util-to-html/compare/9.0.4...9.0.5) Updates `hast-util-to-jsx-runtime` from 2.3.2 to 2.3.4 - [Release notes](https://github.com/syntax-tree/hast-util-to-jsx-runtime/releases) - [Commits](https://github.com/syntax-tree/hast-util-to-jsx-runtime/compare/2.3.2...2.3.4) Updates `preact` from 10.26.0 to 10.26.2 - [Release notes](https://github.com/preactjs/preact/releases) - [Commits](https://github.com/preactjs/preact/compare/10.26.0...10.26.2) Updates `rehype-mathjax` from 7.0.0 to 7.1.0 - [Release notes](https://github.com/remarkjs/remark-math/releases) - [Commits](https://github.com/remarkjs/remark-math/compare/rehype-mathjax@7.0.0...rehype-mathjax@7.1.0) Updates `ws` from 8.18.0 to 8.18.1 - [Release notes](https://github.com/websockets/ws/releases) - [Commits](https://github.com/websockets/ws/compare/8.18.0...8.18.1) Updates `@types/node` from 22.13.4 to 22.13.5 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) Updates `prettier` from 3.5.1 to 3.5.2 - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/3.5.1...3.5.2) Updates `tsx` from 4.19.2 to 4.19.3 - [Release notes](https://github.com/privatenumber/tsx/releases) - [Changelog](https://github.com/privatenumber/tsx/blob/master/release.config.cjs) - [Commits](https://github.com/privatenumber/tsx/compare/v4.19.2...v4.19.3) --- updated-dependencies: - dependency-name: hast-util-to-html dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: hast-util-to-jsx-runtime dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: preact dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: rehype-mathjax dependency-type: direct:production update-type: version-update:semver-minor dependency-group: production-dependencies - dependency-name: ws dependency-type: direct:production update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: "@types/node" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: prettier dependency-type: direct:development update-type: version-update:semver-patch dependency-group: production-dependencies - dependency-name: tsx dependency-type: direct:development update-type: version-update:semver-patch dependency-group: production-dependencies ... Signed-off-by: dependabot[bot] <support@github.com> * chore: update type error Signed-off-by: Aaron Pham <contact@aarnphm.xyz> --------- Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Aaron Pham <contact@aarnphm.xyz> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Aaron Pham <contact@aarnphm.xyz>
2025-02-25 16:52:52 -05:00
) as ComponentChildren
return (
<div class="popover-hint">
<article class={classes}>{content}</article>
<div class="page-listing">
{options.showFolderCount && (
<p>
{i18n(cfg.locale).pages.folderContent.itemsUnderFolder({
count: allPagesInFolder.length,
})}
</p>
)}
<div>
<PageList {...listProps} />
</div>
</div>
</div>
)
}
FolderContent.css = style + PageList.css
return FolderContent
}) satisfies QuartzComponentConstructor