1
0
mirror of https://github.com/kremalicious/astro-redirect-from.git synced 2024-12-22 01:13:23 +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:
Lilith River 2024-08-12 04:39:00 -06:00 committed by GitHub
parent 5b0fcf950c
commit 55269fbf48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 6 deletions

View File

@ -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

View File

@ -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'

View File

@ -0,0 +1,3 @@
---
redirect_from: /hello-once
---

View File

@ -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'

View File

@ -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)
})
})