@profullstack/waitlist 0.2.1 → 0.4.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 +30 -0
- package/bin/waitlist.js +2 -1
- package/package.json +7 -3
- package/src/build.js +51 -17
- package/src/index.js +33 -2
- package/src/plugins.js +129 -0
package/README.md
CHANGED
|
@@ -89,3 +89,33 @@ a list per domain in the owner's dashboard, `waitlist.signup` domain webhooks):
|
|
|
89
89
|
Both send `Access-Control-Allow-Origin: *`.
|
|
90
90
|
|
|
91
91
|
MIT
|
|
92
|
+
|
|
93
|
+
## A/B testing the look (0.3)
|
|
94
|
+
|
|
95
|
+
Every built page runs one experiment by default, `waitlist-look-v1`: the page's own themed
|
|
96
|
+
look (control) against the high converting looks from `@profullstack/css` (`clarity`,
|
|
97
|
+
`split`, `midnight`, `urgency`). A tiny script in `<head>` gives each visitor a sticky id and
|
|
98
|
+
picks their arm before first paint, with the same hash as `@profullstack/abtest`, so the
|
|
99
|
+
page and [CrawlProof](https://crawlproof.com/dashboard/experiments/waitlist-look-v1) agree.
|
|
100
|
+
The page reports one exposure a day; a signup is the conversion. Both go through the
|
|
101
|
+
waitlist API, which holds the CrawlProof token. Stop the experiment with a winner in
|
|
102
|
+
CrawlProof and every page switches to it. Turn it off per site with `"ab": false`.
|
|
103
|
+
|
|
104
|
+
## Plugins (0.4)
|
|
105
|
+
|
|
106
|
+
Every page showcases all plugins by default. Pick per site in `sites.json`:
|
|
107
|
+
`"plugins": ["form", "board"]` keeps only those, `"plugins": { "faq": false }` drops some.
|
|
108
|
+
|
|
109
|
+
| plugin | adds |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| `form` | the waitlist signup |
|
|
112
|
+
| `stats` | who's waiting: a count and a signup chart |
|
|
113
|
+
| `ab` | the look A/B test (see above) |
|
|
114
|
+
| `faq` | visible questions and answers (the FAQPage JSON-LD stays) |
|
|
115
|
+
| `resources` | useful reading from our own search: `pit.world/api/search?q=<keywords>` at build time, cached in `--cache` (default `.waitlist-cache`), shown with summaries, saved as `resources.json` |
|
|
116
|
+
| `board` | WWWThreads-style posts and replies ([@profullstack/comments](https://www.npmjs.com/package/@profullstack/comments)); reading is open, posting needs **Sign in with CoinPay**; RSS of the posts |
|
|
117
|
+
| `share` | a Share button on the board, also behind CoinPay sign-in |
|
|
118
|
+
| `feed` | `feed.xml` (RSS) and `feed.json` (JSON Feed) of the page and its resources, for syndication |
|
|
119
|
+
|
|
120
|
+
`keywords` (also the `<meta name="keywords">`) default to the name's labels plus the tagline's
|
|
121
|
+
meaningful words; set `keywords` or `resourcesQuery` per site to steer the resources.
|
package/bin/waitlist.js
CHANGED
|
@@ -17,6 +17,7 @@ flags:
|
|
|
17
17
|
--description <t> meta description (default: derived from tagline + points)
|
|
18
18
|
--theme dark|light
|
|
19
19
|
--look <look> neon|matrix|terminal|amber|synthwave|geocities|vaporwave (default: picked from the name)
|
|
20
|
+
--cache <dir> where pit.world search results are cached (default .waitlist-cache)
|
|
20
21
|
--no-footers skip the webring + pit.world footers (offline builds)
|
|
21
22
|
--no-images skip the PNG renders (og.png, icons)
|
|
22
23
|
|
|
@@ -42,7 +43,7 @@ for (let i = 0; i < argv.length; i++) {
|
|
|
42
43
|
} else pos.push(a);
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
const opts = { footers: flags.footers !== false, images: flags.images !== false };
|
|
46
|
+
const opts = { footers: flags.footers !== false, images: flags.images !== false, cacheDir: flags.cache || '.waitlist-cache' };
|
|
46
47
|
const pick = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
|
|
47
48
|
if (cmd === 'build') {
|
|
48
49
|
const s = await buildSite(pick({ name: flags.name, title: flags.title, tagline: flags.tagline, about: flags.about, points: flags.point, api: flags.api, host: flags.host, url: flags.url, image: flags.image, description: flags.description, theme: flags.theme, look: flags.look }), flags.out || `./${flags.name}`, opts);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@profullstack/waitlist",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Email waitlist for any launch page: a <waitlist-form> web component, a live signup chart, and a static-site builder on @profullstack/css + @profullstack/charts.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,8 +46,12 @@
|
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@profullstack/charts": "^0.3.0",
|
|
49
|
-
"@profullstack/css": "^0.
|
|
49
|
+
"@profullstack/css": "^0.6.0",
|
|
50
50
|
"@profullstack/footer": "^0.7.3",
|
|
51
|
-
"@resvg/resvg-js": "^2.6.2"
|
|
51
|
+
"@resvg/resvg-js": "^2.6.2",
|
|
52
|
+
"@profullstack/comments": "^0.1.1"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@profullstack/abtest": "^0.1.0"
|
|
52
56
|
}
|
|
53
57
|
}
|
package/src/build.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* for one name, ready for `c0mpute hosting publish <dir> --name <site>`.
|
|
4
4
|
*/
|
|
5
5
|
import { mkdir, writeFile, copyFile, readFile } from 'node:fs/promises';
|
|
6
|
+
import { pluginSet, keywordsFor, runPlugins, baseSlot } from './plugins.js';
|
|
6
7
|
import { createRequire } from 'node:module';
|
|
7
8
|
import { dirname, join } from 'node:path';
|
|
8
9
|
import { fileURLToPath } from 'node:url';
|
|
@@ -19,8 +20,36 @@ const here = dirname(fileURLToPath(import.meta.url));
|
|
|
19
20
|
/** The crawlproof project every Moshpit landing and parked page reports to. */
|
|
20
21
|
export const MOSHPIT_STATS_SITE = '2cbffe9d-1b6e-43f0-85f3-3cadee132048';
|
|
21
22
|
|
|
22
|
-
/**
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* The default A/B test: the page's themed look (control) against the high
|
|
25
|
+
* converting looks from @profullstack/css, recorded in CrawlProof through the
|
|
26
|
+
* waitlist API (moshcoding.com /api/waitlist/ab). Pass `ab: false` to turn it off.
|
|
27
|
+
*/
|
|
28
|
+
export const DEFAULT_AB = { experiment: 'waitlist-look-v1', arms: ['themed', 'clarity', 'split', 'midnight', 'urgency'] };
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Runs in <head> before first paint: a sticky visitor id, the arm from the same
|
|
32
|
+
* cyrb53 hash as @profullstack/abtest's assignArm (equal weights), or a declared
|
|
33
|
+
* winner. Sets data-look and window.__wlab. Tiny and dependency-free on purpose.
|
|
34
|
+
*/
|
|
35
|
+
export function abHeadScript(ab, themed) {
|
|
36
|
+
const cfg = JSON.stringify({ e: ab.experiment, a: ab.arms, t: themed }).replace(/</g, '\\u003c');
|
|
37
|
+
return `<script>(function(c){try{var L=localStorage,u=L.getItem("wl-unit");if(!u){u=Array.from(crypto.getRandomValues(new Uint8Array(12)),function(b){return(b%36).toString(36)}).join("");L.setItem("wl-unit",u)}
|
|
38
|
+
function h(s){var a=3735928559,b=1103547991;for(var i=0;i<s.length;i++){var k=s.charCodeAt(i);a=Math.imul(a^k,2654435761);b=Math.imul(b^k,1597334677)}a=Math.imul(a^a>>>16,2246822507);a^=Math.imul(b^b>>>13,3266489909);b=Math.imul(b^b>>>16,2246822507);b^=Math.imul(a^a>>>13,3266489909);return 4294967296*(2097151&b)+(a>>>0)}
|
|
39
|
+
var w=L.getItem("wl-winner:"+c.e),arm=w&&c.a.indexOf(w)>=0?w:c.a[Math.floor(h(c.e+"\\0"+u)/Math.pow(2,53)*c.a.length)],d=document.documentElement;
|
|
40
|
+
d.dataset.look=arm==="themed"?c.t:arm;d.dataset.abArm=arm;window.__wlab={experiment:c.e,unit:u,arm:arm,themed:c.t,winner:!!w}}catch(x){}})(${cfg});</script>`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Every look @profullstack/css/looks ships (0.6). */
|
|
44
|
+
export const LOOKS = [
|
|
45
|
+
'neon', 'matrix', 'terminal', 'amber', 'synthwave', 'geocities', 'vaporwave',
|
|
46
|
+
'prodigy', 'aol', 'compuserve', 'bbs', 'minitel',
|
|
47
|
+
'editorial', 'swiss', 'brutalist', 'glass', 'bento',
|
|
48
|
+
'clarity', 'split', 'midnight', 'urgency',
|
|
49
|
+
'whitecube', 'darkroom', 'polaroid', 'storefront', 'boutique', 'market',
|
|
50
|
+
];
|
|
51
|
+
/** Looks a name can get by default (its themed look); the high converting ones belong to the A/B test. */
|
|
52
|
+
const THEMED = ['neon', 'matrix', 'terminal', 'amber', 'synthwave', 'geocities', 'vaporwave', 'prodigy', 'aol', 'compuserve', 'bbs', 'minitel', 'editorial', 'swiss', 'brutalist', 'glass', 'bento'];
|
|
24
53
|
|
|
25
54
|
/**
|
|
26
55
|
* A look that fits the name when none is given: newsgroup hierarchies get their
|
|
@@ -29,14 +58,14 @@ export const LOOKS = ['neon', 'matrix', 'terminal', 'amber', 'synthwave', 'geoci
|
|
|
29
58
|
*/
|
|
30
59
|
export function lookFor(name) {
|
|
31
60
|
const first = name.split('.')[0];
|
|
32
|
-
const byPrefix = { comp: 'terminal', news: '
|
|
61
|
+
const byPrefix = { comp: 'terminal', news: 'compuserve', alt: 'geocities', sci: 'matrix', rec: 'synthwave', soc: 'aol', talk: 'bbs', misc: 'prodigy' };
|
|
33
62
|
if (byPrefix[first]) return byPrefix[first];
|
|
34
63
|
const ending = name.split('.').pop();
|
|
35
|
-
const byEnding = { cli: 'terminal', pit: 'terminal', dns: 'terminal', v6: '
|
|
64
|
+
const byEnding = { cli: 'terminal', pit: 'terminal', dns: 'terminal', v6: 'amber', onions: 'matrix', agent: 'matrix', desktop: 'vaporwave', politics: 'editorial', lobby: 'swiss', whatever: 'geocities', yeah: 'synthwave', rank: 'glass', station: 'minitel', indie: 'bento', gambling: 'brutalist' };
|
|
36
65
|
if (byEnding[ending]) return byEnding[ending];
|
|
37
66
|
let h = 0;
|
|
38
67
|
for (const ch of name) h = (h * 31 + ch.charCodeAt(0)) >>> 0;
|
|
39
|
-
return
|
|
68
|
+
return THEMED[h % THEMED.length];
|
|
40
69
|
}
|
|
41
70
|
|
|
42
71
|
/** A Moshpit name (label.ending, no dot-TLD of the old internet) -> its pit.world relay host. */
|
|
@@ -79,6 +108,8 @@ export function normalize(spec) {
|
|
|
79
108
|
look: LOOKS.includes(spec.look) ? spec.look : lookFor(name),
|
|
80
109
|
marquee: spec.marquee || `Welcome to ${name}! You are visitor #000${(name.length * 7919) % 9000 + 1000}. Sign the waitlist below!`,
|
|
81
110
|
crawlproof: spec.crawlproof === undefined ? (moshpit ? MOSHPIT_STATS_SITE : null) : spec.crawlproof,
|
|
111
|
+
plugins: pluginSet(spec.plugins),
|
|
112
|
+
ab: spec.ab === false ? null : { ...DEFAULT_AB, ...(spec.ab || {}) },
|
|
82
113
|
lang: spec.lang || 'en',
|
|
83
114
|
locale: spec.locale || 'en_US',
|
|
84
115
|
siteName: spec.siteName || spec.title || name,
|
|
@@ -98,12 +129,15 @@ export function normalize(spec) {
|
|
|
98
129
|
s.description = spec.description || d.description;
|
|
99
130
|
s.summary = spec.summary || d.summary;
|
|
100
131
|
s.imageAlt = spec.imageAlt || d.imageAlt;
|
|
132
|
+
if (!s.plugins.has('ab')) s.ab = null;
|
|
133
|
+
if (!s.keywords.length) s.keywords = keywordsFor(s);
|
|
134
|
+
s.resourcesQuery = spec.resourcesQuery || '';
|
|
101
135
|
s.faq = Array.isArray(spec.faq) && spec.faq.length ? spec.faq.map((f) => ({ q: f.q ?? f.question, a: f.a ?? f.answer })) : d.faq;
|
|
102
136
|
return s;
|
|
103
137
|
}
|
|
104
138
|
|
|
105
139
|
/** The page HTML. `footers` is pre-rendered HTML (webring row, pit.world footer) placed last. */
|
|
106
|
-
export function pageHtml(s, { footers = '', noindex = false } = {}) {
|
|
140
|
+
export function pageHtml(s, { footers = '', noindex = false, slot = baseSlot(s) } = {}) {
|
|
107
141
|
const points = s.points.length
|
|
108
142
|
? `<section class="stack" aria-labelledby="what"><h2 id="what">What's coming</h2><ul class="waitlist-points">${s.points.map((p) => `<li>${esc(p)}</li>`).join('')}</ul></section>`
|
|
109
143
|
: '';
|
|
@@ -113,12 +147,14 @@ export function pageHtml(s, { footers = '', noindex = false } = {}) {
|
|
|
113
147
|
<head>
|
|
114
148
|
<meta charset="utf-8">
|
|
115
149
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
150
|
+
${s.ab ? abHeadScript(s.ab, s.look) : ''}
|
|
116
151
|
${headTags(s, { noindex })}
|
|
117
152
|
<link rel="stylesheet" href="assets/profullstack.css">
|
|
118
153
|
<link rel="stylesheet" href="assets/looks.css">
|
|
119
154
|
<link rel="stylesheet" href="assets/charts.css">
|
|
120
155
|
<link rel="stylesheet" href="assets/waitlist.css">
|
|
121
156
|
${jsonLdTag(s)}
|
|
157
|
+
${slot.head.join('\n')}
|
|
122
158
|
</head>
|
|
123
159
|
<body class="waitlist-page">
|
|
124
160
|
<div data-look-marquee aria-hidden="true"><span>${esc(s.marquee)}</span></div>
|
|
@@ -129,24 +165,21 @@ ${jsonLdTag(s)}
|
|
|
129
165
|
<p class="waitlist-tagline">${esc(s.tagline)}</p>
|
|
130
166
|
<p class="waitlist-answer">${esc(s.summary)}</p>
|
|
131
167
|
${about}
|
|
132
|
-
|
|
133
|
-
<form class="stack" method="post" action="${esc(s.api)}/api/waitlist"><label for="wl-email">Email</label>
|
|
134
|
-
<div class="field-group"><input id="wl-email" name="email" type="email" autocomplete="email" required><button type="submit">${esc(s.button)}</button></div></form>
|
|
135
|
-
</waitlist-form>
|
|
168
|
+
${slot.hero.join('\n')}
|
|
136
169
|
</header>
|
|
137
170
|
<div class="grid" data-columns="2">
|
|
138
171
|
${points ? `<div class="card">${points}</div>` : ''}
|
|
139
|
-
|
|
140
|
-
<waitlist-stats dn="${esc(s.dn)}" api="${esc(s.api)}" days="30"></waitlist-stats>
|
|
141
|
-
</section>
|
|
172
|
+
${slot.cards.join('\n')}
|
|
142
173
|
</div>
|
|
143
|
-
${faqHtml(s)}
|
|
174
|
+
${s.plugins.has('faq') ? faqHtml(s) : ''}
|
|
175
|
+
${slot.sections.join('\n')}
|
|
144
176
|
<p class="waitlist-meta">${s.moshpit ? `Also at <a href="${esc(s.nativeUrl)}">${esc(s.nativeUrl.replace(/\/$/, ''))}</a> on the Moshpit. ` : ''}One email when it opens, nothing else. Hosted on <a href="https://c0mpute.com/hosting">c0mpute</a>.</p>
|
|
145
177
|
</main>
|
|
146
178
|
<footer class="site-footer container"><p class="waitlist-meta">${esc(s.title)} · <a href="llms.txt">llms.txt</a>${s.moshpit ? ' · <a href="https://pit.world/directory">pit.world directory</a>' : ''}</p></footer>
|
|
147
179
|
${footers}
|
|
148
180
|
<script src="assets/charts.global.js"></script>
|
|
149
181
|
<script type="module" src="assets/waitlist.js"></script>
|
|
182
|
+
${slot.scripts.join('\n')}
|
|
150
183
|
${s.crawlproof ? `<script defer data-site="${esc(s.crawlproof)}" src="https://crawlproof.com/stats.js"></script>` : ''}
|
|
151
184
|
</body>
|
|
152
185
|
</html>
|
|
@@ -182,7 +215,7 @@ export async function renderFooters(s, { fetchImpl = globalThis.fetch } = {}) {
|
|
|
182
215
|
* Write the site for one spec into `out`. Returns the normalized spec.
|
|
183
216
|
* `images: false` skips the PNG renders (og.png, icons), e.g. where @resvg/resvg-js cannot load.
|
|
184
217
|
*/
|
|
185
|
-
export async function buildSite(spec, out, { footers = true, images = true, fetchImpl } = {}) {
|
|
218
|
+
export async function buildSite(spec, out, { footers = true, images = true, fetchImpl, cacheDir = null } = {}) {
|
|
186
219
|
const s = normalize(spec);
|
|
187
220
|
await mkdir(join(out, 'assets'), { recursive: true });
|
|
188
221
|
await mkdir(join(out, '.well-known'), { recursive: true });
|
|
@@ -203,8 +236,9 @@ export async function buildSite(spec, out, { footers = true, images = true, fetc
|
|
|
203
236
|
await writeFile(join(out, 'assets', 'icon-180.png'), await svgToPng(icon, { width: 180 }));
|
|
204
237
|
}
|
|
205
238
|
const foot = footers ? await renderFooters(s, { fetchImpl }) : '';
|
|
206
|
-
await
|
|
207
|
-
await writeFile(join(out, '
|
|
239
|
+
const { slot } = await runPlugins(s, out, { fetchImpl, cacheDir });
|
|
240
|
+
await writeFile(join(out, 'index.html'), pageHtml(s, { footers: foot, slot }));
|
|
241
|
+
await writeFile(join(out, '404.html'), pageHtml(s, { footers: foot, noindex: true, slot }));
|
|
208
242
|
await writeFile(join(out, 'robots.txt'), robotsTxt(s));
|
|
209
243
|
await writeFile(join(out, 'sitemap.xml'), sitemapXml(s));
|
|
210
244
|
await writeFile(join(out, 'llms.txt'), llmsTxt(s));
|
package/src/index.js
CHANGED
|
@@ -42,14 +42,44 @@ export function messageFor(res, email) {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/** POST one signup. Resolves to the API body plus `status`; never throws on HTTP errors. */
|
|
45
|
-
|
|
45
|
+
/** The page's A/B context set by the head script (window.__wlab), or null. */
|
|
46
|
+
export function abContext() {
|
|
47
|
+
const a = globalThis.__wlab;
|
|
48
|
+
return a && a.experiment && a.unit ? { experiment: a.experiment, unit: a.unit } : null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Report that this visitor saw their arm (once per day per experiment), and adopt a
|
|
53
|
+
* winner when the experiment has been stopped with one.
|
|
54
|
+
*/
|
|
55
|
+
export async function reportExposure({ api = DEFAULT_API, dn, fetchImpl = globalThis.fetch, storage = globalThis.localStorage } = {}) {
|
|
56
|
+
const a = globalThis.__wlab;
|
|
57
|
+
if (!a || a.winner) return null;
|
|
58
|
+
try {
|
|
59
|
+
const st = await fetchImpl(new URL(`/api/waitlist/ab?experiment=${encodeURIComponent(a.experiment)}`, api).href).then((r) => (r.ok ? r.json() : null));
|
|
60
|
+
if (st && st.status === 'stopped' && st.winner) {
|
|
61
|
+
storage?.setItem(`wl-winner:${a.experiment}`, st.winner);
|
|
62
|
+
if (globalThis.document) document.documentElement.dataset.look = st.winner === 'themed' ? a.themed : st.winner;
|
|
63
|
+
return { winner: st.winner };
|
|
64
|
+
}
|
|
65
|
+
const key = `wl-exposed:${a.experiment}:${new Date().toISOString().slice(0, 10)}`;
|
|
66
|
+
if (storage?.getItem(key)) return { already: true };
|
|
67
|
+
await fetchImpl(new URL('/api/waitlist/ab', api).href, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ experiment: a.experiment, unit: a.unit, dn }) });
|
|
68
|
+
storage?.setItem(key, '1');
|
|
69
|
+
return { exposed: a.arm };
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function join({ email, dn, ref = null, api = DEFAULT_API, fetchImpl = globalThis.fetch, ab = abContext() }) {
|
|
46
76
|
const clean = cleanEmail(email);
|
|
47
77
|
if (!clean) return { ok: false, status: 400, error: "That doesn't look like an email." };
|
|
48
78
|
try {
|
|
49
79
|
const res = await fetchImpl(new URL('/api/waitlist', api).href, {
|
|
50
80
|
method: 'POST',
|
|
51
81
|
headers: { 'content-type': 'application/json' },
|
|
52
|
-
body: JSON.stringify({ email: clean, dn, ref }),
|
|
82
|
+
body: JSON.stringify(ab ? { email: clean, dn, ref, ab } : { email: clean, dn, ref }),
|
|
53
83
|
});
|
|
54
84
|
const body = await res.json().catch(() => ({}));
|
|
55
85
|
return { ...body, ok: Boolean(res.ok && body.ok), status: res.status };
|
|
@@ -79,6 +109,7 @@ export class WaitlistForm extends HTMLElementBase {
|
|
|
79
109
|
connectedCallback() {
|
|
80
110
|
if (this.dataset.ready) return;
|
|
81
111
|
this.dataset.ready = '1';
|
|
112
|
+
reportExposure({ api: this.getAttribute('api') || DEFAULT_API, dn: this.getAttribute('dn') || location.hostname });
|
|
82
113
|
const id = `wl-${Math.random().toString(36).slice(2, 8)}`;
|
|
83
114
|
this.innerHTML = `
|
|
84
115
|
<form class="stack waitlist-form" novalidate>
|
package/src/plugins.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waitlist plugins: mix and match per site with `plugins` in sites.json. Every
|
|
3
|
+
* plugin is on unless the site says otherwise:
|
|
4
|
+
*
|
|
5
|
+
* "plugins": ["form", "board"] only these
|
|
6
|
+
* "plugins": { "faq": false } all but these
|
|
7
|
+
*
|
|
8
|
+
* A plugin adds HTML to named slots of the page and may write files:
|
|
9
|
+
* head, hero, cards, sections, scripts.
|
|
10
|
+
*/
|
|
11
|
+
import { mkdir, readFile, writeFile, copyFile } from 'node:fs/promises';
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
13
|
+
import { createHash } from 'node:crypto';
|
|
14
|
+
import { dirname, join } from 'node:path';
|
|
15
|
+
import { esc } from './seo.js';
|
|
16
|
+
|
|
17
|
+
const require = createRequire(import.meta.url);
|
|
18
|
+
|
|
19
|
+
export const PLUGINS = ['form', 'stats', 'ab', 'faq', 'resources', 'board', 'share', 'feed'];
|
|
20
|
+
|
|
21
|
+
/** Resolve a site's `plugins` setting to a Set (default: all of them). */
|
|
22
|
+
export function pluginSet(setting) {
|
|
23
|
+
if (Array.isArray(setting)) return new Set(setting.filter((p) => PLUGINS.includes(p)));
|
|
24
|
+
const on = new Set(PLUGINS);
|
|
25
|
+
if (setting && typeof setting === 'object') for (const [k, v] of Object.entries(setting)) if (v === false) on.delete(k);
|
|
26
|
+
return on;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const STOP = new Set('a an and are as at be by for from has have in into is it its of on or that the this to was we with you your our not yet will one all any get it\'s now new site'.split(' '));
|
|
30
|
+
|
|
31
|
+
/** Keywords for a site: its own list, else the name's labels and the tagline's meaningful words. */
|
|
32
|
+
export function keywordsFor(s) {
|
|
33
|
+
if (s.keywords?.length) return s.keywords.slice(0, 10);
|
|
34
|
+
const words = [...s.name.split('.'), ...String(s.tagline).toLowerCase().split(/[^a-z0-9+#-]+/)]
|
|
35
|
+
.map((w) => w.trim()).filter((w) => w.length > 2 && !STOP.has(w));
|
|
36
|
+
return [...new Set(words)].slice(0, 8);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Resources from our own search (pit.world /api/search), fetched once at build and
|
|
41
|
+
* cached on disk by query so rebuilds do not search again. Never throws.
|
|
42
|
+
*/
|
|
43
|
+
export async function fetchResources(s, { fetchImpl = globalThis.fetch, cacheDir = null, limit = 8, endpoint = 'https://pit.world/api/search' } = {}) {
|
|
44
|
+
const q = s.resourcesQuery || keywordsFor(s).slice(0, 4).join(' ');
|
|
45
|
+
if (!q) return { query: '', results: [] };
|
|
46
|
+
const key = createHash('sha256').update(q).digest('hex').slice(0, 16);
|
|
47
|
+
const cacheFile = cacheDir ? join(cacheDir, `${key}.json`) : null;
|
|
48
|
+
if (cacheFile) {
|
|
49
|
+
try { return JSON.parse(await readFile(cacheFile, 'utf8')); } catch { /* not cached */ }
|
|
50
|
+
}
|
|
51
|
+
let results = [];
|
|
52
|
+
try {
|
|
53
|
+
const r = await fetchImpl(`${endpoint}?q=${encodeURIComponent(q)}`, { headers: { accept: 'application/json', 'user-agent': '@profullstack/waitlist (+https://github.com/profullstack/waitlist)' }, signal: AbortSignal.timeout(20000) });
|
|
54
|
+
const body = r.ok ? await r.json() : {};
|
|
55
|
+
const seen = new Set();
|
|
56
|
+
results = [...(body.featured || []), ...(body.results || [])]
|
|
57
|
+
.filter((x) => x && /^https?:\/\//.test(x.url) && !seen.has(x.url) && seen.add(x.url))
|
|
58
|
+
.slice(0, limit)
|
|
59
|
+
.map((x) => ({ url: x.url, title: String(x.title || x.url).slice(0, 140), summary: String(x.description || x.snippet || '').replace(/\s+/g, ' ').trim().slice(0, 300), host: new URL(x.url).hostname.replace(/^www\./, '') }));
|
|
60
|
+
} catch {
|
|
61
|
+
results = [];
|
|
62
|
+
}
|
|
63
|
+
const out = { query: q, fetchedAt: new Date().toISOString(), results };
|
|
64
|
+
if (cacheFile && results.length) { await mkdir(cacheDir, { recursive: true }); await writeFile(cacheFile, JSON.stringify(out, null, 2)); }
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function resourcesHtml(s, res) {
|
|
69
|
+
if (!res?.results?.length) return '';
|
|
70
|
+
return `<section class="stack" aria-labelledby="resources"><h2 id="resources">Resources</h2>
|
|
71
|
+
<p class="muted">Useful reading on ${esc(s.title)} while you wait, from <a href="https://pit.world/search?q=${encodeURIComponent(res.query)}">pit.world search</a>.</p>
|
|
72
|
+
<ul role="list" class="grid" data-columns="2">${res.results.map((r) => `<li class="card stack" data-space="sm"><a href="${esc(r.url)}" rel="noopener"><strong>${esc(r.title)}</strong></a><small class="muted">${esc(r.host)}</small>${r.summary ? `<p>${esc(r.summary)}</p>` : ''}</li>`).join('')}</ul>
|
|
73
|
+
</section>`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** RSS 2.0 + JSON Feed for syndication: the page itself, then its resources. */
|
|
77
|
+
export function feeds(s, res) {
|
|
78
|
+
const items = [
|
|
79
|
+
{ id: s.url, url: s.url, title: `${s.title}: ${s.tagline}`, summary: s.summary || s.tagline, date: `${s.updated}T00:00:00Z` },
|
|
80
|
+
...(res?.results || []).map((r) => ({ id: r.url, url: r.url, title: r.title, summary: r.summary, date: res.fetchedAt })),
|
|
81
|
+
];
|
|
82
|
+
const rss = `<?xml version="1.0" encoding="UTF-8"?>\n<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>${esc(s.title)}</title><link>${esc(s.url)}</link><atom:link href="${esc(s.url)}feed.xml" rel="self" type="application/rss+xml"/><description>${esc(s.tagline)}</description>${items.map((i) => `<item><title>${esc(i.title)}</title><link>${esc(i.url)}</link><guid>${esc(i.id)}</guid><pubDate>${new Date(i.date).toUTCString()}</pubDate><description>${esc(i.summary)}</description></item>`).join('')}</channel></rss>\n`;
|
|
83
|
+
const json = JSON.stringify({ version: 'https://jsonfeed.org/version/1.1', title: s.title, home_page_url: s.url, feed_url: `${s.url}feed.json`, description: s.tagline, items: items.map((i) => ({ id: i.id, url: i.url, title: i.title, summary: i.summary, date_published: new Date(i.date).toISOString() })) }, null, 2) + '\n';
|
|
84
|
+
return { rss, json };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The plugins that need no build step (form, stats), as page slots. */
|
|
88
|
+
export function baseSlot(s) {
|
|
89
|
+
const on = s.plugins || pluginSet();
|
|
90
|
+
const slot = { head: [], hero: [], cards: [], sections: [], scripts: [] };
|
|
91
|
+
if (on.has('form')) {
|
|
92
|
+
slot.hero.push(`<waitlist-form dn="${esc(s.dn)}" api="${esc(s.api)}" button="${esc(s.button)}">
|
|
93
|
+
<form class="stack" method="post" action="${esc(s.api)}/api/waitlist"><label for="wl-email">Email</label>
|
|
94
|
+
<div class="field-group"><input id="wl-email" name="email" type="email" autocomplete="email" required><button type="submit">${esc(s.button)}</button></div></form>
|
|
95
|
+
</waitlist-form>`);
|
|
96
|
+
}
|
|
97
|
+
if (on.has('stats')) {
|
|
98
|
+
slot.cards.push(`<section class="card stack" aria-labelledby="count"><h2 id="count">Who's waiting</h2>
|
|
99
|
+
<waitlist-stats dn="${esc(s.dn)}" api="${esc(s.api)}" days="30"></waitlist-stats></section>`);
|
|
100
|
+
}
|
|
101
|
+
return slot;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Everything the plugins add to one page: { head, hero, cards, sections, scripts } and files to write. */
|
|
105
|
+
export async function runPlugins(s, out, { fetchImpl, cacheDir } = {}) {
|
|
106
|
+
const on = s.plugins;
|
|
107
|
+
const slot = baseSlot(s);
|
|
108
|
+
let res = null;
|
|
109
|
+
if (on.has('resources')) {
|
|
110
|
+
res = await fetchResources(s, { fetchImpl, cacheDir });
|
|
111
|
+
slot.sections.push(resourcesHtml(s, res));
|
|
112
|
+
if (res.results.length) await writeFile(join(out, 'resources.json'), JSON.stringify(res, null, 2) + '\n');
|
|
113
|
+
}
|
|
114
|
+
if (on.has('board')) {
|
|
115
|
+
const dir = dirname(require.resolve('@profullstack/comments'));
|
|
116
|
+
await copyFile(join(dir, 'index.js'), join(out, 'assets', 'comments.js'));
|
|
117
|
+
await copyFile(join(dir, 'core.js'), join(out, 'assets', 'core.js'));
|
|
118
|
+
slot.sections.push(`<comments-board dn="${esc(s.dn)}" api="${esc(s.api)}" feed${on.has('share') ? ' share' : ''} title="Discussion"></comments-board>`);
|
|
119
|
+
slot.scripts.push('<script type="module" src="assets/comments.js"></script>');
|
|
120
|
+
slot.head.push(`<link rel="alternate" type="application/rss+xml" title="${esc(s.title)} discussion" href="${esc(s.api)}/api/comments/feed?dn=${encodeURIComponent(s.dn)}">`);
|
|
121
|
+
}
|
|
122
|
+
if (on.has('feed')) {
|
|
123
|
+
const f = feeds(s, res);
|
|
124
|
+
await writeFile(join(out, 'feed.xml'), f.rss);
|
|
125
|
+
await writeFile(join(out, 'feed.json'), f.json);
|
|
126
|
+
slot.head.push(`<link rel="alternate" type="application/rss+xml" title="${esc(s.title)}" href="feed.xml">`, `<link rel="alternate" type="application/feed+json" title="${esc(s.title)}" href="feed.json">`);
|
|
127
|
+
}
|
|
128
|
+
return { slot, resources: res };
|
|
129
|
+
}
|