import Foundation import OrgSwift import OrgSwiftUI import SwiftUI /// README rendering follows the file's format. Org is rendered natively by the shared /// `OrgSwiftUI` package; 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 = "" @Environment(\.colorScheme) private var colorScheme /// Format follows the file name, never a guess at the content. var isOrg: Bool { name.lowercased().hasSuffix(".org") } var body: some View { if isOrg { // Qualified: `OrgView` here is org-mode, not the organization view. OrgSwiftUI.OrgView( content, options: orgOptions, styler: OrgSyntaxStyler(colorScheme: colorScheme), // org's TODO/DONE are the same open/done distinction the forge already // colors, so they reuse those tokens rather than a third treatment. keywords: OrgKeywordStyle(todo: .gbOK, done: .gbDone) ) } 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" ) } }