diff --git a/package-lock.json b/package-lock.json index 6335b9a..52e4361 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "whimsy", "version": "1.0.0", "license": "ISC", + "dependencies": { + "html-entities": "^2.3.3" + }, "devDependencies": { "@11ty/eleventy": "^2.0.0", "@11ty/eleventy-fetch": "^3.0.0", @@ -1995,6 +1998,11 @@ "integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==", "dev": true }, + "node_modules/html-entities": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz", + "integrity": "sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA==" + }, "node_modules/html-escaper": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz", @@ -6664,6 +6672,11 @@ "integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==", "dev": true }, + "html-entities": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz", + "integrity": "sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA==" + }, "html-escaper": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz", diff --git a/package.json b/package.json index 6c733a8..2a17a1c 100644 --- a/package.json +++ b/package.json @@ -36,5 +36,8 @@ "postcss-nested": "^6.0.0", "prettier": "^2.8.3", "tailwindcss": "^3.2.4" + }, + "dependencies": { + "html-entities": "^2.3.3" } } diff --git a/src/_data/posts.js b/src/_data/posts.js index 3eef4a8..e85fed7 100644 --- a/src/_data/posts.js +++ b/src/_data/posts.js @@ -1,14 +1,54 @@ const wordpressPassword = process.env.WORDPRESS_PASSWORD; +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)) + } +} module.exports = async () => { const response = await fetch("https://lewisdale.dev/wp-json/wp/v2/posts?per_page=100", { headers: { - "Authorization": `Basic lewis:${wordpressPassword}` + "Authorization": `Basic ${auth}` } }); - const posts = await response.json(); - - console.log(posts.map(post => Buffer.from(post.title.rendered).toString("utf-8"))); - return posts; + const posts = (await response.json()); + return Promise.all(posts.map(async post => ({...post, comments: await getComments(post.id) }))); }; \ No newline at end of file diff --git a/src/_includes/base.njk b/src/_includes/base.njk index bcd77e5..8d52a83 100644 --- a/src/_includes/base.njk +++ b/src/_includes/base.njk @@ -1,7 +1,7 @@
-