| @@ -107,9 +107,21 @@ interface PaperbackIndex { |
| 107 | } |
107 | } |
| 108 | |
108 | |
| 109 | interface CloudstreamRepo { |
109 | interface CloudstreamRepo { |
| |
110 | name?: string |
| 110 | pluginLists: string[] |
111 | pluginLists: string[] |
| 111 | } |
112 | } |
| 112 | |
113 | |
| |
114 | interface CloudstreamRepoListItem { |
| |
115 | url: string |
| |
116 | verified?: boolean |
| |
117 | } |
| |
118 | |
| |
119 | export interface RepoListEntry { |
| |
120 | label: string |
| |
121 | indexUrl: string |
| |
122 | repoUrl?: string |
| |
123 | } |
| |
124 | |
| 113 | interface CloudstreamPlugin { |
125 | interface CloudstreamPlugin { |
| 114 | name: string |
126 | name: string |
| 115 | iconUrl?: string |
127 | iconUrl?: string |
| @@ -309,6 +321,81 @@ function cachePathFor(url: string): string { |
| 309 | return join(CACHE_DIR, `${Buffer.from(url).toString('base64url')}.json`) |
321 | return join(CACHE_DIR, `${Buffer.from(url).toString('base64url')}.json`) |
| 310 | } |
322 | } |
| 311 | |
323 | |
| |
324 | function collectRepoListUrls(): string[] { |
| |
325 | const urls = new Set<string>() |
| |
326 | const blockRe = /:::\s*extrepo\s*\{([^}]*)\}/g |
| |
327 | |
| |
328 | for (const file of findMarkdownFiles(DOCS_DIR)) { |
| |
329 | const src = readFileSync(file, 'utf-8') |
| |
330 | |
| |
331 | let match: RegExpExecArray | null |
| |
332 | blockRe.lastIndex = 0 |
| |
333 | while ((match = blockRe.exec(src))) { |
| |
334 | const from = match[1].match(/(?:^|\s)from=(?:"([^"]+)"|'([^']+)'|(\S+))/) |
| |
335 | if (from) urls.add((from[1] ?? from[2] ?? from[3]).trim()) |
| |
336 | } |
| |
337 | } |
| |
338 | return [...urls] |
| |
339 | } |
| |
340 | |
| |
341 | function isCloudstreamRepoList(json: unknown): json is Array<string | CloudstreamRepoListItem> { |
| |
342 | if (!Array.isArray(json) || json.length === 0) return false |
| |
343 | return json.every(item => { |
| |
344 | if (typeof item === 'string') return /^https?:\/\//.test(item) |
| |
345 | if (item && typeof item === 'object') { |
| |
346 | const rec = item as Record<string, unknown> |
| |
347 | // LNReader plugins also carry a `url` field — rule those out. |
| |
348 | if ('name' in rec || 'lang' in rec || 'baseUrl' in rec || 'baseURL' in rec) return false |
| |
349 | return typeof rec.url === 'string' && /^https?:\/\//.test(rec.url) |
| |
350 | } |
| |
351 | return false |
| |
352 | }) |
| |
353 | } |
| |
354 | |
| |
355 | function inferRepoUrl(rawUrl: string): string | undefined { |
| |
356 | try { |
| |
357 | const u = new URL(rawUrl) |
| |
358 | const parts = u.pathname.split('/').filter(Boolean) |
| |
359 | if (u.hostname === 'raw.githubusercontent.com' && parts.length >= 2) { |
| |
360 | return `https://github.com/${parts[0]}/${parts[1]}` |
| |
361 | } |
| |
362 | const dashRaw = u.pathname.indexOf('/-/raw/') |
| |
363 | if (dashRaw > 0) return `${u.origin}${u.pathname.slice(0, dashRaw)}` |
| |
364 | const rawSeg = parts.indexOf('raw') |
| |
365 | if (rawSeg > 1) return `${u.origin}/${parts.slice(0, rawSeg).join('/')}` |
| |
366 | } catch { |
| |
367 | return undefined |
| |
368 | } |
| |
369 | return undefined |
| |
370 | } |
| |
371 | |
| |
372 | function inferLabel(rawUrl: string): string { |
| |
373 | const repoUrl = inferRepoUrl(rawUrl) |
| |
374 | if (repoUrl) { |
| |
375 | try { |
| |
376 | const parts = new URL(repoUrl).pathname.split('/').filter(Boolean) |
| |
377 | if (parts.length >= 2) return `${parts[parts.length - 2]}/${parts[parts.length - 1]}` |
| |
378 | if (parts.length === 1) return decodeURIComponent(parts[0]) |
| |
379 | } catch { |
| |
380 | // fall through to raw-URL fallback below |
| |
381 | } |
| |
382 | } |
| |
383 | try { |
| |
384 | const parts = new URL(rawUrl).pathname.split('/').filter(Boolean) |
| |
385 | return decodeURIComponent(parts[parts.length - 2] ?? parts[parts.length - 1] ?? rawUrl) |
| |
386 | } catch { |
| |
387 | return rawUrl |
| |
388 | } |
| |
389 | } |
| |
390 | |
| |
391 | function repoListLabel(indexUrl: string, repoJson: unknown): string { |
| |
392 | if (repoJson && typeof repoJson === 'object' && !Array.isArray(repoJson)) { |
| |
393 | const name = (repoJson as { name?: unknown }).name |
| |
394 | if (typeof name === 'string' && name.trim() !== '') return name.trim() |
| |
395 | } |
| |
396 | return inferLabel(indexUrl) |
| |
397 | } |
| |
398 | |
| 312 | async function fetchIndex(url: string): Promise<unknown> { |
399 | async function fetchIndex(url: string): Promise<unknown> { |
| 313 | const cachePath = cachePathFor(url) |
400 | const cachePath = cachePathFor(url) |
| 314 | if (existsSync(cachePath) && Date.now() - statSync(cachePath).mtimeMs < CACHE_TTL_MS) { |
401 | if (existsSync(cachePath) && Date.now() - statSync(cachePath).mtimeMs < CACHE_TTL_MS) { |
| @@ -832,6 +919,7 @@ export interface ExtensionRepoData { |
| 832 | sites: Record<string, RepoData> |
919 | sites: Record<string, RepoData> |
| 833 | soraAuthors: Record<string, SoraAuthorRepo[]> |
920 | soraAuthors: Record<string, SoraAuthorRepo[]> |
| 834 | echoAuthors: Record<string, EchoAuthorRepo[]> |
921 | echoAuthors: Record<string, EchoAuthorRepo[]> |
| |
922 | repoLists: Record<string, RepoListEntry[]> |
| 835 | } |
923 | } |
| 836 | |
924 | |
| 837 | export declare const data: ExtensionRepoData |
925 | export declare const data: ExtensionRepoData |
| @@ -840,10 +928,35 @@ export default { |
| 840 | watch: ['../**/*.md'], |
928 | watch: ['../**/*.md'], |
| 841 | async load(): Promise<ExtensionRepoData> { |
929 | async load(): Promise<ExtensionRepoData> { |
| 842 | const urls = collectIndexUrls() |
930 | const urls = collectIndexUrls() |
| |
931 | const listUrls = collectRepoListUrls() |
| |
932 | const listUrlSet = new Set(listUrls) |
| 843 | const sites: Record<string, RepoData> = {} |
933 | const sites: Record<string, RepoData> = {} |
| 844 | const soraAuthors: Record<string, SoraAuthorRepo[]> = {} |
934 | const soraAuthors: Record<string, SoraAuthorRepo[]> = {} |
| 845 | const echoAuthors: Record<string, EchoAuthorRepo[]> = {} |
935 | const echoAuthors: Record<string, EchoAuthorRepo[]> = {} |
| |
936 | const repoLists: Record<string, RepoListEntry[]> = {} |
| |
937 | await Promise.all(listUrls.map(async listUrl => { |
| |
938 | const json = await fetchIndex(listUrl) |
| |
939 | if (!isCloudstreamRepoList(json)) { |
| |
940 | repoLists[listUrl] = [] |
| |
941 | return |
| |
942 | } |
| |
943 | const indexUrls = [...new Set(json.map(item => typeof item === 'string' ? item : item.url))] |
| |
944 | const repoJsons = await Promise.all(indexUrls.map(url => fetchIndex(url))) |
| |
945 | const entries: RepoListEntry[] = indexUrls.map((indexUrl, i) => ({ |
| |
946 | label: repoListLabel(indexUrl, repoJsons[i]), |
| |
947 | indexUrl, |
| |
948 | repoUrl: inferRepoUrl(indexUrl) |
| |
949 | })) |
| |
950 | entries.sort((a, b) => a.label.localeCompare(b.label)) |
| |
951 | repoLists[listUrl] = entries |
| |
952 | await Promise.all(indexUrls.map(async (indexUrl, i) => { |
| |
953 | if (indexUrl in sites) return |
| |
954 | sites[indexUrl] = await toRepoData(indexUrl, repoJsons[i]) |
| |
955 | })) |
| |
956 | })) |
| 846 | await Promise.all(urls.map(async url => { |
957 | await Promise.all(urls.map(async url => { |
| |
958 | if (listUrlSet.has(url)) return |
| |
959 | if (url in sites) return |
| 847 | const json = await fetchIndex(url) |
960 | const json = await fetchIndex(url) |
| 848 | if (isSoraRepoIndex(json)) { |
961 | if (isSoraRepoIndex(json)) { |
| 849 | const result = await loadSoraRepoIndex(url, json) |
962 | const result = await loadSoraRepoIndex(url, json) |
| @@ -861,6 +974,6 @@ export default { |
| 861 | } |
974 | } |
| 862 | sites[url] = await toRepoData(url, json) |
975 | sites[url] = await toRepoData(url, json) |
| 863 | })) |
976 | })) |
| 864 | return { sites, soraAuthors, echoAuthors } |
977 | return { sites, soraAuthors, echoAuthors, repoLists } |
| 865 | } |
978 | } |
| 866 | } |
979 | } |