mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-30 16:24:06 -06:00
fixup! feat: add subfolders to folder page: add dates of latest file to subfolder
This commit is contained in:
parent
2252f3bb43
commit
2602005e0d
@ -2,8 +2,8 @@ import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } fro
|
|||||||
import path from "path"
|
import path from "path"
|
||||||
|
|
||||||
import style from "../styles/listPage.scss"
|
import style from "../styles/listPage.scss"
|
||||||
import { PageList, SortFn } from "../PageList"
|
import { byDateAndAlphabetical, PageList, SortFn } from "../PageList"
|
||||||
import { stripSlashes, simplifySlug, SimpleSlug, joinSegments, FullSlug } from "../../util/path"
|
import { stripSlashes, simplifySlug, joinSegments, FullSlug } from "../../util/path"
|
||||||
import { Root } from "hast"
|
import { Root } from "hast"
|
||||||
import { htmlToJsx } from "../../util/jsx"
|
import { htmlToJsx } from "../../util/jsx"
|
||||||
import { i18n } from "../../i18n"
|
import { i18n } from "../../i18n"
|
||||||
@ -29,14 +29,14 @@ export default ((opts?: Partial<FolderContentOptions>) => {
|
|||||||
const FolderContent: QuartzComponent = (props: QuartzComponentProps) => {
|
const FolderContent: QuartzComponent = (props: QuartzComponentProps) => {
|
||||||
const { tree, fileData, allFiles, cfg } = props
|
const { tree, fileData, allFiles, cfg } = props
|
||||||
const folderSlug = stripSlashes(simplifySlug(fileData.slug!))
|
const folderSlug = stripSlashes(simplifySlug(fileData.slug!))
|
||||||
|
const folderParts = folderSlug.split(path.posix.sep)
|
||||||
|
|
||||||
const allPagesInFolder: QuartzPluginData[] = []
|
const allPagesInFolder: QuartzPluginData[] = []
|
||||||
const allSubfolders: Set<FullSlug> = new Set()
|
const allPagesInSubfolders: Map<FullSlug, QuartzPluginData[]> = new Map()
|
||||||
|
|
||||||
allFiles.forEach((file) => {
|
allFiles.forEach((file) => {
|
||||||
const fileSlug = stripSlashes(simplifySlug(file.slug!))
|
const fileSlug = stripSlashes(simplifySlug(file.slug!))
|
||||||
const prefixed = fileSlug.startsWith(folderSlug) && fileSlug !== folderSlug
|
const prefixed = fileSlug.startsWith(folderSlug) && fileSlug !== folderSlug
|
||||||
const folderParts = folderSlug.split(path.posix.sep)
|
|
||||||
const fileParts = fileSlug.split(path.posix.sep)
|
const fileParts = fileSlug.split(path.posix.sep)
|
||||||
const isDirectChild = fileParts.length === folderParts.length + 1
|
const isDirectChild = fileParts.length === folderParts.length + 1
|
||||||
|
|
||||||
@ -47,14 +47,24 @@ export default ((opts?: Partial<FolderContentOptions>) => {
|
|||||||
if (isDirectChild) {
|
if (isDirectChild) {
|
||||||
allPagesInFolder.push(file)
|
allPagesInFolder.push(file)
|
||||||
} else if (options.showSubfolders) {
|
} else if (options.showSubfolders) {
|
||||||
const folderSlug = joinSegments(...fileParts.slice(0, folderParts.length + 1)) as FullSlug
|
const subfolderSlug = joinSegments(
|
||||||
if (!allSubfolders.has(folderSlug)) {
|
...fileParts.slice(0, folderParts.length + 1),
|
||||||
allPagesInFolder.push(_createFolderData(folderSlug))
|
) as FullSlug
|
||||||
allSubfolders.add(folderSlug)
|
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 cssClasses: string[] = fileData.frontmatter?.cssclasses ?? []
|
||||||
const classes = ["popover-hint", ...cssClasses].join(" ")
|
const classes = ["popover-hint", ...cssClasses].join(" ")
|
||||||
const listProps = {
|
const listProps = {
|
||||||
@ -90,12 +100,3 @@ export default ((opts?: Partial<FolderContentOptions>) => {
|
|||||||
FolderContent.css = style + PageList.css
|
FolderContent.css = style + PageList.css
|
||||||
return FolderContent
|
return FolderContent
|
||||||
}) satisfies QuartzComponentConstructor
|
}) satisfies QuartzComponentConstructor
|
||||||
|
|
||||||
function _createFolderData(folderSlug: FullSlug): QuartzPluginData {
|
|
||||||
return {
|
|
||||||
slug: folderSlug,
|
|
||||||
frontmatter: {
|
|
||||||
title: folderSlug.split(path.posix.sep).at(-1)!,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user