feat: read entries, registry data and READMEs in one loader
renderMarkdown only exists inside a content loader, and the README should render through the site's own pipeline. One loader also means the home and every plugin page share a single registry read per build. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+107
-40
No files matched your search
Generated
+1
@@ -10,6 +10,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/sitemap": "^3.7.3",
|
"@astrojs/sitemap": "^3.7.3",
|
||||||
"astro": "^7.1.6",
|
"astro": "^7.1.6",
|
||||||
|
"js-yaml": "^4.3.2",
|
||||||
"sanitize-html": "^2.17.7"
|
"sanitize-html": "^2.17.7"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/sitemap": "^3.7.3",
|
"@astrojs/sitemap": "^3.7.3",
|
||||||
"astro": "^7.1.6",
|
"astro": "^7.1.6",
|
||||||
|
"js-yaml": "^4.3.2",
|
||||||
"sanitize-html": "^2.17.7"
|
"sanitize-html": "^2.17.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
// The `plugins` collection's loader. It replaces `glob` because `renderMarkdown` only exists inside
|
||||||
|
// a loader, and the README has to be rendered with the site's own pipeline (shiki included).
|
||||||
|
// Wrapping `glob` was tried on 2026-09-24 and the added fields did not persist; a standalone
|
||||||
|
// loader did.
|
||||||
|
import { readdirSync, readFileSync } from 'node:fs';
|
||||||
|
import yaml from 'js-yaml';
|
||||||
|
|
||||||
|
import { readEntry } from './registry.mjs';
|
||||||
|
import { cleanReadme } from './readme.mjs';
|
||||||
|
|
||||||
|
/** `entrySchema` validates one yaml file; it is the contributor-facing contract. */
|
||||||
|
export function pluginsLoader(entrySchema) {
|
||||||
|
return {
|
||||||
|
name: 'plugins',
|
||||||
|
// ponytail: no file watcher — after editing a yaml in `npm run dev`, restart it. Add a watcher
|
||||||
|
// if contributors start previewing entries locally.
|
||||||
|
load: async ({ store, renderMarkdown, config }) => {
|
||||||
|
store.clear();
|
||||||
|
const dir = new URL('src/content/plugins/', config.root);
|
||||||
|
const files = readdirSync(dir).filter((file) => file.endsWith('.yaml') && !file.startsWith('_'));
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
files.map(async (file) => {
|
||||||
|
const parsed = entrySchema.safeParse(yaml.load(readFileSync(new URL(file, dir), 'utf8')));
|
||||||
|
if (!parsed.success) {
|
||||||
|
throw new Error(`src/content/plugins/${file}: ${parsed.error.issues.map((i) => i.message).join('; ')}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const live = await readEntry(parsed.data);
|
||||||
|
if (!live) return;
|
||||||
|
|
||||||
|
const { readme, readmeBase, ...data } = live;
|
||||||
|
const rendered = await renderMarkdown(readme);
|
||||||
|
store.set({
|
||||||
|
id: file.replace(/\.yaml$/, ''),
|
||||||
|
data: { ...parsed.data, ...data },
|
||||||
|
rendered: { ...rendered, html: cleanReadme(rendered.html, readmeBase) },
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
+27
-4
@@ -87,6 +87,13 @@ async function getJson(url, what) {
|
|||||||
return response.json();
|
return response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getText(url, what) {
|
||||||
|
const response = await fetch(url);
|
||||||
|
if (!response.ok) throw new Error(`${what}: ${url} answered ${response.status}`);
|
||||||
|
|
||||||
|
return response.text();
|
||||||
|
}
|
||||||
|
|
||||||
/** The npm packument, reduced to what validation and the card need. */
|
/** The npm packument, reduced to what validation and the card need. */
|
||||||
export async function readNpm(name) {
|
export async function readNpm(name) {
|
||||||
const packument = await getJson(`https://registry.npmjs.org/${name.replace('/', '%2f')}`, `npm ${name}`);
|
const packument = await getJson(`https://registry.npmjs.org/${name.replace('/', '%2f')}`, `npm ${name}`);
|
||||||
@@ -122,8 +129,12 @@ export async function readJsr(name) {
|
|||||||
const versions = (listing.items ?? []).map((item) => item.version);
|
const versions = (listing.items ?? []).map((item) => item.version);
|
||||||
const newest = newestVersion(versions);
|
const newest = newestVersion(versions);
|
||||||
const dependencies = newest ? await getJson(`${base}/versions/${newest}/dependencies`, `jsr ${name} deps`) : [];
|
const dependencies = newest ? await getJson(`${base}/versions/${newest}/dependencies`, `jsr ${name} deps`) : [];
|
||||||
|
const meta = newest ? await getJson(`${base}/versions/${newest}`, `jsr ${name} ${newest}`) : {};
|
||||||
|
const readme = meta.readmePath
|
||||||
|
? await getText(`https://jsr.io/${name}/${newest}${meta.readmePath}`, `jsr ${name} readme`)
|
||||||
|
: '';
|
||||||
|
|
||||||
return { versions, newest, runtimeCompat: info.runtimeCompat ?? {}, dependencies };
|
return { versions, newest, runtimeCompat: info.runtimeCompat ?? {}, dependencies, readme };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -142,7 +153,7 @@ export function declaredRuntimes({ engines, runtimeCompat }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Everything one card needs, live.
|
* Everything a card and a plugin page need, live.
|
||||||
*
|
*
|
||||||
* Under `CI` a registry that does not answer throws and fails the build, which is the rule that
|
* Under `CI` a registry that does not answer throws and fails the build, which is the rule that
|
||||||
* stops a stale or half-empty listing reaching production. Off CI it warns and returns `null`, and
|
* stops a stale or half-empty listing reaching production. Off CI it warns and returns `null`, and
|
||||||
@@ -158,12 +169,24 @@ export async function readEntry(entry) {
|
|||||||
}
|
}
|
||||||
const jsr = entry.jsr ? await readJsr(entry.jsr) : undefined;
|
const jsr = entry.jsr ? await readJsr(entry.jsr) : undefined;
|
||||||
|
|
||||||
|
// JSR is the recommendation, so it is the source of the page whenever the entry declares it.
|
||||||
|
const from = jsr ? { registry: 'jsr', pkg: entry.jsr, info: jsr } : { registry: 'npm', pkg: entry.npm, info: npm };
|
||||||
|
const version = from.info.newest;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
version: (jsr ?? npm).newest,
|
registry: from.registry,
|
||||||
|
pkg: from.pkg,
|
||||||
|
version,
|
||||||
|
versions: byNewest(from.info.versions),
|
||||||
// npm's `engines` is the richer of the two — JSR's `runtimeCompat` is empty far more often,
|
// npm's `engines` is the richer of the two — JSR's `runtimeCompat` is empty far more often,
|
||||||
// including on `@green-tea/core` itself — so it wins when both exist.
|
// including on `@green-tea/core` itself — so it wins when both exist.
|
||||||
runtimes: declaredRuntimes({ engines: npm?.engines, runtimeCompat: jsr?.runtimeCompat }),
|
runtimes: declaredRuntimes({ engines: npm?.engines, runtimeCompat: jsr?.runtimeCompat }),
|
||||||
live: true,
|
readme: from.info.readme,
|
||||||
|
// Where the README's relative links point: the files of this exact published version.
|
||||||
|
readmeBase:
|
||||||
|
from.registry === 'jsr'
|
||||||
|
? `https://jsr.io/${from.pkg}/${version}/`
|
||||||
|
: `https://cdn.jsdelivr.net/npm/${from.pkg}@${version}/`,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (STRICT) throw error;
|
if (STRICT) throw error;
|
||||||
|
|||||||
+20
-10
@@ -1,5 +1,6 @@
|
|||||||
import { defineCollection, z } from 'astro:content';
|
import { defineCollection, z } from 'astro:content';
|
||||||
import { glob } from 'astro/loaders';
|
|
||||||
|
import { pluginsLoader } from '../scripts/loader.mjs';
|
||||||
|
|
||||||
// Naming, from spec 5.2. Enforced here so a wrongly-named submission is rejected by the build
|
// Naming, from spec 5.2. Enforced here so a wrongly-named submission is rejected by the build
|
||||||
// rather than by a reviewer's memory.
|
// rather than by a reviewer's memory.
|
||||||
@@ -8,20 +9,29 @@ import { glob } from 'astro/loaders';
|
|||||||
const NPM_NAME = /^(@green-tea\/[a-z0-9-]+|green-tea-[a-z0-9-]+|@[a-z0-9-]+\/green-tea-[a-z0-9-]+)$/;
|
const NPM_NAME = /^(@green-tea\/[a-z0-9-]+|green-tea-[a-z0-9-]+|@[a-z0-9-]+\/green-tea-[a-z0-9-]+)$/;
|
||||||
const JSR_NAME = /^@[a-z0-9-]+\/(green-tea-)?[a-z0-9-]+$/;
|
const JSR_NAME = /^@[a-z0-9-]+\/(green-tea-)?[a-z0-9-]+$/;
|
||||||
|
|
||||||
const plugins = defineCollection({
|
// `npm` and `jsr` rather than one `name`: a third party's package has a different name on each
|
||||||
loader: glob({ pattern: '**/[^_]*.yaml', base: './src/content/plugins' }),
|
// registry. No `version` and no `runtimes` — both come from the registry, so an entry cannot go
|
||||||
schema: z
|
// stale or claim a runtime the package never declared.
|
||||||
.object({
|
const FIELDS = z.object({
|
||||||
// `npm` and `jsr` rather than one `name`: a third party's package has a different name on
|
|
||||||
// each registry. No `version` and no `runtimes` — both come from the registry, so an entry
|
|
||||||
// cannot go stale or claim a runtime the package never declared.
|
|
||||||
npm: z.string().regex(NPM_NAME, 'npm name must be @green-tea/<x>, green-tea-<x> or @scope/green-tea-<x>').optional(),
|
npm: z.string().regex(NPM_NAME, 'npm name must be @green-tea/<x>, green-tea-<x> or @scope/green-tea-<x>').optional(),
|
||||||
jsr: z.string().regex(JSR_NAME, 'JSR name must be @scope/<x>').optional(),
|
jsr: z.string().regex(JSR_NAME, 'JSR name must be @scope/<x>').optional(),
|
||||||
repo: z.string().url(),
|
repo: z.string().url(),
|
||||||
description: z.string().min(20).max(200),
|
description: z.string().min(20).max(200),
|
||||||
})
|
});
|
||||||
.refine((entry) => entry.npm ?? entry.jsr, {
|
|
||||||
|
const ENTRY = FIELDS.refine((entry) => entry.npm ?? entry.jsr, {
|
||||||
message: 'an entry needs at least one of npm / jsr — otherwise there is nothing to install',
|
message: 'an entry needs at least one of npm / jsr — otherwise there is nothing to install',
|
||||||
|
});
|
||||||
|
|
||||||
|
const plugins = defineCollection({
|
||||||
|
loader: pluginsLoader(ENTRY),
|
||||||
|
// What the loader stores: the entry plus what the registry said.
|
||||||
|
schema: FIELDS.extend({
|
||||||
|
pkg: z.string(),
|
||||||
|
registry: z.enum(['jsr', 'npm']),
|
||||||
|
version: z.string(),
|
||||||
|
versions: z.array(z.string()),
|
||||||
|
runtimes: z.array(z.string()),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+15
-26
@@ -1,37 +1,26 @@
|
|||||||
---
|
---
|
||||||
import { getCollection } from 'astro:content';
|
import { getCollection } from 'astro:content';
|
||||||
import { readEntry } from '../../scripts/registry.mjs';
|
|
||||||
import Header from '../components/Header.astro';
|
import Header from '../components/Header.astro';
|
||||||
import Footer from '../components/Footer.astro';
|
import Footer from '../components/Footer.astro';
|
||||||
import PluginCard from '../components/PluginCard.astro';
|
import PluginCard from '../components/PluginCard.astro';
|
||||||
import '../styles/global.css';
|
import '../styles/global.css';
|
||||||
|
|
||||||
// Read at build time, never at request time — this site is static files on cPanel. Under CI a
|
// Read at build time by the collection's loader (scripts/loader.mjs) — this site is static files
|
||||||
// registry that does not answer fails the build; off CI that entry is left out. See
|
// on cPanel. Under CI a registry that does not answer fails the build; off CI that entry is left out.
|
||||||
// scripts/registry.mjs.
|
const plugins = (await getCollection('plugins'))
|
||||||
const entries = await getCollection('plugins');
|
.map(({ id, data }) => ({
|
||||||
const plugins = (
|
slug: id,
|
||||||
await Promise.all(
|
name: data.pkg,
|
||||||
entries.map(async (entry) => {
|
description: data.description,
|
||||||
const live = await readEntry(entry.data);
|
repo: data.repo,
|
||||||
if (!live) return null;
|
npm: data.npm,
|
||||||
|
jsr: data.jsr,
|
||||||
return {
|
version: data.version,
|
||||||
name: entry.data.jsr ?? entry.data.npm,
|
runtimes: data.runtimes,
|
||||||
description: entry.data.description,
|
|
||||||
repo: entry.data.repo,
|
|
||||||
npm: entry.data.npm,
|
|
||||||
jsr: entry.data.jsr,
|
|
||||||
version: live.version,
|
|
||||||
runtimes: live.runtimes,
|
|
||||||
// An internal plugin could be listed on npm alone and still be ours, so check both fields.
|
// An internal plugin could be listed on npm alone and still be ours, so check both fields.
|
||||||
internal: (entry.data.jsr ?? entry.data.npm ?? '').startsWith('@green-tea/'),
|
internal: (data.jsr ?? data.npm ?? '').startsWith('@green-tea/'),
|
||||||
};
|
}))
|
||||||
}),
|
.sort((a, b) => a.name.localeCompare(b.name));
|
||||||
)
|
|
||||||
).filter((plugin) => plugin !== null);
|
|
||||||
|
|
||||||
plugins.sort((a, b) => a.name.localeCompare(b.name));
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|||||||
Reference in new issue
Block a user