Skip to content

Commit

Permalink
Add slug field
Browse files Browse the repository at this point in the history
  • Loading branch information
ceolson01 committed Mar 31, 2019
1 parent e9fc578 commit 208ff57
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions plugins/gatsby-transform-md-tutorials/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}`,
})
}
};

Expand Down
13 changes: 13 additions & 0 deletions src/queries/tutorial.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions src/types/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ export type Tutorial = {|
slug: string,
htmlAst: HtmlAst,
|};

export type TutorialNode = {|
name: string,
fields: {
slug: string,
},
|};

0 comments on commit 208ff57

Please sign in to comment.