diff --git a/src/utils.ts b/src/utils.ts index 3a6768a..7f86571 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -25,9 +25,9 @@ export function getSlugFromFilePath(filePath: string) { // - folder name if file name is index.md, or // - file name if (parsedPath.base === 'index.md' || parsedPath.base === 'index.mdx') { - slug = `/${parsedPath.dir}` + slug = `${parsedPath.dir}` } else { - slug = `/${parsedPath.dir}/${parsedPath.name}` + slug = `${parsedPath.dir}/${parsedPath.name}` } return slug diff --git a/test/__fixtures__/markdown/hello-astro.md b/test/__fixtures__/markdown/hello-astro.md new file mode 100644 index 0000000..efcc7d1 --- /dev/null +++ b/test/__fixtures__/markdown/hello-astro.md @@ -0,0 +1,6 @@ +--- +slug: hello-astroooooo +redirect_from: + - /hello-astro-old + - hello-astro-old-234837 +--- diff --git a/test/__fixtures__/markdown/hello-draft.md b/test/__fixtures__/markdown/hello-draft.md new file mode 100644 index 0000000..0e1aaf3 --- /dev/null +++ b/test/__fixtures__/markdown/hello-draft.md @@ -0,0 +1,6 @@ +--- +draft: true + +redirect_from: + - /hello-draft-old +--- diff --git a/test/getRedirects.test.ts b/test/getRedirects.test.ts new file mode 100644 index 0000000..4dcdf2a --- /dev/null +++ b/test/getRedirects.test.ts @@ -0,0 +1,27 @@ +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' + }) + }) +}) diff --git a/test/utils.test.ts b/test/utils.test.ts index c041715..348c759 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -11,7 +11,7 @@ describe('getMarkdownFiles', () => { it('should return an array of markdown files from the given directory', async () => { const files = await getMarkdownFiles('./test/__fixtures__/markdown') expect(files).toBeInstanceOf(Array) - expect(files).toHaveLength(2) + expect(files).toHaveLength(4) }) }) @@ -28,17 +28,17 @@ describe('getMarkdownFrontmatter', () => { describe('getSlugFromFilePath', () => { it('should return slug for testDir/testFile.md', () => { const slug = getSlugFromFilePath('testDir/testFile.md') - expect(slug).toBe('/testDir/testFile') + expect(slug).toBe('testDir/testFile') }) it('should return slug for dir/testDir/index.md', () => { const slug = getSlugFromFilePath('dir/testDir/index.md') - expect(slug).toBe('/dir/testDir') + expect(slug).toBe('dir/testDir') }) it('should return slug for dir/dir2/testDir/index.md', () => { const slug = getSlugFromFilePath('dir/dir2/testDir/index.md') - expect(slug).toBe('/dir/dir2/testDir') + expect(slug).toBe('dir/dir2/testDir') }) }) @@ -49,7 +49,7 @@ describe('writeJson', () => { try { await fs.unlink(testFilePath) } catch (error) { - console.error('Error cleaning up test file:', error) + return } })