1
0
Fork 0
blog/gatsby/createMarkdownFields.js

56 lines
1.5 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
2019-10-02 13:35:50 +02:00
function createSlug(node, createNodeField, slugOriginal, parsedFilePath) {
2018-10-14 02:25:24 +02:00
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
})
2019-10-02 13:35:50 +02:00
}
2018-10-14 02:25:24 +02:00
2019-10-02 13:35:50 +02:00
function createDate(node, createNodeField, slugOriginal) {
2019-02-03 21:18:59 +01:00
// grab date from file path
let date = new Date(slugOriginal.substring(1, 11)).toISOString() // grab date from file path
2018-10-14 02:25:24 +02:00
if (node.frontmatter.date) {
2019-02-03 21:18:59 +01:00
date = new Date(node.frontmatter.date).toISOString()
2018-10-14 02:25:24 +02:00
}
createNodeField({
node,
name: 'date',
value: date
})
2019-10-02 13:35:50 +02:00
}
// Create slug, date & github file link 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 })
createSlug(node, createNodeField, slugOriginal, parsedFilePath)
createDate(node, createNodeField, slugOriginal)
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
}