rizaldy.club/quartz/plugins/emitters/aliases.ts

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-23 07:27:41 +07:00
import {
CanonicalSlug,
FilePath,
ServerSlug,
canonicalizeServer,
resolveRelative,
} from "../../path"
2023-06-17 09:41:59 +07:00
import { QuartzEmitterPlugin } from "../types"
2023-07-23 07:27:41 +07:00
import path from "path"
2023-06-17 09:41:59 +07:00
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) {
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) {
const slug = path.posix.join(dir, alias) as ServerSlug
2023-06-17 09:41:59 +07:00
2023-07-23 07:27:41 +07:00
const fp = (slug + ".html") as FilePath
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
2023-07-23 07:27:41 +07:00
},
2023-06-17 09:41:59 +07:00
})