UNPKG

histoire

Version:

A fast story book powered by Vite

59 lines (58 loc) • 2.64 kB
import { histoireIconNames } from '@histoire/shared/dist/icons/index.js'; import { readInstalledPackage } from '../marketplace/inventory/package-resolve.js'; /** Accept bounded nonempty metadata rather than unexpected package JSON shapes. */ function text(value) { return typeof value === 'string' && value.trim() && value.length <= 2048 ? value.trim() : undefined; } /** Admit only web URLs, excluding embedded credentials. */ export function webUrl(value, httpsOnly = false) { const raw = text(value); if (!raw) return; try { const url = new URL(raw); if ((!['http:', 'https:'].includes(url.protocol)) || (httpsOnly && url.protocol !== 'https:') || url.username || url.password) return; return url.href; } catch { return undefined; } } /** Convert common npm Git URLs to browser URLs without accepting arbitrary schemes. */ export function repositoryUrl(value) { const raw = text(typeof value === 'object' && value ? value.url : value); if (!raw) return; const https = raw .replace(/^git\+https:\/\//, 'https://') .replace(/^(?:github:|git@github\.com:|git\+ssh:\/\/git@github\.com\/|git:\/\/github\.com\/)/, 'https://github.com/'); const valid = webUrl(https); return valid?.replace(/\.git\/?$/, ''); } /** Offline icon membership shared by all public metadata projections. */ const icons = new Set(histoireIconNames); /** Project safe public metadata from JSON already read for this plugin. */ export function resolvePackageMetadata(plugin, pkg) { const info = plugin.metadata ?? {}; const version = text(pkg.version); const displayName = text(info.displayName); const description = text(info.description) ?? text(pkg.description); const icon = text(info.icon); const logoUrl = webUrl(info.logoUrl, true); const docsUrl = webUrl(info.docsUrl) ?? webUrl(pkg.homepage); const repoUrl = repositoryUrl(info.repoUrl) ?? repositoryUrl(pkg.repository); return { ...version ? { packageName: pkg.name, version } : {}, ...displayName ? { displayName } : {}, ...description ? { description } : {}, ...icon && icons.has(icon) ? { icon } : {}, ...logoUrl ? { logoUrl } : {}, ...docsUrl ? { docsUrl } : {}, ...repoUrl ? { repoUrl } : {}, }; } /** Resolve package fallback once per plugin, preserving duplicate identities and order. */ export async function resolvePluginMetadata(root, plugins) { return Promise.all(plugins.map(async (plugin) => resolvePackageMetadata(plugin, await readInstalledPackage(root, plugin.name)))); }