commit dfb3bb0954b1933c4d9e36ebc8cb302a0dc70795 Author: Matthias Kretschmann Date: Thu Aug 30 01:11:52 2018 +0200 initial commit 🍷 diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..9441e73 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,5 @@ +version: "2" +checks: + method-lines: + config: + threshold: 30 diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7e07638 --- /dev/null +++ b/.editorconfig @@ -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 \ No newline at end of file diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..cf1c9cc --- /dev/null +++ b/.eslintrc @@ -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 + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5f19d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..2276f67 --- /dev/null +++ b/.npmignore @@ -0,0 +1,6 @@ +node_modules +src +.babelrc +.editorconfig +.eslintrc +.travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..83a38c6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: node + +notifications: + email: false diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..abee374 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7ab09a8 --- /dev/null +++ b/README.md @@ -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` diff --git a/gatsby-node.js b/gatsby-node.js new file mode 100644 index 0000000..db8f9f7 --- /dev/null +++ b/gatsby-node.js @@ -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 + ) + }) + ) + }) +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..f5d87a0 --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +// noop +'use strict' diff --git a/package.json b/package.json new file mode 100644 index 0000000..41226f5 --- /dev/null +++ b/package.json @@ -0,0 +1,41 @@ +{ + "name": "gatsby-redirect-from", + "version": "0.0.1", + "author": "Matthias Kretschmann ", + "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" + ] +}