Retrieve posts from wordpress

This commit is contained in:
Lewis Dale 2023-02-16 10:46:48 +00:00
parent 09ac787260
commit 5b68b1278c
6 changed files with 66 additions and 9 deletions

13
package-lock.json generated
View File

@ -8,6 +8,9 @@
"name": "whimsy", "name": "whimsy",
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": {
"html-entities": "^2.3.3"
},
"devDependencies": { "devDependencies": {
"@11ty/eleventy": "^2.0.0", "@11ty/eleventy": "^2.0.0",
"@11ty/eleventy-fetch": "^3.0.0", "@11ty/eleventy-fetch": "^3.0.0",
@ -1995,6 +1998,11 @@
"integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==", "integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==",
"dev": true "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": { "node_modules/html-escaper": {
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz",
@ -6664,6 +6672,11 @@
"integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==", "integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==",
"dev": true "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": { "html-escaper": {
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz",

View File

@ -36,5 +36,8 @@
"postcss-nested": "^6.0.0", "postcss-nested": "^6.0.0",
"prettier": "^2.8.3", "prettier": "^2.8.3",
"tailwindcss": "^3.2.4" "tailwindcss": "^3.2.4"
},
"dependencies": {
"html-entities": "^2.3.3"
} }
} }

View File

@ -1,14 +1,54 @@
const wordpressPassword = process.env.WORDPRESS_PASSWORD; 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 () => { module.exports = async () => {
const response = await fetch("https://lewisdale.dev/wp-json/wp/v2/posts?per_page=100", { const response = await fetch("https://lewisdale.dev/wp-json/wp/v2/posts?per_page=100", {
headers: { headers: {
"Authorization": `Basic lewis:${wordpressPassword}` "Authorization": `Basic ${auth}`
} }
}); });
const posts = await response.json(); const posts = (await response.json());
return Promise.all(posts.map(async post => ({...post, comments: await getComments(post.id) })));
console.log(posts.map(post => Buffer.from(post.title.rendered).toString("utf-8")));
return posts;
}; };

View File

@ -1,7 +1,7 @@
<!doctype html> <!doctype html>
<html lang={{data.page.lang}}> <html lang={{data.page.lang}}>
<head> <head>
<title>{{ title }} | {{ metadata.site.name }}</title> <title>{{ title | safe }} | {{ metadata.site.name }}</title>
{% if includeFA %} {% if includeFA %}
<link rel="stylesheet" type="text/css" href="/assets/css/fontawesome.css" /> <link rel="stylesheet" type="text/css" href="/assets/css/fontawesome.css" />
{% endif %} {% endif %}

View File

@ -2,11 +2,11 @@
layout: base.njk layout: base.njk
includePrism: true includePrism: true
eleventyComputed: eleventyComputed:
title: "{{ post.title.rendered }}" title: "{{ post.title.rendered | safe }}"
--- ---
<main class="wrapper-md stack-lg"> <main class="wrapper-md stack-lg">
<article class="stack-md"> <article class="stack-md">
<h1><a href="{{ post.url }}">{{ title }}</a></h1> <h1><a href="{{ post.url }}">{{ title | safe }}</a></h1>
{{ content | safe }} {{ content | safe }}
</article> </article>

View File

@ -12,7 +12,7 @@ h3,
h4, h4,
h5 { h5 {
font-weight: bold; font-weight: bold;
line-height: 1; line-height: 1.2;
margin: 0; margin: 0;
} }
@ -21,6 +21,7 @@ h1 {
word-wrap: normal; word-wrap: normal;
text-underline-offset: var(--space-size-3xs); text-underline-offset: var(--space-size-3xs);
text-align: center; text-align: center;
line-height: 1.3;
} }
h2 { h2 {