-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getRedirects.ts
36 lines (30 loc) 路 963 Bytes
/
getRedirects.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import path from 'node:path'
import type { Redirects } from '.'
import { getMarkdownFrontmatter } from './utils.js'
export async function getRedirects(
files: string[],
srcDir: string,
getSlug: (filePath: string) => string,
command: 'dev' | 'build' | 'preview'
) {
const redirects: Redirects = {}
for (const file of files) {
const frontmatter = await getMarkdownFrontmatter(path.join(srcDir, file))
const redirectFrom: string[] = frontmatter?.redirect_from
if (
!frontmatter ||
!redirectFrom ||
(command === 'build' && frontmatter.draft === true)
)
continue
let postSlug = frontmatter.slug || getSlug(file)
if (!postSlug) continue
// Prepend all slugs with a slash if not present
if (!postSlug.startsWith('/')) postSlug = `/${postSlug}`
for (let slug of redirectFrom) {
if (!slug.startsWith('/')) slug = `/${slug}`
redirects[slug] = postSlug
}
}
return redirects
}