From 208ff573307d38f385fba0970fc74c0e4632c67a Mon Sep 17 00:00:00 2001 From: Curtis Olson Date: Sun, 31 Mar 2019 14:55:39 -0500 Subject: [PATCH] Add slug field --- .../gatsby-transform-md-tutorials/gatsby-node.js | 16 ++++++++++++++++ src/queries/tutorial.js | 13 +++++++++++++ src/types/tutorial.js | 7 +++++++ 3 files changed, 36 insertions(+) diff --git a/plugins/gatsby-transform-md-tutorials/gatsby-node.js b/plugins/gatsby-transform-md-tutorials/gatsby-node.js index 0823acc98..7c3494f2f 100644 --- a/plugins/gatsby-transform-md-tutorials/gatsby-node.js +++ b/plugins/gatsby-transform-md-tutorials/gatsby-node.js @@ -22,6 +22,8 @@ const nodeQuery = ` const onCreateNode = async ({ actions: { createNode, createNodeField }, getNode, node }, nodeOptions) => { if (node.internal.type === 'MarkdownRemark' && getNode(node.parent).sourceInstanceName === 'colonyTutorials') { + const { langConfig: { defaultLangKey, prefixDefaultLangKey } } = nodeOptions; + let tutorialNode; const { tutorialName, tutorialId } = getTutorialInfo(node); tutorialNode = getNode(tutorialId); @@ -37,6 +39,20 @@ const onCreateNode = async ({ actions: { createNode, createNodeField }, getNode, value: editUrl, }) node.editUrl = editUrl; + + if (!node.frontmatter.locale) { + node.frontmatter.locale = defaultLangKey; + } + const nodeLocale = node.frontmatter.locale; + const localeSlugPrefix = nodeLocale === defaultLangKey && !prefixDefaultLangKey ? '' : `${nodeLocale}/`; + // Add a slug as the TOC creation requires that (for linking) + node.slug = slugify(node.frontmatter.title, { lower: true }) + // Slug for the actual page + createNodeField({ + node: tutorialNode, + name: 'slug', + value: `/${localeSlugPrefix}${tutorialNode.slug}`, + }) } }; diff --git a/src/queries/tutorial.js b/src/queries/tutorial.js index cc720e68d..6c57058df 100644 --- a/src/queries/tutorial.js +++ b/src/queries/tutorial.js @@ -1,6 +1,19 @@ /* @flow */ import { graphql } from 'gatsby'; +export const allTutorialsFragment = graphql` + fragment allTutorialsFragment on Query { + allTutorials: allTutorial { + edges { + node { + name + slug + } + } + } + } +`; + // eslint-disable-next-line import/prefer-default-export export const singleTutorialFragment = graphql` fragment singleTutorialFragment on Query { diff --git a/src/types/tutorial.js b/src/types/tutorial.js index 105aa9c18..279d27244 100644 --- a/src/types/tutorial.js +++ b/src/types/tutorial.js @@ -17,3 +17,10 @@ export type Tutorial = {| slug: string, htmlAst: HtmlAst, |}; + +export type TutorialNode = {| + name: string, + fields: { + slug: string, + }, +|};