-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgatsby-node.js
98 lines (93 loc) · 2.9 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
const logger = require('loglevel');
const { getChildBlockNodes, getChildSpanNodes } = require('./src/cacheUtils');
const highlight = require('./src/graphql/highlight');
const stylesheet = require('./src/graphql/stylesheet');
exports.createResolvers = ({
createResolvers,
cache,
createNodeId
}, /** @type {PluginOptions} */ pluginOptions) => {
createResolvers({
MarkdownRemark: {
grvscCodeBlocks: {
type: ['GRVSCCodeBlock'],
resolve(source, _, context) {
return getFromCache('GRVSCCodeBlock', cache, source, context);
}
},
grvscCodeSpans: {
type: ['GRVSCCodeSpan'],
resolve(source, _, context) {
return getFromCache('GRVSCCodeSpan', cache, source, context);
}
}
},
Query: {
grvscHighlight: {
type: 'GRVSCCodeBlock',
args: {
source: 'String!',
language: 'String',
meta: 'String',
defaultTheme: 'String',
additionalThemes: ['GRVSCThemeArgument!'],
},
resolve(_, args) {
return highlight(args, pluginOptions, { cache, createNodeId });
}
},
grvscStylesheet: {
type: 'GRVSCStylesheet',
args: {
defaultTheme: 'String',
additionalThemes: ['GRVSCThemeArgument!'],
injectStyles: 'Boolean'
},
resolve(_, args) {
return stylesheet(args, pluginOptions, { cache, createNodeId });
}
}
}
});
};
/**
* @param {string} type
* @param {any} cache
* @param {any} source
* @param {any} context
* @param {boolean=} stop
*/
async function getFromCache(type, cache, source, context, stop) {
const get = type === 'GRVSCCodeBlock' ? getChildBlockNodes : getChildSpanNodes;
const childNodes = await get(cache, source.id, source.internal.contentDigest);
// Hack alert: ensure plugin has been run by querying htmlAst,
// which is set via `setFieldsOnGraphQLNodeType` by gatsby-transformer-remark,
// therefore might not have been run before this resolver runs.
if (!childNodes && !stop) {
await context.nodeModel.runQuery({
query: {
filter: {
id: { eq: source.id },
htmlAst: { ne: null },
},
},
type: 'MarkdownRemark',
firstOnly: true,
});
return getFromCache(type, cache, source, context, true);
}
if (!childNodes) {
logger.error(
'gatsby-remark-vscode couldn’t retrieve up-to-date GRVSCCodeBlock GraphQL nodes. ' +
'The `GRVSCCodeBlocks` field may be missing, empty or stale. ' +
'The Gatsby cache is probably in a weird state. Try running `gatsby clean`, and file an ' +
'issue at https://github.com/andrewbranch/gatsby-remark-vscode/issues/new if the problem persists.'
);
return context.nodeModel.runQuery({
query: { parent: { id: { eq: source.id } } },
type,
firstOnly: false
});
}
return childNodes || [];
}