| @@ -125,22 +125,36 @@ interface KotatsuIndex { |
| 125 | sites: KotatsuSite[] |
125 | sites: KotatsuSite[] |
| 126 | } |
126 | } |
| 127 | |
127 | |
| 128 | interface SoraModule { |
128 | interface SoraLibraryModule { |
| 129 | sourceName: string |
129 | sourceName: string |
| 130 | iconUrl?: string |
130 | iconUrl?: string |
| |
131 | iconURL?: string |
| 131 | language?: string |
132 | language?: string |
| |
133 | languageClean?: string |
| 132 | baseUrl?: string |
134 | baseUrl?: string |
| 133 | manifestUrl: string |
135 | scriptUrl?: string |
| 134 | type?: string |
136 | scriptURL?: string |
| 135 | status?: string |
137 | manifestUrl?: string |
| |
138 | jsonUrl?: string |
| 136 | } |
139 | } |
| 137 | |
140 | |
| 138 | export interface SoraGroup { |
141 | interface SoraLibraryAuthor { |
| |
142 | author: string |
| |
143 | repo: string |
| |
144 | types: Record<string, SoraLibraryModule[]> |
| |
145 | } |
| |
146 | |
| |
147 | export interface SoraTypeGroup { |
| 139 | label: string |
148 | label: string |
| 140 | repoUrl?: string |
| |
| 141 | indexUrl: string |
149 | indexUrl: string |
| 142 | } |
150 | } |
| 143 | |
151 | |
| |
152 | export interface SoraAuthorRepo { |
| |
153 | label: string |
| |
154 | repoUrl?: string |
| |
155 | types: SoraTypeGroup[] |
| |
156 | } |
| |
157 | |
| 144 | const EXT_DIR = dirname(fileURLToPath(import.meta.url)) |
158 | const EXT_DIR = dirname(fileURLToPath(import.meta.url)) |
| 145 | const DOCS_DIR = join(EXT_DIR, '..') |
159 | const DOCS_DIR = join(EXT_DIR, '..') |
| 146 | const CACHE_DIR = join(EXT_DIR, '../.vitepress/cache/extension-repos') |
160 | const CACHE_DIR = join(EXT_DIR, '../.vitepress/cache/extension-repos') |
| @@ -238,7 +252,10 @@ const LANG_ALIASES: Record<string, string> = { |
| 238 | tamil: 'ta', |
252 | tamil: 'ta', |
| 239 | ukrainian: 'uk', |
253 | ukrainian: 'uk', |
| 240 | vietnamese: 'vi', |
254 | vietnamese: 'vi', |
| 241 | thai: 'th' |
255 | thai: 'th', |
| |
256 | hindi: 'hi', |
| |
257 | malay: 'ms', |
| |
258 | serbian: 'sr' |
| 242 | } |
259 | } |
| 243 | |
260 | |
| 244 | const LANG_SUBSTRING_ALIASES: [RegExp, string][] = [ |
261 | const LANG_SUBSTRING_ALIASES: [RegExp, string][] = [ |
| @@ -434,10 +451,10 @@ function toRepoDataKotatsu(index: KotatsuIndex): RepoSite[] { |
| 434 | return sites |
451 | return sites |
| 435 | } |
452 | } |
| 436 | |
453 | |
| 437 | function isSoraIndex(json: unknown): json is SoraModule[] { |
454 | function isSoraLibrary(json: unknown): json is SoraLibraryAuthor[] { |
| 438 | if (!Array.isArray(json) || json.length === 0) return false |
455 | if (!Array.isArray(json) || json.length === 0) return false |
| 439 | const first = json[0] as Record<string, unknown> |
456 | const first = json[0] as Record<string, unknown> |
| 440 | return typeof first.manifestUrl === 'string' && typeof first.sourceName === 'string' |
457 | return typeof first.author === 'string' && typeof first.repo === 'string' && typeof first.types === 'object' |
| 441 | } |
458 | } |
| 442 | |
459 | |
| 443 | function normalizeSoraLang(language: string | undefined): string { |
460 | function normalizeSoraLang(language: string | undefined): string { |
| @@ -446,85 +463,64 @@ function normalizeSoraLang(language: string | undefined): string { |
| 446 | return normalizeLang(stripped) |
463 | return normalizeLang(stripped) |
| 447 | } |
464 | } |
| 448 | |
465 | |
| 449 | function normalizeSoraType(type: string): string { |
466 | const SORA_TYPE_ORDER = ['anime', 'manga', 'novels', 'other'] |
| 450 | return type.toLowerCase().split('/').sort().join('_') |
467 | const SORA_TYPE_LABELS: Record<string, string> = { |
| |
468 | anime: 'Anime', |
| |
469 | manga: 'Manga', |
| |
470 | novels: 'Novels', |
| |
471 | other: 'Other' |
| |
472 | } |
| |
473 | |
| |
474 | function soraModuleInstallUrl(mod: SoraLibraryModule): string | undefined { |
| |
475 | const explicit = mod.manifestUrl ?? mod.jsonUrl |
| |
476 | if (explicit) return explicit |
| |
477 | const script = mod.scriptUrl ?? mod.scriptURL |
| |
478 | return script?.replace(/\.js$/i, '.json') |
| 451 | } |
479 | } |
| 452 | |
480 | |
| 453 | function toRepoDataSora(modules: SoraModule[]): RepoSite[] { |
481 | function toRepoDataSoraModules(modules: SoraLibraryModule[]): RepoSite[] { |
| 454 | const seen = new Set<string>() |
482 | const seen = new Set<string>() |
| 455 | const sites: RepoSite[] = [] |
483 | const sites: RepoSite[] = [] |
| 456 | |
484 | |
| 457 | for (const mod of modules) { |
485 | for (const mod of modules) { |
| 458 | const lang = normalizeSoraLang(mod.language) |
486 | const lang = normalizeSoraLang(mod.languageClean ?? mod.language) |
| 459 | const key = `${mod.sourceName}::${lang}::${mod.manifestUrl}` |
487 | const installUrl = soraModuleInstallUrl(mod) |
| |
488 | const key = `${mod.sourceName}::${lang}::${installUrl ?? ''}` |
| 460 | if (seen.has(key)) continue |
489 | if (seen.has(key)) continue |
| 461 | seen.add(key) |
490 | seen.add(key) |
| 462 | sites.push({ |
491 | sites.push({ |
| 463 | name: mod.sourceName, |
492 | name: mod.sourceName, |
| 464 | lang, |
493 | lang, |
| 465 | icon: mod.iconUrl ?? '', |
494 | icon: mod.iconUrl ?? mod.iconURL ?? '', |
| 466 | url: mod.baseUrl ?? '', |
495 | url: mod.baseUrl ?? '', |
| 467 | rating: 'safe', |
496 | rating: 'safe', |
| 468 | contentType: mod.type ? normalizeSoraType(mod.type) : undefined, |
497 | installUrl: installUrl ? `sora://module?url=${installUrl}` : undefined |
| 469 | isBroken: mod.status === 'down' || undefined, |
| |
| 470 | installUrl: `sora://module?url=${mod.manifestUrl}` |
| |
| 471 | }) |
498 | }) |
| 472 | } |
499 | } |
| 473 | |
500 | |
| |
501 | sites.sort((a, b) => a.name.localeCompare(b.name)) |
| 474 | return sites |
502 | return sites |
| 475 | } |
503 | } |
| 476 | |
504 | |
| 477 | function groupSoraModules(indexUrl: string, modules: SoraModule[]): { groups: SoraGroup[]; sitesByGroup: Record<string, RepoData> } { |
505 | function groupSoraLibrary(indexUrl: string, authors: SoraLibraryAuthor[]): { authorRepos: SoraAuthorRepo[]; sitesByType: Record<string, RepoData> } { |
| 478 | const byAuthor = new Map<string, Map<string, SoraModule[]>>() |
506 | const authorRepos: SoraAuthorRepo[] = [] |
| 479 | for (const mod of modules) { |
507 | const sitesByType: Record<string, RepoData> = {} |
| 480 | const { key, display } = soraAuthorKey(mod.manifestUrl) |
| |
| 481 | let displays = byAuthor.get(key) |
| |
| 482 | if (!displays) { |
| |
| 483 | displays = new Map() |
| |
| 484 | byAuthor.set(key, displays) |
| |
| 485 | } |
| |
| 486 | const list = displays.get(display) |
| |
| 487 | if (list) list.push(mod) |
| |
| 488 | else displays.set(display, [mod]) |
| |
| 489 | } |
| |
| 490 | |
| |
| 491 | const groups: SoraGroup[] = [] |
| |
| 492 | const sitesByGroup: Record<string, RepoData> = {} |
| |
| 493 | for (const key of byAuthor.keys()) { |
| |
| 494 | const displays = [...byAuthor.get(key)!.entries()] |
| |
| 495 | const [display] = SORA_CANONICAL_DISPLAYS.has(key) ? [key] : displays.sort((a, b) => b[1].length - a[1].length)[0] |
| |
| 496 | const allMods = displays.flatMap(([, mods]) => mods) |
| |
| 497 | const label = display.split('/')[1] ?? display |
| |
| 498 | const groupIndexUrl = `${indexUrl}#${key}` |
| |
| 499 | const sites = toRepoDataSora(allMods) |
| |
| 500 | sites.sort((a, b) => a.name.localeCompare(b.name)) |
| |
| 501 | groups.push({ label, repoUrl: soraRepoUrl(display), indexUrl: groupIndexUrl }) |
| |
| 502 | sitesByGroup[groupIndexUrl] = { sites } |
| |
| 503 | } |
| |
| 504 | groups.sort((a, b) => a.label.localeCompare(b.label)) |
| |
| 505 | return { groups, sitesByGroup } |
| |
| 506 | } |
| |
| 507 | |
508 | |
| 508 | function soraAuthorKey(manifestUrl: string): { key: string; display: string } { |
509 | for (const entry of authors) { |
| 509 | try { |
510 | const types: SoraTypeGroup[] = [] |
| 510 | const u = new URL(manifestUrl) |
511 | for (const typeKey of SORA_TYPE_ORDER) { |
| 511 | const [user, repo] = u.pathname.split('/').filter(Boolean) |
512 | const modules = entry.types[typeKey] |
| 512 | const display = user && repo ? `${u.host}/${user}/${repo}` : u.host |
513 | if (!modules?.length) continue |
| 513 | const key = display.toLowerCase() |
514 | const typeIndexUrl = `${indexUrl}#${entry.author}#${typeKey}` |
| 514 | return { key: SORA_AUTHOR_ALIASES[key] ?? key, display } |
515 | sitesByType[typeIndexUrl] = { sites: toRepoDataSoraModules(modules) } |
| 515 | } catch { |
516 | types.push({ label: SORA_TYPE_LABELS[typeKey] ?? typeKey, indexUrl: typeIndexUrl }) |
| 516 | return { key: manifestUrl.toLowerCase(), display: manifestUrl } |
517 | } |
| |
518 | if (!types.length) continue |
| |
519 | authorRepos.push({ label: entry.author, repoUrl: entry.repo.replace(/\.git$/, ''), types }) |
| 517 | } |
520 | } |
| 518 | } |
| |
| 519 | |
| |
| 520 | const SORA_AUTHOR_ALIASES: Record<string, string> = { |
| |
| 521 | 'git.luna-app.eu/emp0ry/sources': 'git.luna-app.eu/anonymous/sources' |
| |
| 522 | } |
| |
| 523 | |
| |
| 524 | const SORA_CANONICAL_DISPLAYS = new Set(Object.values(SORA_AUTHOR_ALIASES)) |
| |
| 525 | |
521 | |
| 526 | function soraRepoUrl(display: string): string { |
522 | authorRepos.sort((a, b) => a.label.localeCompare(b.label)) |
| 527 | return `https://${display.replace(/^raw\.githubusercontent\.com\//, 'github.com/')}` |
523 | return { authorRepos, sitesByType } |
| 528 | } |
524 | } |
| 529 | |
525 | |
| 530 | function toRepoDataLNReader(plugins: LNReaderPlugin[]): RepoSite[] { |
526 | function toRepoDataLNReader(plugins: LNReaderPlugin[]): RepoSite[] { |
| @@ -553,8 +549,8 @@ async function toRepoData(indexUrl: string, json: unknown): Promise<RepoData> { |
| 553 | sites = toRepoDataHayase(json as HayaseExtension[]) |
549 | sites = toRepoDataHayase(json as HayaseExtension[]) |
| 554 | } else if (first && 'iconUrl' in first && 'site' in first) { |
550 | } else if (first && 'iconUrl' in first && 'site' in first) { |
| 555 | sites = toRepoDataLNReader(json as LNReaderPlugin[]) |
551 | sites = toRepoDataLNReader(json as LNReaderPlugin[]) |
| 556 | } else if (isSoraIndex(json)) { |
552 | } else if (isSoraLibrary(json)) { |
| 557 | sites = toRepoDataSora(json) |
553 | sites = toRepoDataSoraModules(json.flatMap(entry => Object.values(entry.types).flat())) |
| 558 | } else if (first && 'sources' in first) { |
554 | } else if (first && 'sources' in first) { |
| 559 | sites = toRepoDataMihon(iconBase, json as MihonExtension[]) |
555 | sites = toRepoDataMihon(iconBase, json as MihonExtension[]) |
| 560 | } else if (first && 'pkg' in first && 'apk' in first) { |
556 | } else if (first && 'pkg' in first && 'apk' in first) { |
| @@ -579,7 +575,7 @@ async function toRepoData(indexUrl: string, json: unknown): Promise<RepoData> { |
| 579 | |
575 | |
| 580 | export interface ExtensionRepoData { |
576 | export interface ExtensionRepoData { |
| 581 | sites: Record<string, RepoData> |
577 | sites: Record<string, RepoData> |
| 582 | soraGroups: Record<string, SoraGroup[]> |
578 | soraAuthors: Record<string, SoraAuthorRepo[]> |
| 583 | } |
579 | } |
| 584 | |
580 | |
| 585 | export declare const data: ExtensionRepoData |
581 | export declare const data: ExtensionRepoData |
| @@ -589,16 +585,16 @@ export default { |
| 589 | async load(): Promise<ExtensionRepoData> { |
585 | async load(): Promise<ExtensionRepoData> { |
| 590 | const urls = collectIndexUrls() |
586 | const urls = collectIndexUrls() |
| 591 | const sites: Record<string, RepoData> = {} |
587 | const sites: Record<string, RepoData> = {} |
| 592 | const soraGroups: Record<string, SoraGroup[]> = {} |
588 | const soraAuthors: Record<string, SoraAuthorRepo[]> = {} |
| 593 | await Promise.all(urls.map(async url => { |
589 | await Promise.all(urls.map(async url => { |
| 594 | const json = await fetchIndex(url) |
590 | const json = await fetchIndex(url) |
| 595 | sites[url] = await toRepoData(url, json) |
591 | sites[url] = await toRepoData(url, json) |
| 596 | if (isSoraIndex(json)) { |
592 | if (isSoraLibrary(json)) { |
| 597 | const grouped = groupSoraModules(url, json) |
593 | const grouped = groupSoraLibrary(url, json) |
| 598 | soraGroups[url] = grouped.groups |
594 | soraAuthors[url] = grouped.authorRepos |
| 599 | Object.assign(sites, grouped.sitesByGroup) |
595 | Object.assign(sites, grouped.sitesByType) |
| 600 | } |
596 | } |
| 601 | })) |
597 | })) |
| 602 | return { sites, soraGroups } |
598 | return { sites, soraAuthors } |
| 603 | } |
599 | } |
| 604 | } |
600 | } |