2023-07-01 14:03:01 +07:00
|
|
|
import { QuartzEmitterPlugin } from "../types"
|
|
|
|
import { QuartzComponentProps } from "../../components/types"
|
|
|
|
import HeaderConstructor from "../../components/Header"
|
|
|
|
import BodyConstructor from "../../components/Body"
|
|
|
|
import { pageResources, renderPage } from "../../components/renderPage"
|
|
|
|
import { ProcessedContent, defaultProcessedContent } from "../vfile"
|
|
|
|
import { FullPageLayout } from "../../cfg"
|
|
|
|
import path from "path"
|
2023-08-20 08:04:17 +07:00
|
|
|
import {
|
|
|
|
FilePath,
|
|
|
|
FullSlug,
|
|
|
|
SimpleSlug,
|
|
|
|
_stripSlashes,
|
|
|
|
joinSegments,
|
2023-09-13 11:29:57 +07:00
|
|
|
pathToRoot,
|
2023-08-20 08:04:17 +07:00
|
|
|
simplifySlug,
|
|
|
|
} from "../../util/path"
|
2023-07-26 13:37:24 +07:00
|
|
|
import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout"
|
|
|
|
import { FolderContent } from "../../components"
|
2024-01-19 01:56:14 +07:00
|
|
|
import { write } from "./helpers"
|
2023-07-01 14:03:01 +07:00
|
|
|
|
2024-01-27 04:40:37 +07:00
|
|
|
export const FolderPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOpts) => {
|
2023-07-26 13:37:24 +07:00
|
|
|
const opts: FullPageLayout = {
|
|
|
|
...sharedPageComponents,
|
|
|
|
...defaultListPageLayout,
|
|
|
|
pageBody: FolderContent(),
|
|
|
|
...userOpts,
|
2023-07-01 14:03:01 +07:00
|
|
|
}
|
|
|
|
|
2023-07-26 13:37:24 +07:00
|
|
|
const { head: Head, header, beforeBody, pageBody, left, right, footer: Footer } = opts
|
2023-07-01 14:03:01 +07:00
|
|
|
const Header = HeaderConstructor()
|
|
|
|
const Body = BodyConstructor()
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: "FolderPage",
|
|
|
|
getQuartzComponents() {
|
2023-07-26 13:37:24 +07:00
|
|
|
return [Head, Header, Body, ...header, ...beforeBody, pageBody, ...left, ...right, Footer]
|
2023-07-01 14:03:01 +07:00
|
|
|
},
|
2024-01-19 01:56:14 +07:00
|
|
|
async emit(ctx, content, resources): Promise<FilePath[]> {
|
2023-07-16 13:02:12 +07:00
|
|
|
const fps: FilePath[] = []
|
2023-07-23 07:27:41 +07:00
|
|
|
const allFiles = content.map((c) => c[1].data)
|
2023-07-24 07:07:19 +07:00
|
|
|
const cfg = ctx.cfg.configuration
|
2023-07-01 14:03:01 +07:00
|
|
|
|
2023-08-20 05:52:25 +07:00
|
|
|
const folders: Set<SimpleSlug> = new Set(
|
2023-07-23 07:27:41 +07:00
|
|
|
allFiles.flatMap((data) => {
|
|
|
|
const slug = data.slug
|
2023-08-20 05:52:25 +07:00
|
|
|
const folderName = path.dirname(slug ?? "") as SimpleSlug
|
2023-07-23 07:27:41 +07:00
|
|
|
if (slug && folderName !== "." && folderName !== "tags") {
|
|
|
|
return [folderName]
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}),
|
|
|
|
)
|
2023-07-01 14:03:01 +07:00
|
|
|
|
2023-07-23 07:27:41 +07:00
|
|
|
const folderDescriptions: Record<string, ProcessedContent> = Object.fromEntries(
|
|
|
|
[...folders].map((folder) => [
|
|
|
|
folder,
|
|
|
|
defaultProcessedContent({
|
2023-08-20 05:52:25 +07:00
|
|
|
slug: joinSegments(folder, "index") as FullSlug,
|
2023-07-23 07:27:41 +07:00
|
|
|
frontmatter: { title: `Folder: ${folder}`, tags: [] },
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
)
|
2023-07-01 14:03:01 +07:00
|
|
|
|
|
|
|
for (const [tree, file] of content) {
|
2023-08-20 06:55:36 +07:00
|
|
|
const slug = _stripSlashes(simplifySlug(file.data.slug!)) as SimpleSlug
|
2023-07-01 14:03:01 +07:00
|
|
|
if (folders.has(slug)) {
|
|
|
|
folderDescriptions[slug] = [tree, file]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const folder of folders) {
|
2023-08-20 05:52:25 +07:00
|
|
|
const slug = joinSegments(folder, "index") as FullSlug
|
2023-09-13 11:29:57 +07:00
|
|
|
const externalResources = pageResources(pathToRoot(slug), resources)
|
2023-07-01 14:03:01 +07:00
|
|
|
const [tree, file] = folderDescriptions[folder]
|
|
|
|
const componentData: QuartzComponentProps = {
|
|
|
|
fileData: file.data,
|
|
|
|
externalResources,
|
|
|
|
cfg,
|
|
|
|
children: [],
|
|
|
|
tree,
|
2023-07-23 07:27:41 +07:00
|
|
|
allFiles,
|
2023-07-01 14:03:01 +07:00
|
|
|
}
|
|
|
|
|
2023-07-23 07:27:41 +07:00
|
|
|
const content = renderPage(slug, componentData, opts, externalResources)
|
2024-01-19 01:56:14 +07:00
|
|
|
const fp = await write({
|
|
|
|
ctx,
|
2023-07-01 14:03:01 +07:00
|
|
|
content,
|
2023-08-20 05:52:25 +07:00
|
|
|
slug,
|
2023-07-01 14:03:01 +07:00
|
|
|
ext: ".html",
|
|
|
|
})
|
|
|
|
|
|
|
|
fps.push(fp)
|
|
|
|
}
|
|
|
|
return fps
|
2023-07-23 07:27:41 +07:00
|
|
|
},
|
2023-07-01 14:03:01 +07:00
|
|
|
}
|
|
|
|
}
|