This project is moving to
@bcgov/gatsby-source-github-raw
repo;
A simplified way of leveraging the Github Contents Api
as Gatsby graphql nodes.
- Implied Mime Types mime/media types are set based on file extensions when available
- Bind Properties you can bind properties that are 'attached' to the raw file node if needed
- Install
npm install --save gatsby-source-github-raw
- Add to your gatsby config
{
resolve: 'gatsby-source-github-raw',
options: {
githubAccessToken: '...',
files: [
'https://github.com/foo/bar/blob/master/something.md'
]
}
}
- githubAccessToken: this is the oauth access token, it will require read priviledges on a repo
- files: this is either a list of files [String], a list of objects, or a name of a resolved transformer json node type, or a function
There are a few ways to pass in files
- as a simple array of strings
['https://github.com/foo/bar/blob/master/something.md', 'https://github.com/foo/bar/blob/master/something2.md']
- as an array of objects, this allows you to bind extra properties to each node
if you are binding properties, you need to ensure that the properties that you bind between files match the same schema since this process depends on gatsby's graphql type inferrence routine
[
{
url: 'https://github.com/foo/bar/blob/master/something.md'
labels: [ // labels will become a bound property that is available at node._xxboundProperties.labels
'cool',
'featured',
]
},
{
url: 'https://github.com/foo/bar/blob/master/something2.md',
labels: [ // labels will become a bound property that is available at node._xxboundProperties.labels
'featured',
]
}
]
- If there are many files you'd like to source, containing them all in the
gatsby-config.js
file is cumbersome. You may store your 'files config' into a json file and havegatsby-transformer-json
pick it up. The impliednodeType
that is created from the directory can be passed in as thefiles
option
foo.json
[
{
"url": "https://github.com/foo/bar/blob/master/something.md",
"labels": [
"cool",
"featured"
]
},
{
"url": "https://github.com/foo/bar/blob/master/something2.md",
"labels": [
"featured"
]
}
]
gatbsy-config
{
resolve: 'gatsby-source-github-raw',
options: {
githubAccessToken: '...',
files: 'fooJson'
}
}
- Files as a function
/**
* fileCallback
* @param {Function} getNodes gatsby getNodes function in case you need it
* @returns {Array<String> | Array<Object>} a list of files in strings or objects
**/
const fileCallback = getNodes => {
// get nodes allows you to produce a set of urls based on your own conditions, for example if you
// had multiple json files that held url information that you wanted to normalize and use
return ['...list of files']
}
{
resolve: 'gatsby-source-github-raw',
options: {
githubAccessToken: '...',
files: () => ['https://github.com/foo/bar/blob/master/blah.md']
}
}
Because manifests can be passed in as plain js objects as well as loaded through the use of
gatsby-transformer-json
, to maintain consistency between these two possible sources, the following
properties are not usable within json
files that are leveraged as a source for files.
- internal
- id
- parent
- children
This is because these properties are default properties applied to any graphql node within gatsby.
Bound Properties are accessed at node._xxboundProperties