2023-07-16 13:02:12 +07:00
|
|
|
import { CanonicalSlug, FilePath, ServerSlug, canonicalizeServer, resolveRelative } from "../../path"
|
2023-06-17 09:41:59 +07:00
|
|
|
import { QuartzEmitterPlugin } from "../types"
|
|
|
|
import path from 'path'
|
|
|
|
|
|
|
|
export const AliasRedirects: QuartzEmitterPlugin = () => ({
|
|
|
|
name: "AliasRedirects",
|
|
|
|
getQuartzComponents() {
|
|
|
|
return []
|
|
|
|
},
|
2023-07-13 14:19:35 +07:00
|
|
|
async emit(contentFolder, _cfg, content, _resources, emit): Promise<FilePath[]> {
|
|
|
|
const fps: FilePath[] = []
|
2023-06-17 09:41:59 +07:00
|
|
|
|
|
|
|
for (const [_tree, file] of content) {
|
2023-07-16 13:02:12 +07:00
|
|
|
const ogSlug = canonicalizeServer(file.data.slug!)
|
2023-06-17 09:41:59 +07:00
|
|
|
const dir = path.relative(contentFolder, file.dirname ?? contentFolder)
|
|
|
|
|
2023-07-13 14:19:35 +07:00
|
|
|
let aliases: CanonicalSlug[] = []
|
2023-06-17 09:41:59 +07: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) {
|
2023-07-16 13:02:12 +07:00
|
|
|
const slug = path.posix.join(dir, alias) as ServerSlug
|
2023-06-17 09:41:59 +07:00
|
|
|
|
2023-07-13 14:19:35 +07:00
|
|
|
const fp = slug + ".html" as FilePath
|
2023-07-16 13:02:12 +07:00
|
|
|
const redirUrl = resolveRelative(canonicalizeServer(slug), ogSlug)
|
2023-06-17 09:41:59 +07: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
|
|
|
|
}
|
|
|
|
})
|