mirror of
https://github.com/kremalicious/blog.git
synced 2024-10-08 00:33:12 +02:00
Switch to astro-redirect-from (#830)
* astro-redirect-from prototype * move out plugin * switch to astro-redirect-from * fixes * new post: astro-redirect-from * fix dependency * test fixes * downgrade and lock astro * until fix for https://github.com/withastro/astro/issues/8649 has been released * mention debug json file * fix e2e test
This commit is contained in:
parent
16b2b0cdf8
commit
c123dd9d68
@ -4,8 +4,9 @@ import remarkToc from '../src/lib/remark-toc'
|
||||
import react from '@astrojs/react'
|
||||
import sitemap from '@astrojs/sitemap'
|
||||
import expressiveCode from 'astro-expressive-code'
|
||||
import redirects from './redirects.json'
|
||||
import redirectFrom from 'astro-redirect-from'
|
||||
import config from './blog.config'
|
||||
import { getSlug } from '../src/lib/astro/getSlug'
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
@ -22,7 +23,6 @@ export default defineConfig({
|
||||
}
|
||||
},
|
||||
server: { host: true },
|
||||
redirects,
|
||||
vite: {
|
||||
resolve: {
|
||||
// for making content -> src/content symlink work
|
||||
@ -49,6 +49,7 @@ export default defineConfig({
|
||||
}
|
||||
}
|
||||
}),
|
||||
redirectFrom({ contentDir: './content', getSlug }),
|
||||
sitemap({
|
||||
filter: (page) =>
|
||||
!page.includes('page/') &&
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -24,3 +24,4 @@ playwright-report/
|
||||
playwright/.cache/
|
||||
**/tmp/
|
||||
public/theme.js
|
||||
src/lib/astro-redirect-from
|
12
README.md
12
README.md
@ -24,7 +24,7 @@
|
||||
- [📝 GitHub changelog rendering](#-github-changelog-rendering)
|
||||
- [🌗 Theme Switcher](#-theme-switcher)
|
||||
- [💎 SVG assets as components](#-svg-assets-as-components)
|
||||
- [`redirect_from`](#redirect_from)
|
||||
- [astro-redirect-from](#astro-redirect-from)
|
||||
- [RSS \& JSON feeds](#rss--json-feeds)
|
||||
- [✨ Development](#-development)
|
||||
- [🔮 Linting](#-linting)
|
||||
@ -145,15 +145,11 @@ If you want to know how this works, have a look at the respective files:
|
||||
|
||||
- [`scripts/create-icons/`](scripts/create-icons/)
|
||||
|
||||
### `redirect_from`
|
||||
### astro-redirect-from
|
||||
|
||||
Still a remnant of the old [Jekyll](https://jekyllrb.com) days, which survived in [gatsby-redirect-from](/gatsby-redirect-from/) and now works in Astro. For all post slugs defined in a `redirect_from` frontmatter key, redirects will be put in place by Astro.
|
||||
Still a remnant of the old [Jekyll](https://jekyllrb.com) days, which survived in [gatsby-redirect-from](https://kremalicious.com/gatsby-redirect-from/) and now works in Astro with [astro-redirect-from](https://kremalicious.com/astro-redirect-from/).
|
||||
|
||||
Before building the site, a script scans all markdown files and creates a json file under `.config/redirects.json`. This file is then imported into `astro.config.ts` under its `redirects` option.
|
||||
|
||||
If you want to know how, have a look at the respective files:
|
||||
|
||||
- [`scripts/redirect-from.ts`](scripts/redirect-from.ts)
|
||||
For all post slugs defined in a `redirect_from` frontmatter key, redirects will be put in place by Astro.
|
||||
|
||||
### RSS & JSON feeds
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
date: 2020-05-22T14:08:00.367Z
|
||||
updated: 2020-05-23T11:35:12+02:00
|
||||
|
||||
title: Redirect plugin for Markdown Pages in Gatsby
|
||||
title: Redirect Plugin for Markdown Pages in Gatsby
|
||||
image: ./gatsby-redirect-from-teaser.png
|
||||
changelog: kremalicious/gatsby-redirect-from
|
||||
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 18 KiB |
Binary file not shown.
After Width: | Height: | Size: 92 KiB |
168
content/articles/2023-09-23-astro-redirect-from/index.md
Normal file
168
content/articles/2023-09-23-astro-redirect-from/index.md
Normal file
@ -0,0 +1,168 @@
|
||||
---
|
||||
date: 2023-09-23T19:20:50.153Z
|
||||
|
||||
title: Redirect Plugin for Markdown Pages in Astro
|
||||
image: ./astro-redirect-from-teaser.png
|
||||
|
||||
tags:
|
||||
- astro
|
||||
- development
|
||||
- goodies
|
||||
|
||||
changelog: kremalicious/astro-redirect-from
|
||||
toc: true
|
||||
---
|
||||
|
||||
Integration plugin for [Astro](https://astro.build) to create redirects based on a list in your Markdown frontmatter, mimicking the behavior of [jekyll-redirect-from](https://github.com/jekyll/jekyll-redirect-from) and [gatsby-redirect-from](/gatsby-redirect-from).
|
||||
|
||||
Imagine you've just revamped your blog, and some of your old URLs have changed. You don't want to lose the SEO juice, nor do you want to leave your readers with broken links. This is where redirects come into play. But managing redirects can be cumbersome, especially if you have to do it manually or through server configurations.
|
||||
|
||||
This plugin automates this process by reading the `redirect_from` field in the frontmatter of your Markdown files and updating Astro's configuration to handle these redirects automatically.
|
||||
|
||||
## How it Works
|
||||
|
||||
By adding a `redirect_from` list in your Markdown frontmatter, the plugin integrates these redirects into Astro's [`redirects`](https://docs.astro.build/en/reference/configuration-reference/#redirects) configuration automatically, whether you're running the development server or building your project.
|
||||
|
||||
The plugin operates during the [`astro:config:setup`](https://docs.astro.build/en/reference/integrations-reference/#astroconfigsetup) lifecycle hook. It scans all Markdown files for the `redirect_from` key and updates Astro's configuration using [`updateConfig()`](https://docs.astro.build/en/reference/integrations-reference/#updateconfig-option). This ensures that any existing redirects are merged with new ones generated by the plugin.
|
||||
|
||||
Astro takes over from there, handling the redirects based on your site's build mode and in combination with any other integrations you might be using:
|
||||
|
||||
> For statically-generated sites with no adapter installed, this will produce a client redirect using a `<meta http-equiv="refresh">` tag and does not support status codes.
|
||||
>
|
||||
> When using SSR or with a static adapter in `output: static` mode, status codes are supported. Astro will serve redirected `GET` requests with a status of `301` and use a status of `308` for any other request method.
|
||||
> [Astro Configuration Reference: redirects](https://docs.astro.build/en/reference/configuration-reference/#redirects)
|
||||
|
||||
This plugin works with various Astro hosting integrations, most of them generate further redirect files in the places they require so this plugin works in combination with them:
|
||||
|
||||
| Provider | Plugin |
|
||||
| ---------- | ---------------------------------------------------------------------------------------- |
|
||||
| Netlify | [@astrojs/netlify](https://docs.astro.build/en/guides/integrations-guide/netlify/) |
|
||||
| Vercel | [@astrojs/vercel](https://docs.astro.build/en/guides/integrations-guide/vercel/) |
|
||||
| Cloudflare | [@astrojs/cloudflare](https://docs.astro.build/en/guides/integrations-guide/cloudflare/) |
|
||||
|
||||
Because Astro integrations are run in the order they are defined in the `integrations` array, this plugin should come before any other integrations which make use of the `redirects` config.
|
||||
|
||||
## 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
|
||||
|
||||
```bash
|
||||
cd yourproject/
|
||||
|
||||
# Using NPM
|
||||
npx astro add astro-redirect-from
|
||||
# Using Yarn
|
||||
yarn astro add astro-redirect-from
|
||||
# Using PNPM
|
||||
pnpm astro add astro-redirect-from
|
||||
```
|
||||
|
||||
If installing manually:
|
||||
|
||||
```bash
|
||||
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:
|
||||
|
||||
```js title="astro.config.mjs"
|
||||
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](#usage)
|
||||
|
||||
### Options
|
||||
|
||||
All options are optional and can be passed in Astro's config file:
|
||||
|
||||
```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_
|
||||
|
||||
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:
|
||||
|
||||
```typescript
|
||||
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.
|
||||
|
||||
```yaml
|
||||
---
|
||||
redirect_from:
|
||||
- /old-url-1
|
||||
- /old-url-2
|
||||
- /old-url-3.html
|
||||
---
|
||||
```
|
||||
|
||||
## Check out & contribute
|
||||
|
||||
Head over to GitHub to take a peek into the code, or to report some bugs.
|
||||
|
||||
<p class="content-download">
|
||||
<a class="icon-github btn btn-primary" href="https://github.com/kremalicious/astro-redirect-from">GitHub</a>
|
||||
</p>
|
206
package-lock.json
generated
206
package-lock.json
generated
@ -16,8 +16,9 @@
|
||||
"@astrojs/ts-plugin": "^1.1.3",
|
||||
"@nanostores/react": "^0.7.1",
|
||||
"@rainbow-me/rainbowkit": "^1.0.11",
|
||||
"astro": "^3.1.0",
|
||||
"astro-expressive-code": "^0.22.2",
|
||||
"astro": "3.1.0",
|
||||
"astro-expressive-code": "^0.26.0",
|
||||
"astro-redirect-from": "^0.2.3",
|
||||
"date-fns": "^2.30.0",
|
||||
"dms2dec": "^1.1.0",
|
||||
"fast-exif": "^2.0.1",
|
||||
@ -51,8 +52,6 @@
|
||||
"eslint-plugin-prettier": "^5.0.0",
|
||||
"eslint-plugin-react": "^7.33.2",
|
||||
"eslint-plugin-testing-library": "^6.0.1",
|
||||
"front-matter": "^4.0.2",
|
||||
"glob": "^10.3.4",
|
||||
"hast-util-to-html": "^9.0.0",
|
||||
"identity-obj-proxy": "^3.0.0",
|
||||
"markdownlint-cli": "^0.36.0",
|
||||
@ -81,6 +80,35 @@
|
||||
"node": "18"
|
||||
}
|
||||
},
|
||||
"astro-redirect-from": {
|
||||
"version": "0.2.3",
|
||||
"extraneous": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"astro": ">= 3",
|
||||
"globby": "^13.2.2",
|
||||
"gray-matter": "^4.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.6.3",
|
||||
"@typescript-eslint/eslint-plugin": "^6.7.2",
|
||||
"auto-changelog": "^2.4.0",
|
||||
"eslint": "^8.49.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-plugin-prettier": "^5.0.0",
|
||||
"prettier": "^3.0.3",
|
||||
"release-it": "^16.1.5",
|
||||
"typescript": "^5.2.2",
|
||||
"vite": "^4.4.9"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.14.1",
|
||||
"npm": ">=6.14.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"astro": ">= 3"
|
||||
}
|
||||
},
|
||||
"node_modules/@aashutoshrathi/word-wrap": {
|
||||
"version": "1.2.6",
|
||||
"dev": true,
|
||||
@ -341,7 +369,8 @@
|
||||
},
|
||||
"node_modules/@astrojs/telemetry": {
|
||||
"version": "3.0.1",
|
||||
"license": "MIT",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/telemetry/-/telemetry-3.0.1.tgz",
|
||||
"integrity": "sha512-7zJMuikRDQ0LLLivteu0+y4pqdgznrChFiRrY3qmKlOEkLWD1T3u1a5M970lvpErP7Vgh4P298JBPjv8LTj+sw==",
|
||||
"dependencies": {
|
||||
"ci-info": "^3.8.0",
|
||||
"debug": "^4.3.4",
|
||||
@ -358,7 +387,8 @@
|
||||
},
|
||||
"node_modules/@astrojs/telemetry/node_modules/is-docker": {
|
||||
"version": "3.0.0",
|
||||
"license": "MIT",
|
||||
"resolved": "https://registry.npmjs.org/is-docker/-/is-docker-3.0.0.tgz",
|
||||
"integrity": "sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==",
|
||||
"bin": {
|
||||
"is-docker": "cli.js"
|
||||
},
|
||||
@ -371,7 +401,8 @@
|
||||
},
|
||||
"node_modules/@astrojs/telemetry/node_modules/is-wsl": {
|
||||
"version": "3.0.0",
|
||||
"license": "MIT",
|
||||
"resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-3.0.0.tgz",
|
||||
"integrity": "sha512-TQ7xXW/fTBaz/HhGSV779AC99ocpvb9qJPuPwyIea+F+Z+htcQ1wouAA0xEQaa4saVqyP8mwkoYp5efeM/4Gbg==",
|
||||
"dependencies": {
|
||||
"is-docker": "^3.0.0"
|
||||
},
|
||||
@ -1002,9 +1033,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@expressive-code/core": {
|
||||
"version": "0.22.2",
|
||||
"resolved": "https://registry.npmjs.org/@expressive-code/core/-/core-0.22.2.tgz",
|
||||
"integrity": "sha512-fVfnopl4dz75KgZ8q9G6sL+GW7QAzuWnNrm4zTKRabRzwdTZ9MCUmGPJvUpxKovJ1Z4t6YIKGHTson0a7fvV5g==",
|
||||
"version": "0.26.0",
|
||||
"resolved": "https://registry.npmjs.org/@expressive-code/core/-/core-0.26.0.tgz",
|
||||
"integrity": "sha512-hhvRbIDiUX3DI3CnX8gDCY5lgn7TzhVz8PGunAfhAoIWp0+P72NSLoaLIg4FwOXFWEomTRLfkGk2cVQrppCXmQ==",
|
||||
"dependencies": {
|
||||
"@ctrl/tinycolor": "^3.6.0",
|
||||
"hast-util-to-html": "^8.0.4",
|
||||
@ -1045,29 +1076,29 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@expressive-code/plugin-frames": {
|
||||
"version": "0.22.2",
|
||||
"resolved": "https://registry.npmjs.org/@expressive-code/plugin-frames/-/plugin-frames-0.22.2.tgz",
|
||||
"integrity": "sha512-Tn4COPTdySVJ6gygCCqYd0KMQXea4l6NN/9Px2uSekPDLUiE9Ff4i3005Pa1rr31m0hLBes4POnFRRmwqIu+ZA==",
|
||||
"version": "0.26.0",
|
||||
"resolved": "https://registry.npmjs.org/@expressive-code/plugin-frames/-/plugin-frames-0.26.0.tgz",
|
||||
"integrity": "sha512-mFPTh8tE1mItQcfJM6F6p8aWaa4Pi+KceBMIW6IsByQZKyMGdyS+PYphn0KbUjYI2za0/DVIJdukM9XueSpJ3A==",
|
||||
"dependencies": {
|
||||
"@expressive-code/core": "^0.22.2",
|
||||
"@expressive-code/core": "^0.26.0",
|
||||
"hastscript": "^7.2.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@expressive-code/plugin-shiki": {
|
||||
"version": "0.22.2",
|
||||
"resolved": "https://registry.npmjs.org/@expressive-code/plugin-shiki/-/plugin-shiki-0.22.2.tgz",
|
||||
"integrity": "sha512-BDNkEb2OwmoL5kJJnNZ6fXk5IytZordEWGjWycEiKyHMXotJ+94S0PIIiTfVIp38H1faL+yd+kz2pF4t7ePcww==",
|
||||
"version": "0.26.0",
|
||||
"resolved": "https://registry.npmjs.org/@expressive-code/plugin-shiki/-/plugin-shiki-0.26.0.tgz",
|
||||
"integrity": "sha512-3qCSfDSR31joT3lQLOVw+fSSNp/ZYpZP3Os8sr1ekTa4yh41SgiwWk5rYm8PhKMGOnw8nhzlknwLkcZJf20sOQ==",
|
||||
"dependencies": {
|
||||
"@expressive-code/core": "^0.22.2",
|
||||
"@expressive-code/core": "^0.26.0",
|
||||
"shiki": "^0.14.1"
|
||||
}
|
||||
},
|
||||
"node_modules/@expressive-code/plugin-text-markers": {
|
||||
"version": "0.22.2",
|
||||
"resolved": "https://registry.npmjs.org/@expressive-code/plugin-text-markers/-/plugin-text-markers-0.22.2.tgz",
|
||||
"integrity": "sha512-9lMON0kVEn0LAIe9mHcXaxdwqnxCF7MR+IExyC4OGhMVEc1p8Vp7rvSxsUPjySQV2QvYNTlQOaKVi/eI5oM1Ag==",
|
||||
"version": "0.26.0",
|
||||
"resolved": "https://registry.npmjs.org/@expressive-code/plugin-text-markers/-/plugin-text-markers-0.26.0.tgz",
|
||||
"integrity": "sha512-klBtYJf3liTOnhiKHNpuO9KC1vAYcANtSWz07yPnnQzCwkRo9bTkdOI+9xSViVisNiAF6TfCuoEKjlB7BmtP7A==",
|
||||
"dependencies": {
|
||||
"@expressive-code/core": "^0.22.2",
|
||||
"@expressive-code/core": "^0.26.0",
|
||||
"hastscript": "^7.2.0",
|
||||
"unist-util-visit-parents": "^5.1.3"
|
||||
}
|
||||
@ -4106,16 +4137,62 @@
|
||||
}
|
||||
},
|
||||
"node_modules/astro-expressive-code": {
|
||||
"version": "0.22.2",
|
||||
"resolved": "https://registry.npmjs.org/astro-expressive-code/-/astro-expressive-code-0.22.2.tgz",
|
||||
"integrity": "sha512-pmyuTJcEzfYzxPNBsIEjVNTzgWHxDGiv4D/oJusvOm30x7ETADMusqf9uYmJ6rS1jlwAgfaUWm7vTv6SaxWrSw==",
|
||||
"version": "0.26.0",
|
||||
"resolved": "https://registry.npmjs.org/astro-expressive-code/-/astro-expressive-code-0.26.0.tgz",
|
||||
"integrity": "sha512-ZofTdPVTu5yXFJJnwx/GDNwihufuGzUqn/sE1PqDWyDyDADsx28G8VitWH3KMVQ1tRSuc/rm6Y7MRgb/ydS1tA==",
|
||||
"dependencies": {
|
||||
"remark-expressive-code": "^0.22.2"
|
||||
"remark-expressive-code": "^0.26.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"astro": "^2.0.0 || ^3.0.0-beta"
|
||||
}
|
||||
},
|
||||
"node_modules/astro-redirect-from": {
|
||||
"version": "0.2.3",
|
||||
"resolved": "https://registry.npmjs.org/astro-redirect-from/-/astro-redirect-from-0.2.3.tgz",
|
||||
"integrity": "sha512-d0CyfAP8NH0oMbkSwnZyYcOHt02RYAmVWPhtnMsi1z+4ihAukS5LDUs6Clis+EF6PEk8K/aBfUHPGQa5j7IZGg==",
|
||||
"dependencies": {
|
||||
"astro": ">= 3",
|
||||
"globby": "^13.2.2",
|
||||
"gray-matter": "^4.0.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.14.1",
|
||||
"npm": ">=6.14.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"astro": ">= 3"
|
||||
}
|
||||
},
|
||||
"node_modules/astro-redirect-from/node_modules/globby": {
|
||||
"version": "13.2.2",
|
||||
"resolved": "https://registry.npmjs.org/globby/-/globby-13.2.2.tgz",
|
||||
"integrity": "sha512-Y1zNGV+pzQdh7H39l9zgB4PJqjRNqydvdYCDG4HFXM4XuvSaQQlEc91IU1yALL8gUTDomgBAfz3XJdmUS+oo0w==",
|
||||
"dependencies": {
|
||||
"dir-glob": "^3.0.1",
|
||||
"fast-glob": "^3.3.0",
|
||||
"ignore": "^5.2.4",
|
||||
"merge2": "^1.4.1",
|
||||
"slash": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/astro-redirect-from/node_modules/slash": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/slash/-/slash-4.0.0.tgz",
|
||||
"integrity": "sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew==",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/astro/node_modules/@astrojs/compiler": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@astrojs/compiler/-/compiler-2.1.0.tgz",
|
||||
@ -4790,6 +4867,8 @@
|
||||
},
|
||||
"node_modules/busboy": {
|
||||
"version": "1.6.0",
|
||||
"resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz",
|
||||
"integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==",
|
||||
"dependencies": {
|
||||
"streamsearch": "^1.1.0"
|
||||
},
|
||||
@ -5003,13 +5082,14 @@
|
||||
},
|
||||
"node_modules/ci-info": {
|
||||
"version": "3.8.0",
|
||||
"resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.8.0.tgz",
|
||||
"integrity": "sha512-eXTggHWSooYhq49F2opQhuHWgzucfF2YgODK4e1566GQs5BIfP30B0oenwBJHfWxAs2fyPB1s7Mg949zLf61Yw==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/sibiraj-s"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
@ -5846,7 +5926,6 @@
|
||||
},
|
||||
"node_modules/dir-glob": {
|
||||
"version": "3.0.1",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"path-type": "^4.0.0"
|
||||
@ -5857,7 +5936,8 @@
|
||||
},
|
||||
"node_modules/dlv": {
|
||||
"version": "1.1.3",
|
||||
"license": "MIT"
|
||||
"resolved": "https://registry.npmjs.org/dlv/-/dlv-1.1.3.tgz",
|
||||
"integrity": "sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA=="
|
||||
},
|
||||
"node_modules/dms2dec": {
|
||||
"version": "1.1.0",
|
||||
@ -5945,7 +6025,8 @@
|
||||
},
|
||||
"node_modules/dset": {
|
||||
"version": "3.1.2",
|
||||
"license": "MIT",
|
||||
"resolved": "https://registry.npmjs.org/dset/-/dset-3.1.2.tgz",
|
||||
"integrity": "sha512-g/M9sqy3oHe477Ar4voQxWtaPIFw1jTdKZuomOjhCcBx9nHUNn0pu6NopuFFrTh/TRZIKEj+76vLWFu9BNKk+Q==",
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
}
|
||||
@ -6960,14 +7041,14 @@
|
||||
}
|
||||
},
|
||||
"node_modules/expressive-code": {
|
||||
"version": "0.22.2",
|
||||
"resolved": "https://registry.npmjs.org/expressive-code/-/expressive-code-0.22.2.tgz",
|
||||
"integrity": "sha512-2KOsjZKx6pRLVhlIo7ikZxL0CJzuvpP4LeGcFiz7YsqUtT3ak4MgEeD1ph82FNp2isl+vnc8OZu1xoGDi9JxMw==",
|
||||
"version": "0.26.0",
|
||||
"resolved": "https://registry.npmjs.org/expressive-code/-/expressive-code-0.26.0.tgz",
|
||||
"integrity": "sha512-1290zk/uHg+1gCiM9TNVl77FZGTTkdlaXDGSJjNffwFlaO6+nF4Mi0zFLKZ0QoPfPlucOF/DwNSVNa74Dxck1g==",
|
||||
"dependencies": {
|
||||
"@expressive-code/core": "^0.22.2",
|
||||
"@expressive-code/plugin-frames": "^0.22.2",
|
||||
"@expressive-code/plugin-shiki": "^0.22.2",
|
||||
"@expressive-code/plugin-text-markers": "^0.22.2"
|
||||
"@expressive-code/core": "^0.26.0",
|
||||
"@expressive-code/plugin-frames": "^0.26.0",
|
||||
"@expressive-code/plugin-shiki": "^0.26.0",
|
||||
"@expressive-code/plugin-text-markers": "^0.26.0"
|
||||
}
|
||||
},
|
||||
"node_modules/extend": {
|
||||
@ -7243,37 +7324,6 @@
|
||||
"url": "https://github.com/sponsors/rawify"
|
||||
}
|
||||
},
|
||||
"node_modules/front-matter": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/front-matter/-/front-matter-4.0.2.tgz",
|
||||
"integrity": "sha512-I8ZuJ/qG92NWX8i5x1Y8qyj3vizhXS31OxjKDu3LKP+7/qBgfIKValiZIEwoVoJKUHlhWtYrktkxV1XsX+pPlg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"js-yaml": "^3.13.1"
|
||||
}
|
||||
},
|
||||
"node_modules/front-matter/node_modules/argparse": {
|
||||
"version": "1.0.10",
|
||||
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
|
||||
"integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"sprintf-js": "~1.0.2"
|
||||
}
|
||||
},
|
||||
"node_modules/front-matter/node_modules/js-yaml": {
|
||||
"version": "3.14.1",
|
||||
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz",
|
||||
"integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"argparse": "^1.0.7",
|
||||
"esprima": "^4.0.0"
|
||||
},
|
||||
"bin": {
|
||||
"js-yaml": "bin/js-yaml.js"
|
||||
}
|
||||
},
|
||||
"node_modules/fs-constants": {
|
||||
"version": "1.0.0",
|
||||
"devOptional": true,
|
||||
@ -8191,7 +8241,6 @@
|
||||
},
|
||||
"node_modules/ignore": {
|
||||
"version": "5.2.4",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 4"
|
||||
@ -11746,7 +11795,6 @@
|
||||
},
|
||||
"node_modules/path-type": {
|
||||
"version": "4.0.0",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
@ -12890,11 +12938,11 @@
|
||||
}
|
||||
},
|
||||
"node_modules/remark-expressive-code": {
|
||||
"version": "0.22.2",
|
||||
"resolved": "https://registry.npmjs.org/remark-expressive-code/-/remark-expressive-code-0.22.2.tgz",
|
||||
"integrity": "sha512-NwVpKDHiHsD+3CwxDrXA6B2fNttVDK0BBnkHLPQoKNOuEokYWA9+3trZQw77txBGimNLsJQCBl/tddsHen2g8w==",
|
||||
"version": "0.26.0",
|
||||
"resolved": "https://registry.npmjs.org/remark-expressive-code/-/remark-expressive-code-0.26.0.tgz",
|
||||
"integrity": "sha512-kh50Wk+o/nNLqa1Z+IhGmhbzHZwiwNpAqcoPuQPBwTBqbleehlcw9m8PtHIwxMFhWqfhzXxtJj9TOmwc+AyHrA==",
|
||||
"dependencies": {
|
||||
"expressive-code": "^0.22.2",
|
||||
"expressive-code": "^0.26.0",
|
||||
"hast-util-to-html": "^8.0.4",
|
||||
"unist-util-visit": "^4.1.2"
|
||||
}
|
||||
@ -13941,6 +13989,8 @@
|
||||
},
|
||||
"node_modules/streamsearch": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz",
|
||||
"integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
@ -15134,8 +15184,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/undici": {
|
||||
"version": "5.23.0",
|
||||
"license": "MIT",
|
||||
"version": "5.25.2",
|
||||
"resolved": "https://registry.npmjs.org/undici/-/undici-5.25.2.tgz",
|
||||
"integrity": "sha512-tch8RbCfn1UUH1PeVCXva4V8gDpGAud/w0WubD6sHC46vYQ3KDxL+xv1A2UxK0N6jrVedutuPHxe1XIoqerwMw==",
|
||||
"dependencies": {
|
||||
"busboy": "^1.6.0"
|
||||
},
|
||||
@ -16261,7 +16312,8 @@
|
||||
},
|
||||
"node_modules/which-pm-runs": {
|
||||
"version": "1.1.0",
|
||||
"license": "MIT",
|
||||
"resolved": "https://registry.npmjs.org/which-pm-runs/-/which-pm-runs-1.1.0.tgz",
|
||||
"integrity": "sha512-n1brCuqClxfFfq/Rb0ICg9giSZqCS+pLtccdag6C2HyufBrh3fBOiy9nb6ggRMvWOVH5GrdJskj5iGTZNxd7SA==",
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
}
|
||||
|
@ -13,7 +13,7 @@
|
||||
"typecheck:astro": "astro check",
|
||||
"typecheck:tsc": "tsc --noEmit",
|
||||
"typecheck": "npm run typecheck:astro && npm run typecheck:tsc",
|
||||
"prebuild": "run-p --silent --continue-on-error create:symlinks create:icons create:redirects move:downloads",
|
||||
"prebuild": "run-p --silent --continue-on-error create:symlinks create:icons move:downloads",
|
||||
"test:unit": "vitest run --config './test/vitest.config.ts' --coverage",
|
||||
"test:e2e": "playwright test --config './test/playwright.config.ts'",
|
||||
"lint": "run-p --silent lint:js lint:css lint:md",
|
||||
@ -36,8 +36,9 @@
|
||||
"@astrojs/ts-plugin": "^1.1.3",
|
||||
"@nanostores/react": "^0.7.1",
|
||||
"@rainbow-me/rainbowkit": "^1.0.11",
|
||||
"astro": "^3.1.0",
|
||||
"astro-expressive-code": "^0.22.2",
|
||||
"astro": "3.1.0",
|
||||
"astro-expressive-code": "^0.26.0",
|
||||
"astro-redirect-from": "^0.2.3",
|
||||
"date-fns": "^2.30.0",
|
||||
"dms2dec": "^1.1.0",
|
||||
"fast-exif": "^2.0.1",
|
||||
@ -71,8 +72,6 @@
|
||||
"eslint-plugin-prettier": "^5.0.0",
|
||||
"eslint-plugin-react": "^7.33.2",
|
||||
"eslint-plugin-testing-library": "^6.0.1",
|
||||
"front-matter": "^4.0.2",
|
||||
"glob": "^10.3.4",
|
||||
"hast-util-to-html": "^9.0.0",
|
||||
"identity-obj-proxy": "^3.0.0",
|
||||
"markdownlint-cli": "^0.36.0",
|
||||
|
@ -26,7 +26,7 @@ test('copyZipFiles should copy zip files', async () => {
|
||||
fail: vi.fn()
|
||||
}
|
||||
|
||||
copyZipFiles(sourceDir, destDir, mockOra as any)
|
||||
await copyZipFiles(sourceDir, destDir, mockOra as any)
|
||||
|
||||
const file1 = await fs.readFile(path.join(destDir, 'file1.zip'), 'utf-8')
|
||||
const file2 = await fs.readFile(path.join(destDir, 'file2.zip'), 'utf-8')
|
||||
|
@ -4,7 +4,7 @@
|
||||
//
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { glob } from 'glob'
|
||||
import globby from 'globby'
|
||||
import ora, { type Ora } from 'ora'
|
||||
import chalk from 'chalk'
|
||||
|
||||
@ -30,7 +30,7 @@ function removeFolderContents(folderPath: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export function copyZipFiles(
|
||||
export async function copyZipFiles(
|
||||
source: string,
|
||||
destination: string,
|
||||
spinner: Ora
|
||||
@ -44,7 +44,7 @@ export function copyZipFiles(
|
||||
}
|
||||
|
||||
// Find all files recursively in the source folder
|
||||
const zipFiles = glob.sync(filesGlob, { cwd: source })
|
||||
const zipFiles = await globby(filesGlob, { cwd: source })
|
||||
|
||||
zipFiles.forEach((zipFile: string) => {
|
||||
const sourcePath = path.join(source, zipFile)
|
||||
@ -70,4 +70,4 @@ export function copyZipFiles(
|
||||
)
|
||||
}
|
||||
|
||||
copyZipFiles(sourceFolder, destinationFolder, spinner)
|
||||
await copyZipFiles(sourceFolder, destinationFolder, spinner)
|
||||
|
@ -1,32 +0,0 @@
|
||||
import { test, expect, vi } from 'vitest'
|
||||
import fs from 'node:fs/promises'
|
||||
import { Stats } from 'node:fs'
|
||||
import { findMarkdownFilesWithRedirects } from './redirect-from'
|
||||
|
||||
test('findMarkdownFilesWithRedirects should generate correct redirects', async () => {
|
||||
const readdirMock = vi.spyOn(fs, 'readdir')
|
||||
readdirMock.mockResolvedValue(['post1.md', 'post2.md'] as any)
|
||||
|
||||
const statMock = vi.spyOn(fs, 'stat')
|
||||
statMock.mockResolvedValue({ isFile: () => true } as Stats)
|
||||
|
||||
const readFileMock = vi.spyOn(fs, 'readFile')
|
||||
readFileMock.mockResolvedValueOnce(
|
||||
'---\nredirect_from: ["/old1", "/old2"]\nslug: /new1\n---'
|
||||
)
|
||||
readFileMock.mockResolvedValueOnce(
|
||||
'---\nredirect_from: ["/old3"]\nslug: /new2\n---'
|
||||
)
|
||||
|
||||
// Mock fs.writeFile to do nothing
|
||||
const writeFileMock = vi.spyOn(fs, 'writeFile')
|
||||
writeFileMock.mockResolvedValue()
|
||||
|
||||
const redirects = await findMarkdownFilesWithRedirects('some/dir')
|
||||
|
||||
expect(redirects).toEqual({
|
||||
'/old1': '/new1',
|
||||
'/old2': '/new1',
|
||||
'/old3': '/new2'
|
||||
})
|
||||
})
|
@ -1,76 +0,0 @@
|
||||
//
|
||||
// astro-redirect-from
|
||||
//
|
||||
import fs from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import frontmatter from 'front-matter'
|
||||
import ora from 'ora'
|
||||
import chalk from 'chalk'
|
||||
|
||||
const contentDir = 'content/'
|
||||
const outputFilePath = '.config/redirects.json'
|
||||
let fileCount = 0
|
||||
|
||||
type Frontmatter = { redirect_from?: string[]; slug?: string }
|
||||
|
||||
const spinner = ora(
|
||||
`${chalk.bold('[redirect-from]')} Extract redirects`
|
||||
).start()
|
||||
|
||||
export async function findMarkdownFilesWithRedirects(
|
||||
dir: string
|
||||
): Promise<{ [old: string]: string }> {
|
||||
const redirects: { [old: string]: string } = {}
|
||||
|
||||
async function processDir(currentDir: string) {
|
||||
const items = await fs.readdir(currentDir, { recursive: true })
|
||||
|
||||
for (const item of items) {
|
||||
const itemPath = path.join(currentDir, item)
|
||||
const stats = await fs.stat(itemPath)
|
||||
|
||||
if (
|
||||
stats.isFile() &&
|
||||
item.endsWith('.md') &&
|
||||
!itemPath.includes('links')
|
||||
) {
|
||||
const fileContent = await fs.readFile(itemPath, 'utf-8')
|
||||
const { attributes }: { attributes: Frontmatter } =
|
||||
frontmatter(fileContent)
|
||||
|
||||
// construct slug from frontmatter or folder name
|
||||
const postSlug =
|
||||
attributes.slug || `/${itemPath.split('/')[2].substring(11)}`
|
||||
|
||||
// Check if the Markdown file has a redirect_from field
|
||||
if (attributes.redirect_from) {
|
||||
fileCount++
|
||||
const redirectFromSlugs = attributes.redirect_from
|
||||
for (const slug of redirectFromSlugs) {
|
||||
// Add entries to the redirects object
|
||||
redirects[slug] = postSlug
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await processDir(dir)
|
||||
return redirects
|
||||
}
|
||||
|
||||
try {
|
||||
const redirects = await findMarkdownFilesWithRedirects(contentDir)
|
||||
const redirectsJSON = JSON.stringify(redirects, null, 2)
|
||||
|
||||
// Write the redirects object to the output file
|
||||
fs.writeFile(outputFilePath, redirectsJSON, 'utf-8')
|
||||
|
||||
spinner.succeed(
|
||||
`${chalk.bold('[redirect-from]')} Extracted ${
|
||||
Object.keys(redirects).length
|
||||
} redirects from ${fileCount} files to ${outputFilePath}`
|
||||
)
|
||||
} catch (error: any) {
|
||||
spinner.fail(`${chalk.bold('[redirect-from]')} ${(error as Error).message}`)
|
||||
}
|
@ -13,7 +13,7 @@ if (!repoInfo) {
|
||||
console.info(`Repo ${repo} not found`)
|
||||
return
|
||||
}
|
||||
const { url, owner, object } = repoInfo
|
||||
const { url, object } = repoInfo
|
||||
const changelogHtml = await markdownToHtml(
|
||||
object.text.replace('### Changelog', '')
|
||||
)
|
||||
@ -27,7 +27,7 @@ const changelogHtml = await markdownToHtml(
|
||||
<p class={styles.source}>
|
||||
sourced from{' '}
|
||||
<a href={`${url}/tree/main/CHANGELOG.md`}>
|
||||
<code>{`${owner?.login}/${repo}:CHANGELOG.md`}</code>
|
||||
<code>{`${repo}:CHANGELOG.md`}</code>
|
||||
</a>
|
||||
</p>
|
||||
<div class={styles.content} set:html={changelogHtml} />
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { type CollectionEntry } from 'astro:content'
|
||||
import { getAllPosts } from './index'
|
||||
import { getAllPosts } from './getAllPosts'
|
||||
|
||||
// helps to reduce DOM size
|
||||
export async function getAllPostsForSearch() {
|
||||
const allPosts = await getAllPosts()
|
||||
if (!allPosts) return
|
||||
if (!allPosts) return []
|
||||
|
||||
const cleaned = await Promise.all(
|
||||
allPosts.map(async (post) => {
|
||||
|
22
src/lib/astro/getSlug.ts
Normal file
22
src/lib/astro/getSlug.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import type { CollectionEntry } from 'astro:content'
|
||||
import path from 'node:path'
|
||||
|
||||
export function getSlug(filePath: string) {
|
||||
const parsedPath = path.parse(filePath)
|
||||
let slug
|
||||
|
||||
// construct slug as full path from either file or folder name,
|
||||
if (parsedPath.base === 'index.md') {
|
||||
slug = parsedPath.dir
|
||||
} else {
|
||||
slug = `${parsedPath.dir}/${parsedPath.name}`
|
||||
}
|
||||
|
||||
// remove folder structure
|
||||
slug = slug.split('/')[1]
|
||||
|
||||
// remove the date prefix
|
||||
slug = slug.substring(11)
|
||||
|
||||
return slug as CollectionEntry<'articles' | 'photos' | 'links'>['slug']
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
import { getCollection, type CollectionEntry } from 'astro:content'
|
||||
import { getCollection } from 'astro:content'
|
||||
import { readOutExif } from '@lib/exif'
|
||||
import path from 'path'
|
||||
import config from '@config/blog.config'
|
||||
import { sortPosts } from './sortPosts'
|
||||
import { getSlug } from './getSlug'
|
||||
|
||||
//
|
||||
// Main loader for all collections content.
|
||||
@ -29,19 +30,9 @@ export async function loadAndFormatCollection(
|
||||
: new Date(post.id.split('/')[0].substring(0, 10))
|
||||
|
||||
//
|
||||
// remove date from slug
|
||||
// construct slug from folder or file name
|
||||
//
|
||||
let slug = post.id.split('/')[0].substring(11) as CollectionEntry<
|
||||
'articles' | 'photos' | 'links'
|
||||
>['slug']
|
||||
|
||||
// links are not folders so remove .md from the end
|
||||
if (post.collection === 'links') {
|
||||
slug = slug.substring(
|
||||
0,
|
||||
slug.length - 3
|
||||
) as CollectionEntry<'links'>['slug']
|
||||
}
|
||||
const slug = getSlug(`${post.collection}/${post.id}`)
|
||||
|
||||
const githubLink = `${config.repoContentPath}/${post.collection}/${post.id}`
|
||||
|
||||
|
@ -9,11 +9,11 @@ test.beforeEach(async ({ page }) => {
|
||||
})
|
||||
|
||||
test('meta is correct', async ({ page }) => {
|
||||
await expect(page).toHaveTitle(/Redirect plugin for Markdown Pages in Gatsby/)
|
||||
await expect(page).toHaveTitle(/Redirect Plugin for Markdown Pages in Gatsby/)
|
||||
|
||||
await expect(page.locator('meta[property="og:title"]')).toHaveAttribute(
|
||||
'content',
|
||||
/Redirect plugin for Markdown Pages in Gatsby/
|
||||
/Redirect Plugin for Markdown Pages in Gatsby/
|
||||
)
|
||||
await expect(page.locator('link[rel="canonical"]')).toHaveAttribute(
|
||||
'href',
|
||||
|
Loading…
Reference in New Issue
Block a user