-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
174 lines (157 loc) · 4.06 KB
/
gatsby-node.js
File metadata and controls
174 lines (157 loc) · 4.06 KB
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
const path = require(`path`)
const cheerio = require(`cheerio`)
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
// query content for WordPress posts
const result = await graphql(`
query {
allWpPage {
nodes {
id
slug
uri
}
}
allWpPost {
nodes {
id
slug
}
}
}
`)
const postTemplate = path.resolve(`./src/templates/post.js`)
result.data.allWpPost.nodes.forEach((node) => {
createPage({
path: `/blog/${node.slug}`,
component: postTemplate,
context: {
id: node.id,
},
})
})
const pageTemplate = path.resolve(`./src/templates/page.js`)
result.data.allWpPage.nodes
.filter((node) => node.slug !== "blog")
.forEach((node) => {
createPage({
path: node.uri,
component: pageTemplate,
context: {
id: node.id,
},
})
})
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes, createFieldExtension } = actions
createFieldExtension({
name: "content",
extend: extendContentField,
})
const typeDefs = `
type WpPost implements Node {
toc: JSON
content: String @content
}
`
createTypes(typeDefs)
}
exports.createResolvers = ({ createResolvers, schema }) =>
createResolvers({
WpPost: {
toc: {
resolve: createTableOfContents,
},
},
})
const createTableOfContents = async (source, args, context, info) => {
const $ = cheerio.load(source.content)
const titles = $("h2,h3,h4,h5")
const getUniqueId = UniqueId()
const headings = Array.from(titles).map((title) => {
const depth = parseInt($(title).prop("tagName").substr(1), 10)
const id = createId($, title)
return { url: `#${getUniqueId(id)}`, title: $(title).text(), depth }
})
const reduced = groupHeadings(0, [], headings)
return { items: reduced }
}
const extendContentField = (options, prevFieldConfig) => {
return {
resolve(source) {
const $ = cheerio.load(source.content)
const titles = $("h2,h3,h4,h5")
const getUniqueId = UniqueId()
Array.from(titles).forEach((title) => {
const id = createId($, title)
$(title).attr("id", getUniqueId(id))
})
return $("body").html()
},
}
}
function createId($, title) {
let id = $(title).attr("id")
if (!id) {
id = $(title)
.text()
.toLowerCase()
.replace("ą", "a")
.replace("ę", "e")
.replace("ż", "z")
.replace("ź", "z")
.replace("ć", "c")
.replace("ł", "l")
.replace("ó", "o")
.replace("ń", "n")
.replace("ś", "s")
.replace(/[^a-z_0-9]+/gi, "-")
.replace(/-+/g, "-")
}
return id
}
function UniqueId() {
const tempMap = {}
return (el) => {
if (tempMap[el]) {
tempMap[el] = tempMap[el] + 1
const result = `${el}-${tempMap[el]}`
tempMap[result] = 1
return result
} else {
tempMap[el] = 1
return el
}
}
}
function groupHeadings(index, grouping, headings) {
if (index < headings.length) {
const nextHeading = headings[index]
if (grouping.length) {
const prevHeading = grouping.slice().pop()
try {
if (nextHeading.depth > prevHeading.depth) {
prevHeading.items = prevHeading.items || []
return groupHeadings(index, prevHeading.items, headings)
} else if (nextHeading.depth == prevHeading.depth) {
grouping.push({ ...nextHeading })
return groupHeadings(++index, grouping, headings)
} else {
throw { index: index, heading: nextHeading }
}
} catch (higherHeading) {
if (higherHeading.heading.depth == prevHeading.depth) {
grouping.push({ ...higherHeading.heading })
return groupHeadings(++higherHeading.index, grouping, headings)
} else {
throw higherHeading
}
}
} else {
grouping.push({ ...nextHeading })
groupHeadings(++index, grouping, headings)
}
}
return grouping
}