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

114 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-05-26 17:50:19 +02:00
/* eslint-disable no-console */
const remark = require('remark')
2019-11-08 23:00:47 +01:00
const parse = require('remark-parse')
const html = require('remark-html')
2019-05-26 17:50:19 +02:00
const fs = require('fs')
const yaml = require('js-yaml')
const reposYaml = yaml.load(fs.readFileSync('./content/repos.yml', 'utf8'))
2019-05-26 22:20:16 +02:00
const { performance } = require('perf_hooks')
const chalk = require('chalk')
2021-03-13 22:50:00 +01:00
const { execSync } = require('child_process')
const { getGithubRepos } = require('./scripts/github')
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) + '...'
)
}
2021-03-13 22:50:00 +01:00
//
// Fetch matomo.js
//
execSync(`node ./scripts/fetch-matomo-js > static/matomo.js`, {
stdio: 'inherit'
})
2019-05-26 22:55:28 +02:00
//
// Get GitHub repos once and store for later build stages
//
2019-05-26 17:50:19 +02:00
let repos
exports.onPreBootstrap = async () => {
2019-05-26 22:20:16 +02:00
const t0 = performance.now()
2019-05-26 17:50:19 +02:00
try {
2019-10-10 00:40:57 +02:00
repos = await getGithubRepos(reposYaml)
2019-05-26 22:20:16 +02:00
const t1 = performance.now()
const ms = t1 - t0
const s = ((ms / 1000) % 60).toFixed(3)
console.log(
chalk.green('success ') + `getGithubRepos: ${repos.length} repos - ${s} s`
)
2019-05-26 17:50:19 +02:00
} catch (error) {
2019-05-26 22:20:16 +02:00
throw Error(error.message)
2019-05-26 17:50:19 +02:00
}
}
2019-05-26 22:55:28 +02:00
//
// Add pageContext
2019-05-26 22:55:28 +02:00
//
exports.onCreatePage = async ({ page, actions }) => {
const { createPage, deletePage } = actions
// Regex for auto-attaching project images to pages based on slug.
// Image file names follow the pattern slug-01.png.
// Regex inspiration from https://stackoverflow.com/a/7124976
2020-11-22 00:25:43 +01:00
const imageRegex = `/${page.path.replace(/\//g, '')}+?(?=-\\d)/`
deletePage(page)
createPage({
...page,
context: {
...page.context,
imageRegex,
// Add repos only to front page's context
...(page.path === '/' && { repos })
}
})
2019-05-26 22:55:28 +02: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()
2019-11-08 23:00:47 +01:00
.use(parse, { 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
})
}
}