@y1feng200156/sairin-core 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +151 -0
- package/lib/index.js +208 -0
- package/lib/queries.d.ts +45 -0
- package/lib/queries.js +48 -0
- package/package.json +27 -0
- package/src/index.tsx +234 -0
- package/src/queries.ts +86 -0
- package/tsconfig.json +105 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/// <reference types="lodash" />
|
|
2
|
+
export type FrontMatter = {
|
|
3
|
+
path: string;
|
|
4
|
+
};
|
|
5
|
+
export type SairinConfig = {
|
|
6
|
+
siteConfig: {
|
|
7
|
+
title: string;
|
|
8
|
+
author?: string;
|
|
9
|
+
url?: string;
|
|
10
|
+
};
|
|
11
|
+
theme: any;
|
|
12
|
+
themeConfig: Record<string, any>;
|
|
13
|
+
allowUsers?: string[];
|
|
14
|
+
};
|
|
15
|
+
export declare class Sairin {
|
|
16
|
+
config: SairinConfig;
|
|
17
|
+
resolvedConfig: {
|
|
18
|
+
ghToken: string | undefined;
|
|
19
|
+
repo: string;
|
|
20
|
+
ghUserName: string;
|
|
21
|
+
repoSlug: string;
|
|
22
|
+
};
|
|
23
|
+
private PAGE_PATH_PLACEHOLDER;
|
|
24
|
+
private REVALIDATE;
|
|
25
|
+
private allowUsers;
|
|
26
|
+
private authHeaders;
|
|
27
|
+
private request;
|
|
28
|
+
constructor(config: SairinConfig);
|
|
29
|
+
getPostList: () => Promise<{
|
|
30
|
+
html: any;
|
|
31
|
+
attributes: FrontMatter;
|
|
32
|
+
id: string;
|
|
33
|
+
title: string;
|
|
34
|
+
url: string;
|
|
35
|
+
createdAt: string;
|
|
36
|
+
updatedAt: string;
|
|
37
|
+
body: string;
|
|
38
|
+
comments: {
|
|
39
|
+
nodes: {
|
|
40
|
+
url: string;
|
|
41
|
+
createdAt: string;
|
|
42
|
+
author: {
|
|
43
|
+
login: string;
|
|
44
|
+
url: string;
|
|
45
|
+
avatarUrl: string;
|
|
46
|
+
};
|
|
47
|
+
body: string;
|
|
48
|
+
}[];
|
|
49
|
+
};
|
|
50
|
+
reactionGroups: {
|
|
51
|
+
content: string;
|
|
52
|
+
reactors: {
|
|
53
|
+
totalCount: number;
|
|
54
|
+
};
|
|
55
|
+
}[];
|
|
56
|
+
author: {
|
|
57
|
+
login: string;
|
|
58
|
+
url: string;
|
|
59
|
+
avatarUrl: any;
|
|
60
|
+
};
|
|
61
|
+
}[]>;
|
|
62
|
+
private processBody;
|
|
63
|
+
private getUser;
|
|
64
|
+
getStaticPaths: () => Promise<{
|
|
65
|
+
paths: string[];
|
|
66
|
+
fallback: boolean;
|
|
67
|
+
}>;
|
|
68
|
+
getHomePageStaticProps: () => Promise<{
|
|
69
|
+
props: {
|
|
70
|
+
posts: import("lodash").Omit<{
|
|
71
|
+
html: any;
|
|
72
|
+
attributes: FrontMatter;
|
|
73
|
+
id: string;
|
|
74
|
+
title: string;
|
|
75
|
+
url: string;
|
|
76
|
+
createdAt: string;
|
|
77
|
+
updatedAt: string;
|
|
78
|
+
body: string;
|
|
79
|
+
comments: {
|
|
80
|
+
nodes: {
|
|
81
|
+
url: string;
|
|
82
|
+
createdAt: string;
|
|
83
|
+
author: {
|
|
84
|
+
login: string;
|
|
85
|
+
url: string;
|
|
86
|
+
avatarUrl: string;
|
|
87
|
+
};
|
|
88
|
+
body: string;
|
|
89
|
+
}[];
|
|
90
|
+
};
|
|
91
|
+
reactionGroups: {
|
|
92
|
+
content: string;
|
|
93
|
+
reactors: {
|
|
94
|
+
totalCount: number;
|
|
95
|
+
};
|
|
96
|
+
}[];
|
|
97
|
+
author: {
|
|
98
|
+
login: string;
|
|
99
|
+
url: string;
|
|
100
|
+
avatarUrl: any;
|
|
101
|
+
};
|
|
102
|
+
}, "body" | "html">[];
|
|
103
|
+
themeConfig: Record<string, any>;
|
|
104
|
+
};
|
|
105
|
+
revalidate: number;
|
|
106
|
+
}>;
|
|
107
|
+
getPostPageStaticProps: (ctx: any) => Promise<{
|
|
108
|
+
props: {
|
|
109
|
+
post: {
|
|
110
|
+
html: any;
|
|
111
|
+
attributes: FrontMatter;
|
|
112
|
+
id: string;
|
|
113
|
+
title: string;
|
|
114
|
+
url: string;
|
|
115
|
+
createdAt: string;
|
|
116
|
+
updatedAt: string;
|
|
117
|
+
body: string;
|
|
118
|
+
comments: {
|
|
119
|
+
nodes: {
|
|
120
|
+
url: string;
|
|
121
|
+
createdAt: string;
|
|
122
|
+
author: {
|
|
123
|
+
login: string;
|
|
124
|
+
url: string;
|
|
125
|
+
avatarUrl: string;
|
|
126
|
+
};
|
|
127
|
+
body: string;
|
|
128
|
+
}[];
|
|
129
|
+
};
|
|
130
|
+
reactionGroups: {
|
|
131
|
+
content: string;
|
|
132
|
+
reactors: {
|
|
133
|
+
totalCount: number;
|
|
134
|
+
};
|
|
135
|
+
}[];
|
|
136
|
+
author: {
|
|
137
|
+
login: string;
|
|
138
|
+
url: string;
|
|
139
|
+
avatarUrl: any;
|
|
140
|
+
};
|
|
141
|
+
} | null;
|
|
142
|
+
themeConfig: Record<string, any>;
|
|
143
|
+
};
|
|
144
|
+
revalidate: number;
|
|
145
|
+
}>;
|
|
146
|
+
private generateFeed;
|
|
147
|
+
rssHandler: (req: any, res: any) => Promise<void>;
|
|
148
|
+
DocumentHead: () => import("react/jsx-runtime").JSX.Element;
|
|
149
|
+
}
|
|
150
|
+
export type HomePageThemeProps = Awaited<ReturnType<Sairin["getHomePageStaticProps"]>>["props"];
|
|
151
|
+
export type PostPageThemeProps = Awaited<ReturnType<Sairin["getPostPageStaticProps"]>>["props"];
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.Sairin = void 0;
|
|
39
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
40
|
+
const axios_1 = __importDefault(require("axios"));
|
|
41
|
+
const marked_1 = require("marked");
|
|
42
|
+
const marked_katex_extension_1 = __importDefault(require("marked-katex-extension"));
|
|
43
|
+
const front_matter_1 = __importDefault(require("front-matter"));
|
|
44
|
+
const lodash_1 = require("lodash");
|
|
45
|
+
const feed_1 = require("feed");
|
|
46
|
+
const prism = __importStar(require("prismjs"));
|
|
47
|
+
const queries_1 = require("./queries");
|
|
48
|
+
class Sairin {
|
|
49
|
+
request(query) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
const res = yield axios_1.default.post("https://api.github.com/graphql", query, {
|
|
52
|
+
headers: this.authHeaders,
|
|
53
|
+
});
|
|
54
|
+
return res.data.data;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
constructor(config) {
|
|
58
|
+
var _a;
|
|
59
|
+
this.config = config;
|
|
60
|
+
this.resolvedConfig = (() => {
|
|
61
|
+
const ghToken = process.env.GITHUB_TOKEN || undefined;
|
|
62
|
+
const repo = process.env.REPO ||
|
|
63
|
+
`${process.env.VERCEL_GIT_REPO_OWNER}/${process.env.VERCEL_GIT_REPO_SLUG}`;
|
|
64
|
+
const [ghUserName, repoSlug] = repo.split("/");
|
|
65
|
+
return {
|
|
66
|
+
ghToken,
|
|
67
|
+
repo,
|
|
68
|
+
ghUserName,
|
|
69
|
+
repoSlug,
|
|
70
|
+
};
|
|
71
|
+
})();
|
|
72
|
+
this.PAGE_PATH_PLACEHOLDER = "path";
|
|
73
|
+
this.REVALIDATE = 60;
|
|
74
|
+
this.allowUsers = [this.resolvedConfig.ghUserName].concat(((_a = this.config) === null || _a === void 0 ? void 0 : _a.allowUsers) || []);
|
|
75
|
+
this.authHeaders = this.resolvedConfig.ghToken
|
|
76
|
+
? {
|
|
77
|
+
Authorization: `bearer ${this.resolvedConfig.ghToken}`,
|
|
78
|
+
}
|
|
79
|
+
: {};
|
|
80
|
+
this.getPostList = () => __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const result = yield this.request((0, queries_1.GetPostsQuery)({
|
|
82
|
+
owner: this.resolvedConfig.ghUserName,
|
|
83
|
+
repo: this.resolvedConfig.repoSlug,
|
|
84
|
+
}));
|
|
85
|
+
const posts = result.repository.issues.nodes
|
|
86
|
+
.filter((post) => {
|
|
87
|
+
return this.allowUsers.indexOf(post.author.login) !== -1;
|
|
88
|
+
})
|
|
89
|
+
.map((post) => {
|
|
90
|
+
const { html, attributes } = this.processBody(post.body);
|
|
91
|
+
post.comments.nodes = post.comments.nodes.map((comment) => {
|
|
92
|
+
const { html } = this.processBody(comment.body);
|
|
93
|
+
return Object.assign(Object.assign({}, comment), { html });
|
|
94
|
+
});
|
|
95
|
+
return Object.assign(Object.assign({}, post), { html,
|
|
96
|
+
attributes });
|
|
97
|
+
});
|
|
98
|
+
return posts;
|
|
99
|
+
});
|
|
100
|
+
this.getStaticPaths = () => __awaiter(this, void 0, void 0, function* () {
|
|
101
|
+
const posts = yield this.getPostList();
|
|
102
|
+
return {
|
|
103
|
+
paths: posts.map((p) => {
|
|
104
|
+
// TODO: the fallback path should be issue id
|
|
105
|
+
return `/${p.attributes.path || ""}`;
|
|
106
|
+
}),
|
|
107
|
+
fallback: true,
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
this.getHomePageStaticProps = () => __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
const posts = (yield this.getPostList()).map((post) => {
|
|
112
|
+
return (0, lodash_1.omit)(post, ["body", "html"]);
|
|
113
|
+
});
|
|
114
|
+
return {
|
|
115
|
+
props: {
|
|
116
|
+
// TODO: reduce post body size
|
|
117
|
+
posts,
|
|
118
|
+
themeConfig: this.config.themeConfig || {},
|
|
119
|
+
},
|
|
120
|
+
revalidate: this.REVALIDATE,
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
this.getPostPageStaticProps = (ctx) => __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
var _b;
|
|
125
|
+
const posts = yield this.getPostList();
|
|
126
|
+
const post = posts.find((p) => p.attributes.path === ctx.params[this.PAGE_PATH_PLACEHOLDER].join("/")) || null;
|
|
127
|
+
return {
|
|
128
|
+
props: {
|
|
129
|
+
post,
|
|
130
|
+
themeConfig: ((_b = this.config) === null || _b === void 0 ? void 0 : _b.themeConfig) || {},
|
|
131
|
+
},
|
|
132
|
+
revalidate: this.REVALIDATE,
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
this.generateFeed = () => __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
const feed = new feed_1.Feed({
|
|
137
|
+
title: this.config.siteConfig.title,
|
|
138
|
+
copyright: this.config.siteConfig.title,
|
|
139
|
+
id: this.config.siteConfig.title,
|
|
140
|
+
author: {
|
|
141
|
+
name: this.config.siteConfig.author || this.resolvedConfig.ghUserName,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
if (!this.config.siteConfig.url) {
|
|
145
|
+
return feed.atom1();
|
|
146
|
+
}
|
|
147
|
+
const posts = yield this.getPostList();
|
|
148
|
+
posts.forEach((post) => {
|
|
149
|
+
feed.addItem({
|
|
150
|
+
date: new Date(post.updatedAt),
|
|
151
|
+
link: `${this.config.siteConfig.url}/${post.attributes.path}`,
|
|
152
|
+
title: post.title,
|
|
153
|
+
content: post.html,
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
return feed.atom1();
|
|
157
|
+
});
|
|
158
|
+
this.rssHandler = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
res.setHeader("Content-Type", "application/xml");
|
|
160
|
+
// https://vercel.com/docs/concepts/edge-network/caching#stale-while-revalidate
|
|
161
|
+
res.setHeader("Cache-Control", `s-maxage=1 stale-while-revalidate=${10 * 60}`);
|
|
162
|
+
res.send(yield this.generateFeed());
|
|
163
|
+
});
|
|
164
|
+
this.DocumentHead = () => {
|
|
165
|
+
var _a;
|
|
166
|
+
return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: ((_a = this.config) === null || _a === void 0 ? void 0 : _a.theme.Head) && ((0, jsx_runtime_1.jsx)(this.config.theme.Head, { sairinConfig: this.config })) }));
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
processBody(body) {
|
|
170
|
+
const { body: rawBody, attributes } = (0, front_matter_1.default)(body);
|
|
171
|
+
marked_1.marked.use((0, marked_katex_extension_1.default)({
|
|
172
|
+
throwOnError: false,
|
|
173
|
+
}));
|
|
174
|
+
marked_1.marked.use({
|
|
175
|
+
renderer: {
|
|
176
|
+
code(code, lang, escaped) {
|
|
177
|
+
code = this.options.highlight(code, lang);
|
|
178
|
+
if (!lang) {
|
|
179
|
+
return `<pre><code>${code}</code></pre>`;
|
|
180
|
+
}
|
|
181
|
+
var langClass = "language-" + lang;
|
|
182
|
+
return `<pre class="${langClass}"><code class="${langClass}">${code}</code></pre>`;
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
const parsed = marked_1.marked.parse(rawBody, {
|
|
187
|
+
highlight: (code, lang) => {
|
|
188
|
+
if (prism.languages[lang]) {
|
|
189
|
+
return prism.highlight(code, prism.languages[lang], lang);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
return code;
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
return {
|
|
197
|
+
html: parsed,
|
|
198
|
+
attributes,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
getUser(username) {
|
|
202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
+
const res = yield axios_1.default.get(`https://api.github.com/users/${username}`);
|
|
204
|
+
return res.data;
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.Sairin = Sairin;
|
package/lib/queries.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type GetPostQueryVar = {
|
|
2
|
+
owner: string;
|
|
3
|
+
repo: string;
|
|
4
|
+
};
|
|
5
|
+
export type GetPostQueryProps = {
|
|
6
|
+
repository: {
|
|
7
|
+
issues: {
|
|
8
|
+
nodes: {
|
|
9
|
+
id: string;
|
|
10
|
+
title: string;
|
|
11
|
+
url: string;
|
|
12
|
+
createdAt: string;
|
|
13
|
+
updatedAt: string;
|
|
14
|
+
body: string;
|
|
15
|
+
comments: {
|
|
16
|
+
nodes: {
|
|
17
|
+
url: string;
|
|
18
|
+
createdAt: string;
|
|
19
|
+
author: {
|
|
20
|
+
login: string;
|
|
21
|
+
url: string;
|
|
22
|
+
avatarUrl: string;
|
|
23
|
+
};
|
|
24
|
+
body: string;
|
|
25
|
+
}[];
|
|
26
|
+
};
|
|
27
|
+
reactionGroups: {
|
|
28
|
+
content: string;
|
|
29
|
+
reactors: {
|
|
30
|
+
totalCount: number;
|
|
31
|
+
};
|
|
32
|
+
}[];
|
|
33
|
+
author: {
|
|
34
|
+
login: string;
|
|
35
|
+
url: string;
|
|
36
|
+
avatarUrl: any;
|
|
37
|
+
};
|
|
38
|
+
}[];
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export declare const GetPostsQuery: (variables: GetPostQueryVar) => {
|
|
43
|
+
query: string;
|
|
44
|
+
variables: GetPostQueryVar;
|
|
45
|
+
};
|
package/lib/queries.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GetPostsQuery = void 0;
|
|
4
|
+
const GetPostsQuery = (variables) => ({
|
|
5
|
+
query: `
|
|
6
|
+
query GetPosts($owner: String!, $repo: String!) {
|
|
7
|
+
repository(owner: $owner, name: $repo) {
|
|
8
|
+
issues(first: 100, orderBy: {
|
|
9
|
+
field: CREATED_AT, direction: DESC
|
|
10
|
+
}, labels: ["published"]) {
|
|
11
|
+
nodes {
|
|
12
|
+
id,
|
|
13
|
+
url,
|
|
14
|
+
title,
|
|
15
|
+
updatedAt,
|
|
16
|
+
createdAt,
|
|
17
|
+
body,
|
|
18
|
+
comments (first: 100) {
|
|
19
|
+
nodes {
|
|
20
|
+
createdAt,
|
|
21
|
+
url,
|
|
22
|
+
author {
|
|
23
|
+
login,
|
|
24
|
+
url,
|
|
25
|
+
avatarUrl,
|
|
26
|
+
},
|
|
27
|
+
body
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
reactionGroups {
|
|
31
|
+
content,
|
|
32
|
+
reactors {
|
|
33
|
+
totalCount,
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
author {
|
|
37
|
+
login,
|
|
38
|
+
url,
|
|
39
|
+
avatarUrl
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
`,
|
|
46
|
+
variables,
|
|
47
|
+
});
|
|
48
|
+
exports.GetPostsQuery = GetPostsQuery;
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@y1feng200156/sairin-core",
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"main": "lib/index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "tsc --watch",
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"prepublish": "npm run build"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@types/lodash": "^4.14.178",
|
|
12
|
+
"@types/node": "^17.0.45",
|
|
13
|
+
"typescript": "^4.5.5"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@types/react": "^17.0.39",
|
|
17
|
+
"axios": "^0.26.0",
|
|
18
|
+
"debug": "^4.3.3",
|
|
19
|
+
"feed": "^4.2.2",
|
|
20
|
+
"front-matter": "^4.0.2",
|
|
21
|
+
"katex": "^0.16.27",
|
|
22
|
+
"lodash": "^4.17.21",
|
|
23
|
+
"marked": "^4.0.12",
|
|
24
|
+
"marked-katex-extension": "^5.1.6",
|
|
25
|
+
"prismjs": "^1.27.0"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { marked } from "marked";
|
|
3
|
+
import markedKatex from "marked-katex-extension";
|
|
4
|
+
import fm from "front-matter";
|
|
5
|
+
import { omit, pick } from "lodash";
|
|
6
|
+
import { Feed } from "feed";
|
|
7
|
+
|
|
8
|
+
import * as prism from "prismjs";
|
|
9
|
+
import { GetPostQueryProps, GetPostsQuery } from "./queries";
|
|
10
|
+
|
|
11
|
+
export type FrontMatter = {
|
|
12
|
+
path: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type SairinConfig = {
|
|
16
|
+
siteConfig: {
|
|
17
|
+
title: string;
|
|
18
|
+
author?: string;
|
|
19
|
+
url?: string;
|
|
20
|
+
};
|
|
21
|
+
theme: any;
|
|
22
|
+
themeConfig: Record<string, any>;
|
|
23
|
+
allowUsers?: string[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export class Sairin {
|
|
27
|
+
resolvedConfig = (() => {
|
|
28
|
+
const ghToken = (process.env.GITHUB_TOKEN as string) || undefined;
|
|
29
|
+
const repo =
|
|
30
|
+
process.env.REPO ||
|
|
31
|
+
(`${process.env.VERCEL_GIT_REPO_OWNER}/${process.env.VERCEL_GIT_REPO_SLUG}` as string);
|
|
32
|
+
|
|
33
|
+
const [ghUserName, repoSlug] = repo.split("/");
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
ghToken,
|
|
37
|
+
repo,
|
|
38
|
+
ghUserName,
|
|
39
|
+
repoSlug,
|
|
40
|
+
};
|
|
41
|
+
})();
|
|
42
|
+
|
|
43
|
+
private PAGE_PATH_PLACEHOLDER = "path";
|
|
44
|
+
|
|
45
|
+
private REVALIDATE = 60;
|
|
46
|
+
|
|
47
|
+
private allowUsers: string[] = [this.resolvedConfig.ghUserName].concat(
|
|
48
|
+
this.config?.allowUsers || []
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
private authHeaders = this.resolvedConfig.ghToken
|
|
52
|
+
? {
|
|
53
|
+
Authorization: `bearer ${this.resolvedConfig.ghToken}`,
|
|
54
|
+
}
|
|
55
|
+
: ({} as any);
|
|
56
|
+
|
|
57
|
+
private async request<T>(query: { query: string; variables: any }) {
|
|
58
|
+
const res = await axios.post("https://api.github.com/graphql", query, {
|
|
59
|
+
headers: this.authHeaders,
|
|
60
|
+
});
|
|
61
|
+
return res.data.data as T;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
constructor(public config: SairinConfig) {}
|
|
65
|
+
|
|
66
|
+
getPostList = async () => {
|
|
67
|
+
const result = await this.request<GetPostQueryProps>(
|
|
68
|
+
GetPostsQuery({
|
|
69
|
+
owner: this.resolvedConfig.ghUserName,
|
|
70
|
+
repo: this.resolvedConfig.repoSlug,
|
|
71
|
+
})
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const posts = result.repository.issues.nodes
|
|
75
|
+
.filter((post) => {
|
|
76
|
+
return this.allowUsers.indexOf(post.author.login) !== -1;
|
|
77
|
+
})
|
|
78
|
+
.map((post) => {
|
|
79
|
+
const { html, attributes } = this.processBody(post.body);
|
|
80
|
+
post.comments.nodes = post.comments.nodes.map((comment) => {
|
|
81
|
+
const { html } = this.processBody(comment.body);
|
|
82
|
+
return {
|
|
83
|
+
...comment,
|
|
84
|
+
html,
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
...post,
|
|
90
|
+
html,
|
|
91
|
+
attributes,
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return posts;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
private processBody(body: string) {
|
|
99
|
+
const { body: rawBody, attributes } = fm<FrontMatter>(body);
|
|
100
|
+
marked.use(
|
|
101
|
+
markedKatex({
|
|
102
|
+
throwOnError: false,
|
|
103
|
+
} as any)
|
|
104
|
+
);
|
|
105
|
+
marked.use({
|
|
106
|
+
renderer: {
|
|
107
|
+
code(code, lang, escaped) {
|
|
108
|
+
code = this.options.highlight(code, lang);
|
|
109
|
+
if (!lang) {
|
|
110
|
+
return `<pre><code>${code}</code></pre>`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
var langClass = "language-" + lang;
|
|
114
|
+
return `<pre class="${langClass}"><code class="${langClass}">${code}</code></pre>`;
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
const parsed = marked.parse(rawBody, {
|
|
119
|
+
highlight: (code, lang) => {
|
|
120
|
+
if (prism.languages[lang]) {
|
|
121
|
+
return prism.highlight(code, prism.languages[lang], lang);
|
|
122
|
+
} else {
|
|
123
|
+
return code;
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
return {
|
|
128
|
+
html: parsed,
|
|
129
|
+
attributes,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private async getUser(username: string) {
|
|
134
|
+
const res = await axios.get(`https://api.github.com/users/${username}`);
|
|
135
|
+
return res.data;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
getStaticPaths = async () => {
|
|
139
|
+
const posts = await this.getPostList();
|
|
140
|
+
return {
|
|
141
|
+
paths: posts.map((p) => {
|
|
142
|
+
// TODO: the fallback path should be issue id
|
|
143
|
+
return `/${p.attributes.path || ""}`;
|
|
144
|
+
}),
|
|
145
|
+
fallback: true,
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
getHomePageStaticProps = async () => {
|
|
150
|
+
const posts = (await this.getPostList()).map((post) => {
|
|
151
|
+
return omit(post, ["body", "html"]);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
props: {
|
|
156
|
+
// TODO: reduce post body size
|
|
157
|
+
posts,
|
|
158
|
+
themeConfig: this.config.themeConfig || {},
|
|
159
|
+
},
|
|
160
|
+
revalidate: this.REVALIDATE,
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
getPostPageStaticProps = async (ctx) => {
|
|
165
|
+
const posts = await this.getPostList();
|
|
166
|
+
const post =
|
|
167
|
+
posts.find(
|
|
168
|
+
(p) =>
|
|
169
|
+
p.attributes.path === ctx.params[this.PAGE_PATH_PLACEHOLDER].join("/")
|
|
170
|
+
) || null;
|
|
171
|
+
return {
|
|
172
|
+
props: {
|
|
173
|
+
post,
|
|
174
|
+
themeConfig: this.config?.themeConfig || {},
|
|
175
|
+
},
|
|
176
|
+
revalidate: this.REVALIDATE,
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
private generateFeed = async () => {
|
|
181
|
+
const feed = new Feed({
|
|
182
|
+
title: this.config.siteConfig.title,
|
|
183
|
+
copyright: this.config.siteConfig.title,
|
|
184
|
+
id: this.config.siteConfig.title,
|
|
185
|
+
author: {
|
|
186
|
+
name: this.config.siteConfig.author || this.resolvedConfig.ghUserName,
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
if (!this.config.siteConfig.url) {
|
|
191
|
+
return feed.atom1();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const posts = await this.getPostList();
|
|
195
|
+
|
|
196
|
+
posts.forEach((post) => {
|
|
197
|
+
feed.addItem({
|
|
198
|
+
date: new Date(post.updatedAt),
|
|
199
|
+
link: `${this.config.siteConfig.url}/${post.attributes.path}`,
|
|
200
|
+
title: post.title,
|
|
201
|
+
content: post.html,
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
return feed.atom1();
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
rssHandler = async (req, res) => {
|
|
209
|
+
res.setHeader("Content-Type", "application/xml");
|
|
210
|
+
// https://vercel.com/docs/concepts/edge-network/caching#stale-while-revalidate
|
|
211
|
+
res.setHeader(
|
|
212
|
+
"Cache-Control",
|
|
213
|
+
`s-maxage=1 stale-while-revalidate=${10 * 60}`
|
|
214
|
+
);
|
|
215
|
+
res.send(await this.generateFeed());
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
DocumentHead = () => (
|
|
219
|
+
<>
|
|
220
|
+
{this.config?.theme.Head && (
|
|
221
|
+
<this.config.theme.Head
|
|
222
|
+
sairinConfig={this.config}
|
|
223
|
+
></this.config.theme.Head>
|
|
224
|
+
)}
|
|
225
|
+
</>
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export type HomePageThemeProps = Awaited<
|
|
230
|
+
ReturnType<Sairin["getHomePageStaticProps"]>
|
|
231
|
+
>["props"];
|
|
232
|
+
export type PostPageThemeProps = Awaited<
|
|
233
|
+
ReturnType<Sairin["getPostPageStaticProps"]>
|
|
234
|
+
>["props"];
|
package/src/queries.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
|
|
2
|
+
export type GetPostQueryVar = {
|
|
3
|
+
owner: string,
|
|
4
|
+
repo: string
|
|
5
|
+
}
|
|
6
|
+
export type GetPostQueryProps = {
|
|
7
|
+
repository: {
|
|
8
|
+
issues: {
|
|
9
|
+
nodes: {
|
|
10
|
+
id: string;
|
|
11
|
+
title: string;
|
|
12
|
+
url: string;
|
|
13
|
+
createdAt: string;
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
body: string;
|
|
16
|
+
comments: {
|
|
17
|
+
nodes: {
|
|
18
|
+
url: string,
|
|
19
|
+
createdAt: string,
|
|
20
|
+
author: {
|
|
21
|
+
login: string;
|
|
22
|
+
url: string;
|
|
23
|
+
avatarUrl: string;
|
|
24
|
+
};
|
|
25
|
+
body: string;
|
|
26
|
+
}[];
|
|
27
|
+
};
|
|
28
|
+
reactionGroups: {
|
|
29
|
+
content: string;
|
|
30
|
+
reactors: {
|
|
31
|
+
totalCount: number;
|
|
32
|
+
};
|
|
33
|
+
}[];
|
|
34
|
+
author: {
|
|
35
|
+
login: string;
|
|
36
|
+
url: string;
|
|
37
|
+
avatarUrl;
|
|
38
|
+
};
|
|
39
|
+
}[];
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
export const GetPostsQuery = (variables: GetPostQueryVar) => ({
|
|
44
|
+
query: `
|
|
45
|
+
query GetPosts($owner: String!, $repo: String!) {
|
|
46
|
+
repository(owner: $owner, name: $repo) {
|
|
47
|
+
issues(first: 100, orderBy: {
|
|
48
|
+
field: CREATED_AT, direction: DESC
|
|
49
|
+
}, labels: ["published"]) {
|
|
50
|
+
nodes {
|
|
51
|
+
id,
|
|
52
|
+
url,
|
|
53
|
+
title,
|
|
54
|
+
updatedAt,
|
|
55
|
+
createdAt,
|
|
56
|
+
body,
|
|
57
|
+
comments (first: 100) {
|
|
58
|
+
nodes {
|
|
59
|
+
createdAt,
|
|
60
|
+
url,
|
|
61
|
+
author {
|
|
62
|
+
login,
|
|
63
|
+
url,
|
|
64
|
+
avatarUrl,
|
|
65
|
+
},
|
|
66
|
+
body
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
reactionGroups {
|
|
70
|
+
content,
|
|
71
|
+
reactors {
|
|
72
|
+
totalCount,
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
author {
|
|
76
|
+
login,
|
|
77
|
+
url,
|
|
78
|
+
avatarUrl
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
`,
|
|
85
|
+
variables,
|
|
86
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
|
4
|
+
|
|
5
|
+
"rootDir": "src",
|
|
6
|
+
"outDir": "lib",
|
|
7
|
+
|
|
8
|
+
/* Projects */
|
|
9
|
+
// "incremental": true, /* Enable incremental compilation */
|
|
10
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
11
|
+
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
|
12
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
|
13
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
14
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
15
|
+
|
|
16
|
+
/* Language and Environment */
|
|
17
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
18
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
19
|
+
"jsx": "react-jsx",
|
|
20
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
21
|
+
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
22
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
23
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
|
24
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
25
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
|
26
|
+
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
|
27
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
28
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
29
|
+
|
|
30
|
+
/* Modules */
|
|
31
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
32
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
33
|
+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
34
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
35
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
36
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
37
|
+
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
|
38
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
39
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
40
|
+
// "resolveJsonModule": true, /* Enable importing .json files */
|
|
41
|
+
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
|
42
|
+
|
|
43
|
+
/* JavaScript Support */
|
|
44
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
|
45
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
46
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
|
47
|
+
|
|
48
|
+
/* Emit */
|
|
49
|
+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
50
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
51
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
52
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
53
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
|
54
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
55
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
56
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
57
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
58
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
|
59
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
60
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
61
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
62
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
63
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
64
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
65
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
66
|
+
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
|
67
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
|
68
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
69
|
+
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
|
70
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
71
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
72
|
+
|
|
73
|
+
/* Interop Constraints */
|
|
74
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
75
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
76
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
|
|
77
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
78
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
79
|
+
|
|
80
|
+
/* Type Checking */
|
|
81
|
+
// "strict": true, /* Enable all strict type-checking options. */
|
|
82
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
|
83
|
+
"strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
|
84
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
85
|
+
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
|
|
86
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
87
|
+
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
|
|
88
|
+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
|
|
89
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
90
|
+
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
|
|
91
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
|
|
92
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
93
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
94
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
95
|
+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
|
96
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
97
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
|
98
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
99
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
100
|
+
|
|
101
|
+
/* Completeness */
|
|
102
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
103
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
104
|
+
}
|
|
105
|
+
}
|