1
0
mirror of https://github.com/kremalicious/gatsby-redirect-from.git synced 2024-12-22 09:13:20 +01:00

initial commit 🍷

This commit is contained in:
Matthias Kretschmann 2018-08-30 01:11:52 +02:00
commit dfb3bb0954
Signed by: m
GPG Key ID: 606EEEF3C479A91F
11 changed files with 232 additions and 0 deletions

5
.codeclimate.yml Normal file
View File

@ -0,0 +1,5 @@
version: "2"
checks:
method-lines:
config:
threshold: 30

10
.editorconfig Normal file
View File

@ -0,0 +1,10 @@
# EditorConfig is awesome: http://EditorConfig.org
[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true

25
.eslintrc Normal file
View File

@ -0,0 +1,25 @@
{
"extends": ["eslint:recommended"],
"rules": {
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
},
"parserOptions": {
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"modules": true
}
},
"env": {
"browser": true,
"node": true,
"es6": true
}
}

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
package-lock.json

6
.npmignore Normal file
View File

@ -0,0 +1,6 @@
node_modules
src
.babelrc
.editorconfig
.eslintrc
.travis.yml

5
.travis.yml Normal file
View File

@ -0,0 +1,5 @@
language: node_js
node_js: node
notifications:
email: false

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright (c) 2018 Matthias Kretschmann m@kretschmann.io
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.

62
README.md Normal file
View File

@ -0,0 +1,62 @@
# gatsby-redirect-from
> Set redirect urls in your YAML frontmatter within your [Gatsby](https://www.gatsbyjs.org) site's Markdown files. Mimics the behavior of [jekyll-redirect-from](https://github.com/jekyll/jekyll-redirect-from).
By adding a list of urls to the YAML frontmatter, this plugin creates redirects for all of them at build time. It uses Gatsby's [createRedirect](https://next.gatsbyjs.org/docs/actions/#createRedirect) under the hood.
## Prerequisites
- Gatsby v2
- only parses Markdown files delivered from [gatsby-transformer-remark](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-remark)
- `slug` on `fields`
## Installation
```bash
cd yourproject/
npm i gatsby-redirect-from
```
Then load the plugin from your `gatsby-config.js`:
```js
plugins: ['gatsby-redirect-from']
```
## Usage
In your Markdown file's YAML frontmatter, use the key `redirect_from` followed by a list:
```yaml
---
title: Aperture File Types
redirect_from:
# forward slashes are required
- /new-goodies-aperture-file-types-icons/
- /goodie-updated-aperture-file-types-v11/
- /aperture-file-types-v12-released/
- /2008/04/aperture-file-types/
---
```
## License
The MIT License
Copyright (c) 2018 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](https://matthiaskretschmann.com) ([@kremalicious](https://github.com/kremalicious))
Say thanks with BTC:
`35UUssHexVK48jbiSgTxa4QihEoCqrwCTG`
Say thanks with ETH:
`0x04354F554536DA108726829207958d3E277f0210`

67
gatsby-node.js Normal file
View File

@ -0,0 +1,67 @@
'use strict'
const chalk = require('chalk')
exports.createPages = ({ graphql, actions }) => {
const { createRedirect } = actions
return new Promise((resolve, reject) => {
resolve(
graphql(
`
{
allMarkdownRemark(
filter: { frontmatter: { redirect_from: { ne: null } } }
) {
edges {
node {
fields {
slug
}
frontmatter {
redirect_from
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors) // eslint-disable-line no-console
reject(result.errors)
}
const allPosts = result.data.allMarkdownRemark.edges
const redirects = []
// For all posts with redirect_from frontmatter,
// extract all values and push to redirects array
allPosts.forEach(post => {
redirects.push({
from: post.node.frontmatter.redirect_from,
to: post.node.fields.slug
})
})
// Create redirects from the just constructed array
redirects.forEach(({ from, to }) => {
// iterate through all `from` array items
from.forEach(from => {
createRedirect({
fromPath: from,
toPath: to,
isPermanent: true,
redirectInBrowser: true
})
})
})
resolve(
console.log(`${chalk.green('success')} Create redirects`) // eslint-disable-line no-console
)
})
)
})
}

2
index.js Normal file
View File

@ -0,0 +1,2 @@
// noop
'use strict'

41
package.json Normal file
View File

@ -0,0 +1,41 @@
{
"name": "gatsby-redirect-from",
"version": "0.0.1",
"author": "Matthias Kretschmann <m@kretschmann.io>",
"description": "Set redirect urls in your YAML frontmatter within your Gatsby site's Markdown files. Mimics the behavior of jekyll-redirect-from.",
"homepage": "https://github.com/kremalicious/gatsby-redirect-from",
"license": "MIT",
"main": "index.js",
"scripts": {
"test": "eslint ./src/**/*.js",
"release": "./node_modules/release-it/bin/release-it.js --src.tagName='v%s' --github.release --npm.publish --non-interactive",
"release-minor": "./node_modules/release-it/bin/release-it.js minor --src.tagName='v%s' --github.release --npm.publish --non-interactive",
"release-major": "./node_modules/release-it/bin/release-it.js major --src.tagName='v%s' --github.release --npm.publish --non-interactive",
"prepublishOnly": "cross-env NODE_ENV=production npm run build"
},
"browserslist": [
"last 3 versions"
],
"dependencies": {
"chalk": "^2.4.1"
},
"devDependencies": {
"eslint": "^5.4.0",
"release-it": "^7.6.0"
},
"peerDependencies": {
"gatsby": ">=2"
},
"engines": {
"node": "^10.0.0"
},
"repository": "github:kremalicious/gatsby-redirect-from",
"bugs": {
"url": "https://github.com/kremalicious/gatsby-redirect-from/issues"
},
"keywords": [
"gatsby",
"gatsby-plugin",
"redirect"
]
}