mirror of
https://github.com/kremalicious/astro-redirect-from.git
synced 2025-02-14 21:10:35 +01:00
34 lines
826 B
TypeScript
34 lines
826 B
TypeScript
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
|
|
if (!postSlug) postSlug = getSlug(file)
|
|
if (!postSlug) continue
|
|
|
|
for (const slug of redirectFrom) {
|
|
redirects[slug] = postSlug
|
|
}
|
|
}
|
|
|
|
return redirects
|
|
}
|