Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion quartz/plugins/pageTypes/dispatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import test, { describe } from "node:test"
import assert from "node:assert"
import { collectComponents, resolveLayout } from "./dispatcher"
import fs from "fs"
import os from "os"
import path from "path"
import {
collectComponents,
pageOutputPath,
removeStalePageOutput,
resolveLayout,
} from "./dispatcher"
import { QuartzPageTypePluginInstance } from "../types"
import { QuartzComponent } from "../../components/types"
import { FilePath, FullSlug } from "../../util/path"

const StubA: QuartzComponent = (() => null) as unknown as QuartzComponent
const StubB: QuartzComponent = (() => null) as unknown as QuartzComponent
Expand Down Expand Up @@ -142,3 +151,52 @@ describe("collectComponents", () => {
assert.ok(result.every((component) => component))
})
})

describe("incremental page deletion", () => {
test("maps Markdown sources to their emitted HTML paths", () => {
const cases: Array<[FilePath, string]> = [
["note.md" as FilePath, "public/note.html"],
["index.md" as FilePath, "public/index.html"],
["folder/index.md" as FilePath, "public/folder/index.html"],
["folder/folder.md" as FilePath, "public/folder/index.html"],
]

for (const [sourcePath, expected] of cases) {
assert.strictEqual(pageOutputPath("public", sourcePath), expected)
}
})

test("removes stale output and tolerates an already-missing file", async (t) => {
const outputDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "quartz-page-delete-"))
t.after(() => fs.promises.rm(outputDir, { recursive: true, force: true }))

const sourcePath = "folder/folder.md" as FilePath
const outputPath = pageOutputPath(outputDir, sourcePath)
await fs.promises.mkdir(path.dirname(outputPath), { recursive: true })
await fs.promises.writeFile(outputPath, "stale")

const removed = await removeStalePageOutput(outputDir, sourcePath, new Set<FullSlug>())
assert.strictEqual(removed, outputPath)
await assert.rejects(fs.promises.access(outputPath), { code: "ENOENT" })

await removeStalePageOutput(outputDir, sourcePath, new Set<FullSlug>())
})

test("preserves output when another source still owns the slug", async (t) => {
const outputDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "quartz-page-delete-"))
t.after(() => fs.promises.rm(outputDir, { recursive: true, force: true }))

const sourcePath = "folder/folder.md" as FilePath
const outputPath = pageOutputPath(outputDir, sourcePath)
await fs.promises.mkdir(path.dirname(outputPath), { recursive: true })
await fs.promises.writeFile(outputPath, "current")

const removed = await removeStalePageOutput(
outputDir,
sourcePath,
new Set(["folder/index" as FullSlug]),
)
assert.strictEqual(removed, undefined)
assert.strictEqual(await fs.promises.readFile(outputPath, "utf8"), "current")
})
})
43 changes: 39 additions & 4 deletions quartz/plugins/pageTypes/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { QuartzEmitterPlugin, QuartzPageTypePluginInstance, TreeTransform } from
import { QuartzComponent, QuartzComponentProps } from "../../components/types"
import { pageResources, renderPage } from "../../components/renderPage"
import { FullPageLayout } from "../../cfg"
import { FilePath, FullSlug, pathToRoot } from "../../util/path"
import { FilePath, FullSlug, joinSegments, pathToRoot, slugifyFilePath } from "../../util/path"
import { ProcessedContent, defaultProcessedContent } from "../vfile"
import { write } from "../emitters/helpers"
import { BuildCtx, trieFromAllFiles } from "../../util/ctx"
import { StaticResources } from "../../util/resources"
import { render } from "preact-render-to-string"
import { fromHtml } from "hast-util-from-html"
import { Root as HtmlRoot } from "hast"
import fs from "fs"
import path from "path"

function getPageTypes(ctx: BuildCtx): QuartzPageTypePluginInstance[] {
return (ctx.cfg.plugins.pageTypes ?? []) as unknown as QuartzPageTypePluginInstance[]
Expand Down Expand Up @@ -109,6 +111,31 @@ async function emitPage(
})
}

/** @internal Exported for testing only. */
export function pageOutputPath(
outputDir: string,
sourcePath: FilePath,
fileSlug?: FullSlug,
): FilePath {
const slug = fileSlug ?? slugifyFilePath(sourcePath)
return joinSegments(outputDir, `${slug}.html`) as FilePath
}

/** @internal Exported for testing only. */
export async function removeStalePageOutput(
outputDir: string,
sourcePath: FilePath,
currentSlugs: ReadonlySet<FullSlug>,
fileSlug?: FullSlug,
): Promise<FilePath | undefined> {
const slug = fileSlug ?? slugifyFilePath(sourcePath)
if (currentSlugs.has(slug)) return

const outputPath = pageOutputPath(outputDir, sourcePath, slug)
await fs.promises.rm(outputPath, { force: true })
return outputPath
}

/**
* Render each virtual page's Body component to HTML and parse it to a hast tree,
* populating both the ProcessedContent tree and vfile.data.htmlAst so that
Expand Down Expand Up @@ -259,11 +286,19 @@ export const PageTypeDispatcher: QuartzEmitterPlugin<Partial<DispatcherOptions>>
// Rebuild trie on partial emit to reflect file changes
ctx.trie = trieFromAllFiles(allFiles)

const currentSlugs = new Set(
allFiles.flatMap((file) => (file.slug ? [file.slug] : [])),
) as Set<FullSlug>
const changedSlugs = new Set<string>()
for (const changeEvent of changeEvents) {
if (!changeEvent.file) continue
if (changeEvent.type === "add" || changeEvent.type === "change") {
changedSlugs.add(changeEvent.file.data.slug!)
if (path.extname(changeEvent.path).toLowerCase() !== ".md") continue

const slug = changeEvent.file?.data.slug ?? slugifyFilePath(changeEvent.path)
if (currentSlugs.has(slug)) {
// A remaining source may have taken ownership after a slug collision.
changedSlugs.add(slug)
} else {
await removeStalePageOutput(ctx.argv.output, changeEvent.path, currentSlugs, slug)
}
}

Expand Down
Loading