From 2602005e0d76d05f734fa964bd451bb627eeaba2 Mon Sep 17 00:00:00 2001 From: Thomas Hack Date: Fri, 16 Aug 2024 21:37:01 +0200 Subject: [PATCH] fixup! feat: add subfolders to folder page: add dates of latest file to subfolder --- quartz/components/pages/FolderContent.tsx | 37 ++++++++++++----------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/quartz/components/pages/FolderContent.tsx b/quartz/components/pages/FolderContent.tsx index 865e895fc..79ef87baf 100644 --- a/quartz/components/pages/FolderContent.tsx +++ b/quartz/components/pages/FolderContent.tsx @@ -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) => { 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 = new Set() + const allPagesInSubfolders: Map = 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) => { 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) => { 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)!, - }, - } -}