Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed Feb 16, 2018
1 parent aa56a76 commit 458c70f
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions packages/gatsby-source-filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ You can query file nodes like the following:
## Helper functions

`gatsby-source-filesystem` exports two helper functions:
- `createFilePath`
- `createRemoteFileNode`

* `createFilePath`
* `createRemoteFileNode`

### createFilePath

Expand All @@ -87,25 +88,30 @@ createFilePath({
```

#### Example usage

The following is taken from [Gatsby Tutorial, Part Four](https://www.gatsbyjs.org/tutorial/part-four/#programmatically-creating-pages-from-data) and is used to create URL slugs for markdown pages.

```javascript
const { createFilePath } = require(`gatsby-source-filesystem`)
const { createFilePath } = require(`gatsby-source-filesystem`);

exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
const { createNodeField } = boundActionCreators
// Ensures we are processing only markdown files
if (node.internal.type === 'MarkdownRemark') {
// Use `createFilePath` to turn markdown files in our `data/faqs` directory into `/faqs/slug`
const relativeFilePath = createFilePath({ node, getNode, basePath: 'data/faqs/' })

// Creates new query'able field with name of 'slug'
createNodeField({
node,
name: 'slug',
value: `/faqs${relativeFilePath}`
})
}
const { createNodeField } = boundActionCreators;
// Ensures we are processing only markdown files
if (node.internal.type === "MarkdownRemark") {
// Use `createFilePath` to turn markdown files in our `data/faqs` directory into `/faqs/slug`
const relativeFilePath = createFilePath({
node,
getNode,
basePath: "data/faqs/",
});

// Creates new query'able field with name of 'slug'
createNodeField({
node,
name: "slug",
value: `/faqs${relativeFilePath}`,
});
}
};
```

Expand Down

0 comments on commit 458c70f

Please sign in to comment.