cook-ssg 2.11.0 → 2.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/package.json +1 -1
- package/scripts/build.js +6 -0
- package/scripts/dev.js +5 -1
- package/scripts/plugins/social-feed.js +185 -0
- package/scripts/plugins/social-links.js +2 -1
- package/scripts/social/feeds/block.js +175 -0
- package/scripts/social/feeds/core.js +414 -0
- package/scripts/social/feeds/fetch.js +150 -0
- package/scripts/social/feeds/snapshots.js +39 -0
- package/scripts/social/index.js +6 -1
- package/scripts/social/socialspec.js +3 -2
- package/scripts/utils/config/config.js +17 -3
package/README.md
CHANGED
|
@@ -148,6 +148,7 @@ The build process performs the following steps, in this order:
|
|
|
148
148
|
* Replaces custom elements and `[data-component]` elements with templates (nesting supported; flat or Declarative Shadow DOM)
|
|
149
149
|
* Replaces ES6 template strings (`${}`) with front matter + global data
|
|
150
150
|
* Fills empty `<social-links>` elements with profile links from `config/social.json`
|
|
151
|
+
* Fills empty `<social-feed>` elements with recent posts from those accounts (snapshots in `data/social-feeds/`)
|
|
151
152
|
* Rewrites `<img>` to responsive `<picture>` elements
|
|
152
153
|
* Detects custom elements and manages component JS loading (`autoComponents` config)
|
|
153
154
|
* Pre-renders Declarative Shadow DOM from a manifest (`dsd` config)
|
package/package.json
CHANGED
package/scripts/build.js
CHANGED
|
@@ -39,6 +39,7 @@ import replaceInclude from './plugins/replace-include.js';
|
|
|
39
39
|
import replaceInline from './plugins/replace-inline.js';
|
|
40
40
|
import replaceMissingExternalLinkProtocol from './plugins/replace-external-link-protocol.js';
|
|
41
41
|
import { socialData, fillSocialLinks } from './plugins/social-links.js';
|
|
42
|
+
import { fillSocialFeeds, resetSocialFeeds } from './plugins/social-feed.js';
|
|
42
43
|
import { writeSocialspecFiles } from './plugins/socialspec-files.js';
|
|
43
44
|
import { writeHeaderBlocks } from './plugins/headers.js';
|
|
44
45
|
import replaceTemplateStrings from './plugins/replace-template-strings.js';
|
|
@@ -112,6 +113,8 @@ class Build {
|
|
|
112
113
|
|
|
113
114
|
// PLUGIN: Social: `social` data from config/social.json (or main.js `social`)
|
|
114
115
|
await socialData({data});
|
|
116
|
+
// <social-feed>: per-build fetch cache (cook dev keeps fresh results)
|
|
117
|
+
resetSocialFeeds();
|
|
115
118
|
|
|
116
119
|
// PLUGIN: Generate responsive image derivatives (AVIF/WebP at multiple widths)
|
|
117
120
|
await optimizeImagesGenerate({store});
|
|
@@ -200,6 +203,9 @@ class Build {
|
|
|
200
203
|
// PLUGIN: Fill empty <social-links> elements with the site's profile links
|
|
201
204
|
fillSocialLinks({file, data, allowType: ['.html']});
|
|
202
205
|
|
|
206
|
+
// PLUGIN: Fill empty <social-feed> elements with recent posts (fetched once per account, snapshots on failure)
|
|
207
|
+
await fillSocialFeeds({ file, allowType: ['.html'] });
|
|
208
|
+
|
|
203
209
|
// PLUGIN: Rewrite <img> to <picture> with responsive srcset
|
|
204
210
|
await optimizeImagesRewrite({file, store, allowType: ['.html']});
|
|
205
211
|
|
package/scripts/dev.js
CHANGED
|
@@ -10,6 +10,7 @@ import path from 'node:path';
|
|
|
10
10
|
import { runBuild } from './build.js';
|
|
11
11
|
import fs from 'fs-extra';
|
|
12
12
|
import { loadProgramAreas, isProgramFile } from './plugins/programs.js';
|
|
13
|
+
import { setFeedCacheMaxAge } from './plugins/social-feed.js';
|
|
13
14
|
|
|
14
15
|
const browserSync = browserSyncLib.create('Dev Server');
|
|
15
16
|
|
|
@@ -174,6 +175,9 @@ browserSync.watch(themeJson).on('change', file => { rebuild(file); }).on('add',
|
|
|
174
175
|
const programsJson = `${process.cwd()}/config/programs.json`;
|
|
175
176
|
browserSync.watch(programsJson).on('change', file => { rebuild(file); }).on('add', file => { rebuild(file); });
|
|
176
177
|
|
|
177
|
-
//
|
|
178
|
+
// <social-feed>: reuse fetched posts for 10 minutes across rebuilds.
|
|
179
|
+
setFeedCacheMaxAge(10 * 60 * 1000);
|
|
180
|
+
|
|
181
|
+
// config/social.json (Vanilla Press) feeds <social-links> and <social-feed>: rebuild on change.
|
|
178
182
|
const socialJson = `${process.cwd()}/config/social.json`;
|
|
179
183
|
browserSync.watch(socialJson).on('change', file => { rebuild(file); }).on('add', file => { rebuild(file); });
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file social-feed.js
|
|
3
|
+
* @description Fills each empty `<social-feed>` at build with recent posts from
|
|
4
|
+
* the site's Bluesky, Mastodon and GitHub accounts (config/social.json).
|
|
5
|
+
*
|
|
6
|
+
* Each account is fetched at most once per build (`cook dev` reuses a result
|
|
7
|
+
* for 10 minutes): one page when that covers every block's `items`, and for a
|
|
8
|
+
* block with `tags` further pages from the cached cursor until enough posts
|
|
9
|
+
* match, at most 3 pages in all. A successful fetch with at least one item writes
|
|
10
|
+
* data/social-feeds/<service>-<slug>.json; a failed one falls back to that
|
|
11
|
+
* snapshot. `COOK_OFFLINE=1` uses snapshots only (one note per build; an
|
|
12
|
+
* account with no snapshot still warns). Feeds never fail a build.
|
|
13
|
+
*
|
|
14
|
+
* `--social-feed-height` is written inline only from a valid `height`; without
|
|
15
|
+
* one the component's CSS default (32rem) applies.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import chalk from 'chalk';
|
|
19
|
+
import Util from '../utils/util/util.js';
|
|
20
|
+
import { getSocial } from '../utils/config/config.js';
|
|
21
|
+
import { normalizeAccounts } from '../social/accounts.js';
|
|
22
|
+
import { feedAccounts, parseFeedAttrs, renderSocialFeed } from '../social/feeds/block.js';
|
|
23
|
+
import { fetchAccountPage } from '../social/feeds/fetch.js';
|
|
24
|
+
import { matchesTags } from '../social/feeds/core.js';
|
|
25
|
+
import { readSnapshot, writeSnapshot } from '../social/feeds/snapshots.js';
|
|
26
|
+
|
|
27
|
+
const TAG = '[social-feed] ';
|
|
28
|
+
const SKIP_INSIDE = 'pre, code, template, textarea';
|
|
29
|
+
const ATTRS = ['accounts', 'tags', 'items', 'height'];
|
|
30
|
+
/** Pages fetched per account per build, at most. */
|
|
31
|
+
const MAX_PAGES = 3;
|
|
32
|
+
const HEADERS = { 'User-Agent': 'cook-ssg' };
|
|
33
|
+
|
|
34
|
+
/** account key → { at, target, items, cursor, done, pages, stopped, failed?, chain } */
|
|
35
|
+
const cache = new Map();
|
|
36
|
+
let maxAgeMs = 0;
|
|
37
|
+
let feedFetch = null;
|
|
38
|
+
let allAccounts = null;
|
|
39
|
+
let warned = new Set();
|
|
40
|
+
|
|
41
|
+
/** Tests and tools: use this fetch instead of the global one (null restores it). */
|
|
42
|
+
export function setFeedFetch(fn) {
|
|
43
|
+
feedFetch = typeof fn === 'function' ? fn : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** `cook dev`: keep successful results this long across rebuilds. */
|
|
47
|
+
export function setFeedCacheMaxAge(ms) {
|
|
48
|
+
maxAgeMs = Number.isFinite(ms) && ms > 0 ? ms : 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Start a build: forget the accounts and warnings, and drop cached results
|
|
53
|
+
* except successful ones younger than the max age (0 outside `cook dev`).
|
|
54
|
+
*/
|
|
55
|
+
export function resetSocialFeeds() {
|
|
56
|
+
allAccounts = null;
|
|
57
|
+
warned = new Set();
|
|
58
|
+
const now = Date.now();
|
|
59
|
+
for (const [key, entry] of cache) {
|
|
60
|
+
if (entry.failed || !(maxAgeMs > 0 && now - entry.at < maxAgeMs)) cache.delete(key);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function warn(message) {
|
|
65
|
+
if (warned.has(message)) return;
|
|
66
|
+
warned.add(message);
|
|
67
|
+
console.log(chalk.yellow(message));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const offline = () => {
|
|
71
|
+
const v = process.env.COOK_OFFLINE;
|
|
72
|
+
return !!v && v !== '0' && v.toLowerCase() !== 'false';
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Make sure the account's cached result has at least `want` items passing
|
|
77
|
+
* `tags`, fetching further pages from the cached cursor as needed (at most 3
|
|
78
|
+
* pages per account per build). The first page failing falls back to the
|
|
79
|
+
* snapshot; a later page failing keeps what was fetched. Never rejects.
|
|
80
|
+
* @returns {Promise<{ items: object[], cursor: string|null, done: boolean }>}
|
|
81
|
+
* a copy taken now, so a block's state matches the pages fetched so far
|
|
82
|
+
*/
|
|
83
|
+
function accountResult(account, { want, tags }) {
|
|
84
|
+
let entry = cache.get(account.key);
|
|
85
|
+
if (!entry) {
|
|
86
|
+
const target = { key: account.key, service: account.service, handle: account.handle };
|
|
87
|
+
if (account.did) target.did = account.did;
|
|
88
|
+
entry = { at: Date.now(), target, items: [], cursor: null, done: false, pages: 0, stopped: false, chain: Promise.resolve() };
|
|
89
|
+
cache.set(account.key, entry);
|
|
90
|
+
}
|
|
91
|
+
entry.chain = entry.chain.then(() => extend(account, entry, { want, tags }));
|
|
92
|
+
return entry.chain.then(() => ({ items: [...entry.items], cursor: entry.cursor, done: entry.done }));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function extend(account, entry, { want, tags }) {
|
|
96
|
+
const rootDir = process.cwd();
|
|
97
|
+
const { key } = account;
|
|
98
|
+
if (entry.failed) return;
|
|
99
|
+
if (entry.pages === 0 && offline()) return useSnapshot(account, entry, 'COOK_OFFLINE is set', { quiet: true });
|
|
100
|
+
const passing = () => entry.items.filter((i) => matchesTags(i, tags)).length;
|
|
101
|
+
while (!entry.done && !entry.stopped && entry.pages < MAX_PAGES && (entry.pages === 0 || passing() < want)) {
|
|
102
|
+
try {
|
|
103
|
+
const res = await fetchAccountPage(entry.target, {
|
|
104
|
+
fetch: feedFetch || globalThis.fetch,
|
|
105
|
+
cursor: entry.cursor,
|
|
106
|
+
mode: 'build',
|
|
107
|
+
token: process.env.GITHUB_TOKEN || undefined,
|
|
108
|
+
headers: HEADERS,
|
|
109
|
+
});
|
|
110
|
+
entry.pages += 1;
|
|
111
|
+
entry.items.push(...res.items);
|
|
112
|
+
entry.cursor = res.cursor;
|
|
113
|
+
entry.done = res.done;
|
|
114
|
+
if (entry.items.length) writeSnapshot(rootDir, account, entry.items);
|
|
115
|
+
} catch (err) {
|
|
116
|
+
const why = err && err.message ? err.message : String(err);
|
|
117
|
+
if (entry.pages === 0) return useSnapshot(account, entry, why);
|
|
118
|
+
entry.stopped = true;
|
|
119
|
+
warn(`${TAG}${key}: ${why}; showing the posts fetched before it`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* A failed or offline account: its snapshot items, no cursor, not done.
|
|
126
|
+
* `quiet` (offline): a found snapshot gives one note per build, not a line per account.
|
|
127
|
+
*/
|
|
128
|
+
function useSnapshot(account, entry, why, { quiet = false } = {}) {
|
|
129
|
+
entry.failed = true;
|
|
130
|
+
const items = readSnapshot(process.cwd(), account);
|
|
131
|
+
if (items && quiet) warn(`${TAG}${why}; using snapshots`);
|
|
132
|
+
else warn(`${TAG}${account.key}: ${why}; ${items ? 'using the snapshot' : 'no snapshot, so nothing from this account'}`);
|
|
133
|
+
entry.items = items || [];
|
|
134
|
+
entry.cursor = null;
|
|
135
|
+
entry.done = false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The element's style with `--social-feed-height` set (replacing an earlier one). */
|
|
139
|
+
function withHeight(style, height) {
|
|
140
|
+
const rest = (style || '')
|
|
141
|
+
.split(';')
|
|
142
|
+
.map((d) => d.trim())
|
|
143
|
+
.filter((d) => d && !/^--social-feed-height\s*:/.test(d));
|
|
144
|
+
return [...rest, `--social-feed-height: ${height}`].join('; ');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Fill each `<social-feed>` that has no element child and is not inside
|
|
149
|
+
* pre, code, template or textarea. A page with no live `<social-feed>` keeps
|
|
150
|
+
* its bytes.
|
|
151
|
+
*/
|
|
152
|
+
export async function fillSocialFeeds({ file, allowType }) {
|
|
153
|
+
if (!Util.isAllowedType({ file, allowType })) return;
|
|
154
|
+
if (typeof file.src !== 'string' || !file.src.includes('<social-feed')) return;
|
|
155
|
+
const dom = Util.fileDom(file);
|
|
156
|
+
const live = [...dom.window.document.querySelectorAll('social-feed')]
|
|
157
|
+
.filter((el) => !el.closest(SKIP_INSIDE) && el.children.length === 0);
|
|
158
|
+
if (!live.length) return;
|
|
159
|
+
|
|
160
|
+
if (allAccounts === null) {
|
|
161
|
+
const raw = getSocial();
|
|
162
|
+
allAccounts = raw === undefined ? [] : normalizeAccounts(raw).all;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
for (const el of live) {
|
|
166
|
+
const attrs = Object.fromEntries(ATTRS.map((name) => [name, el.getAttribute(name)]));
|
|
167
|
+
attrs['hide-counts'] = el.hasAttribute('hide-counts') ? '' : null;
|
|
168
|
+
const opts = parseFeedAttrs(attrs);
|
|
169
|
+
const selected = feedAccounts(allAccounts, attrs.accounts);
|
|
170
|
+
for (const w of [...opts.warnings, ...selected.warnings]) warn(w);
|
|
171
|
+
const accounts = selected.accounts.filter((a) => !opts.tags.length || a.service !== 'github');
|
|
172
|
+
|
|
173
|
+
const results = await Promise.all(accounts.map((a) => accountResult(a, { want: opts.items, tags: opts.tags })));
|
|
174
|
+
const itemsByKey = {};
|
|
175
|
+
const cursorsByKey = {};
|
|
176
|
+
accounts.forEach((a, i) => {
|
|
177
|
+
itemsByKey[a.key] = results[i].items;
|
|
178
|
+
cursorsByKey[a.key] = { cursor: results[i].cursor, done: results[i].done };
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
el.innerHTML = renderSocialFeed(attrs, { accounts, itemsByKey, cursorsByKey });
|
|
182
|
+
if (opts.height) el.setAttribute('style', withHeight(el.getAttribute('style'), opts.height));
|
|
183
|
+
}
|
|
184
|
+
Util.commitDom(file, dom, true);
|
|
185
|
+
}
|
|
@@ -27,7 +27,8 @@ function aboutHtml(about) {
|
|
|
27
27
|
/** Set `social` on the data store when a social config exists. */
|
|
28
28
|
export async function socialData({ data }) {
|
|
29
29
|
visibleAccounts = [];
|
|
30
|
-
|
|
30
|
+
// Once per build: read the file afresh; later readers this build get this result (and no repeat warning).
|
|
31
|
+
const raw = getSocial({ fresh: true });
|
|
31
32
|
if (raw === undefined) return;
|
|
32
33
|
const { all, visible, warnings } = normalizeAccounts(raw);
|
|
33
34
|
for (const w of warnings) console.log(chalk.yellow(`${TAG}${w}`));
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file block.js
|
|
3
|
+
* @description One `<social-feed>` block: which accounts it reads, its
|
|
4
|
+
* attributes, and the inner HTML Cook writes (entries, "Nothing yet", the
|
|
5
|
+
* "More on" line and the state JSON). Pure: no fs, no network, so Vanilla
|
|
6
|
+
* Press's preview reuses it with snapshot items.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { SERVICES, serviceById } from '../catalog.js';
|
|
10
|
+
import { profileUrl } from '../accounts.js';
|
|
11
|
+
import { mergeItems, renderFeedParts, safeHttps } from './core.js';
|
|
12
|
+
|
|
13
|
+
const TAG = '[social-feed] ';
|
|
14
|
+
const FEED_SERVICES = ['bluesky', 'mastodon', 'github'];
|
|
15
|
+
const DEFAULT_ITEMS = 12;
|
|
16
|
+
const HEIGHT = /^\d+(\.\d+)?(rem|em|px|vh)$/;
|
|
17
|
+
|
|
18
|
+
const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
19
|
+
const SERVICE_NAMES = Object.fromEntries(SERVICES.map((s) => [s.id, { name: s.name }]));
|
|
20
|
+
const keyOf = (a) => `${a.service}:${a.handle}`;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The accounts one block reads.
|
|
24
|
+
* Empty `keysAttr`: every visible account of a feed service (bluesky, mastodon,
|
|
25
|
+
* github). Named keys (`<service>:<handle>`, comma separated) include hidden
|
|
26
|
+
* accounts; a key not in the config is skipped with a warning; a key of a
|
|
27
|
+
* non-feed service is ignored silently.
|
|
28
|
+
* @param {object[]|{ all: object[] }} normalized `all` from normalizeAccounts (or its whole result)
|
|
29
|
+
* @param {string|null|undefined} keysAttr the `accounts` attribute
|
|
30
|
+
* @returns {{ accounts: Array<{ key: string, service: string, handle: string, did?: string, profileUrl: string|null }>, warnings: string[] }}
|
|
31
|
+
*/
|
|
32
|
+
export function feedAccounts(normalized, keysAttr) {
|
|
33
|
+
const list = Array.isArray(normalized) ? normalized : isObj(normalized) && Array.isArray(normalized.all) ? normalized.all : [];
|
|
34
|
+
const feedList = list.filter((a) => isObj(a) && FEED_SERVICES.includes(a.service) && typeof a.handle === 'string' && a.handle);
|
|
35
|
+
const toFeed = (a) => {
|
|
36
|
+
const out = { key: keyOf(a), service: a.service, handle: a.handle };
|
|
37
|
+
if (typeof a.did === 'string' && a.did) out.did = a.did;
|
|
38
|
+
out.profileUrl = safeHttps(a.url) || profileUrl(a);
|
|
39
|
+
return out;
|
|
40
|
+
};
|
|
41
|
+
const raw = typeof keysAttr === 'string' ? keysAttr.trim() : '';
|
|
42
|
+
if (!raw) return { accounts: feedList.filter((a) => !a.hidden).map(toFeed), warnings: [] };
|
|
43
|
+
|
|
44
|
+
const accounts = [];
|
|
45
|
+
const warnings = [];
|
|
46
|
+
const seen = new Set();
|
|
47
|
+
for (const key of raw.split(',').map((s) => s.trim()).filter(Boolean)) {
|
|
48
|
+
const service = key.includes(':') ? key.slice(0, key.indexOf(':')) : '';
|
|
49
|
+
if (serviceById(service) && !FEED_SERVICES.includes(service)) continue;
|
|
50
|
+
const found = feedList.find((a) => keyOf(a).toLowerCase() === key.toLowerCase());
|
|
51
|
+
if (!found) {
|
|
52
|
+
warnings.push(`${TAG}accounts: "${key}" is not in config/social.json; skipped`);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (seen.has(keyOf(found))) continue;
|
|
56
|
+
seen.add(keyOf(found));
|
|
57
|
+
accounts.push(toFeed(found));
|
|
58
|
+
}
|
|
59
|
+
return { accounts, warnings };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Read a block's attributes. An invalid `items` falls back to the default
|
|
64
|
+
* with a warning; an invalid `height` warns and is dropped (null), as is a
|
|
65
|
+
* missing one, so the element's own CSS default applies.
|
|
66
|
+
* @param {{ accounts?: string|null, tags?: string|null, items?: string|null,
|
|
67
|
+
* height?: string|null, 'hide-counts'?: string|boolean|null }} attrs
|
|
68
|
+
* attribute values as authored; a missing attribute is null or undefined
|
|
69
|
+
* @returns {{ accounts: string, tags: string[], items: number, height: string|null, hideCounts: boolean, warnings: string[] }}
|
|
70
|
+
*/
|
|
71
|
+
export function parseFeedAttrs(attrs = {}) {
|
|
72
|
+
const a = isObj(attrs) ? attrs : {};
|
|
73
|
+
const warnings = [];
|
|
74
|
+
const has = (v) => v !== undefined && v !== null && v !== false;
|
|
75
|
+
|
|
76
|
+
let items = DEFAULT_ITEMS;
|
|
77
|
+
if (has(a.items)) {
|
|
78
|
+
const s = String(a.items).trim();
|
|
79
|
+
const n = /^\d+$/.test(s) ? Number(s) : NaN;
|
|
80
|
+
if (n >= 1 && n <= 50) items = n;
|
|
81
|
+
else warnings.push(`${TAG}items="${a.items}" is not a whole number from 1 to 50; using ${DEFAULT_ITEMS}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let height = null;
|
|
85
|
+
if (has(a.height)) {
|
|
86
|
+
const s = String(a.height).trim();
|
|
87
|
+
if (HEIGHT.test(s)) height = s;
|
|
88
|
+
else warnings.push(`${TAG}height="${a.height}" is not a rem, em, px or vh length; ignored`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const tags = [];
|
|
92
|
+
for (const t of (has(a.tags) ? String(a.tags) : '').split(',')) {
|
|
93
|
+
const c = t.trim().replace(/^#+/, '').toLowerCase();
|
|
94
|
+
if (c && !tags.includes(c)) tags.push(c);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
accounts: has(a.accounts) ? String(a.accounts) : '',
|
|
99
|
+
tags,
|
|
100
|
+
items,
|
|
101
|
+
height,
|
|
102
|
+
hideCounts: has(a['hide-counts']),
|
|
103
|
+
warnings,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Keep only items with the fields the merge and renderer rely on; snapshots are
|
|
109
|
+
* hand-editable. Dates are normalized to ISO 8601 UTC (an unparseable one drops
|
|
110
|
+
* the item), so mergeItems' string comparison orders them by instant.
|
|
111
|
+
*/
|
|
112
|
+
function cleanItems(list, key) {
|
|
113
|
+
return (Array.isArray(list) ? list : [])
|
|
114
|
+
.filter((i) => isObj(i) && typeof i.id === 'string' && i.id && typeof i.date === 'string'
|
|
115
|
+
&& !Number.isNaN(Date.parse(i.date)) && safeHttps(i.url))
|
|
116
|
+
.map((i) => ({ ...i, date: new Date(i.date).toISOString(), source: key }));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** JSON safe inside a `<script>`: no `<`, `>` or `&` survive as raw characters. */
|
|
120
|
+
const scriptJson = (value) => JSON.stringify(value)
|
|
121
|
+
.replace(/</g, '\\u003c').replace(/>/g, '\\u003e').replace(/&/g, '\\u0026')
|
|
122
|
+
.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The inner HTML of one `<social-feed>`.
|
|
126
|
+
* @param {object} attrs attribute values as authored (see parseFeedAttrs)
|
|
127
|
+
* @param {{ accounts?: Array<{ key: string, service: string, handle: string, did?: string, profileUrl?: string|null }>,
|
|
128
|
+
* itemsByKey?: Record<string, object[]>,
|
|
129
|
+
* cursorsByKey?: Record<string, { cursor?: string|null, done?: boolean } | string | null> }} data
|
|
130
|
+
* accounts from feedAccounts; items per account key (fetched or snapshot);
|
|
131
|
+
* paging state per account key (a missing entry means no cursor, not done)
|
|
132
|
+
* @returns {string}
|
|
133
|
+
*/
|
|
134
|
+
export function renderSocialFeed(attrs, { accounts = [], itemsByKey = {}, cursorsByKey = {} } = {}) {
|
|
135
|
+
const opts = parseFeedAttrs(attrs);
|
|
136
|
+
const chosen = (Array.isArray(accounts) ? accounts : [])
|
|
137
|
+
.filter((a) => isObj(a) && typeof a.key === 'string')
|
|
138
|
+
.filter((a) => !opts.tags.length || a.service !== 'github');
|
|
139
|
+
const byKey = isObj(itemsByKey) ? itemsByKey : {};
|
|
140
|
+
const cursors = isObj(cursorsByKey) ? cursorsByKey : {};
|
|
141
|
+
|
|
142
|
+
const merged = mergeItems(chosen.map((a) => cleanItems(byKey[a.key], a.key)), { tags: opts.tags });
|
|
143
|
+
const shown = merged.slice(0, opts.items);
|
|
144
|
+
const buffer = merged.slice(opts.items);
|
|
145
|
+
const newest = {};
|
|
146
|
+
for (const item of merged) if (!(item.source in newest)) newest[item.source] = item.date;
|
|
147
|
+
|
|
148
|
+
const state = {
|
|
149
|
+
accounts: chosen.map((a) => {
|
|
150
|
+
const c = cursors[a.key];
|
|
151
|
+
const cursor = typeof c === 'string' ? c : isObj(c) && typeof c.cursor === 'string' ? c.cursor : null;
|
|
152
|
+
const out = { key: a.key, service: a.service, handle: a.handle };
|
|
153
|
+
if (a.did) out.did = a.did;
|
|
154
|
+
if (cursor) out.cursor = cursor;
|
|
155
|
+
out.done = isObj(c) && c.done === true;
|
|
156
|
+
return out;
|
|
157
|
+
}),
|
|
158
|
+
tags: opts.tags,
|
|
159
|
+
newest,
|
|
160
|
+
buffer,
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const { entries, empty, more } = renderFeedParts({
|
|
164
|
+
items: shown, accounts: chosen, hideCounts: opts.hideCounts, services: SERVICE_NAMES,
|
|
165
|
+
});
|
|
166
|
+
const parts = [
|
|
167
|
+
`<activity-feed aria-label="Recent posts"${entries ? ' data-infinite' : ''}>`,
|
|
168
|
+
...(entries ? [entries] : []),
|
|
169
|
+
'</activity-feed>',
|
|
170
|
+
];
|
|
171
|
+
if (empty) parts.push(empty);
|
|
172
|
+
if (more) parts.push(more);
|
|
173
|
+
parts.push(`<script type="application/json" class="social-feed-state">${scriptJson(state)}</script>`);
|
|
174
|
+
return parts.join('\n');
|
|
175
|
+
}
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file core.js
|
|
3
|
+
* @description The pure core of `<social-feed>`: turn Bluesky, Mastodon and
|
|
4
|
+
* GitHub API responses into one item shape, filter and merge them, and render
|
|
5
|
+
* the entry markup. It imports nothing and uses no Node built-ins, so Vanilla
|
|
6
|
+
* Breeze copies it verbatim into the browser.
|
|
7
|
+
*
|
|
8
|
+
* Item shape (shared with Vanilla Breeze):
|
|
9
|
+
* { id, service, source, url, date, text, tags, reposted,
|
|
10
|
+
* author: { name, handle, avatar?, url? },
|
|
11
|
+
* image?: { url, alt },
|
|
12
|
+
* counts?: { replies, reposts, likes },
|
|
13
|
+
* release?: { repo, tag, repoUrl?, description, language } }
|
|
14
|
+
* Every URL is https:; a field whose URL is not is left out, and an item whose
|
|
15
|
+
* own `url` is not is dropped.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
19
|
+
const str = (v) => (v === null || v === undefined ? '' : String(v));
|
|
20
|
+
|
|
21
|
+
/** Escape text for HTML text and double- or single-quoted attribute values. */
|
|
22
|
+
export const escapeHtml = (s) => str(s)
|
|
23
|
+
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
24
|
+
.replace(/"/g, '"').replace(/'/g, ''');
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {unknown} url
|
|
28
|
+
* @returns {string|null} the normalized URL when it parses as https:, else null
|
|
29
|
+
*/
|
|
30
|
+
export function safeHttps(url) {
|
|
31
|
+
if (typeof url !== 'string' || !url) return null;
|
|
32
|
+
try {
|
|
33
|
+
const u = new URL(url);
|
|
34
|
+
return u.protocol === 'https:' ? u.href : null;
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @returns {string|null} ISO 8601, or null for a missing or bad date */
|
|
41
|
+
function isoDate(v) {
|
|
42
|
+
if (typeof v !== 'string' || !v) return null;
|
|
43
|
+
const d = new Date(v);
|
|
44
|
+
return Number.isNaN(d.getTime()) ? null : d.toISOString();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const cleanTag = (t) => str(t).trim().replace(/^#+/, '').toLowerCase();
|
|
48
|
+
|
|
49
|
+
function tagList(list) {
|
|
50
|
+
const out = [];
|
|
51
|
+
for (const t of list) {
|
|
52
|
+
const c = cleanTag(t);
|
|
53
|
+
if (c && !out.includes(c)) out.push(c);
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const num = (v) => (Number.isFinite(Number(v)) ? Number(v) : 0);
|
|
59
|
+
|
|
60
|
+
function makeAuthor({ name, handle, avatar, url }) {
|
|
61
|
+
const author = { name: str(name || handle), handle: str(handle) };
|
|
62
|
+
const a = safeHttps(avatar);
|
|
63
|
+
if (a) author.avatar = a;
|
|
64
|
+
const u = safeHttps(url);
|
|
65
|
+
if (u) author.url = u;
|
|
66
|
+
return author;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function makeImage(url, alt) {
|
|
70
|
+
const u = safeHttps(url);
|
|
71
|
+
return u ? { url: u, alt: str(alt) } : undefined;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Assemble an item in the contract's key order, leaving out absent optional parts. */
|
|
75
|
+
function makeItem({ id, service, source, url, date, text, tags, reposted, author, image, counts, release }) {
|
|
76
|
+
const safeUrl = safeHttps(url);
|
|
77
|
+
if (!safeUrl || !date) return null;
|
|
78
|
+
const item = { id, service, source, url: safeUrl, date, text: str(text), tags, reposted: !!reposted, author };
|
|
79
|
+
if (image) item.image = image;
|
|
80
|
+
if (counts) item.counts = counts;
|
|
81
|
+
if (release) item.release = release;
|
|
82
|
+
return item;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* ---------------------------------------------------------------- Bluesky */
|
|
86
|
+
|
|
87
|
+
function bskyImage(embed) {
|
|
88
|
+
if (!isObj(embed)) return undefined;
|
|
89
|
+
const type = str(embed.$type);
|
|
90
|
+
if (type.startsWith('app.bsky.embed.external#view') && isObj(embed.external)) {
|
|
91
|
+
return makeImage(embed.external.thumb, embed.external.title);
|
|
92
|
+
}
|
|
93
|
+
if (type.startsWith('app.bsky.embed.images#view') && Array.isArray(embed.images) && isObj(embed.images[0])) {
|
|
94
|
+
return makeImage(embed.images[0].thumb || embed.images[0].fullsize, embed.images[0].alt);
|
|
95
|
+
}
|
|
96
|
+
if (type.startsWith('app.bsky.embed.recordWithMedia#view')) return bskyImage(embed.media);
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param {unknown} body an app.bsky.feed.getAuthorFeed response
|
|
102
|
+
* @param {{ key: string, service: string, handle: string, did?: string }} account
|
|
103
|
+
* @returns {{ items: object[], cursor: string|null }}
|
|
104
|
+
*/
|
|
105
|
+
export function normalizeBluesky(body, account) {
|
|
106
|
+
const feed = isObj(body) && Array.isArray(body.feed) ? body.feed : [];
|
|
107
|
+
const items = [];
|
|
108
|
+
for (const entry of feed) {
|
|
109
|
+
if (!isObj(entry) || !isObj(entry.post)) continue;
|
|
110
|
+
const { post } = entry;
|
|
111
|
+
const record = isObj(post.record) ? post.record : {};
|
|
112
|
+
const reposted = isObj(entry.reason) && entry.reason.$type === 'app.bsky.feed.defs#reasonRepost';
|
|
113
|
+
if (!reposted && (record.reply || entry.reply)) continue;
|
|
114
|
+
const author = isObj(post.author) ? post.author : {};
|
|
115
|
+
const handle = str(author.handle || author.did);
|
|
116
|
+
const rkey = str(post.uri).split('/').pop();
|
|
117
|
+
if (!handle || !rkey) continue;
|
|
118
|
+
const facetTags = (Array.isArray(record.facets) ? record.facets : [])
|
|
119
|
+
.flatMap((f) => (isObj(f) && Array.isArray(f.features) ? f.features : []))
|
|
120
|
+
.filter((f) => isObj(f) && f.$type === 'app.bsky.richtext.facet#tag')
|
|
121
|
+
.map((f) => f.tag);
|
|
122
|
+
const item = makeItem({
|
|
123
|
+
id: `bluesky:${post.uri}`,
|
|
124
|
+
service: 'bluesky',
|
|
125
|
+
source: account.key,
|
|
126
|
+
url: `https://bsky.app/profile/${encodeURIComponent(handle)}/post/${encodeURIComponent(rkey)}`,
|
|
127
|
+
date: isoDate(reposted ? entry.reason.indexedAt : record.createdAt || post.indexedAt),
|
|
128
|
+
text: record.text,
|
|
129
|
+
tags: tagList([...facetTags, ...(Array.isArray(record.tags) ? record.tags : [])]),
|
|
130
|
+
reposted,
|
|
131
|
+
author: makeAuthor({
|
|
132
|
+
name: author.displayName,
|
|
133
|
+
handle: `@${handle}`,
|
|
134
|
+
avatar: author.avatar,
|
|
135
|
+
url: `https://bsky.app/profile/${encodeURIComponent(handle)}`,
|
|
136
|
+
}),
|
|
137
|
+
image: bskyImage(post.embed),
|
|
138
|
+
counts: { replies: num(post.replyCount), reposts: num(post.repostCount), likes: num(post.likeCount) },
|
|
139
|
+
});
|
|
140
|
+
if (item) items.push(item);
|
|
141
|
+
}
|
|
142
|
+
const cursor = isObj(body) && typeof body.cursor === 'string' && body.cursor ? body.cursor : null;
|
|
143
|
+
return { items, cursor };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/* --------------------------------------------------------------- Mastodon */
|
|
147
|
+
|
|
148
|
+
const NAMED_ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' };
|
|
149
|
+
|
|
150
|
+
function decodeEntities(s) {
|
|
151
|
+
return s.replace(/&(#[xX][0-9a-fA-F]+|#[0-9]+|[a-zA-Z]+);/g, (whole, body) => {
|
|
152
|
+
if (body[0] === '#') {
|
|
153
|
+
const code = body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
|
|
154
|
+
return code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : whole;
|
|
155
|
+
}
|
|
156
|
+
const named = NAMED_ENTITIES[body.toLowerCase()];
|
|
157
|
+
return named === undefined ? whole : named;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Mastodon status HTML to plain text: `<br>` and paragraph breaks become newlines. */
|
|
162
|
+
function htmlToText(html) {
|
|
163
|
+
const text = str(html)
|
|
164
|
+
.replace(/<br\s*\/?>/gi, '\n')
|
|
165
|
+
.replace(/<\/p>\s*<p(?:\s[^>]*)?>/gi, '\n')
|
|
166
|
+
.replace(/<[^<>]*>/g, '');
|
|
167
|
+
return decodeEntities(text).trim();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function hostOf(url) {
|
|
171
|
+
try { return new URL(url).host; } catch { return ''; }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function mastodonHandle(acc) {
|
|
175
|
+
const acct = str(acc.acct || acc.username);
|
|
176
|
+
if (!acct) return '';
|
|
177
|
+
if (acct.includes('@')) return `@${acct}`;
|
|
178
|
+
const host = hostOf(acc.url);
|
|
179
|
+
return host ? `@${acct}@${host}` : `@${acct}`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function mastodonImage(status) {
|
|
183
|
+
const media = (Array.isArray(status.media_attachments) ? status.media_attachments : [])
|
|
184
|
+
.find((m) => isObj(m) && (m.type === 'image' || m.type === 'gifv'));
|
|
185
|
+
if (media) return makeImage(media.preview_url || media.url, media.description);
|
|
186
|
+
if (isObj(status.card)) return makeImage(status.card.image, status.card.title);
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* @param {unknown} statuses a /api/v1/accounts/:id/statuses response
|
|
192
|
+
* @param {{ key: string, service: string, handle: string }} account
|
|
193
|
+
* @returns {{ items: object[], cursor: string|null }} cursor is the last status id
|
|
194
|
+
*/
|
|
195
|
+
export function normalizeMastodon(statuses, account) {
|
|
196
|
+
const list = Array.isArray(statuses) ? statuses : [];
|
|
197
|
+
const items = [];
|
|
198
|
+
for (const outer of list) {
|
|
199
|
+
if (!isObj(outer)) continue;
|
|
200
|
+
const reposted = isObj(outer.reblog);
|
|
201
|
+
const status = reposted ? outer.reblog : outer;
|
|
202
|
+
if (!reposted && outer.in_reply_to_id) continue;
|
|
203
|
+
const acc = isObj(status.account) ? status.account : {};
|
|
204
|
+
const item = makeItem({
|
|
205
|
+
id: `mastodon:${status.uri || status.id}`,
|
|
206
|
+
service: 'mastodon',
|
|
207
|
+
source: account.key,
|
|
208
|
+
url: status.url || status.uri,
|
|
209
|
+
date: isoDate(outer.created_at),
|
|
210
|
+
text: htmlToText(status.content),
|
|
211
|
+
tags: tagList((Array.isArray(status.tags) ? status.tags : []).map((t) => (isObj(t) ? t.name : t))),
|
|
212
|
+
reposted,
|
|
213
|
+
author: makeAuthor({ name: acc.display_name || acc.username, handle: mastodonHandle(acc), avatar: acc.avatar, url: acc.url }),
|
|
214
|
+
image: mastodonImage(status),
|
|
215
|
+
counts: { replies: num(status.replies_count), reposts: num(status.reblogs_count), likes: num(status.favourites_count) },
|
|
216
|
+
});
|
|
217
|
+
if (item) items.push(item);
|
|
218
|
+
}
|
|
219
|
+
const last = list[list.length - 1];
|
|
220
|
+
const cursor = isObj(last) && last.id !== undefined && last.id !== null ? String(last.id) : null;
|
|
221
|
+
return { items, cursor };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/* ----------------------------------------------------------------- GitHub */
|
|
225
|
+
|
|
226
|
+
function releaseItem(release, repo, account) {
|
|
227
|
+
if (!isObj(release) || release.draft || release.prerelease) return null;
|
|
228
|
+
if (release.id === undefined || release.id === null) return null;
|
|
229
|
+
const tag = str(release.tag_name);
|
|
230
|
+
const repoName = str(repo && repo.name);
|
|
231
|
+
const by = isObj(release.author) ? release.author : {};
|
|
232
|
+
const login = str(by.login || account.handle);
|
|
233
|
+
const details = { repo: repoName, tag };
|
|
234
|
+
const repoUrl = safeHttps(repo && repo.html_url);
|
|
235
|
+
if (repoUrl) details.repoUrl = repoUrl;
|
|
236
|
+
details.description = repo && repo.description != null ? str(repo.description) : null;
|
|
237
|
+
details.language = repo && repo.language != null ? str(repo.language) : null;
|
|
238
|
+
return makeItem({
|
|
239
|
+
id: `github:${release.id}`,
|
|
240
|
+
service: 'github',
|
|
241
|
+
source: account.key,
|
|
242
|
+
url: release.html_url,
|
|
243
|
+
date: isoDate(release.published_at),
|
|
244
|
+
text: `Released ${repoName} ${tag}`,
|
|
245
|
+
tags: [],
|
|
246
|
+
reposted: false,
|
|
247
|
+
author: makeAuthor({
|
|
248
|
+
name: login,
|
|
249
|
+
handle: login,
|
|
250
|
+
avatar: by.avatar_url,
|
|
251
|
+
url: by.html_url || (login ? `https://github.com/${encodeURIComponent(login)}` : null),
|
|
252
|
+
}),
|
|
253
|
+
release: details,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @param {unknown} releases a /repos/:owner/:repo/releases response
|
|
259
|
+
* @param {{ name: string, html_url: string, description?: string|null, language?: string|null }} repo
|
|
260
|
+
* @param {{ key: string, service: string, handle: string }} account
|
|
261
|
+
* @returns {object[]} published, non-draft, non-prerelease releases
|
|
262
|
+
*/
|
|
263
|
+
export function normalizeGithubReleases(releases, repo, account) {
|
|
264
|
+
return (Array.isArray(releases) ? releases : [])
|
|
265
|
+
.map((r) => releaseItem(r, repo, account))
|
|
266
|
+
.filter(Boolean);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* @param {unknown} events a /users/:user/events/public response
|
|
271
|
+
* @param {{ key: string, service: string, handle: string }} account
|
|
272
|
+
* @returns {object[]} items for published stable releases, ids matching normalizeGithubReleases
|
|
273
|
+
*/
|
|
274
|
+
export function normalizeGithubEvents(events, account) {
|
|
275
|
+
const items = [];
|
|
276
|
+
for (const ev of Array.isArray(events) ? events : []) {
|
|
277
|
+
if (!isObj(ev) || ev.type !== 'ReleaseEvent' || !isObj(ev.payload)) continue;
|
|
278
|
+
if (ev.payload.action !== 'published' || !isObj(ev.payload.release)) continue;
|
|
279
|
+
const fullName = str(isObj(ev.repo) ? ev.repo.name : '');
|
|
280
|
+
if (!fullName) continue;
|
|
281
|
+
const actor = isObj(ev.actor) ? ev.actor : {};
|
|
282
|
+
const release = isObj(ev.payload.release.author) || !actor.login
|
|
283
|
+
? ev.payload.release
|
|
284
|
+
: { ...ev.payload.release, author: { login: actor.login, avatar_url: actor.avatar_url } };
|
|
285
|
+
const repo = {
|
|
286
|
+
name: fullName.split('/').pop(),
|
|
287
|
+
html_url: `https://github.com/${fullName.split('/').map(encodeURIComponent).join('/')}`,
|
|
288
|
+
description: null,
|
|
289
|
+
language: null,
|
|
290
|
+
};
|
|
291
|
+
const item = releaseItem(release, repo, account);
|
|
292
|
+
if (item) items.push(item);
|
|
293
|
+
}
|
|
294
|
+
return items;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/* ---------------------------------------------------------- filter, merge */
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* @param {{ tags?: string[] }} item
|
|
301
|
+
* @param {string[]|undefined} tags wanted hashtags, '#' optional, any case
|
|
302
|
+
* @returns {boolean} true when no tags are wanted or the item carries one of them
|
|
303
|
+
*/
|
|
304
|
+
export function matchesTags(item, tags) {
|
|
305
|
+
const wanted = tagList(Array.isArray(tags) ? tags : []);
|
|
306
|
+
if (!wanted.length) return true;
|
|
307
|
+
const have = isObj(item) && Array.isArray(item.tags) ? item.tags.map(cleanTag) : [];
|
|
308
|
+
return wanted.some((t) => have.includes(t));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Filter by tags, sort newest first (ties by id), dedupe by id keeping the
|
|
313
|
+
* newest, then take `limit`.
|
|
314
|
+
* @param {object[][]} lists
|
|
315
|
+
* @param {{ tags?: string[], limit?: number }} [options]
|
|
316
|
+
* @returns {object[]}
|
|
317
|
+
*/
|
|
318
|
+
export function mergeItems(lists, { tags, limit } = {}) {
|
|
319
|
+
const all = (Array.isArray(lists) ? lists : [])
|
|
320
|
+
.flatMap((l) => (Array.isArray(l) ? l : []))
|
|
321
|
+
.filter((i) => isObj(i) && safeHttps(i.url) && matchesTags(i, tags));
|
|
322
|
+
all.sort((a, b) => {
|
|
323
|
+
if (a.date !== b.date) return a.date < b.date ? 1 : -1;
|
|
324
|
+
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
325
|
+
});
|
|
326
|
+
const seen = new Set();
|
|
327
|
+
const out = [];
|
|
328
|
+
for (const item of all) {
|
|
329
|
+
if (seen.has(item.id)) continue;
|
|
330
|
+
seen.add(item.id);
|
|
331
|
+
out.push(item);
|
|
332
|
+
}
|
|
333
|
+
return Number.isInteger(limit) && limit > 0 ? out.slice(0, limit) : out;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/* ----------------------------------------------------------------- render */
|
|
337
|
+
|
|
338
|
+
const DATE_FORMAT = new Intl.DateTimeFormat('en', { dateStyle: 'medium', timeZone: 'UTC' });
|
|
339
|
+
|
|
340
|
+
function mediumDate(iso) {
|
|
341
|
+
const d = new Date(iso);
|
|
342
|
+
return Number.isNaN(d.getTime()) ? '' : DATE_FORMAT.format(d);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const COUNT_LABELS = {
|
|
346
|
+
mastodon: ['replies', 'boosts', 'favourites'],
|
|
347
|
+
default: ['replies', 'reposts', 'likes'],
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
function renderCounts(item) {
|
|
351
|
+
const labels = COUNT_LABELS[item.service] || COUNT_LABELS.default;
|
|
352
|
+
const c = item.counts;
|
|
353
|
+
const values = [c.replies, c.reposts, c.likes];
|
|
354
|
+
const glyphs = ['↩', '⟲', '♡'];
|
|
355
|
+
const lis = glyphs.map((g, i) =>
|
|
356
|
+
`<li><span aria-hidden="true">${g}</span> ${num(values[i])}<span class="visually-hidden"> ${labels[i]}</span></li>`);
|
|
357
|
+
return `<ul class="social-feed-counts">${lis.join('')}</ul>`;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* @param {object} item
|
|
362
|
+
* @param {{ hideCounts?: boolean, serviceName?: string }} [options]
|
|
363
|
+
* @returns {string} one `<article data-activity>`, or '' when the item has no https url
|
|
364
|
+
*/
|
|
365
|
+
export function renderEntry(item, { hideCounts = false, serviceName } = {}) {
|
|
366
|
+
if (!isObj(item)) return '';
|
|
367
|
+
const url = safeHttps(item.url);
|
|
368
|
+
if (!url) return '';
|
|
369
|
+
const e = escapeHtml;
|
|
370
|
+
const author = isObj(item.author) ? item.author : {};
|
|
371
|
+
const parts = [`<article data-activity data-id="${e(item.id)}" data-service="${e(item.service)}">`];
|
|
372
|
+
const avatar = safeHttps(author.avatar);
|
|
373
|
+
if (avatar) parts.push(`<img class="social-feed-avatar" src="${e(avatar)}" alt="" width="40" height="40" loading="lazy">`);
|
|
374
|
+
parts.push(`<header class="social-feed-head"><b>${e(author.name || author.handle)}</b> <span class="social-feed-handle">${e(author.handle)}</span> <a class="social-feed-permalink" href="${e(url)}"><time datetime="${e(item.date)}">${e(mediumDate(item.date))}</time></a> <span class="social-feed-source">${e(serviceName || item.service)}</span></header>`);
|
|
375
|
+
if (item.reposted) parts.push('<p class="social-feed-reposted">Reposted</p>');
|
|
376
|
+
// `<br>`, not a raw newline: a whitespace-collapsing minifier keeps it.
|
|
377
|
+
parts.push(`<p class="social-feed-text">${e(item.text).replace(/\r?\n/g, '<br>')}</p>`);
|
|
378
|
+
const image = isObj(item.image) ? safeHttps(item.image.url) : null;
|
|
379
|
+
if (image) parts.push(`<a class="social-feed-image" href="${e(url)}"><img src="${e(image)}" alt="${e(item.image.alt)}" loading="lazy"></a>`);
|
|
380
|
+
if (isObj(item.release)) {
|
|
381
|
+
const r = item.release;
|
|
382
|
+
const repoUrl = safeHttps(r.repoUrl);
|
|
383
|
+
const href = repoUrl ? ` href="${e(repoUrl)}"` : '';
|
|
384
|
+
parts.push(`<div class="social-feed-release"><a${href}>${e(r.repo)}</a> <span>${e(r.description)}</span> <span>${e(r.language)}</span></div>`);
|
|
385
|
+
}
|
|
386
|
+
if (!hideCounts && isObj(item.counts)) parts.push(renderCounts(item));
|
|
387
|
+
parts.push('</article>');
|
|
388
|
+
return parts.join('\n');
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* The three HTML fragments Cook writes into `<social-feed>`.
|
|
393
|
+
* @param {{ items: object[], accounts: Array<{ service: string, profileUrl?: string }>,
|
|
394
|
+
* hideCounts?: boolean, services: Record<string, { name: string }> }} options
|
|
395
|
+
* @returns {{ entries: string, empty: string, more: string }}
|
|
396
|
+
*/
|
|
397
|
+
export function renderFeedParts({ items, accounts, hideCounts = false, services } = {}) {
|
|
398
|
+
const nameOf = (id) => (isObj(services) && isObj(services[id]) && services[id].name) || str(id);
|
|
399
|
+
const list = Array.isArray(items) ? items : [];
|
|
400
|
+
const entries = list
|
|
401
|
+
.map((item) => renderEntry(item, { hideCounts, serviceName: isObj(item) ? nameOf(item.service) : '' }))
|
|
402
|
+
.filter(Boolean)
|
|
403
|
+
.join('\n');
|
|
404
|
+
const empty = entries ? '' : '<p class="social-feed-empty">Nothing yet</p>';
|
|
405
|
+
const links = (Array.isArray(accounts) ? accounts : [])
|
|
406
|
+
.filter(isObj)
|
|
407
|
+
.map((a) => {
|
|
408
|
+
const href = safeHttps(a.profileUrl);
|
|
409
|
+
return href ? `<a href="${escapeHtml(href)}">${escapeHtml(nameOf(a.service))}</a>` : '';
|
|
410
|
+
})
|
|
411
|
+
.filter(Boolean);
|
|
412
|
+
const more = links.length ? `<p class="social-feed-more">More on ${links.join(' · ')}</p>` : '';
|
|
413
|
+
return { entries, empty, more };
|
|
414
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file fetch.js
|
|
3
|
+
* @description Fetch one page of an account's posts from Bluesky, Mastodon or
|
|
4
|
+
* GitHub through an injected `fetch`, and page until enough items pass a tag
|
|
5
|
+
* filter. It imports only `./core.js` and uses no Node built-ins, so Vanilla
|
|
6
|
+
* Breeze copies it verbatim into the browser.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
normalizeBluesky, normalizeMastodon, normalizeGithubReleases, normalizeGithubEvents, matchesTags,
|
|
11
|
+
} from './core.js';
|
|
12
|
+
|
|
13
|
+
const MAX_PAGES = 3;
|
|
14
|
+
const MAX_REPOS = 10;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* GET and parse JSON under one time budget that covers headers and body.
|
|
18
|
+
* Rejects with an Error naming the URL on timeout, non-OK status or bad JSON.
|
|
19
|
+
*/
|
|
20
|
+
async function getJson(url, { fetch: doFetch, timeoutMs = 5000, headers }) {
|
|
21
|
+
const ctrl = new AbortController();
|
|
22
|
+
let timer;
|
|
23
|
+
const timeout = new Promise((_, reject) => {
|
|
24
|
+
timer = setTimeout(() => {
|
|
25
|
+
ctrl.abort();
|
|
26
|
+
reject(new Error(`Timed out after ${timeoutMs} ms: ${url}`));
|
|
27
|
+
}, timeoutMs);
|
|
28
|
+
});
|
|
29
|
+
const work = (async () => {
|
|
30
|
+
let res;
|
|
31
|
+
try {
|
|
32
|
+
res = await doFetch(url, { headers, signal: ctrl.signal });
|
|
33
|
+
} catch (err) {
|
|
34
|
+
throw new Error(`Request failed for ${url}: ${err && err.message ? err.message : err}`);
|
|
35
|
+
}
|
|
36
|
+
if (!res || !res.ok) throw new Error(`HTTP ${res ? res.status : 'error'} for ${url}`);
|
|
37
|
+
try {
|
|
38
|
+
return await res.json();
|
|
39
|
+
} catch {
|
|
40
|
+
throw new Error(`Bad JSON from ${url}`);
|
|
41
|
+
}
|
|
42
|
+
})();
|
|
43
|
+
work.catch(() => {});
|
|
44
|
+
try {
|
|
45
|
+
return await Promise.race([work, timeout]);
|
|
46
|
+
} finally {
|
|
47
|
+
clearTimeout(timer);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const expectShape = (ok, url) => {
|
|
52
|
+
if (!ok) throw new Error(`Unexpected response shape from ${url}`);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
function splitMastodon(handle) {
|
|
56
|
+
const [user, instance] = String(handle).replace(/^@/, '').split('@');
|
|
57
|
+
return { user, instance };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function blueskyPage(account, opts) {
|
|
61
|
+
const actor = account.did || account.handle;
|
|
62
|
+
let url = `https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed?actor=${encodeURIComponent(actor)}&filter=posts_no_replies&limit=30`;
|
|
63
|
+
if (opts.cursor) url += `&cursor=${encodeURIComponent(opts.cursor)}`;
|
|
64
|
+
const body = await getJson(url, opts);
|
|
65
|
+
expectShape(body && typeof body === 'object' && Array.isArray(body.feed), url);
|
|
66
|
+
const { items, cursor } = normalizeBluesky(body, account);
|
|
67
|
+
return { items, cursor, done: !cursor };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function mastodonPage(account, opts) {
|
|
71
|
+
const { user, instance } = splitMastodon(account.handle);
|
|
72
|
+
if (!user || !instance) throw new Error(`Bad Mastodon handle: ${account.handle}`);
|
|
73
|
+
if (!account.mastodonId) {
|
|
74
|
+
const lookupUrl = `https://${instance}/api/v1/accounts/lookup?acct=${encodeURIComponent(user)}`;
|
|
75
|
+
const found = await getJson(lookupUrl, opts);
|
|
76
|
+
expectShape(found && found.id !== undefined && found.id !== null && found.id !== '', lookupUrl);
|
|
77
|
+
account.mastodonId = String(found.id);
|
|
78
|
+
}
|
|
79
|
+
let url = `https://${instance}/api/v1/accounts/${encodeURIComponent(account.mastodonId)}/statuses?exclude_replies=true&limit=40`;
|
|
80
|
+
if (opts.cursor) url += `&max_id=${encodeURIComponent(opts.cursor)}`;
|
|
81
|
+
const list = await getJson(url, opts);
|
|
82
|
+
expectShape(Array.isArray(list), url);
|
|
83
|
+
const { items, cursor } = normalizeMastodon(list, account);
|
|
84
|
+
return { items, cursor, done: !cursor };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function githubPage(account, opts) {
|
|
88
|
+
const user = encodeURIComponent(account.handle);
|
|
89
|
+
const headers = { ...(opts.headers || {}) };
|
|
90
|
+
if (opts.token && opts.mode !== 'browser') headers.Authorization = `Bearer ${opts.token}`;
|
|
91
|
+
const o = { ...opts, headers };
|
|
92
|
+
if (opts.mode === 'browser') {
|
|
93
|
+
const url = `https://api.github.com/users/${user}/events/public?per_page=30`;
|
|
94
|
+
const events = await getJson(url, o);
|
|
95
|
+
expectShape(Array.isArray(events), url);
|
|
96
|
+
return { items: normalizeGithubEvents(events, account), cursor: null, done: true };
|
|
97
|
+
}
|
|
98
|
+
const reposUrl = `https://api.github.com/users/${user}/repos?sort=pushed&per_page=10`;
|
|
99
|
+
const repos = await getJson(reposUrl, o);
|
|
100
|
+
expectShape(Array.isArray(repos), reposUrl);
|
|
101
|
+
const chosen = repos
|
|
102
|
+
.filter((r) => r && typeof r === 'object' && !r.fork && !r.archived && r.name)
|
|
103
|
+
.slice(0, MAX_REPOS);
|
|
104
|
+
const lists = await Promise.all(chosen.map(async (repo) => {
|
|
105
|
+
const url = `https://api.github.com/repos/${user}/${encodeURIComponent(repo.name)}/releases?per_page=5`;
|
|
106
|
+
const releases = await getJson(url, o);
|
|
107
|
+
expectShape(Array.isArray(releases), url);
|
|
108
|
+
return normalizeGithubReleases(releases, repo, account);
|
|
109
|
+
}));
|
|
110
|
+
return { items: lists.flat(), cursor: null, done: true };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @param {{ key: string, service: string, handle: string, did?: string, mastodonId?: string }} account
|
|
115
|
+
* a Mastodon account gets `mastodonId` set after the first lookup
|
|
116
|
+
* @param {{ fetch: Function, cursor?: string|null, timeoutMs?: number, token?: string,
|
|
117
|
+
* mode?: 'build'|'browser', headers?: Record<string,string> }} options
|
|
118
|
+
* @returns {Promise<{ items: object[], cursor: string|null, done: boolean }>}
|
|
119
|
+
*/
|
|
120
|
+
export async function fetchAccountPage(account, { fetch: doFetch, cursor, timeoutMs = 5000, token, mode = 'build', headers } = {}) {
|
|
121
|
+
if (typeof doFetch !== 'function') throw new Error('fetchAccountPage needs a fetch function');
|
|
122
|
+
const opts = { fetch: doFetch, cursor, timeoutMs, token, mode, headers };
|
|
123
|
+
switch (account.service) {
|
|
124
|
+
case 'bluesky': return blueskyPage(account, opts);
|
|
125
|
+
case 'mastodon': return mastodonPage(account, opts);
|
|
126
|
+
case 'github': return githubPage(account, opts);
|
|
127
|
+
default: throw new Error(`No feed for service: ${account.service}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Page until `want` items pass the tag filter, at most 3 pages.
|
|
133
|
+
* Returns every item fetched (unfiltered); the caller filters and merges.
|
|
134
|
+
* @returns {Promise<{ items: object[], cursor: string|null, done: boolean }>}
|
|
135
|
+
*/
|
|
136
|
+
export async function collectItems(account, { fetch: doFetch, tags, want = 12, token, mode = 'build', cursor = null, timeoutMs, headers } = {}) {
|
|
137
|
+
const items = [];
|
|
138
|
+
let passing = 0;
|
|
139
|
+
let next = cursor;
|
|
140
|
+
let done = false;
|
|
141
|
+
for (let page = 0; page < MAX_PAGES && !done; page++) {
|
|
142
|
+
const res = await fetchAccountPage(account, { fetch: doFetch, cursor: next, token, mode, timeoutMs, headers });
|
|
143
|
+
items.push(...res.items);
|
|
144
|
+
passing += res.items.filter((i) => matchesTags(i, tags)).length;
|
|
145
|
+
next = res.cursor;
|
|
146
|
+
done = res.done;
|
|
147
|
+
if (passing >= want) break;
|
|
148
|
+
}
|
|
149
|
+
return { items, cursor: next, done };
|
|
150
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file snapshots.js
|
|
3
|
+
* @description Per-account feed snapshots at data/social-feeds/<service>-<slug>.json.
|
|
4
|
+
* Node only.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
8
|
+
import { join, dirname } from 'node:path';
|
|
9
|
+
|
|
10
|
+
const MAX_ITEMS = 100;
|
|
11
|
+
|
|
12
|
+
const slugOf = (handle) => String(handle).toLowerCase().replace(/[^a-z0-9.]+/g, '-').replace(/^-+|-+$/g, '');
|
|
13
|
+
|
|
14
|
+
/** @returns {string} <rootDir>/data/social-feeds/<service>-<slug>.json */
|
|
15
|
+
export function snapshotPath(rootDir, account) {
|
|
16
|
+
return join(rootDir, 'data', 'social-feeds', `${account.service}-${slugOf(account.handle)}.json`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @returns {object[]|null} the snapshot's items, or null when missing or unreadable */
|
|
20
|
+
export function readSnapshot(rootDir, account) {
|
|
21
|
+
try {
|
|
22
|
+
const data = JSON.parse(readFileSync(snapshotPath(rootDir, account), 'utf8'));
|
|
23
|
+
return data && Array.isArray(data.items) ? data.items : null;
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Write at most 100 items. Never throws; warns instead. */
|
|
30
|
+
export function writeSnapshot(rootDir, account, items) {
|
|
31
|
+
try {
|
|
32
|
+
const file = snapshotPath(rootDir, account);
|
|
33
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
34
|
+
const body = { fetchedAt: new Date().toISOString(), items: items.slice(0, MAX_ITEMS) };
|
|
35
|
+
writeFileSync(file, `${JSON.stringify(body, null, 2)}\n`);
|
|
36
|
+
} catch (err) {
|
|
37
|
+
console.warn(`[social-feed] Could not write snapshot for ${account && account.key}: ${err.message}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
package/scripts/social/index.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file index.js
|
|
3
3
|
* @description Public entry for `cook-ssg/social`: the service catalog, account
|
|
4
|
-
* parsing
|
|
4
|
+
* parsing, the profile-links renderer and the `<social-feed>` helpers. No
|
|
5
|
+
* dependencies; everything is pure except `readSnapshot` and `snapshotPath`,
|
|
6
|
+
* which use Node's fs and path.
|
|
5
7
|
*/
|
|
6
8
|
|
|
7
9
|
export { SERVICES } from './catalog.js';
|
|
8
10
|
export { parseHandle, profileUrl, normalizeAccounts } from './accounts.js';
|
|
9
11
|
export { renderSocialLinks } from './render.js';
|
|
10
12
|
export { renderOpml, renderAbout, renderVcard } from './socialspec.js';
|
|
13
|
+
export { renderEntry, renderFeedParts, mergeItems, matchesTags } from './feeds/core.js';
|
|
14
|
+
export { readSnapshot, snapshotPath } from './feeds/snapshots.js';
|
|
15
|
+
export { renderSocialFeed, feedAccounts, parseFeedAttrs } from './feeds/block.js';
|
|
@@ -109,7 +109,7 @@ function fold(line) {
|
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
111
|
* vCard 4.0 for contact.vcf.
|
|
112
|
-
* @param {{ name?: string, email?: string, url?: string, org?: string, title?: string }} contact
|
|
112
|
+
* @param {{ name?: string, email?: string|string[], url?: string, org?: string, title?: string }} contact
|
|
113
113
|
* @param {{ siteUrl?: string }} [options] `URL` defaults to siteUrl
|
|
114
114
|
* @returns {{ text: string|null, warning?: string }}
|
|
115
115
|
*/
|
|
@@ -121,7 +121,8 @@ export function renderVcard(contact, { siteUrl } = {}) {
|
|
|
121
121
|
const lines = ['BEGIN:VCARD', 'VERSION:4.0', `FN:${escapeVcardText(c.name.trim())}`];
|
|
122
122
|
if (nonEmpty(c.org)) lines.push(`ORG:${escapeVcardText(c.org.trim())}`);
|
|
123
123
|
if (nonEmpty(c.title)) lines.push(`TITLE:${escapeVcardText(c.title.trim())}`);
|
|
124
|
-
|
|
124
|
+
const emails = (Array.isArray(c.email) ? c.email : [c.email]).filter(nonEmpty);
|
|
125
|
+
emails.forEach((e, i) => lines.push(`${i === 0 ? 'EMAIL;PREF=1' : 'EMAIL'}:${escapeVcardText(e.trim())}`));
|
|
125
126
|
const url = nonEmpty(c.url) ? c.url : siteUrl;
|
|
126
127
|
// URI value type: not text-escaped (RFC 6350); only strip whitespace so it cannot break a line.
|
|
127
128
|
if (nonEmpty(url)) lines.push(`URL:${String(url).replace(/\s+/g, '')}`);
|
|
@@ -52,9 +52,8 @@ function getPrograms() {
|
|
|
52
52
|
// config/social.json (written by Vanilla Press): the site's social accounts; main.js `social` wins when defined
|
|
53
53
|
const socialJsonPath = `${cwd}/config/social.json`;
|
|
54
54
|
|
|
55
|
-
/**
|
|
56
|
-
function
|
|
57
|
-
if (userConfig.social !== undefined) return userConfig.social;
|
|
55
|
+
/** config/social.json parsed, or undefined (missing or invalid, with a warning). */
|
|
56
|
+
function readSocialJson() {
|
|
58
57
|
if (!fs.existsSync(socialJsonPath)) return undefined;
|
|
59
58
|
try {
|
|
60
59
|
return JSON.parse(fs.readFileSync(socialJsonPath, 'utf8'));
|
|
@@ -64,6 +63,21 @@ function getSocial() {
|
|
|
64
63
|
}
|
|
65
64
|
}
|
|
66
65
|
|
|
66
|
+
/** The last read of config/social.json, as `{ value }` (null before the first). */
|
|
67
|
+
let socialRead = null;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The social config.
|
|
71
|
+
* @param {{ fresh?: boolean }} [opts] `fresh` re-reads config/social.json; the
|
|
72
|
+
* build does so once at its start (socialData), and later readers in that
|
|
73
|
+
* build reuse the result, so a parse error is reported once per build
|
|
74
|
+
*/
|
|
75
|
+
function getSocial({ fresh = false } = {}) {
|
|
76
|
+
if (userConfig.social !== undefined) return userConfig.social;
|
|
77
|
+
if (fresh || !socialRead) socialRead = { value: readSocialJson() };
|
|
78
|
+
return socialRead.value;
|
|
79
|
+
}
|
|
80
|
+
|
|
67
81
|
|
|
68
82
|
// DEFINE DEFAULT CONFIG
|
|
69
83
|
// -----------------------------
|