Files
indiekit-blog/_data/mastodonFeed.js
svemagie ec9887a6d7 fix: social activity widget - Bluesky and Mastodon feeds not rendering
- deploy.yml: add missing BLUESKY_HANDLE env var to Build site step
- deploy.yml: fix GITHUB_USERNAME secret name (was GH_USERNAME)
- mastodonFeed.js: read MASTODON_URL first, fall back to MASTODON_INSTANCE
- mastodonFeed.js: add early-return guard when instance/user vars are empty
- mastodonFeed.js: also accept MASTODON_USERNAME as fallback for MASTODON_USER

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-10 01:31:19 +01:00

112 lines
3.0 KiB
JavaScript

/**
* Mastodon Feed Data
* Fetches recent posts from Mastodon using the public API
*/
import EleventyFetch from "@11ty/eleventy-fetch";
export default async function () {
const instance = (
process.env.MASTODON_URL ||
process.env.MASTODON_INSTANCE ||
""
)
.replace(/^https?:\/\//, "")
.replace(/\/+$/, "");
const username = (
process.env.MASTODON_USER ||
process.env.MASTODON_USERNAME ||
""
).trim().replace(/^@+/, "");
if (!instance || !username) {
console.log("[mastodonFeed] MASTODON_URL/MASTODON_USER not set, skipping");
return [];
}
try {
// First, look up the account ID
const lookupUrl = `https://${instance}/api/v1/accounts/lookup?acct=${username}`;
const account = await EleventyFetch(lookupUrl, {
duration: "1h", // Cache account lookup for 1 hour
type: "json",
fetchOptions: {
headers: {
Accept: "application/json",
},
},
});
if (!account || !account.id) {
console.log("Mastodon account not found:", username);
return [];
}
// Fetch recent statuses (excluding replies and boosts for cleaner feed)
const statusesUrl = `https://${instance}/api/v1/accounts/${account.id}/statuses?limit=10&exclude_replies=true&exclude_reblogs=true`;
const statuses = await EleventyFetch(statusesUrl, {
duration: "15m", // Cache for 15 minutes
type: "json",
fetchOptions: {
headers: {
Accept: "application/json",
},
},
});
if (!statuses || !Array.isArray(statuses)) {
console.log("No Mastodon statuses found for:", username);
return [];
}
// Transform statuses into a simpler format
return statuses.map((status) => ({
id: status.id,
url: status.url,
text: stripHtml(status.content),
htmlContent: status.content,
createdAt: status.created_at,
author: {
username: status.account.username,
displayName: status.account.display_name || status.account.username,
avatar: status.account.avatar,
url: status.account.url,
},
favouritesCount: status.favourites_count || 0,
reblogsCount: status.reblogs_count || 0,
repliesCount: status.replies_count || 0,
// Media attachments
media: status.media_attachments
? status.media_attachments.map((m) => ({
type: m.type,
url: m.url,
previewUrl: m.preview_url,
description: m.description,
}))
: [],
}));
} catch (error) {
console.error("Error fetching Mastodon feed:", error.message);
return [];
}
}
// Simple HTML stripper for plain text display
function stripHtml(html) {
if (!html) return "";
return html
.replace(/<br\s*\/?>/gi, " ")
.replace(/<\/p>/gi, " ")
.replace(/<[^>]+>/g, "")
.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&nbsp;/g, " ")
.replace(/\s+/g, " ")
.trim();
}