mirror of
https://github.com/kremalicious/astro-redirect-from.git
synced 2024-11-22 01:47:01 +01:00
getRedirects splitup, more tests
This commit is contained in:
parent
23b8f01601
commit
3d921d93aa
17
src/createRedirect.ts
Normal file
17
src/createRedirect.ts
Normal 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
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import path from 'node:path'
|
||||
import type { Redirects } from '.'
|
||||
import { getMarkdownFrontmatter } from './utils.js'
|
||||
import { createRedirect } from './createRedirect'
|
||||
|
||||
export async function getRedirects(
|
||||
files: string[],
|
||||
@ -8,28 +9,18 @@ export async function getRedirects(
|
||||
getSlug: (filePath: string) => string,
|
||||
command: 'dev' | 'build' | 'preview'
|
||||
) {
|
||||
const redirects: Redirects = {}
|
||||
let 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
|
||||
const isExcluded = command === 'build' && frontmatter?.draft === true
|
||||
if (!frontmatter || !redirectFrom || isExcluded) continue
|
||||
|
||||
let postSlug = frontmatter.slug || getSlug(file)
|
||||
const 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
|
||||
}
|
||||
redirects = createRedirect(redirects, redirectFrom, postSlug)
|
||||
}
|
||||
|
||||
return redirects
|
||||
|
@ -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
64
test/redirects.test.ts
Normal 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'
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user