Add some Git information on File
fields from the latest (or latest matching) commit:
date, author, and email.
This fork was created to add information for files that are added via symlink, such as to combine files from other Git repositories. The plugin resolves the symlink to the original file and base the information on the Git repository at the original location.
You can also set the plugin to return the latest commit that matches a given regex.
yarn add @colliercz/gatsby-transformer-gitinfo
Note: You also need to have gatsby-source-filesystem
installed
and configured so it points to your files.
In your gatsby-config.js
, add:
module.exports = {
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
path: `./src/data/`,
},
},
`@colliercz/gatsby-transformer-gitinfo`,
],
}
Where the source folder ./src/data/
is a git versioned directory.
The plugin will add several fields to File
nodes:
gitLogLatestAuthorName
, gitLogLatestAuthorEmail
, and gitLogLatestDate
.
These fields are related to the latest commit touching that file.
If the file is not versionned, these fields will be null
.
They are exposed in your graphql schema which you can query:
query {
allFile {
edges {
node {
fields {
gitLogLatestAuthorName
gitLogLatestAuthorEmail
gitLogLatestDate
}
}
}
}
}
Now you have a File
node to work with:
{
"data": {
"allFile": {
"edges": [
{
"node": {
"fields": {
"gitLogLatestAuthorName":"John Doe",
"gitLogLatestAuthorEmail": "john.doe@github.com",
"gitLogLatestDate": "2020-10-14T12:58:39.000Z"
}
}
}
]
}
}
}
This plugin will try to match the absolute path of the file with the include
regex.
If it does not match, the file will be skipped.
module.exports = {
plugins: [
{
resolve: `gatsby-transformer-gitinfo`,
options: {
include: /\.md$/i, // Only .md files
},
},
],
}
This plugin will try to match the absolute path of the file with the ignore
regex.
If it does match, the file will be skipped.
module.exports = {
plugins: [
{
resolve: `gatsby-transformer-gitinfo`,
options: {
ignore: /\.jpeg$/i, // All files except .jpeg
},
},
],
}
Return the latest commit where a given regex matches the title or body of the commit log. If no commit matches, the file is skipped.
The object has two keys:
regex
: [string][required] the POSIX basic regular expression to matchinvert
: [bool][optional] whether to invert the match (return the latest commit that does not match the regex) (default:false
)
module.exports = {
plugins: [
{
resolve: `gatsby-transformer-gitinfo`,
options: {
// Return the latest commit with a commit message
// that does NOT start with "skip-this:".
match: {
regex: "^skip-this:",
invert: true,
}
},
},
],
}
The root of the git repository. Will use the current directory if not provided.
Note that including this option will override resolution of symlinks. All files will be checked against the given git repository.
Note: the execution order is first ìnclude
, then ignore
.
module.exports = {
plugins: [
{
resolve: `gatsby-transformer-gitinfo`,
options: {
include: /\.md$/i,
ignore: /README/i, // Will match all .md files, except README.md
},
},
],
}