mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
Merge pull request #2713 from jackyzha0/dev-mode/prerelease-link-tests
test: broken-link marking for published folder notes and after page generation
This commit is contained in:
3 files changed
+125
-2
No files matched your search
@@ -1079,6 +1079,51 @@ describe("links to pages removed by filters (crawl-links#7)", () => {
|
||||
assert.deepStrictEqual([...changed], ["index"])
|
||||
})
|
||||
|
||||
test("links to published folder notes and pages are not marked once page types ran", () => {
|
||||
// `notes/index` is a content page (notes/index.md or a folder note), so no page type
|
||||
// generates it: it is live because it is published, not because it was generated
|
||||
const home = page("index", [
|
||||
link("notes/index"),
|
||||
link("projects/index"),
|
||||
link("published"),
|
||||
link("aka"),
|
||||
link("tags/x"),
|
||||
])
|
||||
const notes = page("notes/index", [])
|
||||
const projects = page("projects/index", [], { aliases: ["aka"] })
|
||||
const published = page("published", [])
|
||||
const changed = new Set<FullSlug>()
|
||||
const marked = markUnpublishedLinks(
|
||||
[home, notes, projects, published],
|
||||
new Map(),
|
||||
new Set(["tags/x"]),
|
||||
changed,
|
||||
)
|
||||
assert.strictEqual(marked, 0)
|
||||
for (const a of anchors(home)) {
|
||||
assert.deepStrictEqual(a.properties.className, ["internal", "internal-link"])
|
||||
assert.strictEqual(a.properties["data-missing"], undefined)
|
||||
assert.strictEqual(a.properties["data-filtered"], undefined)
|
||||
}
|
||||
assert.deepStrictEqual([...changed], [])
|
||||
})
|
||||
|
||||
test("a folder note that is a draft: its folder page is generated, or the link is missing", () => {
|
||||
const home = page("index", [link("notes/index"), link("drafts/index")])
|
||||
const notesIndex = page("notes/index", [])
|
||||
const draftsIndex = page("drafts/index", [])
|
||||
const all = [home, notesIndex, draftsIndex]
|
||||
const removed = removedPages(all, [home], () => "RemoveDrafts")
|
||||
// folder index notes are left to the generated pages
|
||||
assert.deepStrictEqual([...removed], [])
|
||||
markUnpublishedLinks([home], removed, new Set(["notes/index"]))
|
||||
const [toNotes, toDrafts] = anchors(home)
|
||||
assert.ok(!(toNotes.properties.className as string[]).includes("broken"))
|
||||
assert.ok((toDrafts.properties.className as string[]).includes("broken"))
|
||||
assert.strictEqual(toDrafts.properties["data-missing"], "")
|
||||
assert.strictEqual(toDrafts.properties["data-filtered"], undefined)
|
||||
})
|
||||
|
||||
test("reports the pages whose marks changed, so watch mode re-emits them", () => {
|
||||
const home = page("index", [link("draft")])
|
||||
const other = page("other", [link("published")])
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
collectTreeTransforms,
|
||||
folderTrie,
|
||||
generateVirtualPages,
|
||||
markLinksToUnpublishedPages,
|
||||
pageOutputPath,
|
||||
processVirtualPageHtml,
|
||||
referencesByTarget,
|
||||
@@ -19,7 +20,10 @@ import { QuartzComponent } from "../../components/types"
|
||||
import { FilePath, FullSlug, SimpleSlug, setSlugOptions } from "../../util/path"
|
||||
import { Root as HtmlRoot } from "hast"
|
||||
import { BuildCtx } from "../../util/ctx"
|
||||
import { defaultProcessedContent } from "../vfile"
|
||||
import { defaultProcessedContent, ProcessedContent } from "../vfile"
|
||||
import { removedPages } from "../builtins/links"
|
||||
import { Element, Root } from "hast"
|
||||
import { VFile } from "vfile"
|
||||
|
||||
const StubA: QuartzComponent = (() => null) as unknown as QuartzComponent
|
||||
const StubB: QuartzComponent = (() => null) as unknown as QuartzComponent
|
||||
@@ -557,3 +561,77 @@ describe("publish filters on pages that stand for files (bases-page#11)", () =>
|
||||
assert.strictEqual(processVirtualPageHtml(ve, makeCtx(), []), true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("links to unpublished pages, after the page types ran (crawl-links#7)", () => {
|
||||
const link = (slug: string): Element => ({
|
||||
type: "element",
|
||||
tagName: "a",
|
||||
properties: { href: `./${slug}`, "data-slug": slug, className: ["internal"] },
|
||||
children: [],
|
||||
})
|
||||
const content = (slug: string, links: string[] = []): ProcessedContent => {
|
||||
const file = new VFile("")
|
||||
file.data = { slug: slug as FullSlug, frontmatter: { title: slug } } as VFile["data"]
|
||||
return [{ type: "root", children: links.map(link) } as Root, file]
|
||||
}
|
||||
const marks = (item: ProcessedContent) =>
|
||||
((item[0] as Root).children as Element[]).map((a) =>
|
||||
(a.properties.className as string[]).includes("broken")
|
||||
? (a.properties["data-filtered"] ?? (a.properties["data-missing"] === "" ? "missing" : "?"))
|
||||
: "ok",
|
||||
)
|
||||
// a folder page type that generates a page for each folder with a published note, as folder-page does
|
||||
const folders = makePageType({
|
||||
name: "folders",
|
||||
generate: ({ content }) =>
|
||||
[...new Set(content.map(([, f]) => f.data.slug!.split("/").slice(0, -1).join("/")))]
|
||||
.filter((f) => f !== "" && !content.some(([, g]) => g.data.slug === `${f}/index`))
|
||||
.map((f) => ({ slug: `${f}/index` as FullSlug, title: f, data: {} })),
|
||||
})
|
||||
|
||||
const setup = async (removedBy: ((slug: string) => string | undefined) | undefined) => {
|
||||
const home = content("index", [
|
||||
"notes/a",
|
||||
"notes/index",
|
||||
"drafts/index",
|
||||
"drafts/b",
|
||||
"projects/index",
|
||||
"missing-in-source",
|
||||
])
|
||||
const all = [home, content("notes/a"), content("drafts/b"), content("projects/index")]
|
||||
const published = all.filter(([, f]) => f.data.slug !== "drafts/b")
|
||||
const ctx = {
|
||||
cfg: { configuration: {} },
|
||||
virtualPages: [],
|
||||
removedPages: removedBy ? removedPages(all, published, removedBy) : undefined,
|
||||
} as unknown as BuildCtx
|
||||
const entries = await generateVirtualPages([folders], published, ctx, { head: StubHead }, {})
|
||||
return { home, ctx, published, entries }
|
||||
}
|
||||
|
||||
test("marks drafts with their filter and folders no page type generated as missing", async () => {
|
||||
const { home, ctx, published, entries } = await setup(() => "RemoveDrafts")
|
||||
assert.deepStrictEqual(
|
||||
entries.map((e) => e.vpSlug),
|
||||
["notes/index"],
|
||||
)
|
||||
const changed = markLinksToUnpublishedPages(ctx, published, entries)
|
||||
assert.deepStrictEqual(marks(home), [
|
||||
"ok", // notes/a: published
|
||||
"ok", // notes/index: generated folder page
|
||||
"missing", // drafts/index: its only note is a draft, so no folder page
|
||||
"RemoveDrafts", // drafts/b
|
||||
"ok", // projects/index: a published folder note
|
||||
"ok", // not in the content: the Links transformer's own check, not this one
|
||||
])
|
||||
assert.deepStrictEqual([...changed], ["index"])
|
||||
// run again (watch mode, nothing changed): nothing to re-emit
|
||||
assert.deepStrictEqual([...markLinksToUnpublishedPages(ctx, published, entries)], [])
|
||||
})
|
||||
|
||||
test("marks nothing when the Links transformer doesn't mark broken links", async () => {
|
||||
const { home, ctx, published, entries } = await setup(undefined)
|
||||
assert.deepStrictEqual([...markLinksToUnpublishedPages(ctx, published, entries)], [])
|
||||
assert.ok(marks(home).every((m) => m === "ok"))
|
||||
})
|
||||
})
|
||||
@@ -388,7 +388,7 @@ function populateVirtualPageHtmlAst(
|
||||
* type generated (markUnpublishedLinks). Returns the pages whose links changed, which watch mode
|
||||
* re-emits although their source didn't change.
|
||||
*/
|
||||
function markLinksToUnpublishedPages(
|
||||
export function markLinksToUnpublishedPages(
|
||||
ctx: BuildCtx,
|
||||
content: ProcessedContent[],
|
||||
virtualEntries: VirtualEntry[],
|
||||
|
||||
Reference in new issue
Block a user