1
0
mirror of https://github.com/kremalicious/astro-redirect-from.git synced 2024-11-22 09:57:03 +01:00

getRedirects splitup, more tests

This commit is contained in:
Matthias Kretschmann 2023-09-24 00:57:32 +01:00
parent 23b8f01601
commit 3d921d93aa
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 87 additions and 42 deletions

17
src/createRedirect.ts Normal file
View File

@ -0,0 +1,17 @@
import type { Redirects } from './index.js'
export function createRedirect(
redirects: Redirects,
redirectFrom: string[],
postSlug: string
) {
// 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
}

View File

@ -1,6 +1,7 @@
import path from 'node:path' import path from 'node:path'
import type { Redirects } from '.' import type { Redirects } from '.'
import { getMarkdownFrontmatter } from './utils.js' import { getMarkdownFrontmatter } from './utils.js'
import { createRedirect } from './createRedirect'
export async function getRedirects( export async function getRedirects(
files: string[], files: string[],
@ -8,28 +9,18 @@ export async function getRedirects(
getSlug: (filePath: string) => string, getSlug: (filePath: string) => string,
command: 'dev' | 'build' | 'preview' command: 'dev' | 'build' | 'preview'
) { ) {
const redirects: Redirects = {} let redirects: Redirects = {}
for (const file of files) { for (const file of files) {
const frontmatter = await getMarkdownFrontmatter(path.join(srcDir, file)) const frontmatter = await getMarkdownFrontmatter(path.join(srcDir, file))
const redirectFrom: string[] = frontmatter?.redirect_from const redirectFrom: string[] = frontmatter?.redirect_from
if ( const isExcluded = command === 'build' && frontmatter?.draft === true
!frontmatter || if (!frontmatter || !redirectFrom || isExcluded) continue
!redirectFrom ||
(command === 'build' && frontmatter.draft === true)
)
continue
let postSlug = frontmatter.slug || getSlug(file) const postSlug = frontmatter.slug || getSlug(file)
if (!postSlug) continue if (!postSlug) continue
// Prepend all slugs with a slash if not present redirects = createRedirect(redirects, redirectFrom, postSlug)
if (!postSlug.startsWith('/')) postSlug = `/${postSlug}`
for (let slug of redirectFrom) {
if (!slug.startsWith('/')) slug = `/${slug}`
redirects[slug] = postSlug
}
} }
return redirects return redirects

View File

@ -1,27 +0,0 @@
import { expect, it, describe } from 'vitest'
import { getRedirects } from '../src/getRedirects'
import { getMarkdownFiles, getSlugFromFilePath } from '../src/utils'
describe('getRedirects', async () => {
// handling this more as an integration test
const srcDir = './test/__fixtures__/markdown'
const files = await getMarkdownFiles(srcDir)
it('should return redirects', async () => {
const result = await getRedirects(
files,
srcDir,
getSlugFromFilePath,
'build'
)
expect(result).toBeInstanceOf(Object)
expect(result).toStrictEqual({
'/hello-astro-old': '/hello-astroooooo',
'/hello-astro-old-234837': '/hello-astroooooo',
'/hello-world-old': '/hello-world',
'/hello-world-old-234837': '/hello-world',
'/hello-markdown-old': '/hello-markdown',
'/hello-markdown-old-234837': '/hello-markdown'
})
})
})

64
test/redirects.test.ts Normal file
View File

@ -0,0 +1,64 @@
import { expect, it, describe } from 'vitest'
import { getRedirects } from '../src/getRedirects'
import { getMarkdownFiles, getSlugFromFilePath } from '../src/utils'
import { createRedirect } from '../src/createRedirect'
describe('getRedirects', async () => {
// handling this more as an integration test
const srcDir = './test/__fixtures__/markdown'
const files = await getMarkdownFiles(srcDir)
it('should return correct redirects', async () => {
const result = await getRedirects(
files,
srcDir,
getSlugFromFilePath,
'build'
)
expect(result).toBeInstanceOf(Object)
expect(result).toStrictEqual({
'/hello-astro-old': '/hello-astroooooo',
'/hello-astro-old-234837': '/hello-astroooooo',
'/hello-world-old': '/hello-world',
'/hello-world-old-234837': '/hello-world',
'/hello-markdown-old': '/hello-markdown',
'/hello-markdown-old-234837': '/hello-markdown'
})
})
})
describe('createRedirect', () => {
it('should merge initialRedirects with newly found ones', () => {
const initialRedirects: Record<string, string> = {
'/existing-old-url': '/existing-new-url'
}
const redirectFrom = ['old-url-1', '/old-url-2']
const postSlug = 'new-url'
const result = createRedirect(initialRedirects, redirectFrom, postSlug)
expect(result).toStrictEqual({
'/existing-old-url': '/existing-new-url',
'/old-url-1': '/new-url',
'/old-url-2': '/new-url'
})
it('should prepend slash everywhere', () => {
const initialRedirects: Record<string, string> = {}
const redirectFrom = ['old-url-1']
let postSlug = '/new-url'
let result = createRedirect(initialRedirects, redirectFrom, postSlug)
expect(result).toStrictEqual({
'/old-url-1': '/new-url'
})
postSlug = 'new-url'
result = createRedirect(initialRedirects, redirectFrom, postSlug)
expect(result).toStrictEqual({
'/old-url-1': '/new-url'
})
})
})
})