fixup! feat: add subfolders to folder page: add dates of latest file to subfolder

This commit is contained in:
Thomas Hack 2024-08-16 21:37:01 +02:00
parent 2252f3bb43
commit 2602005e0d

View File

@ -2,8 +2,8 @@ import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } fro
import path from "path"
import style from "../styles/listPage.scss"
import { PageList, SortFn } from "../PageList"
import { stripSlashes, simplifySlug, SimpleSlug, joinSegments, FullSlug } from "../../util/path"
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"
@ -29,14 +29,14 @@ export default ((opts?: Partial<FolderContentOptions>) => {
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 allSubfolders: Set<FullSlug> = new Set()
const allPagesInSubfolders: Map<FullSlug, QuartzPluginData[]> = new Map()
allFiles.forEach((file) => {
const fileSlug = stripSlashes(simplifySlug(file.slug!))
const prefixed = fileSlug.startsWith(folderSlug) && fileSlug !== folderSlug
const folderParts = folderSlug.split(path.posix.sep)
const fileParts = fileSlug.split(path.posix.sep)
const isDirectChild = fileParts.length === folderParts.length + 1
@ -47,14 +47,24 @@ export default ((opts?: Partial<FolderContentOptions>) => {
if (isDirectChild) {
allPagesInFolder.push(file)
} else if (options.showSubfolders) {
const folderSlug = joinSegments(...fileParts.slice(0, folderParts.length + 1)) as FullSlug
if (!allSubfolders.has(folderSlug)) {
allPagesInFolder.push(_createFolderData(folderSlug))
allSubfolders.add(folderSlug)
}
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 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 },
})
})
const cssClasses: string[] = fileData.frontmatter?.cssclasses ?? []
const classes = ["popover-hint", ...cssClasses].join(" ")
const listProps = {
@ -90,12 +100,3 @@ export default ((opts?: Partial<FolderContentOptions>) => {
FolderContent.css = style + PageList.css
return FolderContent
}) satisfies QuartzComponentConstructor
function _createFolderData(folderSlug: FullSlug): QuartzPluginData {
return {
slug: folderSlug,
frontmatter: {
title: folderSlug.split(path.posix.sep).at(-1)!,
},
}
}