Jacky Zhao
2025-03-13 d9159e0ac9bfc22e584c78bc8aa04ecd82c14eea
quartz/plugins/emitters/aliases.ts
@@ -1,32 +1,31 @@
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 emit(contentFolder, _cfg, content, _resources, emit): Promise<FilePath[]> {
    const fps: FilePath[] = []
  async getDependencyGraph(ctx, content, _resources) {
    const graph = new DepGraph<FilePath>()
    const { argv } = ctx
    for (const [_tree, file] of content) {
      const ogSlug = canonicalizeServer(file.data.slug!)
      const dir = path.relative(contentFolder, file.dirname ?? contentFolder)
      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 slug of getAliasSlugs(file.data.frontmatter?.aliases ?? [], argv, file)) {
        graph.addEdge(file.data.filePath!, joinSegments(argv.output, slug + ".html") as FilePath)
      }
    }
      for (const alias of aliases) {
        const slug = path.posix.join(dir, alias) as ServerSlug
    return graph
  },
  async *emit(ctx, content, _resources) {
    for (const [_tree, file] of content) {
      const ogSlug = simplifySlug(file.data.slug!)
        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!)
        yield write({
          ctx,
          content: `
            <!DOCTYPE html>
            <html lang="en-us">
@@ -42,10 +41,7 @@
          slug,
          ext: ".html",
        })
        fps.push(fp)
      }
    }
    return fps
  }
  },
})