forked from binnyva/gatsby-garden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
executable file
·352 lines (309 loc) · 10.3 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const { paginate } = require(`gatsby-awesome-pagination`)
const path = require(`path`)
const makeSlug = require(`./src/utils/make-slug`)
const _ = require(`lodash`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const siteConfig = require(`./gatsby-config`)
exports.onCreateNode = ({ node, getNode, actions }) => {
if (node.internal.type === `Mdx`) {
const { createNodeField } = actions
const fileName = createFilePath({ node, getNode, basePath: `_notes` }).replace(/^\/(.+)\/$/, '$1')
const title = node.frontmatter.title || fileName
const slug = node.frontmatter.slug
? makeSlug(node.frontmatter.slug)
: makeSlug(fileName)
const fileNode = getNode(node.parent)
const date = node.frontmatter.date || fileNode.mtime
const visibility = node.frontmatter.visibility || 'public'
const excerpt = node.frontmatter.excerpt || node.excerpt
// If you are adding new fields here, add it to createSchemaCustomization() as well.
createNodeField({
node,
name: `slug`,
value: `/${slug}`,
})
createNodeField({
node,
name: `fileName`,
value: fileName,
})
createNodeField({
node,
name: `date`,
value: date,
})
createNodeField({
node,
name: `title`,
value: title.replace(/\//g, ``),
})
createNodeField({
node,
name: `excerpt`,
value: excerpt,
})
createNodeField({
node,
name: `visibility`,
value: visibility,
})
// :TODO: Add tags. Ideally, every supported frontmatter should be added as a field.
}
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage, createRedirect } = actions
// Process for notes all public notes
const result = await graphql(`
query {
allMdx(filter: { fields: { visibility: { eq: "public" } } }) {
edges {
node {
fields {
slug
fileName
title
visibility
excerpt
}
frontmatter {
tags
title
date
aliases
}
excerpt
rawBody
body
}
}
}
tags: allMdx(limit: 2000) {
group(field: frontmatter___tags) {
fieldValue
}
}
}
`)
const allNotes = _.get(result, `data.allMdx.edges`)
// Make a map of how notes link to other links. This is necessary to have back links and graph visualisation
let refersTo = {} // refersTo['note title'] = ['note that "note title" linked to', 'another note that "note title" linked to', ...]
let referredBy = {} // referredBy['note title'] = [{title: 'note that linked to "note title"' ...}, {title: 'another note that linked to "note title"', ...}, ...]
let linkageCache = {} // Caches all linking. To prevent duplicate linking. Eg. linkageCache['note title->linked note'] = true
const allNoteTitles = allNotes.map(note => note.node.fields.title) // A list of all note titles. Helps in finding the correct title in a case-insensitive manner.
let allNotesByTitle = {}
// I didn't used the much more cleaner foreach because the `refersTo` was not working well with that.
for (let i = 0; i < result.data.allMdx.edges.length; i++) {
const node = result.data.allMdx.edges[i].node
const title = node.fields.title
const slug = node.fields.slug
const excerpt = node.fields.excerpt || node.excerpt
allNotesByTitle[title] = node
// Go thru all the notes, create a map of how references map.
const outgoingLinks = findReferences(node.rawBody) // All outgoing links from this note
refersTo[title] = outgoingLinks.map(outTitle =>
getPreExistingTitle(outTitle, allNoteTitles)
)
for (let j = 0; j < outgoingLinks.length; j++) {
const tle = outgoingLinks[j]
const linkTitle = getPreExistingTitle(tle, allNoteTitles)
// Why this instead of just going thru the array to search? Optimizing. Might be premature. But, this function is already very slow.
if (linkageCache[title + '->' + linkTitle] !== undefined) continue
if (referredBy[linkTitle] === undefined) referredBy[linkTitle] = [] // If undefined, initialize
referredBy[linkTitle].push({
title: title,
excerpt: excerpt,
slug: slug,
body: node.body
})
linkageCache[title + '->' + linkTitle] = true
}
}
// Create pages for all notes.
for (let i = 0; i < result.data.allMdx.edges.length; i++) {
const node = result.data.allMdx.edges[i].node
const title = node.fields.title || node.frontmatter.title
const aliases = node.frontmatter.aliases || []
// Add all notes linked to and from this note together.
let linkedNoteTitles = []
let linkedNotes = {}
if(refersTo[title]) linkedNoteTitles = refersTo[title]
if(referredBy[title]) linkedNoteTitles = linkedNoteTitles.concat(referredBy[title].map(note => note.title))
for(let j = 0; j < linkedNoteTitles.length; j++) {
let linkTitle = linkedNoteTitles[j]
if(allNotesByTitle[linkTitle] === undefined ) continue
// This reduces the page context size. Use only things used in note.jsx. Otherwise I would have just set it as `node`.
// Only the linked(from and to the current note) needs to be in this.
linkedNotes[linkTitle.toLowerCase()] = {
title: allNotesByTitle[linkTitle].title,
slug: allNotesByTitle[linkTitle].fields.slug,
body: allNotesByTitle[linkTitle].body
}
}
// Context is becaming too big. I'm getting this warnding...
// `The size of at least one page context chunk exceeded 500kb, which could lead to degraded performance. Consider putting less data in the page context.`
createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/note.jsx`),
context: {
title: title,
slug: node.fields.slug,
refersTo: refersTo[title] || [],
referredBy: referredBy[title] || [],
linkedNotes: linkedNotes
},
})
// Handling Aliases
for (let j = 0; j < aliases.length; j++) {
createRedirect({
fromPath: `/${makeSlug(aliases[j])}`,
toPath: node.fields.slug,
redirectInBrowser: true,
isPermanent: true,
})
}
if(node.fields.slug != '/' + makeSlug(node.fields.fileName)) { // If there is a custom slug, setup a redirect.
createRedirect({
fromPath: `/${makeSlug(node.fields.fileName)}`,
toPath: node.fields.slug,
redirectInBrowser: true,
isPermanent: true,
})
}
}
createPage({
path: `/note-map`,
component: path.resolve(`./src/templates/note-map.jsx`),
context: {
allRefersTo: refersTo,
allReferredBy: referredBy,
allNotes: allNotes
},
})
// Handle all tag pages.
result.data.tags.group.forEach(tag => {
const taggedNotes = allNotes.filter(note =>
note.node.frontmatter.tags
? note.node.frontmatter.tags.includes(tag.fieldValue)
: false
)
paginate({
createPage,
items: taggedNotes,
itemsPerPage: 10,
pathPrefix: `/tags/${makeSlug(tag.fieldValue)}`,
component: path.resolve(`./src/templates/tag.jsx`),
context: {
tag: tag.fieldValue,
},
})
})
createPage({
path: `/tags`,
component: path.resolve(`./src/templates/tag-list.jsx`),
})
// Paginate Sitemap - that page has list of all notes
paginate({
createPage,
items: allNotes,
itemsPerPage: 10,
pathPrefix: `/sitemap`,
component: path.resolve(`./src/templates/sitemap.jsx`),
})
// Unlisted notes should be made a page.
const privateNotes = await graphql(`
query {
allMdx(filter: { fields: { visibility: { eq: "unlisted" } } }) {
edges {
node {
fields {
slug
title
visibility
excerpt
}
frontmatter {
tags
title
date
aliases
}
excerpt
rawBody
}
}
}
}
`)
for (let i = 0; i < privateNotes.data.allMdx.edges.length; i++) {
const node = privateNotes.data.allMdx.edges[i].node
const title = node.fields.title ? node.fields.title : node.frontmatter.title
createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/note.jsx`),
context: {
title: title,
slug: node.fields.slug,
refersTo: refersTo[title] || [],
referredBy: referredBy[title] || [],
linkedNotes: []
},
})
}
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
"""
Markdown Node
"""
type Mdx implements Node @infer {
frontmatter: Frontmatter
fields: Fields
}
"""
Markdown Fields
"""
type Fields @infer {
title: String
date: Date @dateformat
slug: String
visibility: String
excerpt: String
}
"""
Markdown Frontmatter
"""
type Frontmatter @infer {
title: String
fileName: String
date: Date @dateformat
tags: [String]
aliases: [String]
slug: String
source: String
visibility: String
excerpt: String
}
`
createTypes(typeDefs)
}
function findReferences(content) {
// Handles both [[Note Title]] and [[Note Title|text to show]] formats
const linkRegex = /\[\[([^\]\|]+)(\|.+)?\]\]/g
const links = [...content.matchAll(linkRegex)]
const matchedNotes = links.map(lnk => lnk[1])
const uniqueNotes = _.uniqWith(
matchedNotes,
(a, b) => a.toLowerCase() === b.toLowerCase()
) // Returns only the unique notes - in an case insensitive manner
// :TODO: Send a bit of text around the link back as well. Can be used in back links for context.
// :TODO: This will break if custom slug is used.
// :TODO: Handle # in the link - eg [[Note Title#section-3]]
return matchedNotes
}
// This makes the keys case insensitive. [Permenent Notes] and [permenant notes] should be treated the same.
function getPreExistingTitle(title, obj) {
const key = obj.find(key => key.toLowerCase() === title.toLowerCase())
if (key === undefined) return title // If obj is empty(first time its called) or we didn't find any match.
return key
}