Files
openidea/scripts/gen-plugin-catalog.ts
T
l0ng-ai c1782f01eb docs: add a landing page and a plugin marketplace to the documentation site (#4033)
/en and /zh open a landing page: the product in the dark theme, startup and
memory numbers, the main features, featured plugins and the default keymap.
The introduction pages move to /en/introduction and /zh/introduction.

/plugins and /zh/plugins list the marketplace's plugins with search,
category filters, sorting and a details dialog. The list is a snapshot of
plugins-index.json written by `pnpm gen:plugin-catalog`.
2026-11-18 02:03:42 +08:00

94 lines
3.1 KiB
TypeScript

// Writes docs/snippets/plugin-catalog.jsx, the snapshot of the plugin marketplace the
// documentation site shows, from the release's plugins-index.json (or a local file).
//
// node scripts/gen-plugin-catalog.ts [index.json | url]
import { readFileSync, writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
const OFFICIAL_INDEX = "https://github.com/l0ng-ai/openidea/releases/latest/download/plugins-index.json";
const OUT = fileURLToPath(new URL("../docs/snippets/plugin-catalog.jsx", import.meta.url));
interface IndexPlugin {
id: string;
name: string;
version: string;
description: string;
publisher: string;
categories?: string[];
icon?: { light: string | null; dark: string | null } | null;
homepage?: string | null;
downloads?: number | null;
}
interface Index {
publishers: { id: string; name: string }[];
plugins: IndexPlugin[];
}
async function load(source: string): Promise<Index> {
if (/^https?:\/\//.test(source)) {
const res = await fetch(source);
if (!res.ok) throw new Error(`${source}: HTTP ${res.status}`);
return (await res.json()) as Index;
}
return JSON.parse(readFileSync(source, "utf8")) as Index;
}
function compareVersions(a: string, b: string): number {
const pa = a.split(/[.-]/).map(Number);
const pb = b.split(/[.-]/).map(Number);
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
const d = (pa[i] || 0) - (pb[i] || 0);
if (d) return d;
}
return 0;
}
const svgUri = (svg: string) => `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`;
function icon(p: IndexPlugin) {
const light = p.icon?.light ?? p.icon?.dark;
const dark = p.icon?.dark ?? p.icon?.light;
if (!light || !dark) return null;
return light === dark ? { light: svgUri(light) } : { light: svgUri(light), dark: svgUri(dark) };
}
const index = await load(process.argv[2] ?? OFFICIAL_INDEX);
const publishers = new Map(index.publishers.map((p) => [p.id, p.name]));
const latest = new Map<string, IndexPlugin>();
for (const plugin of index.plugins) {
const seen = latest.get(plugin.id);
if (!seen || compareVersions(plugin.version, seen.version) > 0) latest.set(plugin.id, plugin);
}
const plugins = [...latest.values()]
// API examples document the plugin API; they are not offered to users here.
.filter((p) => !/\(API Example\)$/.test(p.name))
.map((p) => ({
id: p.id,
name: p.name,
version: p.version,
description: p.description,
publisher: publishers.get(p.publisher) ?? p.publisher,
official: p.publisher === "openidea",
categories: p.categories?.length ? p.categories : ["other"],
downloads: p.downloads ?? 0,
homepage: p.homepage ?? null,
icon: icon(p),
}))
.sort((a, b) => b.downloads - a.downloads || a.name.localeCompare(b.name));
const catalog = {
generatedAt: new Date().toISOString().slice(0, 10),
publishers: new Set(plugins.map((p) => p.publisher)).size,
plugins,
};
writeFileSync(
OUT,
`// Generated by scripts/gen-plugin-catalog.ts. Do not edit.\nexport const pluginCatalog = ${JSON.stringify(catalog)};\n`,
);
console.log(`${OUT}: ${plugins.length} plugins from ${catalog.publishers} publishers`);