diff --git a/content/versions.yml b/content/versions.yml new file mode 100644 index 00000000..e379ad32 --- /dev/null +++ b/content/versions.yml @@ -0,0 +1,14 @@ +- title: '16.3.2' + changelog: https://github.com/facebook/react/blob/master/CHANGELOG.md#1632-april-16-2018 +- title: '16.2.0' + path: /version/16.2 + url: https://5abc31d8be40f1556f06c4be--reactjs.netlify.com + changelog: https://github.com/facebook/react/blob/master/CHANGELOG.md#1620-november-28-2017 +- title: '16.1.1' + path: /version/16.1 + url: https://5a1dbcf14c4b93299e65b9a9--reactjs.netlify.com + changelog: https://github.com/facebook/react/blob/master/CHANGELOG.md#1611-november-13-2017 +- title: '16.0.0' + path: /version/16.0 + url: https://5a046bf5a6188f4b8fa4938a--reactjs.netlify.com + changelog: https://github.com/facebook/react/blob/master/CHANGELOG.md#1600-september-26-2017 \ No newline at end of file diff --git a/gatsby-config.js b/gatsby-config.js index 66f84a54..fe4410e9 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -20,6 +20,7 @@ module.exports = { 'gatsby-source-react-error-codes', 'gatsby-transformer-authors-yaml', 'gatsby-transformer-home-example-code', + 'gatsby-transformer-versions-yaml', 'gatsby-plugin-netlify', 'gatsby-plugin-glamor', 'gatsby-plugin-react-next', diff --git a/plugins/gatsby-transformer-versions-yaml/create-redirects.js b/plugins/gatsby-transformer-versions-yaml/create-redirects.js new file mode 100644 index 00000000..af9c49a5 --- /dev/null +++ b/plugins/gatsby-transformer-versions-yaml/create-redirects.js @@ -0,0 +1,67 @@ +const {appendFile, exists, readFile, writeFile} = require('fs-extra'); + +const HEADER_COMMENT = `## Created with gatsby-transformer-versions-yaml`; + +// Patterned after the 'gatsby-plugin-netlify' plug-in: +// https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-netlify/src/create-redirects.js +module.exports = async function writeRedirectsFile( + redirects, + redirectsFilePath, +) { + if (!redirects.length) { + return null; + } + + // Map redirect data to the format Netlify expects + // https://www.netlify.com/docs/redirects/ + redirects = redirects.map(redirect => { + const { + fromPath, + isPermanent, + redirectInBrowser, // eslint-disable-line no-unused-vars + toPath, + ...rest + } = redirect; + + // The order of these parameters is significant. + const pieces = [ + fromPath, + toPath, + isPermanent ? 301 : 302, // Status + ]; + + for (let key in rest) { + const value = rest[key]; + + if (typeof value === `string` && value.indexOf(` `) >= 0) { + console.warn( + `Invalid redirect value "${value}" specified for key "${key}". ` + + `Values should not contain spaces.`, + ); + } else { + pieces.push(`${key}=${value}`); + } + } + + return pieces.join(` `); + }); + + let appendToFile = false; + + // Websites may also have statically defined redirects + // In that case we should append to them (not overwrite) + // Make sure we aren't just looking at previous build results though + const fileExists = await exists(redirectsFilePath); + if (fileExists) { + const fileContents = await readFile(redirectsFilePath); + if (fileContents.indexOf(HEADER_COMMENT) < 0) { + appendToFile = true; + } + } + + const data = `${HEADER_COMMENT}\n\n${redirects.join(`\n`)}`; + + return appendToFile + ? appendFile(redirectsFilePath, `\n\n${data}`) + : writeFile(redirectsFilePath, data); +}; diff --git a/plugins/gatsby-transformer-versions-yaml/gatsby-node.js b/plugins/gatsby-transformer-versions-yaml/gatsby-node.js new file mode 100644 index 00000000..e73b566f --- /dev/null +++ b/plugins/gatsby-transformer-versions-yaml/gatsby-node.js @@ -0,0 +1,29 @@ +const readFileSync = require('fs').readFileSync; +const resolve = require('path').resolve; +const safeLoad = require('js-yaml').safeLoad; +const createRedirects = require('./create-redirects'); +const path = require('path'); + +// Reads versions.yml data into GraphQL. +// This is used to generate redirect rules for older documentation versions. +exports.onPostBuild = async ({store}) => { + const versionsFile = resolve(__dirname, '../../content/versions.yml'); + const file = readFileSync(versionsFile, 'utf8'); + const versions = safeLoad(file); + + const {program} = store.getState(); + const redirectsFilePath = path.join( + program.directory, + 'public', + '_redirects', + ); + + // versions.yml structure is [{path: string, url: string, ...}, ...] + createRedirects( + versions.filter(version => version.path && version.url).map(version => ({ + fromPath: version.path, + toPath: version.url, + })), + redirectsFilePath, + ); +}; diff --git a/plugins/gatsby-transformer-versions-yaml/package.json b/plugins/gatsby-transformer-versions-yaml/package.json new file mode 100644 index 00000000..dcfada5c --- /dev/null +++ b/plugins/gatsby-transformer-versions-yaml/package.json @@ -0,0 +1,4 @@ +{ + "name": "gatsby-transformer-versions-yaml", + "version": "0.0.1" +} \ No newline at end of file diff --git a/src/components/LayoutHeader/Header.js b/src/components/LayoutHeader/Header.js index f5ec82d3..35e6c55c 100644 --- a/src/components/LayoutHeader/Header.js +++ b/src/components/LayoutHeader/Header.js @@ -162,9 +162,7 @@ const Header = ({location}: {location: Location}) => ( borderRadius: 15, }, }} - href="https://github.com/facebook/react/releases" - target="_blank" - rel="noopener"> + href="/versions"> v{version} ( + +
+
+
React Versions
+ +
+

+ A complete release history for React is available{' '} + + in GitHub + . Documentation for recent releases can also be found below: +

+ {versions.map(version => ( +
+

{version.title}

+ +
+ ))} +
+
+
+ +); + +export default Versions; diff --git a/static/robots.txt b/static/robots.txt new file mode 100644 index 00000000..30823580 --- /dev/null +++ b/static/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: /version/ \ No newline at end of file