|
| 1 | +# gatsby-transformer-gitinfo |
| 2 | + |
| 3 | +Add some git information on `File` fields: latest commit date, author and email. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +`npm install --save gatsby-transformer-gitinfo` |
| 8 | + |
| 9 | +**Note:** You also need to have `gatsby-source-filesystem` installed and configured so it |
| 10 | +points to your files. |
| 11 | + |
| 12 | +## How to use |
| 13 | + |
| 14 | +In your `gatsby-config.js` |
| 15 | + |
| 16 | +```javascript |
| 17 | +module.exports = { |
| 18 | + plugins: [ |
| 19 | + `gatsby-transformer-gitinfo`, |
| 20 | + { |
| 21 | + resolve: `gatsby-source-filesystem`, |
| 22 | + options: { |
| 23 | + path: `./src/data/`, |
| 24 | + }, |
| 25 | + }, |
| 26 | + ], |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +Where the _source folder_ `./src/data/` is a git versionned directory. |
| 31 | + |
| 32 | +The plugin will add three fields to `File` nodes: `gitDate`, `gitAuthorName` and `gitAuthorEmail`. These fields are relative to the latest commit touching that file. |
| 33 | + |
| 34 | +If the file is not versionned, these feilds will be `null`. |
| 35 | + |
| 36 | +They are exposed in your graphql schema which you can query: |
| 37 | + |
| 38 | +```graphql |
| 39 | +query { |
| 40 | + allFile { |
| 41 | + edges { |
| 42 | + node { |
| 43 | + fields { |
| 44 | + gitDate |
| 45 | + gitAuthorName |
| 46 | + gitAuthorEmail |
| 47 | + } |
| 48 | + internal { |
| 49 | + type |
| 50 | + mediaType |
| 51 | + description |
| 52 | + owner |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Now you have a File node to work with: |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | + "data": { |
| 65 | + "allFile": { |
| 66 | + "edges": [ |
| 67 | + { |
| 68 | + "node": { |
| 69 | + "fields": { |
| 70 | + "gitDate": "2019-10-14T12:58:39.000Z", |
| 71 | + "gitAuthorName": "John Doe", |
| 72 | + "gitAuthorEmail": "[email protected]" |
| 73 | + }, |
| 74 | + "internal": { |
| 75 | + "contentDigest": "c1644b03f380bc5508456ce91faf0c08", |
| 76 | + "type": "File", |
| 77 | + "mediaType": "text/yaml", |
| 78 | + "description": "File \"src/data/example.yml\"", |
| 79 | + "owner": "gatsby-source-filesystem" |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + ] |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## Configuration options |
| 90 | + |
| 91 | +**`whitelist`** [regex][optional] |
| 92 | + |
| 93 | +This plugin will try to match the absolute path of the file with the `whitelist` regex. |
| 94 | +If it *does not* match, the file will be skipped. |
| 95 | + |
| 96 | +```javascript |
| 97 | +module.exports = { |
| 98 | + plugins: [ |
| 99 | + { |
| 100 | + resolve: `gatsby-transformer-gitinfo`, |
| 101 | + options: { |
| 102 | + whitelist: /\.md$/i, // Only .md files |
| 103 | + }, |
| 104 | + }, |
| 105 | + ], |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | +**`blacklist`** [regex][optional] |
| 111 | + |
| 112 | +This plugin will try to match the absolute path of the file with the `blacklist` regex. |
| 113 | +If it *does* match, the file will be skipped. |
| 114 | + |
| 115 | +```javascript |
| 116 | +module.exports = { |
| 117 | + plugins: [ |
| 118 | + { |
| 119 | + resolve: `gatsby-transformer-gitinfo`, |
| 120 | + options: { |
| 121 | + blacklist: /\.jpeg$/i, // All files except .jpeg |
| 122 | + }, |
| 123 | + }, |
| 124 | + ], |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | + |
| 129 | +Note: the execution order is first whitelist, then blacklist. |
| 130 | + |
| 131 | +```javascript |
| 132 | +module.exports = { |
| 133 | + plugins: [ |
| 134 | + { |
| 135 | + resolve: `gatsby-transformer-gitinfo`, |
| 136 | + options: { |
| 137 | + whitelist: /\.md$/i, |
| 138 | + blacklist: /README/i, // Will match all .md files, except README.md |
| 139 | + }, |
| 140 | + }, |
| 141 | + ], |
| 142 | +} |
| 143 | +``` |
0 commit comments