2023-08-17 05:04:15 +00:00
|
|
|
import { FilePath, joinSegments, slugifyFilePath } from "../../util/path"
|
2023-07-24 00:07:19 +00:00
|
|
|
import { QuartzEmitterPlugin } from "../types"
|
|
|
|
import path from "path"
|
|
|
|
import fs from "fs"
|
2023-08-17 05:04:15 +00:00
|
|
|
import { glob } from "../../util/glob"
|
2024-02-09 15:07:32 +00:00
|
|
|
import DepGraph from "../../depgraph"
|
2024-02-11 20:20:44 +00:00
|
|
|
import { Argv } from "../../util/ctx"
|
|
|
|
import { QuartzConfig } from "../../cfg"
|
|
|
|
|
|
|
|
const filesToCopy = async (argv: Argv, cfg: QuartzConfig) => {
|
|
|
|
// glob all non MD files in content folder and copy it over
|
|
|
|
return await glob("**", argv.directory, ["**/*.md", ...cfg.configuration.ignorePatterns])
|
|
|
|
}
|
2023-07-24 00:07:19 +00:00
|
|
|
|
2023-08-03 05:10:13 +00:00
|
|
|
export const Assets: QuartzEmitterPlugin = () => {
|
2023-07-24 00:42:00 +00:00
|
|
|
return {
|
|
|
|
name: "Assets",
|
|
|
|
getQuartzComponents() {
|
|
|
|
return []
|
|
|
|
},
|
2024-02-09 15:07:32 +00:00
|
|
|
async getDependencyGraph(ctx, _content, _resources) {
|
|
|
|
const { argv, cfg } = ctx
|
|
|
|
const graph = new DepGraph<FilePath>()
|
|
|
|
|
2024-02-11 20:20:44 +00:00
|
|
|
const fps = await filesToCopy(argv, cfg)
|
2024-02-09 15:07:32 +00:00
|
|
|
|
|
|
|
for (const fp of fps) {
|
|
|
|
const ext = path.extname(fp)
|
|
|
|
const src = joinSegments(argv.directory, fp) as FilePath
|
|
|
|
const name = (slugifyFilePath(fp as FilePath, true) + ext) as FilePath
|
|
|
|
|
|
|
|
const dest = joinSegments(argv.output, name) as FilePath
|
|
|
|
|
|
|
|
graph.addEdge(src, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return graph
|
|
|
|
},
|
2024-01-18 18:56:14 +00:00
|
|
|
async emit({ argv, cfg }, _content, _resources): Promise<FilePath[]> {
|
2023-08-07 03:52:17 +00:00
|
|
|
const assetsPath = argv.output
|
2024-02-11 20:20:44 +00:00
|
|
|
const fps = await filesToCopy(argv, cfg)
|
2023-08-03 05:10:13 +00:00
|
|
|
const res: FilePath[] = []
|
|
|
|
for (const fp of fps) {
|
2023-07-24 00:42:00 +00:00
|
|
|
const ext = path.extname(fp)
|
2023-08-03 05:10:13 +00:00
|
|
|
const src = joinSegments(argv.directory, fp) as FilePath
|
2023-08-17 07:55:28 +00:00
|
|
|
const name = (slugifyFilePath(fp as FilePath, true) + ext) as FilePath
|
2023-07-24 00:42:00 +00:00
|
|
|
|
2023-08-03 05:10:13 +00:00
|
|
|
const dest = joinSegments(assetsPath, name) as FilePath
|
2023-07-24 00:42:00 +00:00
|
|
|
const dir = path.dirname(dest) as FilePath
|
|
|
|
await fs.promises.mkdir(dir, { recursive: true }) // ensure dir exists
|
|
|
|
await fs.promises.copyFile(src, dest)
|
2023-08-12 06:25:44 +00:00
|
|
|
res.push(dest)
|
2023-07-24 00:42:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 05:10:13 +00:00
|
|
|
return res
|
2023-07-24 00:42:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|