-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
58 lines (47 loc) · 1.31 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
const fs = require('fs');
const util = require('util');
const gm = require('gray-matter');
const remark = require('remark');
remark.mdx = require('remark-mdx');
remark.html = require('remark-html');
const readFile = util.promisify(fs.readFile);
exports.onCreatePage = async ({ page, actions }) => {
if (!page.context.frontmatter || !/^\/blog\//.test(page.path)) return;
const src = await readFile(page.component, 'utf8');
const post = gm(src, {
excerpt: (file) => {
const [first] = file.content.split('\n\n');
file.excerpt = first;
},
});
const compiler = remark().use(remark.mdx).use(remark.html);
const html = await compiler.process(post.content);
const excerpt = await compiler.process(post.excerpt);
const next = Object.assign({}, page, {
context: {
...page.context,
html: html.contents,
excerpt: excerpt.contents,
},
});
actions.deletePage(page);
actions.createPage(next);
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type SitePage implements Node {
context: Context
}
type Context {
frontmatter: Frontmatter
}
type Frontmatter {
title: String
date: Date
draft: Boolean
minRead: String
}
`;
createTypes(typeDefs);
};