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

87 lines
1.9 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
2019-01-04 18:49:39 +01:00
const template = path.resolve('src/templates/Project.jsx')
2018-04-07 14:18:06 +02:00
2019-01-04 18:49:39 +01:00
const result = await graphql(`
{
allProjectsYaml {
edges {
node {
slug
2018-04-07 14:18:06 +02:00
}
}
2019-01-04 18:49:39 +01:00
}
}
`)
2018-04-07 14:18:06 +02:00
2019-01-04 18:49:39 +01:00
if (result.errors) throw result.errors
2018-04-07 14:18:06 +02:00
2019-01-04 18:49:39 +01:00
result.data.allProjectsYaml.edges.forEach(({ node }) => {
const { slug } = node
2018-04-07 14:18:06 +02:00
2019-01-04 18:49:39 +01:00
createPage({
path: slug,
component: template,
context: { slug }
})
2018-04-07 14:18:06 +02:00
})
}