gatsby-source-filesystem 5.5.0-next.1 → 5.6.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +68 -86
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # gatsby-source-filesystem
2
2
 
3
- A Gatsby source plugin for sourcing data into your Gatsby application from your local filesystem.
3
+ A Gatsby plugin for sourcing data into your Gatsby application from your local filesystem.
4
4
 
5
- The plugin creates `File` nodes from files. The various "transformer" plugins can transform `File` nodes into various other types of data e.g. [`gatsby-transformer-json`](https://www.gatsbyjs.com/plugins/gatsby-transformer-json/) transforms JSON files into JSON data nodes and [`gatsby-transformer-remark`](https://www.gatsbyjs.com/plugins/gatsby-transformer-remark/) transforms markdown files into `MarkdownRemark` nodes from which you can query an HTML representation of the markdown.
5
+ The plugin creates `File` nodes from files. The various [transformer plugins](https://www.gatsbyjs.com/plugins/?=gatsby-transformer) can transform `File` nodes into other types of data e.g. [`gatsby-transformer-json`](https://www.gatsbyjs.com/plugins/gatsby-transformer-json/) transforms JSON files into `JSON` nodes and [`gatsby-transformer-remark`](https://www.gatsbyjs.com/plugins/gatsby-transformer-remark/) transforms markdown files into `MarkdownRemark` nodes.
6
6
 
7
7
  ## Install
8
8
 
@@ -12,7 +12,7 @@ npm install gatsby-source-filesystem
12
12
 
13
13
  ## How to use
14
14
 
15
- You can have multiple instances of this plugin to read source nodes from different locations on your filesystem. Be sure to give each instance a unique `name`.
15
+ You can have multiple instances of this plugin in your `gatsby-config` to read files from different locations on your filesystem. Be sure to give each instance a unique `name`.
16
16
 
17
17
  ```js:title=gatsby-config.js
18
18
  module.exports = {
@@ -41,6 +41,8 @@ module.exports = {
41
41
  }
42
42
  ```
43
43
 
44
+ In the above example every file under `src/pages` and `src/data` will be made available as a `File` node inside GraphQL. You don't need to set up another instance of `gatsby-source-filesystem` for e.g. `src/data/images` (since those files are already sourced). However, if you want to be able to filter your files you can set up a new instance and later use the `sourceInstanceName`.
45
+
44
46
  ## Options
45
47
 
46
48
  ### name
@@ -82,43 +84,41 @@ By default, `gatsby-source-filesystem` creates an MD5 hash of each file to deter
82
84
 
83
85
  ### Environment variables
84
86
 
85
- To prevent concurrent requests overload of `processRemoteNode`, you can adjust the `200` default concurrent downloads, with `GATSBY_CONCURRENT_DOWNLOAD` environment variable.
87
+ - `GATSBY_CONCURRENT_DOWNLOAD` (default: `200`). To prevent concurrent requests you can configure the concurrency of `processRemoteNode`.
86
88
 
87
- In case that due to spotty network, or slow connection, some remote files fail to download. Even after multiple retries and adjusting concurrent downloads, you can adjust timeout and retry settings with these environment variables:
89
+ If you have a spotty network or slow connection, you can adjust the retries and timeouts:
88
90
 
89
- - `GATSBY_STALL_RETRY_LIMIT`, default: `3`
90
- - `GATSBY_STALL_TIMEOUT`, default: `30000`
91
- - `GATSBY_CONNECTION_TIMEOUT`, default: `30000`
91
+ - `GATSBY_STALL_RETRY_LIMIT` (default: `3`)
92
+ - `GATSBY_STALL_TIMEOUT` (default: `30000`)
93
+ - `GATSBY_CONNECTION_TIMEOUT` (default: `30000`)
92
94
 
93
95
  ## How to query
94
96
 
95
- You can query file nodes like the following:
97
+ You can query the `File` nodes as following:
96
98
 
97
99
  ```graphql
98
100
  {
99
101
  allFile {
100
- edges {
101
- node {
102
- extension
103
- dir
104
- modifiedTime
105
- }
102
+ nodes {
103
+ extension
104
+ dir
105
+ modifiedTime
106
106
  }
107
107
  }
108
108
  }
109
109
  ```
110
110
 
111
- To filter by the `name` you specified in the config, use `sourceInstanceName`:
111
+ Use [GraphiQL](https://www.gatsbyjs.com/docs/how-to/querying-data/running-queries-with-graphiql/) to explore all available keys.
112
+
113
+ To filter by the `name` you specified in the `gatsby-config`, use `sourceInstanceName`:
112
114
 
113
115
  ```graphql
114
116
  {
115
117
  allFile(filter: { sourceInstanceName: { eq: "data" } }) {
116
- edges {
117
- node {
118
- extension
119
- dir
120
- modifiedTime
121
- }
118
+ nodes {
119
+ extension
120
+ dir
121
+ modifiedTime
122
122
  }
123
123
  }
124
124
  }
@@ -128,24 +128,24 @@ To filter by the `name` you specified in the config, use `sourceInstanceName`:
128
128
 
129
129
  `gatsby-source-filesystem` exports three helper functions:
130
130
 
131
- - `createFilePath`
132
- - `createRemoteFileNode`
133
- - `createFileNodeFromBuffer`
131
+ - [`createFilePath`](#createfilepath)
132
+ - [`createRemoteFileNode`](#createremotefilenode)
133
+ - [`createFileNodeFromBuffer`](#createfilenodefrombuffer)
134
134
 
135
- ### createFilePath
135
+ ### `createFilePath`
136
136
 
137
- When building pages from files, you often want to create a URL from a file's path on the file system. E.g. if you have a markdown file at `src/content/2018-01-23-an-exploration-of-the-nature-of-reality/index.md`, you might want to turn that into a page on your site at `example.com/2018-01-23-an-exploration-of-the-nature-of-reality/`. `createFilePath` is a helper function to make this task easier.
137
+ When building pages from files, you often want to create a URL from a file's path on the filesystem. For example, if you have a markdown file at `src/content/2018-01-23-my-blog-post/index.md`, you might want to turn that into a page on your site at `example.com/blog/2018-01-23-my-blog-post/`. `createFilePath` is a helper function to make this task easier.
138
138
 
139
139
  ```javascript
140
140
  createFilePath({
141
141
  // The node you'd like to convert to a path
142
- // e.g. from a markdown, JSON, YAML file, etc
142
+ // e.g. from a markdown, JSON, YAML file, etc.
143
143
  node,
144
144
  // Method used to get a node
145
145
  // The parameter from `onCreateNode` should be passed in here
146
146
  getNode,
147
147
  // The base path for your files.
148
- // It is relative to the `options.path` setting in the `gatsby-source-filesystem` entries of your `gatsby-config.js`.
148
+ // It is relative to the `options.path` setting in the `gatsby-source-filesystem` entries of your `gatsby-config`.
149
149
  // Defaults to `src/pages`. For the example above, you'd use `src/content`.
150
150
  basePath,
151
151
  // Whether you want your file paths to contain a trailing `/` slash
@@ -154,35 +154,35 @@ createFilePath({
154
154
  })
155
155
  ```
156
156
 
157
- #### Example usage
157
+ #### Example
158
158
 
159
- ```javascript
159
+ ```js:title=gatsby-node.js
160
160
  const { createFilePath } = require(`gatsby-source-filesystem`)
161
161
 
162
162
  exports.onCreateNode = ({ node, getNode, actions }) => {
163
163
  const { createNodeField } = actions
164
164
  // Ensures we are processing only markdown files
165
165
  if (node.internal.type === "MarkdownRemark") {
166
- // Use `createFilePath` to turn markdown files in our `data/faqs` directory into `/faqs/slug`
166
+ // Use `createFilePath` to turn markdown files in our `src/content` directory into `/blog/slug`
167
167
  const relativeFilePath = createFilePath({
168
168
  node,
169
169
  getNode,
170
- basePath: "data/faqs/",
170
+ basePath: "src/content",
171
171
  })
172
172
 
173
173
  // Creates new query'able field with name of 'slug'
174
174
  createNodeField({
175
175
  node,
176
176
  name: "slug",
177
- value: `/faqs${relativeFilePath}`,
177
+ value: `/blog${relativeFilePath}`,
178
178
  })
179
179
  }
180
180
  }
181
181
  ```
182
182
 
183
- ### createRemoteFileNode
183
+ ### `createRemoteFileNode`
184
184
 
185
- When building source plugins for remote data sources such as headless CMSs, their data will often link to files stored remotely that are often convenient to download so you can work with them locally.
185
+ When building source plugins for remote data sources (Headless CMSs, APIs, etc.), their data will often link to files stored remotely that are often convenient to download so you can work with them locally.
186
186
 
187
187
  The `createRemoteFileNode` helper makes it easy to download remote files and add them to your site's GraphQL schema.
188
188
 
@@ -192,80 +192,63 @@ While downloading the assets, special characters (regex: `/:|\/|\*|\?|"|<|>|\||\
192
192
  createRemoteFileNode({
193
193
  // The source url of the remote file
194
194
  url: `https://example.com/a-file.jpg`,
195
-
196
- // The id of the parent node (i.e. the node to which the new remote File node will be linked to.
195
+ // The id of the parent node (i.e. the node to which the new remote File node will be linked to)
197
196
  parentNodeId,
198
-
199
197
  // Gatsby's cache which the helper uses to check if the file has been downloaded already. It's passed to all Node APIs.
200
198
  getCache,
201
-
202
199
  // The action used to create nodes
203
200
  createNode,
204
-
205
201
  // A helper function for creating node Ids
206
202
  createNodeId,
207
-
208
203
  // OPTIONAL
209
204
  // Adds htaccess authentication to the download request if passed in.
210
205
  auth: { htaccess_user: `USER`, htaccess_pass: `PASSWORD` },
211
-
212
206
  // OPTIONAL
213
207
  // Adds extra http headers to download request if passed in.
214
208
  httpHeaders: { Authorization: `Bearer someAccessToken` },
215
-
216
209
  // OPTIONAL
217
210
  // Sets the file extension
218
- ext: ".jpg",
211
+ ext: `.jpg`,
219
212
  })
220
213
  ```
221
214
 
222
- #### Example usage
215
+ #### Example
223
216
 
224
- The following example is pulled from [gatsby-source-wordpress](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-wordpress). Downloaded files are created as `File` nodes and then linked to the WordPress Media node, so it can be queried both as a regular `File` node and from the `localFile` field in the Media node.
217
+ The following example is pulled from the [Preprocessing External Images guide](https://www.gatsbyjs.com/docs/how-to/images-and-media/preprocessing-external-images/). Downloaded files are created as `File` nodes and then linked to the `MarkdownRemark` node, so it can be used with e.g. [`gatsby-plugin-image`](https://www.gatsbyjs.com/docs/how-to/images-and-media/using-gatsby-plugin-image/). The file node can then be queried using GraphQL.
225
218
 
226
- ```javascript
227
- const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
219
+ ```js:title=gatsby-node.js
220
+ const { createRemoteFileNode } = require("gatsby-source-filesystem")
228
221
 
229
- exports.downloadMediaFiles = ({
230
- nodes,
231
- getCache,
232
- createNode,
222
+ exports.onCreateNode = async ({
223
+ node,
224
+ actions: { createNode, createNodeField },
233
225
  createNodeId,
234
- _auth,
226
+ getCache,
235
227
  }) => {
236
- nodes.map(async node => {
237
- let fileNode
238
- // Ensures we are only processing Media Files
239
- // `wordpress__wp_media` is the media file type name for WordPress
240
- if (node.__type === `wordpress__wp_media`) {
241
- try {
242
- fileNode = await createRemoteFileNode({
243
- url: node.source_url,
244
- parentNodeId: node.id,
245
- getCache,
246
- createNode,
247
- createNodeId,
248
- auth: _auth,
249
- })
250
- } catch (e) {
251
- // Ignore
252
- }
253
- }
228
+ // For all MarkdownRemark nodes that have a featured image url, call createRemoteFileNode
229
+ if (
230
+ node.internal.type === "MarkdownRemark" &&
231
+ node.frontmatter.featuredImgUrl !== null
232
+ ) {
233
+ const fileNode = await createRemoteFileNode({
234
+ url: node.frontmatter.featuredImgUrl, // string that points to the URL of the image
235
+ parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
236
+ createNode, // helper function in gatsby-node to generate the node
237
+ createNodeId, // helper function in gatsby-node to generate the node id
238
+ getCache,
239
+ })
254
240
 
255
- // Adds a field `localFile` to the node
256
- // ___NODE appendix tells Gatsby that this field will link to another node
241
+ // if the file was created, extend the node with "localFile"
257
242
  if (fileNode) {
258
- node.localFile___NODE = fileNode.id
243
+ createNodeField({ node, name: "localFile", value: fileNode.id })
259
244
  }
260
- })
245
+ }
261
246
  }
262
247
  ```
263
248
 
264
- The file node can then be queried using GraphQL. See an example of this in the [gatsby-source-wordpress README](/plugins/gatsby-source-wordpress/#image-processing) where downloaded images are queried using [gatsby-transformer-sharp](/plugins/gatsby-transformer-sharp/) to use in the component [gatsby-image](/plugins/gatsby-image/).
265
-
266
249
  #### Retrieving the remote file name and extension
267
250
 
268
- The helper tries first to retrieve the file name and extension by parsing the url and the path provided (e.g. if the url is `https://example.com/image.jpg`, the extension will be inferred as `.jpg` and the name as `image`). If the url does not contain an extension, we use the [`file-type`](https://www.npmjs.com/package/file-type) package to infer the file type. Finally, the name and the extension _can_ be explicitly passed, like so:
251
+ The helper first tries to retrieve the file name and extension by parsing the url and the path provided (e.g. if the url is `https://example.com/image.jpg`, the extension will be inferred as `.jpg` and the name as `image`). If the url does not contain an extension, `createRemoteFileNode` use the [`file-type`](https://www.npmjs.com/package/file-type) package to infer the file type. Finally, the name and the extension _can_ be explicitly passed, like so:
269
252
 
270
253
  ```javascript
271
254
  createRemoteFileNode({
@@ -276,25 +259,24 @@ createRemoteFileNode({
276
259
  createNode,
277
260
  createNodeId,
278
261
  // if necessary!
279
- ext: ".jpg",
280
- name: "image",
262
+ ext: `.jpg`,
263
+ name: `image`,
281
264
  })
282
265
  ```
283
266
 
284
- ### createFileNodeFromBuffer
267
+ ### `createFileNodeFromBuffer`
285
268
 
286
269
  When working with data that isn't already stored in a file, such as when querying binary/blob fields from a database, it's helpful to cache that data to the filesystem in order to use it with other transformers that accept files as input.
287
270
 
288
- The `createFileNodeFromBuffer` helper accepts a `Buffer`, caches its contents to disk, and creates a file node that points to it.
271
+ The `createFileNodeFromBuffer` helper accepts a `Buffer`, caches its contents to disk, and creates a `File` node that points to it.
289
272
 
290
273
  The name of the file can be passed to the `createFileNodeFromBuffer` helper. If no name is given, the content hash will be used to determine the name.
291
274
 
292
- #### Example usage
275
+ #### Example
293
276
 
294
277
  The following example is adapted from the source of [`gatsby-source-mysql`](https://github.com/malcolm-kee/gatsby-source-mysql):
295
278
 
296
- ```js
297
- // gatsby-node.js
279
+ ```js:title=gatsby-node.js
298
280
  const createMySqlNodes = require(`./create-nodes`)
299
281
 
300
282
  exports.sourceNodes = async ({ actions, createNodeId, getCache }, config) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gatsby-source-filesystem",
3
3
  "description": "Gatsby source plugin for building websites from local data. Markdown, JSON, images, YAML, CSV, and dozens of other data types supported.",
4
- "version": "5.5.0-next.1",
4
+ "version": "5.6.0-next.0",
5
5
  "author": "Kyle Mathews <mathews.kyle@gmail.com>",
6
6
  "bugs": {
7
7
  "url": "https://github.com/gatsbyjs/gatsby/issues"
@@ -11,7 +11,7 @@
11
11
  "chokidar": "^3.5.3",
12
12
  "file-type": "^16.5.4",
13
13
  "fs-extra": "^11.1.0",
14
- "gatsby-core-utils": "^4.5.0-next.1",
14
+ "gatsby-core-utils": "^4.6.0-next.0",
15
15
  "mime": "^3.0.0",
16
16
  "pretty-bytes": "^5.6.0",
17
17
  "valid-url": "^1.0.9",
@@ -20,7 +20,7 @@
20
20
  "devDependencies": {
21
21
  "@babel/cli": "^7.20.7",
22
22
  "@babel/core": "^7.20.7",
23
- "babel-preset-gatsby-package": "^3.5.0-next.0",
23
+ "babel-preset-gatsby-package": "^3.6.0-next.0",
24
24
  "cross-env": "^7.0.3"
25
25
  },
26
26
  "homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem#readme",
@@ -46,5 +46,5 @@
46
46
  "engines": {
47
47
  "node": ">=18.0.0"
48
48
  },
49
- "gitHead": "f8f084ae96bd105509903216daa766325ec65b9e"
49
+ "gitHead": "ede0901d34f1224914a873d59c2a370297e47422"
50
50
  }