This repository was archived by the owner on Apr 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgatsby-node.js
More file actions
109 lines (94 loc) · 2.74 KB
/
gatsby-node.js
File metadata and controls
109 lines (94 loc) · 2.74 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
const Prism = require('prismjs')
const loadLanguages = require('prismjs/components/')
const { request } = require('@octokit/request')
const terminalSlides = require('./content/home-slides')
loadLanguages(['bash', 'python'])
const getBashHtml = bashText =>
Prism.highlight(bashText, Prism.languages.bash, 'bash')
const getPythonHtml = pythonText =>
Prism.highlight(pythonText, Prism.languages.python, 'python')
const formattedTerminalLines = terminalSlides.map(string => {
const lines = string.split(/\r?\n/).map(line => {
const l = line.trim()
const commandPromptReg = /^(\$|>{3}|\.{3})/
if (commandPromptReg.test(l)) {
const prompt = l.match(commandPromptReg)[0]
const text = l.replace(commandPromptReg, '')
return {
promptString: prompt,
text: prompt === '$' ? getBashHtml(text) : getPythonHtml(text)
}
} else {
return { text: l, promptString: null }
}
})
return lines[0].text === '' ? lines.slice(1) : lines
})
async function getStars({ owner, repo }) {
const response = await request({
method: 'GET',
url: '/repos/{owner}/{repo}',
owner,
repo,
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`
}
})
const stars = response.data.stargazers_count
return stars
}
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
const { createNode } = actions
formattedTerminalLines.forEach((terminalLines, i) => {
const formattedTerminalSlide = { lines: terminalLines }
const nodeContent = JSON.stringify(formattedTerminalSlide)
const nodeMeta = {
id: createNodeId(`terminal-slide-${i}`),
parent: null,
children: [],
internal: {
type: 'TerminalSlide',
content: nodeContent,
contentDigest: createContentDigest(formattedTerminalSlide)
}
}
const node = Object.assign({}, formattedTerminalSlide, nodeMeta)
createNode(node)
})
}
exports.createSchemaCustomization = async ({
actions: { createTypes },
schema
}) => {
createTypes(
schema.buildObjectType({
name: 'StaticGithubData',
fields: {
stars: 'String!'
}
})
)
}
exports.createResolvers = async ({ createResolvers }) => {
createResolvers({
Query: {
staticGithubData: {
type: 'StaticGithubData',
async resolve() {
const staticStar = 719
try {
const { GITHUB_TOKEN } = process.env
if (GITHUB_TOKEN) {
const stars = await getStars({ owner: 'iterative', repo: 'mlem' })
return { stars }
}
return { stars: staticStar }
} catch (error) {
console.error(error)
return { stars: staticStar }
}
}
}
}
})
}