/
gatsby-node.js
80 lines (67 loc) · 1.84 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const fs = require("fs")
const path = require(`path`)
const blogPost = path.resolve(`./src/templates/blog-post.js`)
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(`
query {
allTexto {
nodes {
id
slug
title # used in prev/next
}
}
}
`)
if (result.errors) {
throw result.errors
}
const posts = result.data.allTexto.nodes
posts.forEach(({ id, slug }, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1]
const next = index === 0 ? null : posts[index - 1]
createPage({
path: slug,
component: blogPost,
context: {
id,
slug,
previous,
next,
},
})
})
}
exports.shouldOnCreateNode = ({ node }) =>
node.internal.type === "File"
exports.onCreateNode = ({ node, actions, createNodeId }) => {
// Do minimal processing to get some key pieces. This could be gatsby-transformer-text or -html :p
const html = fs.readFileSync(node.absolutePath, "utf8")
const base = path.basename(node.absolutePath)
const slug = base.slice(11, -5) // remove date prefix and `..txt` tail
const date = base.slice(0, 10)
const offset1 = html.indexOf("<h1>")
const title = html.slice(
offset1 + "<h1>".length,
html.indexOf("</h1>", offset1)
)
const offset2 = html.indexOf("<blockquote>", offset1)
const description = html.slice(
offset2 + "<blockquote>".length,
html.indexOf("</blockquote>", offset2)
)
actions.createNode({
id: createNodeId(slug),
slug,
date,
title,
description,
html,
internal: {
type: "Texto",
contentDigest: html,
},
parent: node.id, // Required otherwise the node is not cached and a warm build screws up. TODO: is touchNode faster?
})
}