forked from gatsbyjs/gatsby
/
extend-node-type.js
206 lines (187 loc) · 5.09 KB
/
extend-node-type.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
const {
GraphQLObjectType,
GraphQLList,
GraphQLString,
GraphQLInt,
GraphQLEnumType,
GraphQLJSON
} = require("gatsby/graphql");
const _ = require("lodash");
const remark = require("remark");
const retext = require("retext");
const visit = require("unist-util-visit");
const remove = require("unist-util-remove");
const toString = require("mdast-util-to-string");
const stripMarkdown = require("strip-markdown");
const grayMatter = require("gray-matter");
const { createMdxAstCompiler } = require("@mdx-js/mdx");
const prune = require("underscore.string/prune");
const mdx = require("./utils/mdx");
const defaultOptions = require("./utils/default-options");
const stripFrontmatter = source => grayMatter(source).content;
module.exports = (
{ type, store, pathPrefix, getNode, getNodes, cache, reporter },
pluginOptions
) => {
if (type.name !== `Mdx`) {
return {};
}
const options = defaultOptions(pluginOptions);
const compiler = createMdxAstCompiler(options);
return new Promise((resolve, reject) => {
async function getAST(mdxNode) {
return compiler.parse(stripFrontmatter(mdxNode.rawBody));
}
async function getText(mdxNode) {
const ast = await getAST(mdxNode);
// convert the mdxast to back to mdast
remove(ast, "import");
remove(ast, "export");
visit(ast, "jsx", node => {
node.type = "html";
});
const textAst = await remark()
.use(stripMarkdown)
.run(ast);
return remark().stringify(textAst);
}
async function getCode(mdxNode) {
const code = await mdx(mdxNode.rawBody, options);
return `import React from 'react'
import { MDXTag } from '@mdx-js/tag'
${code}`;
}
const HeadingType = new GraphQLObjectType({
name: `MdxHeading`,
fields: {
value: {
type: GraphQLString,
resolve(heading) {
return heading.value;
}
},
depth: {
type: GraphQLInt,
resolve(heading) {
return heading.depth;
}
}
}
});
const Headings = new GraphQLEnumType({
name: `Headings`,
values: {
h1: { value: 1 },
h2: { value: 2 },
h3: { value: 3 },
h4: { value: 4 },
h5: { value: 5 },
h6: { value: 6 }
}
});
return resolve({
code: {
type: GraphQLString,
resolve(markdownNode) {
return getCode(markdownNode);
}
},
excerpt: {
type: GraphQLString,
args: {
pruneLength: {
type: GraphQLInt,
defaultValue: 140
}
},
async resolve(mdxNode, { pruneLength }) {
if (mdxNode.excerpt) {
return Promise.resolve(mdxNode.excerpt);
}
const ast = await getAST(mdxNode);
const excerptNodes = [];
visit(ast, node => {
if (node.type === "text" || node.type === "inlineCode") {
excerptNodes.push(node.value);
}
return;
});
return prune(excerptNodes.join(" "), pruneLength, "…");
}
},
headings: {
type: new GraphQLList(HeadingType),
args: {
depth: {
type: Headings
}
},
async resolve(mdxNode, { depth }) {
const ast = await getAST(mdxNode);
let headings = [];
visit(ast, "heading", heading => {
headings.push({
value: toString(heading),
depth: heading.depth
});
});
if (typeof depth === `number`) {
headings = headings.filter(heading => heading.depth === depth);
}
return headings;
}
},
timeToRead: {
type: GraphQLInt,
async resolve(mdxNode) {
const text = await getText(mdxNode);
let timeToRead = 0;
const avgWPM = 265;
const wordCount = _.words(text).length;
timeToRead = Math.round(wordCount / avgWPM);
if (timeToRead === 0) {
timeToRead = 1;
}
return timeToRead;
}
},
wordCount: {
type: new GraphQLObjectType({
name: `wordCounts`,
fields: {
paragraphs: {
type: GraphQLInt
},
sentences: {
type: GraphQLInt
},
words: {
type: GraphQLInt
}
}
}),
async resolve(mdxNode) {
let counts = {};
const text = await getText(mdxNode);
await retext()
.use(count)
.process(text);
return {
paragraphs: counts.ParagraphNode,
sentences: counts.SentenceNode,
words: counts.WordNode
};
function count() {
return counter;
function counter(tree) {
visit(tree, visitor);
function visitor(node) {
counts[node.type] = (counts[node.type] || 0) + 1;
}
}
}
}
}
});
});
};