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