/** * Copyright (c) 2013-present, Facebook, Inc. * * @emails react-core */ 'use strict'; // Parse date information out of blog post filename. const BLOG_POST_FILENAME_REGEX = /([0-9]+)\-([0-9]+)\-([0-9]+)\-(.+)\.md$/; // Add custom fields to MarkdownRemark nodes. module.exports = exports.onCreateNode = ({node, boundActionCreators, getNode}) => { const {createNodeField} = boundActionCreators; switch (node.internal.type) { case 'MarkdownRemark': const {permalink, redirect_from} = node.frontmatter; const {relativePath} = getNode(node.parent); let slug = permalink; if (!slug) { if (relativePath.includes('blog')) { // Blog posts don't have embedded permalinks. // Their slugs follow a pattern: /blog////.html // The date portion comes from the file name: -.md const match = BLOG_POST_FILENAME_REGEX.exec(relativePath); const year = match[1]; const month = match[2]; const day = match[3]; const filename = match[4]; slug = `/blog/${year}/${month}/${day}/${filename}.html`; const date = new Date(year, month - 1, day); // Blog posts are sorted by date and display the date in their header. createNodeField({ node, name: 'date', value: date.toJSON(), }); } } if (!slug) { // This will likely only happen for the partials in /content/home. slug = `/${relativePath.replace('.md', '.html')}`; } // Used to generate URL to view this content. createNodeField({ node, name: 'slug', value: slug, }); // Used to generate a GitHub edit link. createNodeField({ node, name: 'path', value: relativePath, }); // Used by createPages() above to register redirects. createNodeField({ node, name: 'redirect', value: redirect_from ? JSON.stringify(redirect_from) : '', }); return; } };