2018-04-07 14:18:06 +02:00
|
|
|
const path = require('path')
|
2018-11-25 01:52:25 +01:00
|
|
|
const remark = require('remark')
|
|
|
|
const markdown = require('remark-parse')
|
|
|
|
const html = require('remark-html')
|
|
|
|
|
2018-12-07 11:20:02 +01:00
|
|
|
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) + '...'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-25 01:52:25 +01:00
|
|
|
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
|
|
|
|
})
|
2018-12-07 11:20:02 +01:00
|
|
|
|
|
|
|
// Create excerpt from description
|
|
|
|
const excerpt = truncate.apply(description, [320, true])
|
|
|
|
|
|
|
|
createNodeField({
|
|
|
|
node,
|
|
|
|
name: 'excerpt',
|
|
|
|
value: excerpt
|
|
|
|
})
|
2018-11-25 01:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
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 }) => {
|
2018-06-13 20:46:36 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|