diff --git a/package.json b/package.json index cddccc9..a005152 100644 --- a/package.json +++ b/package.json @@ -68,9 +68,7 @@ "dayjs": "^1.11.10", "highlight.js": "^11.9.0", "parse-diff": "^0.11.1", - "ramda": "^0.29.1" - }, - "resolutions": { - "jackspeak": "2.1.1" + "ramda": "^0.29.1", + "svelte-markdown": "^0.4.1" } } diff --git a/src/lib/components/repo/type.ts b/src/lib/components/repo/type.ts index 388361a..00e1038 100644 --- a/src/lib/components/repo/type.ts +++ b/src/lib/components/repo/type.ts @@ -67,3 +67,15 @@ export const summary_defaults: RepoSummary = { loading: false, created_at: 0, } + +export interface RepoReadme { + md: string + loading: boolean + failed: boolean +} + +export const readme_defaults: RepoReadme = { + md: '', + loading: true, + failed: false, +} diff --git a/src/lib/stores/repo.ts b/src/lib/stores/repo.ts index 0422cfb..f384823 100644 --- a/src/lib/stores/repo.ts +++ b/src/lib/stores/repo.ts @@ -1,8 +1,17 @@ import { writable, type Unsubscriber, type Writable } from 'svelte/store' -import type { RepoCollection, RepoEvent } from '$lib/components/repo/type' -import { collection_defaults, event_defaults } from '$lib/components/repo/type' +import type { + RepoCollection, + RepoEvent, + RepoReadme, +} from '$lib/components/repo/type' +import { + collection_defaults, + event_defaults, + readme_defaults, +} from '$lib/components/repo/type' import { ensureRepoCollection } from './repos' import { selectRepoFromCollection } from '$lib/components/repo/utils' +import { get } from 'svelte/store' export const selected_repo_collection: Writable = writable({ ...collection_defaults, @@ -28,12 +37,19 @@ export const ensureSelectedRepoCollection = ( if ( selected_repo_unique_commit_or_identifier !== unique_commit_or_identifier ) { + let loading = true selected_repo_unique_commit_or_identifier = unique_commit_or_identifier if (selected_unsubscriber) selected_unsubscriber() selected_unsubscriber = ensureRepoCollection( unique_commit_or_identifier ).subscribe((repo_collection) => { selected_repo_collection.set({ ...repo_collection }) + if (loading && !repo_collection.loading) { + loading = false + const repo_event = selectRepoFromCollection(repo_collection) + if (repo_event) + ensureRepoReadme(repo_event.clone, repo_collection.identifier) + } }) } return selected_repo_collection @@ -59,3 +75,69 @@ export const awaitSelectedRepoCollection = async ( }) }) } + +export const selected_repo_readme: Writable = writable({ + ...readme_defaults, +}) + +const ensureRepoReadme = async ( + clone: string, + unique_commit_or_identifier: string +): Promise => { + selected_repo_readme.set({ ...readme_defaults }) + + /** update writable unless selected readme has changed */ + const update = (md: string | undefined = undefined): void => { + const latest_collection = get(selected_repo_collection) + if ( + [latest_collection.identifier, latest_collection.unique_commit].includes( + unique_commit_or_identifier + ) + ) { + selected_repo_readme.set({ + md: md || '', + loading: false, + failed: !md, + }) + } + } + try { + const github_details = extractGithubDetails(clone) + // feature stapled off + const feature_staple = false + if (github_details && feature_staple) { + const res = await fetch( + // `/git_proxy/readme?clone=${encodeURIComponent(clone)}` + `https://raw.githubusercontent.com/${github_details.org}/${github_details.repo_name}/HEAD/README.md` + ) + if (!res.ok) { + throw 'api request error' + } + let text = '' + text = await res.text() + update(text) + } else { + // use proxy to get readme using 'git archive' or 'git clone' + } + } catch (e) { + update() + } +} + +const extractGithubDetails = ( + clone: string +): { org: string; repo_name: string } | undefined => { + if (clone.indexOf('github.') > -1) { + const g_split = clone.split('github.') + if (g_split.length > 0) { + const slash_split = g_split[1].split('/') + if (slash_split.length > 2) { + return { + org: slash_split[1], + repo_name: slash_split[2].split('.')[0], + } + } + } + } + return undefined +} diff --git a/src/routes/repo/[repo_id]/+page.svelte b/src/routes/repo/[repo_id]/+page.svelte index e7f4267..9da9e25 100644 --- a/src/routes/repo/[repo_id]/+page.svelte +++ b/src/routes/repo/[repo_id]/+page.svelte @@ -4,6 +4,7 @@ ensureSelectedRepoCollection, selected_repo_collection, selected_repo_event, + selected_repo_readme, } from '$lib/stores/repo' import RepoHeader from '$lib/components/repo/RepoHeader.svelte' import Container from '$lib/components/Container.svelte' @@ -13,6 +14,7 @@ } from '$lib/stores/Proposals' import ProposalsList from '$lib/components/proposals/ProposalsList.svelte' import { ensureIssueSummaries, issue_summaries } from '$lib/stores/Issues' + import SvelteMarkdown from 'svelte-markdown' export let data: { repo_id: string } let identifier = data.repo_id @@ -137,6 +139,22 @@ create issue {/if} + {#if false} + + {#if $selected_repo_readme.loading} +
loading readme...
+ {:else if $selected_repo_readme.failed} +
failed to load readme from git server...
+ {:else} +

README.md

+
+ +
+ {/if} + {/if}