Add a blog: posts at /blog/:id and a Community page section
Admin → Blog → Posts (migration 031): our own posts with a content-block body, cover image, excerpt, date, tags, and authors shown as person tiles, either People records or a guest name and photo. A post is a draft until published; Listed works as it does elsewhere. Posts are on the QR page too. Admin → Blog → Community section: the first section of the Community page, with its heading and blurb from the admin, in two halves, each pinned first and then newest up to its slots. Our posts come from blog_posts; community posts are pinned link or video cards (reusing featured cards and Fetch preview), then the newest items from a list of RSS or Atom feeds. Feeds are read through the link preview's guarded fetch and cached for 30 minutes, and a failing feed keeps its last good items. content_blocks and featured_cards are rebuilt to accept the new owner kinds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d4f65d93b4
commit
d374f9ac17
21 changed files with 1275 additions and 15 deletions
|
|
@ -22,7 +22,8 @@
|
|||
· TIMEOUT_MS per request, and at most MAX_HTML_BYTES of page
|
||||
or MAX_UPLOAD_BYTES of image is read
|
||||
|
||||
Editors and up only (routes/media.js).
|
||||
Editors and up only (routes/media.js). feeds.js reads RSS
|
||||
through the same safeGet.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { lookup } from "node:dns/promises";
|
||||
|
|
@ -102,7 +103,7 @@ async function vet(raw) {
|
|||
|
||||
/* ── One request, pinned to the vetted address ─────────────── */
|
||||
|
||||
function requestOnce({ url, address }, { maxBytes, accept }) {
|
||||
function requestOnce({ url, address }, { maxBytes, accept, truncate = false }) {
|
||||
const client = url.protocol === "https:" ? https : http;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
|
@ -140,7 +141,7 @@ function requestOnce({ url, address }, { maxBytes, accept }) {
|
|||
// A page's head is at the top; stop reading and use what
|
||||
// arrived. An image that's too big is refused outright.
|
||||
res.destroy();
|
||||
if (accept.startsWith("text/html")) {
|
||||
if (truncate) {
|
||||
resolve({ body: Buffer.concat(chunks), type: res.headers["content-type"] ?? "" });
|
||||
} else {
|
||||
reject(new MediaError(413, "The preview image is too large."));
|
||||
|
|
@ -163,7 +164,8 @@ function requestOnce({ url, address }, { maxBytes, accept }) {
|
|||
});
|
||||
}
|
||||
|
||||
async function safeGet(raw, options) {
|
||||
/* Also used by feeds.js, so feeds get the same address checks. */
|
||||
export async function safeGet(raw, options) {
|
||||
let next = raw;
|
||||
for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {
|
||||
const target = await vet(next);
|
||||
|
|
@ -178,7 +180,7 @@ async function safeGet(raw, options) {
|
|||
|
||||
const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " " };
|
||||
|
||||
function decode(text) {
|
||||
export function decodeEntities(text) {
|
||||
return text
|
||||
.replace(/&#x([0-9a-f]+);/gi, (_, hex) => String.fromCodePoint(parseInt(hex, 16)))
|
||||
.replace(/&#(\d+);/g, (_, dec) => String.fromCodePoint(Number(dec)))
|
||||
|
|
@ -197,11 +199,11 @@ function readMeta(html) {
|
|||
for (const tag of html.match(/<meta\b[^>]*>/gi) ?? []) {
|
||||
const key = (attr(tag, "property") ?? attr(tag, "name") ?? "").toLowerCase();
|
||||
const content = attr(tag, "content");
|
||||
if (key && content != null && !(key in meta)) meta[key] = decode(content);
|
||||
if (key && content != null && !(key in meta)) meta[key] = decodeEntities(content);
|
||||
}
|
||||
const title = html.match(/<title[^>]*>([\s\S]*?)<\/title>/i)?.[1];
|
||||
return {
|
||||
title: meta["og:title"] || meta["twitter:title"] || (title ? decode(title) : null) || null,
|
||||
title: meta["og:title"] || meta["twitter:title"] || (title ? decodeEntities(title) : null) || null,
|
||||
description:
|
||||
meta["og:description"] || meta["twitter:description"] || meta.description || null,
|
||||
image: meta["og:image:secure_url"] || meta["og:image"] || meta["twitter:image"] || null,
|
||||
|
|
@ -212,6 +214,7 @@ function readMeta(html) {
|
|||
export async function previewLink(raw) {
|
||||
const page = await safeGet(raw, {
|
||||
maxBytes: MAX_HTML_BYTES,
|
||||
truncate: true,
|
||||
accept: "text/html,application/xhtml+xml;q=0.9,*/*;q=0.1",
|
||||
});
|
||||
if (!/html/i.test(page.type)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue