refactor static and asset emission to be actual emitter plugins

This commit is contained in:
Jacky Zhao 2023-07-23 17:07:19 -07:00
parent 000eb4c3c0
commit 9e83af04a7
18 changed files with 197 additions and 173 deletions

View file

@ -13,12 +13,12 @@ export const AliasRedirects: QuartzEmitterPlugin = () => ({
getQuartzComponents() {
return []
},
async emit(contentFolder, _cfg, content, _resources, emit): Promise<FilePath[]> {
async emit({argv}, content, _resources, emit): Promise<FilePath[]> {
const fps: FilePath[] = []
for (const [_tree, file] of content) {
const ogSlug = canonicalizeServer(file.data.slug!)
const dir = path.relative(contentFolder, file.dirname ?? contentFolder)
const dir = path.relative(argv.directory, file.dirname ?? argv.directory)
let aliases: CanonicalSlug[] = []
if (file.data.frontmatter?.aliases) {

View file

@ -0,0 +1,36 @@
import { globbyStream } from "globby"
import {
FilePath, slugifyFilePath,
} from "../../path"
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<FilePath[]> {
// 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
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)
}
return fps
},
})

View file

@ -68,7 +68,8 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
opts = { ...defaultOptions, ...opts }
return {
name: "ContentIndex",
async emit(_contentDir, cfg, content, _resources, emit) {
async emit(ctx, content, _resources, emit) {
const cfg = ctx.cfg.configuration
const emitted: FilePath[] = []
const linkIndex: ContentIndex = new Map()
for (const [_tree, file] of content) {

View file

@ -22,7 +22,8 @@ export const ContentPage: QuartzEmitterPlugin<FullPageLayout> = (opts) => {
getQuartzComponents() {
return [Head, Header, Body, ...header, ...beforeBody, Content, ...left, ...right, Footer]
},
async emit(_contentDir, cfg, content, resources, emit): Promise<FilePath[]> {
async emit(ctx, content, resources, emit): Promise<FilePath[]> {
const cfg = ctx.cfg.configuration
const fps: FilePath[] = []
const allFiles = content.map((c) => c[1].data)
for (const [tree, file] of content) {

View file

@ -22,9 +22,10 @@ export const FolderPage: QuartzEmitterPlugin<FullPageLayout> = (opts) => {
getQuartzComponents() {
return [Head, Header, Body, ...header, ...beforeBody, Content, ...left, ...right, Footer]
},
async emit(_contentDir, cfg, content, resources, emit): Promise<FilePath[]> {
async emit(ctx, content, resources, emit): Promise<FilePath[]> {
const fps: FilePath[] = []
const allFiles = content.map((c) => c[1].data)
const cfg = ctx.cfg.configuration
const folders: Set<CanonicalSlug> = new Set(
allFiles.flatMap((data) => {

View file

@ -3,3 +3,5 @@ export { TagPage } from "./tagPage"
export { FolderPage } from "./folderPage"
export { ContentIndex } from "./contentIndex"
export { AliasRedirects } from "./aliases"
export { Assets } from "./assets"
export { Static } from "./static"

View file

@ -0,0 +1,21 @@
import { globby } from "globby"
import {
FilePath, QUARTZ
} from "../../path"
import { QuartzEmitterPlugin } from "../types"
import path from "path"
import fs from "fs"
export const Static: QuartzEmitterPlugin = () => ({
name: "Static",
getQuartzComponents() {
return []
},
async emit({ argv }, _content, _resources, _emit): Promise<FilePath[]> {
const staticPath = path.join(QUARTZ, "static")
const fps = await globby("*", { cwd: staticPath })
await fs.promises.cp(staticPath, path.join(argv.output, "static"), { recursive: true })
return fps.map(fp => path.join("static", fp)) as FilePath[]
},
})

View file

@ -21,9 +21,10 @@ export const TagPage: QuartzEmitterPlugin<FullPageLayout> = (opts) => {
getQuartzComponents() {
return [Head, Header, Body, ...header, ...beforeBody, Content, ...left, ...right, Footer]
},
async emit(_contentDir, cfg, content, resources, emit): Promise<FilePath[]> {
async emit(ctx, content, resources, emit): Promise<FilePath[]> {
const fps: FilePath[] = []
const allFiles = content.map((c) => c[1].data)
const cfg = ctx.cfg.configuration
const tags: Set<string> = new Set(allFiles.flatMap((data) => data.frontmatter?.tags ?? []))
const tagDescriptions: Record<string, ProcessedContent> = Object.fromEntries(

View file

@ -110,7 +110,7 @@ const commentRegex = new RegExp(/%%(.+)%%/, "g")
// from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts
const calloutRegex = new RegExp(/^\[\!(\w+)\]([+-]?)/)
// (?:^| ) -> non-capturing group, tag should start be separated by a space or be the start of the line
// #(\w+) -> tag itself is # followed by a string of alpha-numeric characters
// #(\w+) -> tag itself is # followed by a string of alpha-numeric characters
const tagRegex = new RegExp(/(?:^| )#(\w+)/, "g")
export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options> | undefined> = (
@ -225,7 +225,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
findAndReplace(tree, commentRegex, (_value: string, ..._capture: string[]) => {
return {
type: "text",
value: ""
value: "",
}
})
}
@ -296,8 +296,9 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
node.data = {
hProperties: {
...(node.data?.hProperties ?? {}),
className: `callout ${collapse ? "is-collapsible" : ""} ${defaultState === "collapsed" ? "is-collapsed" : ""
}`,
className: `callout ${collapse ? "is-collapsible" : ""} ${
defaultState === "collapsed" ? "is-collapsed" : ""
}`,
"data-callout": calloutType,
"data-callout-fold": collapse,
},

View file

@ -4,6 +4,7 @@ import { ProcessedContent } from "./vfile"
import { GlobalConfiguration } from "../cfg"
import { QuartzComponent } from "../components/types"
import { FilePath, ServerSlug } from "../path"
import { BuildCtx } from "../ctx"
export interface PluginTypes {
transformers: QuartzTransformerPluginInstance[]
@ -37,8 +38,7 @@ export type QuartzEmitterPlugin<Options extends OptionType = undefined> = (
export type QuartzEmitterPluginInstance = {
name: string
emit(
contentDir: string,
cfg: GlobalConfiguration,
ctx: BuildCtx,
content: ProcessedContent[],
resources: StaticResources,
emitCallback: EmitCallback,