1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-15 09:35:21 +01:00
blog/gatsby/onCreateNode.js

52 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-10-14 02:25:24 +02:00
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
2018-11-18 23:11:36 +01:00
const { repoContentPath } = require('../config')
2018-10-14 02:25:24 +02:00
// Create slug & date for posts from file path values
exports.createMarkdownFields = (node, createNodeField, getNode) => {
const fileNode = getNode(node.parent)
const parsedFilePath = path.parse(fileNode.relativePath)
const slugOriginal = createFilePath({ node, getNode })
// slug
let slug
if (parsedFilePath.name === 'index') {
slug = `/${parsedFilePath.dir.substring(11)}` // remove date from file dir
} else {
slug = `/${slugOriginal.substring(12)}` // remove first slash & date from file path
}
createNodeField({
node,
name: 'slug',
value: slug
})
// date
let date
if (node.frontmatter.date) {
date = `${node.frontmatter.date}`
} else {
date = `${slugOriginal.substring(1, 10)}` // grab date from file path
}
createNodeField({
node,
name: 'date',
value: date
})
2018-11-18 23:11:36 +01:00
// github file link
const type = fileNode.sourceInstanceName
const file = fileNode.relativePath
const githubLink = `${repoContentPath}/${type}/${file}`
createNodeField({
node,
name: 'githubLink',
value: githubLink
})
2018-10-14 02:25:24 +02:00
}