1
0
mirror of https://github.com/kremalicious/astro-redirect-from.git synced 2024-11-25 20:03:12 +01:00
🎯 Set redirect urls in your frontmatter within your Astro site's Markdown files. https://kremalicious.com/astro-redirect-from/
Go to file
dependabot[bot] 18c2550ee1
Bump vite from 5.4.0 to 5.4.6
Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 5.4.0 to 5.4.6.
- [Release notes](https://github.com/vitejs/vite/releases)
- [Changelog](https://github.com/vitejs/vite/blob/v5.4.6/packages/vite/CHANGELOG.md)
- [Commits](https://github.com/vitejs/vite/commits/v5.4.6/packages/vite)

---
updated-dependencies:
- dependency-name: vite
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-09-17 18:49:28 +00:00
.github disable windows CI test runs 2024-08-12 13:22:12 +02:00
.husky dependency updates 2024-02-26 11:51:52 +00:00
src formatting fixes 2024-08-12 12:54:20 +02:00
test formatting fixes 2024-08-12 12:54:20 +02:00
.editorconfig prototype 🏗️ 2023-09-22 03:45:36 +01:00
.gitignore test setup, unit tests for all utils 2023-09-23 23:25:14 +01:00
.nvmrc dependency updates, require Node.js v18.18.x 2024-04-08 11:43:18 +01:00
biome.json migrate to biome (#149) 2024-07-26 14:13:02 +01:00
CHANGELOG.md Release 1.2.0 2024-08-12 13:26:48 +02:00
LICENSE prototype 🏗️ 2023-09-22 03:45:36 +01:00
package-lock.json Bump vite from 5.4.0 to 5.4.6 2024-09-17 18:49:28 +00:00
package.json Release 1.2.0 2024-08-12 13:26:48 +02:00
README.md split up lint & format task 2024-08-12 12:55:22 +02:00
tsconfig.json build tweaks 2023-09-23 17:55:14 +01:00

astro-redirect-from

astro-redirect-from

npm package CI Maintainability Test Coverage

🎯 Set redirect urls in your frontmatter within your Astro site's Markdown files. Mimics the behavior of jekyll-redirect-from and gatsby-redirect-from.

https://kremalicious.com/astro-redirect-from/


Table of Contents


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)
  • Markdown files should be in a directory (default is src/pages/)
  • Slugs are extracted either from the frontmatter or the file/folder path

Installation

cd yourproject/

npx astro add astro-redirect-from

If installing manually:

npm i astro-redirect-from

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:

import { defineConfig } from 'astro/config'
import redirectFrom from 'astro-redirect-from'

export default defineConfig({
  // ...
  integrations: [
    // make sure this is listed BEFORE any hosting integration
    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.

For easy debugging, a redirect_from.json is written out into Astro's cacheDir setting, which by default is under node_modules/.astro.

See Usage

Options

All options are optional and can be passed in Astro's config file:

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

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.

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:

function getSlugFromFilePath(filePath: string) {
  const parsedPath = path.parse(filePath)
  let slug

  // construct slug as full path from either:
  // - folder name if file name is index.md, or
  // - file name
  if (parsedPath.base === 'index.md' || parsedPath.base === 'index.mdx') {
    slug = `${parsedPath.dir}`
  } else {
    slug = `${parsedPath.dir}/${parsedPath.name}`
  }

  return slug
}

Usage

In your Markdown file's frontmatter, use the key redirect_from followed by a list.

---
redirect_from:
  - /old-url-1
  - /old-url-2
  - /old-url-3.html
---

Alternatively, you can use a string instead of a list for a single redirect:

redirect_from: /old-url

More Documentation

Find more explanations, all about server-side redirects, and learn about additional integrations which can be used in combination with astro-redirect-from.

Plugin Development

npm i
npm start

# production build
npm run build

# publishing to npm & GitHub releases
# uses https://github.com/webpro/release-it
npm run release

Testing

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:

npm run format

Changelog

See CHANGELOG.md.

Sponsorship

License

The MIT License

Copyright (c) 2023 Matthias Kretschmann

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 (@kremalicious)