generated from OneGraph/oneblog
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRssFeed.js
132 lines (122 loc) · 3.29 KB
/
RssFeed.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
// @flow
import React from 'react';
import {renderToStaticMarkup} from 'react-dom/server';
import {Feed} from 'feed';
import graphql from 'babel-plugin-relay/macro';
import {createEnvironment} from './Environment';
import {fetchQuery} from 'react-relay/hooks';
import {computePostDate, postPath} from './Post';
import {RssMarkdownRenderer} from './MarkdownRenderer';
import {ServerStyleSheet} from 'styled-components';
import inlineCss from 'inline-css/lib/inline-css';
import {Grommet} from 'grommet/components/Grommet';
import appCss from './App.css';
import config from './config';
import type {RssFeed_QueryResponse} from './__generated__/RssFeed_Query.graphql';
import theme from './lib/theme';
const feedQuery = graphql`
query RssFeed_Query($repoOwner: String!, $repoName: String!)
@persistedQueryConfiguration(
fixedVariables: {environmentVariable: "REPOSITORY_FIXED_VARIABLES"}
cacheSeconds: 300
) {
repository(name: $repoName, owner: $repoOwner) {
issues(
first: 20
orderBy: {direction: DESC, field: CREATED_AT}
labels: ["publish", "Publish"]
) {
nodes {
...Post_post @relay(mask: false)
}
}
}
}
`;
function renderPostHtml(post) {
const sheet = new ServerStyleSheet();
const markup = renderToStaticMarkup(
sheet.collectStyles(
<Grommet theme={theme}>
<div
style={{
maxWidth: 704,
}}>
<RssMarkdownRenderer trustedInput={true} source={post.body} />
</div>
</Grommet>,
),
);
const css = sheet.instance.toString();
return inlineCss(markup, `${appCss.toString()}\n${css}`, {codeBlocks: {}});
}
function removeTrailingSlash(s: ?string): string {
if (!s) {
return '';
}
if (s[s.length - 1] === '/') {
return s.substr(0, s.length - 1);
}
return s;
}
export async function buildFeed({
basePath,
siteHostname,
}: {
basePath?: ?string,
siteHostname?: ?string,
}) {
const markdowns = [];
const environment = createEnvironment({
registerMarkdown: function (m) {
markdowns.push(m);
},
});
const data: ?RssFeed_QueryResponse = await fetchQuery(
environment,
feedQuery,
{},
).toPromise();
const posts = data?.repository?.issues.nodes || [];
const latestPost = posts[0];
const baseUrl = removeTrailingSlash(
`${removeTrailingSlash(siteHostname)}${basePath ? basePath : ''}`,
);
const feed = new Feed({
title: config.title,
description: config.description,
id: baseUrl,
link: baseUrl,
language: 'en',
image: `${baseUrl}/logo.png`,
favicon: `${baseUrl}/favicon.ico`,
updated: latestPost ? computePostDate(latestPost) : null,
generator: '',
feedLinks: {
json: `${baseUrl}/feed.json`,
atom: `${baseUrl}/feed.atom`,
rss2: `${baseUrl}/feed.rss`,
},
});
for (const post of posts) {
if (post) {
const content = renderPostHtml(post);
feed.addItem({
title: post.title,
id: post.id,
link: `${baseUrl}${postPath({post})}`,
content,
author: (post.assignees.nodes || []).map((node) =>
node
? {
name: node.name,
link: node.url,
}
: null,
),
date: computePostDate(post),
});
}
}
return feed;
}