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