import Foundation import OrgSwift import SwiftUI /// README rendering follows the file's format. Org is rendered by the shared `OrgSwift` /// package (in a web view); everything else keeps the established Markdown renderer. struct ReadmeView: View { let name: String let content: String /// Instance host and `owner/repo`, used to resolve repository-relative links and images. /// Empty leaves relative references unresolved. var host: String = "" var repoPath: String = "" /// Format follows the file name, never a guess at the content. var isOrg: Bool { name.lowercased().hasSuffix(".org") } var body: some View { if isOrg { OrgReadmeWebView(source: content, options: orgOptions) } else { MarkdownView(markdown: content) } } /// gitbay serves raw bytes at `/{owner}/{repo}/raw/{ref}/…` and the file page at /// `/blob/…`, so relative images resolve against `raw` and links against `blob`. private var orgOptions: OrgRenderOptions { let parts = repoPath.split(separator: "/", maxSplits: 1).map(String.init) guard parts.count == 2, !host.isEmpty else { return OrgRenderOptions() } return OrgRenderOptions( host: host, owner: parts[0], repositoryName: parts[1], ref: "HEAD", readmePath: name, imagePathSegment: "raw", linkPathSegment: "blob" ) } }