-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathposts.js
416 lines (326 loc) · 9.84 KB
/
posts.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import { getApolloClient } from 'lib/apollo-client';
import { updateUserAvatar } from 'lib/users';
import { sortObjectsByDate } from 'lib/datetime';
import {
QUERY_ALL_POSTS_INDEX,
QUERY_ALL_POSTS_ARCHIVE,
QUERY_ALL_POSTS,
QUERY_POST_BY_SLUG,
QUERY_POSTS_BY_AUTHOR_SLUG_INDEX,
QUERY_POSTS_BY_AUTHOR_SLUG_ARCHIVE,
QUERY_POSTS_BY_AUTHOR_SLUG,
QUERY_POSTS_BY_CATEGORY_ID_INDEX,
QUERY_POSTS_BY_CATEGORY_ID_ARCHIVE,
QUERY_POSTS_BY_CATEGORY_ID,
QUERY_POST_SEO_BY_SLUG,
QUERY_POST_PER_PAGE,
} from 'data/posts';
/**
* postPathBySlug
*/
export function postPathBySlug(slug) {
return `/posts/${slug}`;
}
/**
* getPostBySlug
*/
export async function getPostBySlug(slug) {
const apolloClient = getApolloClient();
const apiHost = new URL(process.env.WORDPRESS_GRAPHQL_ENDPOINT).host;
let postData;
let seoData;
try {
postData = await apolloClient.query({
query: QUERY_POST_BY_SLUG,
variables: {
slug,
},
});
} catch (e) {
console.log(`[posts][getPostBySlug] Failed to query post data: ${e.message}`);
throw e;
}
if (!postData?.data.post) return { post: undefined };
const post = [postData?.data.post].map(mapPostData)[0];
// If the SEO plugin is enabled, look up the data
// and apply it to the default settings
if (process.env.WORDPRESS_PLUGIN_SEO === true) {
try {
seoData = await apolloClient.query({
query: QUERY_POST_SEO_BY_SLUG,
variables: {
slug,
},
});
} catch (e) {
console.log(`[posts][getPostBySlug] Failed to query SEO plugin: ${e.message}`);
console.log('Is the SEO Plugin installed? If not, disable WORDPRESS_PLUGIN_SEO in next.config.js.');
throw e;
}
const { seo = {} } = seoData?.data?.post || {};
post.metaTitle = seo.title;
post.metaDescription = seo.metaDesc;
post.readingTime = seo.readingTime;
// The SEO plugin by default includes a canonical link, but we don't want to use that
// because it includes the WordPress host, not the site host. We manage the canonical
// link along with the other metadata, but explicitly check if there's a custom one
// in here by looking for the API's host in the provided canonical link
if (seo.canonical && !seo.canonical.includes(apiHost)) {
post.canonical = seo.canonical;
}
post.og = {
author: seo.opengraphAuthor,
description: seo.opengraphDescription,
image: seo.opengraphImage,
modifiedTime: seo.opengraphModifiedTime,
publishedTime: seo.opengraphPublishedTime,
publisher: seo.opengraphPublisher,
title: seo.opengraphTitle,
type: seo.opengraphType,
};
post.article = {
author: post.og.author,
modifiedTime: post.og.modifiedTime,
publishedTime: post.og.publishedTime,
publisher: post.og.publisher,
};
post.robots = {
nofollow: seo.metaRobotsNofollow,
noindex: seo.metaRobotsNoindex,
};
post.twitter = {
description: seo.twitterDescription,
image: seo.twitterImage,
title: seo.twitterTitle,
};
}
return {
post,
};
}
/**
* getAllPosts
*/
const allPostsIncludesTypes = {
all: QUERY_ALL_POSTS,
archive: QUERY_ALL_POSTS_ARCHIVE,
index: QUERY_ALL_POSTS_INDEX,
};
export async function getAllPosts(options = {}) {
const { queryIncludes = 'index' } = options;
const apolloClient = getApolloClient();
const data = await apolloClient.query({
query: allPostsIncludesTypes[queryIncludes],
});
const posts = data?.data.posts.edges.map(({ node = {} }) => node);
return {
posts: Array.isArray(posts) && posts.map(mapPostData),
};
}
/**
* getPostsByAuthorSlug
*/
const postsByAuthorSlugIncludesTypes = {
all: QUERY_POSTS_BY_AUTHOR_SLUG,
archive: QUERY_POSTS_BY_AUTHOR_SLUG_ARCHIVE,
index: QUERY_POSTS_BY_AUTHOR_SLUG_INDEX,
};
export async function getPostsByAuthorSlug({ slug, ...options }) {
const { queryIncludes = 'index' } = options;
const apolloClient = getApolloClient();
let postData;
try {
postData = await apolloClient.query({
query: postsByAuthorSlugIncludesTypes[queryIncludes],
variables: {
slug,
},
});
} catch (e) {
console.log(`[posts][getPostsByAuthorSlug] Failed to query post data: ${e.message}`);
throw e;
}
const posts = postData?.data.posts.edges.map(({ node = {} }) => node);
return {
posts: Array.isArray(posts) && posts.map(mapPostData),
};
}
/**
* getPostsByCategoryId
*/
const postsByCategoryIdIncludesTypes = {
all: QUERY_POSTS_BY_CATEGORY_ID,
archive: QUERY_POSTS_BY_CATEGORY_ID_ARCHIVE,
index: QUERY_POSTS_BY_CATEGORY_ID_INDEX,
};
export async function getPostsByCategoryId({ categoryId, ...options }) {
const { queryIncludes = 'index' } = options;
const apolloClient = getApolloClient();
let postData;
try {
postData = await apolloClient.query({
query: postsByCategoryIdIncludesTypes[queryIncludes],
variables: {
categoryId,
},
});
} catch (e) {
console.log(`[posts][getPostsByCategoryId] Failed to query post data: ${e.message}`);
throw e;
}
const posts = postData?.data.posts.edges.map(({ node = {} }) => node);
return {
posts: Array.isArray(posts) && posts.map(mapPostData),
};
}
/**
* getRecentPosts
*/
export async function getRecentPosts({ count, ...options }) {
const { posts } = await getAllPosts(options);
const sorted = sortObjectsByDate(posts);
return {
posts: sorted.slice(0, count),
};
}
/**
* sanitizeExcerpt
*/
export function sanitizeExcerpt(excerpt) {
if (typeof excerpt !== 'string') {
throw new Error(`Failed to sanitize excerpt: invalid type ${typeof excerpt}`);
}
let sanitized = excerpt;
// If the theme includes [...] as the more indication, clean it up to just ...
sanitized = sanitized.replace(/\s?\[…\]/, '…');
// If after the above replacement, the ellipsis includes 4 dots, it's
// the end of a setence
sanitized = sanitized.replace('....', '.');
sanitized = sanitized.replace('.…', '.');
// If the theme is including a "Continue..." link, remove it
sanitized = sanitized.replace(/\w*<a class="more-link".*<\/a>/, '');
return sanitized;
}
/**
* mapPostData
*/
export function mapPostData(post = {}) {
const data = { ...post };
// Clean up the author object to avoid someone having to look an extra
// level deeper into the node
if (data.author) {
data.author = {
...data.author.node,
};
}
// The URL by default that comes from Gravatar / WordPress is not a secure
// URL. This ends up redirecting to https, but it gives mixed content warnings
// as the HTML shows it as http. Replace the url to avoid those warnings
// and provide a secure URL by default
if (data.author?.avatar) {
data.author.avatar = updateUserAvatar(data.author.avatar);
}
// Clean up the categories to make them more easy to access
if (data.categories) {
data.categories = data.categories.edges.map(({ node }) => {
return {
...node,
};
});
}
// Clean up the featured image to make them more easy to access
if (data.featuredImage) {
data.featuredImage = data.featuredImage.node;
}
return data;
}
/**
* getRelatedPosts
*/
export async function getRelatedPosts(categories, postId, count = 5) {
if (!Array.isArray(categories) || categories.length === 0) return;
let related = {
category: categories && categories.shift(),
};
if (related.category) {
const { posts } = await getPostsByCategoryId({
categoryId: related.category.databaseId,
queryIncludes: 'archive',
});
const filtered = posts.filter(({ postId: id }) => id !== postId);
const sorted = sortObjectsByDate(filtered);
related.posts = sorted.map((post) => ({ title: post.title, slug: post.slug }));
}
if (!Array.isArray(related.posts) || related.posts.length === 0) {
const relatedPosts = await getRelatedPosts(categories, postId, count);
related = relatedPosts || related;
}
if (Array.isArray(related.posts) && related.posts.length > count) {
return related.posts.slice(0, count);
}
return related;
}
/**
* sortStickyPosts
*/
export function sortStickyPosts(posts) {
return [...posts].sort((post) => (post.isSticky ? -1 : 1));
}
/**
* getPostsPerPage
*/
export async function getPostsPerPage() {
//If POST_PER_PAGE is defined at next.config.js
if (process.env.POSTS_PER_PAGE) {
console.warn(
'You are using the deprecated POST_PER_PAGE variable. Use your WordPress instance instead to set this value ("Settings" > "Reading" > "Blog pages show at most").'
);
return Number(process.env.POSTS_PER_PAGE);
}
try {
const apolloClient = getApolloClient();
const { data } = await apolloClient.query({
query: QUERY_POST_PER_PAGE,
});
return Number(data.allSettings.readingSettingsPostsPerPage);
} catch (e) {
console.log(`Failed to query post per page data: ${e.message}`);
throw e;
}
}
/**
* getPageCount
*/
export async function getPagesCount(posts, postsPerPage) {
const _postsPerPage = postsPerPage ?? (await getPostsPerPage());
return Math.ceil(posts.length / _postsPerPage);
}
/**
* getPaginatedPosts
*/
export async function getPaginatedPosts({ currentPage = 1, ...options } = {}) {
const { posts } = await getAllPosts(options);
const postsPerPage = await getPostsPerPage();
const pagesCount = await getPagesCount(posts, postsPerPage);
let page = Number(currentPage);
if (typeof page === 'undefined' || isNaN(page)) {
page = 1;
} else if (page > pagesCount) {
return {
posts: [],
pagination: {
currentPage: undefined,
pagesCount,
},
};
}
const offset = postsPerPage * (page - 1);
const sortedPosts = sortStickyPosts(posts);
return {
posts: sortedPosts.slice(offset, offset + postsPerPage),
pagination: {
currentPage: page,
pagesCount,
},
};
}