-
Notifications
You must be signed in to change notification settings - Fork 8
/
gatsby-node.js
134 lines (120 loc) · 3.69 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
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
import fs from 'fs';
import { Feed } from 'feed';
import dayjs from 'dayjs';
import { defaultOptions } from './internals';
import { addLeadingSlash } from './util';
const undefIfFalse = (condition, value) =>
condition !== false ? value : undefined;
function addItemToFeed(feed, siteMetadata, options) {
return (page) => {
feed.addItem({
title: page.frontmatter.title,
id: `${siteMetadata.siteUrl}${page.fields.slug}`,
link: `${siteMetadata.siteUrl}${page.fields.slug}`,
date: dayjs(page.frontmatter.date).toDate(),
content: page.html,
author: [
{
name: options.author || siteMetadata.author.name,
email: undefIfFalse(options.email, options.email),
link: options.link || siteMetadata.siteUrl,
},
],
});
};
}
function buildFeed(pages, siteMetadata, options, output) {
const feed = new Feed({
title: options.title || siteMetadata.title,
description: options.description || siteMetadata.description,
link: options.link || siteMetadata.siteUrl,
id: options.id || siteMetadata.siteUrl,
copyright:
options.copyright ||
`All rights reserved ${new Date().getUTCFullYear()}, ${
options.author || siteMetadata.author.name
}`,
feedLinks: {
atom: `${siteMetadata.siteUrl}${addLeadingSlash(output.atom)}`,
rss2: `${siteMetadata.siteUrl}${addLeadingSlash(output.rss2)}`,
json: `${siteMetadata.siteUrl}${addLeadingSlash(output.json)}`,
},
author: {
name: options.author || siteMetadata.author.name,
email: undefIfFalse(options.email, options.email),
link: siteMetadata.siteUrl,
},
});
pages
.map((page) => page.node)
.forEach(addItemToFeed(feed, siteMetadata, options));
feed.addContributor({
name: siteMetadata.author.name,
email: options.email,
link: siteMetadata.siteUrl,
});
return feed;
}
function generateAtomFeed(feed, name) {
return fs.writeFileSync(`./public/${name}`, feed.atom1());
}
function generateRSSFeed(feed, name) {
return fs.writeFileSync(`./public/${name}`, feed.rss2());
}
function generateJSONFeed(feed, name) {
return fs.writeFileSync(`./public/${name}`, feed.json1());
}
async function generateFeed({ graphql }, feedOptions) {
const output = { ...defaultOptions.output, ...feedOptions.output };
const options = { ...defaultOptions, ...feedOptions };
const result = await graphql(`
{
site {
siteMetadata {
title
description
author { name }
siteUrl
}
}
allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "${options.match}" } }
sort: { fields: [frontmatter___date], order: DESC }
limit: ${options.limit}
) {
edges {
node {
fields {
slug
}
frontmatter {
title
date
}
html
}
}
}
}
`);
if (result.errors) {
throw result.errors;
}
const posts = result.data.allMarkdownRemark.edges;
const siteMetadata = result.data.site.siteMetadata;
const feed = buildFeed(posts, siteMetadata, options, output);
generateAtomFeed(feed, output.atom);
generateRSSFeed(feed, output.rss2);
generateJSONFeed(feed, output.json);
}
exports.onPostBuild = async ({ graphql }, pluginOptions) => {
if (pluginOptions.feeds && !Array.isArray(pluginOptions.feeds)) {
throw new Error('@fec/gatsby-plugin-feed `feeds` option must be an array.');
} else if (!pluginOptions.feeds) {
await generateFeed({ graphql }, {});
return;
}
for (let i = 0; i < pluginOptions.feeds.length; i += 1) {
await generateFeed({ graphql }, pluginOptions.feeds[i]);
}
};