From 8fd75ffbfda30edd5a134a1fbf9b81ac3cebb2ff Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Sun, 23 Jul 2023 17:42:00 -0700 Subject: [PATCH] support attachments folder --- content/features/upcoming features.md | 2 + quartz.config.ts | 2 +- quartz/plugins/emitters/assets.ts | 73 +++++++++++++++++---------- quartz/processors/emit.ts | 1 - 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/content/features/upcoming features.md b/content/features/upcoming features.md index 65fb56dac..fbfccf86f 100644 --- a/content/features/upcoming features.md +++ b/content/features/upcoming features.md @@ -5,6 +5,8 @@ draft: true ## high priority - attachments path +- inspect bundle size generated by esbuild (it shouldnt be that high) +- component resources should be emitted by an emitter - https://help.obsidian.md/Editing+and+formatting/Tags#Nested+tags nested tags?? - watch mode for config/source code - https://help.obsidian.md/Editing+and+formatting/Basic+formatting+syntax#Task+lists task list styling diff --git a/quartz.config.ts b/quartz.config.ts index 6ead8a0e5..0bbc18136 100644 --- a/quartz.config.ts +++ b/quartz.config.ts @@ -114,7 +114,7 @@ const config: QuartzConfig = { enableSiteMap: true, enableRSS: true, }), - Plugin.Assets(), + Plugin.Assets({ attachmentsFolder: "attachments" }), Plugin.Static(), ], }, diff --git a/quartz/plugins/emitters/assets.ts b/quartz/plugins/emitters/assets.ts index 51c8d8446..d231d1d23 100644 --- a/quartz/plugins/emitters/assets.ts +++ b/quartz/plugins/emitters/assets.ts @@ -4,31 +4,52 @@ import { QuartzEmitterPlugin } from "../types" import path from "path" import fs from "fs" -export const Assets: QuartzEmitterPlugin = () => ({ - name: "Assets", - getQuartzComponents() { - return [] - }, - async emit({ argv }, _content, _resources, _emit): Promise { - // glob all non MD/MDX/HTML files in content folder and copy it over - const assetsPath = path.join(argv.output, "assets") +interface Options { + attachmentsFolder: string | null +} - const fps: FilePath[] = [] - for await (const rawFp of globbyStream("**", { - ignore: ["**/*.md"], - cwd: argv.directory, - })) { - const fp = rawFp as FilePath - const ext = path.extname(fp) - const src = path.join(argv.directory, fp) as FilePath - const name = (slugifyFilePath(fp as FilePath) + ext) as FilePath - const dest = path.join(assetsPath, name) as FilePath - const dir = path.dirname(dest) as FilePath - await fs.promises.mkdir(dir, { recursive: true }) // ensure dir exists - await fs.promises.copyFile(src, dest) - fps.push(path.join("assets", fp) as FilePath) - } +const defaultOptions: Options = { + attachmentsFolder: null, +} - return fps - }, -}) +export const Assets: QuartzEmitterPlugin = (userOpts?: Options) => { + const { attachmentsFolder } = { ...defaultOptions, ...userOpts } + + return { + name: "Assets", + getQuartzComponents() { + return [] + }, + async emit({ argv }, _content, _resources, _emit): Promise { + // glob all non MD/MDX/HTML files in content folder and copy it over + const assetsPath = path.join(argv.output, "assets") + + const fps: FilePath[] = [] + for await (const rawFp of globbyStream("**", { + ignore: ["**/*.md"], + cwd: argv.directory, + })) { + const fp = rawFp as FilePath + const ext = path.extname(fp) + const src = path.join(argv.directory, fp) as FilePath + let name = (slugifyFilePath(fp as FilePath) + ext) as FilePath + + if (attachmentsFolder) { + const segments = name.split("/") + if (segments.at(-2) === attachmentsFolder) { + segments.splice(-2, 1) + name = segments.join("/") as FilePath + } + } + + const dest = path.join(assetsPath, name) as FilePath + const dir = path.dirname(dest) as FilePath + await fs.promises.mkdir(dir, { recursive: true }) // ensure dir exists + await fs.promises.copyFile(src, dest) + fps.push(path.join("assets", fp) as FilePath) + } + + return fps + }, + } +} diff --git a/quartz/processors/emit.ts b/quartz/processors/emit.ts index 4cb25f697..ea7fda9e9 100644 --- a/quartz/processors/emit.ts +++ b/quartz/processors/emit.ts @@ -86,7 +86,6 @@ function addGlobalPageResources( export async function emitContent(ctx: BuildCtx, content: ProcessedContent[]) { const { argv, cfg } = ctx - const contentFolder = argv.directory const perf = new PerfTimer() const log = new QuartzLogger(ctx.argv.verbose)