Jacky Zhao
2025-03-10 23df17233da3f16db5166cf8a05b2089bd1f006a
quartz/plugins/emitters/aliases.ts
@@ -1,32 +1,33 @@
import { CanonicalSlug, FilePath, ServerSlug, canonicalizeServer, resolveRelative } from "../../path"
import { FilePath, joinSegments, resolveRelative, simplifySlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import path from 'path'
import { write } from "./helpers"
import DepGraph from "../../depgraph"
import { getAliasSlugs } from "../transformers/frontmatter"
export const AliasRedirects: QuartzEmitterPlugin = () => ({
  name: "AliasRedirects",
  getQuartzComponents() {
    return []
  async getDependencyGraph(ctx, content, _resources) {
    const graph = new DepGraph<FilePath>()
    const { argv } = ctx
    for (const [_tree, file] of content) {
      for (const slug of getAliasSlugs(file.data.frontmatter?.aliases ?? [], argv, file)) {
        graph.addEdge(file.data.filePath!, joinSegments(argv.output, slug + ".html") as FilePath)
      }
    }
    return graph
  },
  async emit(contentFolder, _cfg, content, _resources, emit): Promise<FilePath[]> {
  async emit(ctx, content, _resources): 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 ogSlug = simplifySlug(file.data.slug!)
      let aliases: CanonicalSlug[] = []
      if (file.data.frontmatter?.aliases) {
        aliases = file.data.frontmatter?.aliases
      } else if (file.data.frontmatter?.alias) {
        aliases = [file.data.frontmatter?.alias]
      }
      for (const alias of aliases) {
        const slug = path.posix.join(dir, alias) as ServerSlug
        const fp = slug + ".html" as FilePath
        const redirUrl = resolveRelative(canonicalizeServer(slug), ogSlug)
        await emit({
      for (const slug of file.data.aliases ?? []) {
        const redirUrl = resolveRelative(slug, file.data.slug!)
        const fp = await write({
          ctx,
          content: `
            <!DOCTYPE html>
            <html lang="en-us">
@@ -47,5 +48,5 @@
      }
    }
    return fps
  }
  },
})