roadmap/quartz/plugins/emitters/aliases.ts

52 lines
1.6 KiB
TypeScript
Raw Normal View History

import { CanonicalSlug, FilePath, ServerSlug, canonicalizeServer, resolveRelative } from "../../path"
2023-06-17 02:41:59 +00:00
import { QuartzEmitterPlugin } from "../types"
import path from 'path'
export const AliasRedirects: QuartzEmitterPlugin = () => ({
name: "AliasRedirects",
getQuartzComponents() {
return []
},
2023-07-13 07:19:35 +00:00
async emit(contentFolder, _cfg, content, _resources, emit): Promise<FilePath[]> {
const fps: FilePath[] = []
2023-06-17 02:41:59 +00:00
for (const [_tree, file] of content) {
const ogSlug = canonicalizeServer(file.data.slug!)
2023-06-17 02:41:59 +00:00
const dir = path.relative(contentFolder, file.dirname ?? contentFolder)
2023-07-13 07:19:35 +00:00
let aliases: CanonicalSlug[] = []
2023-06-17 02:41:59 +00:00
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
2023-06-17 02:41:59 +00:00
2023-07-13 07:19:35 +00:00
const fp = slug + ".html" as FilePath
const redirUrl = resolveRelative(canonicalizeServer(slug), ogSlug)
2023-06-17 02:41:59 +00:00
await emit({
content: `
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>${ogSlug}</title>
<link rel="canonical" href="${redirUrl}">
<meta name="robots" content="noindex">
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url=${redirUrl}">
</head>
</html>
`,
slug,
ext: ".html",
})
fps.push(fp)
}
}
return fps
}
})