2023-08-17 12:04:15 +07:00
|
|
|
import { FilePath, joinSegments, slugifyFilePath } from "../../util/path"
|
2023-07-24 07:07:19 +07:00
|
|
|
import { QuartzEmitterPlugin } from "../types"
|
|
|
|
import path from "path"
|
|
|
|
import fs from "fs"
|
2023-08-17 12:04:15 +07:00
|
|
|
import { glob } from "../../util/glob"
|
2024-02-09 22:07:32 +07:00
|
|
|
import DepGraph from "../../depgraph"
|
2024-02-12 03:20:44 +07: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 07:07:19 +07:00
|
|
|
|
2023-08-03 12:10:13 +07:00
|
|
|
export const Assets: QuartzEmitterPlugin = () => {
|
2023-07-24 07:42:00 +07:00
|
|
|
return {
|
|
|
|
name: "Assets",
|
|
|
|
getQuartzComponents() {
|
|
|
|
return []
|
|
|
|
},
|
2024-02-09 22:07:32 +07:00
|
|
|
async getDependencyGraph(ctx, _content, _resources) {
|
|
|
|
const { argv, cfg } = ctx
|
|
|
|
const graph = new DepGraph<FilePath>()
|
|
|
|
|
2024-02-12 03:20:44 +07:00
|
|
|
const fps = await filesToCopy(argv, cfg)
|
2024-02-09 22:07:32 +07: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-19 01:56:14 +07:00
|
|
|
async emit({ argv, cfg }, _content, _resources): Promise<FilePath[]> {
|
2023-08-07 10:52:17 +07:00
|
|
|
const assetsPath = argv.output
|
2024-02-12 03:20:44 +07:00
|
|
|
const fps = await filesToCopy(argv, cfg)
|
2023-08-03 12:10:13 +07:00
|
|
|
const res: FilePath[] = []
|
|
|
|
for (const fp of fps) {
|
2023-07-24 07:42:00 +07:00
|
|
|
const ext = path.extname(fp)
|
2023-08-03 12:10:13 +07:00
|
|
|
const src = joinSegments(argv.directory, fp) as FilePath
|
2023-08-17 14:55:28 +07:00
|
|
|
const name = (slugifyFilePath(fp as FilePath, true) + ext) as FilePath
|
2023-07-24 07:42:00 +07:00
|
|
|
|
2023-08-03 12:10:13 +07:00
|
|
|
const dest = joinSegments(assetsPath, name) as FilePath
|
2023-07-24 07:42:00 +07: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 13:25:44 +07:00
|
|
|
res.push(dest)
|
2023-07-24 07:42:00 +07:00
|
|
|
}
|
|
|
|
|
2023-08-03 12:10:13 +07:00
|
|
|
return res
|
2023-07-24 07:42:00 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|