1
0
mirror of https://github.com/kremalicious/astro-redirect-from.git synced 2024-12-22 09:23:21 +01:00
astro-redirect-from/README.md

214 lines
6.9 KiB
Markdown
Raw Normal View History

2023-09-23 19:35:37 +02:00
[![astro-redirect-from](https://raw.githubusercontent.com/kremalicious/astro-redirect-from/main/src/astro-redirect-from-teaser.png)](https://kremalicious.com/astro-redirect-from/)
2023-09-22 04:43:19 +02:00
# astro-redirect-from
[![npm package](https://img.shields.io/npm/v/astro-redirect-from.svg)](https://www.npmjs.com/package/astro-redirect-from)
2023-09-23 19:35:37 +02:00
[![CI](https://github.com/kremalicious/astro-redirect-from/actions/workflows/ci.yml/badge.svg)](https://github.com/kremalicious/astro-redirect-from/actions/workflows/ci.yml)
2023-09-23 22:11:07 +02:00
[![Maintainability](https://api.codeclimate.com/v1/badges/a20dc7ebee797c2d1e43/maintainability)](https://codeclimate.com/github/kremalicious/astro-redirect-from/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/a20dc7ebee797c2d1e43/test_coverage)](https://codeclimate.com/github/kremalicious/astro-redirect-from/test_coverage)
2023-09-22 04:43:19 +02:00
> 🎯 Set redirect urls in your frontmatter within your [Astro](https://astro.build) site's Markdown files. Mimics the behavior of [jekyll-redirect-from](https://github.com/jekyll/jekyll-redirect-from) and [gatsby-redirect-from](https://kremalicious.com/gatsby-redirect-from/).
>
> https://kremalicious.com/astro-redirect-from/
---
**Table of Contents**
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Options](#options)
- [`contentDir: string`](#contentdir-string)
- [`getSlug: (filePath: string) => string`](#getslug-filepath-string--string)
- [Usage](#usage)
2023-09-23 19:59:22 +02:00
- [More Documentation](#more-documentation)
2023-09-22 04:43:19 +02:00
- [Plugin Development](#plugin-development)
2023-09-24 00:25:14 +02:00
- [Testing](#testing)
2023-09-22 04:43:19 +02:00
- [Changelog](#changelog)
2023-10-25 01:17:09 +02:00
- [Sponsorship](#sponsorship)
2023-09-22 04:43:19 +02:00
- [License](#license)
---
## Prerequisites
The plugin is designed to work without configuration, especially if your project aligns with Astro's default settings.
- Astro v3+ (v2 probably works too, but is not tested)
2023-09-22 04:43:19 +02:00
- Markdown files should be in a directory (default is `src/pages/`)
- Slugs are extracted either from the frontmatter or the file/folder path
## Installation
```bash
cd yourproject/
npx astro add astro-redirect-from
```
If installing manually:
```bash
npm i astro-redirect-from
```
2023-09-23 19:59:22 +02:00
Then load the plugin in your Astro config file, making sure this plugin comes before any other integrations which make use of the `redirects` config:
2023-09-22 04:43:19 +02:00
```js title="astro.config.mjs"
import { defineConfig } from 'astro/config'
import redirectFrom from 'astro-redirect-from'
export default defineConfig({
// ...
integrations: [
2023-09-27 14:50:23 +02:00
// make sure this is listed BEFORE any hosting integration
2023-09-22 04:43:19 +02:00
redirectFrom()
]
// ...
)}
```
That's it. Your redirects will be automatically added the next time you run `astro dev` or `astro build`. If any of them have a `redirect_from` field, that is.
2023-09-23 22:11:07 +02:00
For easy debugging, a `redirect_from.json` is written out into Astro's `cacheDir` setting, which by default is under `node_modules/.astro`.
2023-09-22 04:43:19 +02:00
[See Usage](#usage)
### Options
2023-09-23 19:59:22 +02:00
All options are optional and can be passed in Astro's config file:
2023-09-22 04:43:19 +02:00
```js title="astro.config.mjs"
import { defineConfig } from 'astro/config'
import redirectFrom from 'astro-redirect-from'
import { getMySlug } from './your-slug-function'
export default defineConfig({
// ...
integrations: [
redirectFrom({
contentDir: './content',
getSlug: getMySlug
})
]
// ...
)}
```
#### `contentDir: string`
_Default: `src/pages/`_
Specify a different directory for your Markdown files, relative to the project root.
#### `getSlug: (filePath: string) => string`
_Default: `getSlugFromFilePath()`, see below_
2023-09-23 22:19:11 +02:00
If you need a custom slug structure, pass a function to construct your slug from the file path. The file path should be relative to the content directory.
2023-09-22 04:43:19 +02:00
If you use a `slug` field in your frontmatter, this will be preferred by the plugin and passing any `getSlug` function will have no effect in that case.
The default function is a great starting point:
```typescript
function getSlugFromFilePath(filePath: string) {
const parsedPath = path.parse(filePath)
let slug
// construct slug as full path from either:
2023-09-22 14:17:25 +02:00
// - folder name if file name is index.md, or
// - file name
2023-09-22 04:43:19 +02:00
if (parsedPath.base === 'index.md' || parsedPath.base === 'index.mdx') {
2023-10-25 01:17:09 +02:00
slug = `${parsedPath.dir}`
2023-09-22 04:43:19 +02:00
} else {
2023-10-25 01:17:09 +02:00
slug = `${parsedPath.dir}/${parsedPath.name}`
2023-09-22 04:43:19 +02:00
}
return slug
}
```
## Usage
2023-09-22 14:17:25 +02:00
In your Markdown file's frontmatter, use the key `redirect_from` followed by a list.
2023-09-22 04:43:19 +02:00
```yaml
---
redirect_from:
- /old-url-1
- /old-url-2
- /old-url-3.html
---
```
2024-08-12 12:53:28 +02:00
Alternatively, you can use a string instead of a list for a single redirect:
```yaml
redirect_from: /old-url
```
2023-09-23 19:59:22 +02:00
## [More Documentation](https://kremalicious.com/astro-redirect-from/)
Find more explanations, all about server-side redirects, and learn about additional integrations which can be used in combination with astro-redirect-from.
- **[Documentation →](https://kremalicious.com/astro-redirect-from/)**
2023-09-22 04:43:19 +02:00
## Plugin Development
```bash
npm i
npm start
# production build
npm run build
# publishing to npm & GitHub releases
# uses https://github.com/webpro/release-it
npm run release
```
2023-09-24 00:25:14 +02:00
### Testing
```bash
npm run lint
npm run typecheck
# runs unit tests through vitest
npm run test:unit
# all of the above commands together
npm test
```
If linting errors are encountered, try to fix it automatically with:
```bash
npm run format
```
2023-09-22 04:43:19 +02:00
## Changelog
See [CHANGELOG.md](CHANGELOG.md).
2023-10-25 01:17:09 +02:00
## Sponsorship
2023-10-25 01:45:24 +02:00
[![](https://img.shields.io/static/v1?label=Say%20Thanks%20With%20Web3&labelColor=%2343a699&message=%E2%9D%A4&logo=Ethereum&color=%23fe8e86&style=for-the-badge)](https://kremalicious.com/thanks)
2023-10-25 01:17:09 +02:00
2023-10-25 01:45:24 +02:00
[![](https://img.shields.io/static/v1?label=Say%20Thanks%20With%20GitHub&labelColor=%2343a699&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86&style=for-the-badge)](https://github.com/sponsors/kremalicious)
2023-10-25 01:17:09 +02:00
2023-09-22 04:43:19 +02:00
## License
The MIT License
Copyright (c) 2024 Matthias Kretschmann
2023-09-22 04:43:19 +02:00
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---
Made with ♥ by [Matthias Kretschmann](https://matthiaskretschmann.com) ([@kremalicious](https://github.com/kremalicious))