lewisdale.dev/src/_data/posts.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-02-15 20:38:52 +00:00
const wordpressPassword = process.env.WORDPRESS_PASSWORD;
2023-02-16 10:46:48 +00:00
const auth = Buffer.from(`lewis:${wordpressPassword}`).toString('base64');
const mapComment = comment => ({
author: {
name: comment.author_name,
avatars: comment.author_avatar_urls,
url: comment.meta.semantic_linkbacks_author_url,
},
content: comment.content.rendered,
canonical: comment.meta.semantic_linkbacks_canonical,
date: comment.data,
});
const comments = async pid => {
const response = await fetch(`https://lewisdale.dev/wp-json/wp/v2/comments?per_page=100&post=${pid}`);
return (await response.json()).map(mapComment);
}
const webmentions = async (pid) => {
const response = await fetch(`https://lewisdale.dev/wp-json/wp/v2/comments?per_page=100&post=${pid}&type=Webmention`, {
headers: {
"Authorization": `Basic ${auth}`
}
});
return (await response.json())
.reduce((comments, comment) => {
if (!comments[comment.meta.semantic_linkbacks_type]) {
comments[comment.meta.semantic_linkbacks_type] = [];
}
comments[comment.meta.semantic_linkbacks_type].push(mapComment(comment));
return comments;
}, { like: [], repost: [] });
}
const getComments = async pid => {
return {
comments: await comments(pid),
...(await webmentions(pid))
}
}
2023-02-15 20:38:52 +00:00
module.exports = async () => {
const response = await fetch("https://lewisdale.dev/wp-json/wp/v2/posts?per_page=100", {
headers: {
2023-02-16 10:46:48 +00:00
"Authorization": `Basic ${auth}`
2023-02-15 20:38:52 +00:00
}
});
2023-02-16 10:46:48 +00:00
const posts = (await response.json());
return Promise.all(posts.map(async post => ({...post, comments: await getComments(post.id) })));
2023-02-15 20:38:52 +00:00
};