1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2024-12-23 01:29:41 +01:00
portfolio/gatsby-node.js

94 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-04-07 14:18:06 +02:00
const path = require('path')
const remark = require('remark')
const markdown = require('remark-parse')
const html = require('remark-html')
function truncate(n, useWordBoundary) {
if (this.length <= n) {
return this
}
const subString = this.substr(0, n - 1)
return (
(useWordBoundary
? subString.substr(0, subString.lastIndexOf(' '))
: subString) + '...'
)
}
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
// Projects YAML nodes
if (node.internal.type === 'ProjectsYaml') {
// Add transformed Markdown descriptions
const description = node.description
const descriptionWithLineBreaks = description.split('\n').join('\n\n')
let descriptionHtml
remark()
.use(markdown, { gfm: true, commonmark: true, pedantic: true })
.use(html)
.process(descriptionWithLineBreaks, (err, file) => {
if (err) throw Error('Could not transform project description')
descriptionHtml = file.contents
return descriptionHtml
})
createNodeField({
node,
name: 'descriptionHtml',
value: descriptionHtml
})
// Create excerpt from description
const excerpt = truncate.apply(description, [320, true])
createNodeField({
node,
name: 'excerpt',
value: excerpt
})
}
}
2018-04-06 17:24:35 +02:00
2018-06-19 22:48:33 +02:00
//
// Create project pages from projects.yml
//
2018-06-23 21:53:57 +02:00
exports.createPages = async ({ actions, graphql }) => {
const { createPage } = actions
2018-04-07 14:18:06 +02:00
return new Promise((resolve, reject) => {
2018-04-25 23:37:23 +02:00
const template = path.resolve('src/templates/Project.jsx')
2018-04-07 14:18:06 +02:00
2018-05-12 22:14:53 +02:00
resolve(
graphql(`
2018-04-07 14:18:06 +02:00
{
2018-05-05 00:17:26 +02:00
allProjectsYaml {
2018-04-07 14:18:06 +02:00
edges {
node {
slug
}
}
}
}
`).then(result => {
2018-09-24 00:13:41 +02:00
if (result.errors) reject(result.errors)
2018-04-07 14:18:06 +02:00
2018-06-19 22:48:33 +02:00
result.data.allProjectsYaml.edges.forEach(({ node }) => {
2018-09-24 00:13:41 +02:00
const { slug } = node
2018-04-07 14:18:06 +02:00
2018-06-19 22:48:33 +02:00
createPage({
path: slug,
component: template,
2018-09-24 00:13:41 +02:00
context: { slug }
2018-06-19 22:48:33 +02:00
})
})
2018-04-07 14:18:06 +02:00
resolve()
2018-05-12 22:14:53 +02:00
})
)
2018-04-07 14:18:06 +02:00
})
}