mirror of
https://github.com/kremalicious/astro-redirect-from.git
synced 2024-11-21 17:37:02 +01:00
Warn on parsing failures; support redirect_from: url
(#157)
* Update getRedirects.ts * Update index.ts * Update index.ts Ensure redirect file parent folder is created * Update index.ts * Update ci.yml * Update getRedirects.ts Fix lints * Update getRedirects.ts * Update getRedirects.ts * Update redirects.test.ts * Update getRedirects.ts * Create hello-once.md * Update redirects.test.ts * Update utils.test.ts * Update ci.yml
This commit is contained in:
parent
5b0fcf950c
commit
55269fbf48
@ -3,19 +3,43 @@ import type { Redirects } from '.'
|
||||
import { createRedirect } from './createRedirect.js'
|
||||
import { getMarkdownFrontmatter } from './utils.js'
|
||||
|
||||
function isValidDomainRelativePath(path: string): boolean {
|
||||
return !path.includes('://') && !path.includes(' ') && !path.includes('\n')
|
||||
}
|
||||
|
||||
export async function getRedirects(
|
||||
files: string[],
|
||||
srcDir: string,
|
||||
getSlug: (filePath: string) => string,
|
||||
command: 'dev' | 'build' | 'preview'
|
||||
command: 'dev' | 'build' | 'preview',
|
||||
logger: { warn: (msg: string) => void }
|
||||
) {
|
||||
let redirects: Redirects = {}
|
||||
|
||||
for (const file of files) {
|
||||
const frontmatter = await getMarkdownFrontmatter(path.join(srcDir, file))
|
||||
const redirectFrom: string[] = frontmatter?.redirect_from
|
||||
let redirectFrom: string[] = []
|
||||
|
||||
if (frontmatter?.redirect_from){
|
||||
if (typeof frontmatter?.redirect_from === 'string') {
|
||||
redirectFrom = [frontmatter.redirect_from]
|
||||
} else if (Array.isArray(frontmatter?.redirect_from)) {
|
||||
redirectFrom = frontmatter.redirect_from
|
||||
} else {
|
||||
logger.warn(`Unexpected type ${typeof frontmatter?.redirect_from} for redirect_from in file: ${file}`)
|
||||
}
|
||||
}
|
||||
|
||||
redirectFrom = redirectFrom.filter(redirect => {
|
||||
if (isValidDomainRelativePath(redirect)) {
|
||||
return true
|
||||
}
|
||||
logger.warn(`Invalid redirect path: ${redirect} in file: ${file}`)
|
||||
return false
|
||||
})
|
||||
|
||||
const isExcluded = command === 'build' && frontmatter?.draft === true
|
||||
if (!frontmatter || !redirectFrom || isExcluded) continue
|
||||
if (!frontmatter || !redirectFrom.length || isExcluded) continue
|
||||
|
||||
const postSlug = frontmatter.slug || getSlug(file)
|
||||
if (!postSlug) continue
|
||||
|
@ -1,4 +1,5 @@
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
import type { AstroIntegration } from 'astro'
|
||||
import { getRedirects } from './getRedirects.js'
|
||||
import { getMarkdownFiles, getSlugFromFilePath, writeJson } from './utils.js'
|
||||
@ -36,7 +37,8 @@ export async function initPlugin(
|
||||
markdownFiles,
|
||||
_contentDirPath,
|
||||
_getSlug,
|
||||
command
|
||||
command,
|
||||
logger
|
||||
)
|
||||
if (!redirects || !Object.keys(redirects).length) {
|
||||
logger.warn('No redirects found in markdown files')
|
||||
@ -45,6 +47,9 @@ export async function initPlugin(
|
||||
|
||||
updateConfig({ redirects })
|
||||
|
||||
if (!fs.existsSync(config.cacheDir.pathname)) {
|
||||
fs.mkdirSync(config.cacheDir.pathname, { recursive: true })
|
||||
}
|
||||
const redirectFilePath = path.join(
|
||||
config.cacheDir.pathname, // Default is ./node_modules/.astro/
|
||||
'redirect_from.json'
|
||||
|
3
test/__fixtures__/markdown/posts/hello-once.md
Normal file
3
test/__fixtures__/markdown/posts/hello-once.md
Normal file
@ -0,0 +1,3 @@
|
||||
---
|
||||
redirect_from: /hello-once
|
||||
---
|
@ -3,6 +3,8 @@ import { createRedirect } from '../src/createRedirect'
|
||||
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'
|
||||
@ -13,13 +15,15 @@ describe('getRedirects', async () => {
|
||||
files,
|
||||
srcDir,
|
||||
getSlugFromFilePath,
|
||||
'build'
|
||||
'build',
|
||||
console
|
||||
)
|
||||
expect(result).toBeInstanceOf(Object)
|
||||
expect(result).toStrictEqual({
|
||||
'/hello-astro-old': '/hello-astroooooo',
|
||||
'/hello-astro-old-234837': '/hello-astroooooo',
|
||||
'/hello-world-old': '/posts/hello-world',
|
||||
"/hello-once": "/posts/hello-once",
|
||||
'/hello-world-old-234837': '/posts/hello-world',
|
||||
'/hello-markdown-old': '/hello-markdown',
|
||||
'/hello-markdown-old-234837': '/hello-markdown'
|
||||
|
@ -12,7 +12,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(4)
|
||||
expect(files).toHaveLength(5)
|
||||
})
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user