gitbay/Views/Repos/ReadmeView.swift
43 lines · 1495 bytes
1import Foundation
2import OrgSwift
3import SwiftUI
4
5/// README rendering follows the file's format. Org is rendered by the shared `OrgSwift`
6/// package (in a web view); everything else keeps the established Markdown renderer.
7struct ReadmeView: View {
8 let name: String
9 let content: String
10 /// Instance host and `owner/repo`, used to resolve repository-relative links and images.
11 /// Empty leaves relative references unresolved.
12 var host: String = ""
13 var repoPath: String = ""
14
15 /// Format follows the file name, never a guess at the content.
16 var isOrg: Bool { name.lowercased().hasSuffix(".org") }
17
18 var body: some View {
19 if isOrg {
20 OrgReadmeWebView(source: content, options: orgOptions)
21 } else {
22 MarkdownView(markdown: content)
23 }
24 }
25
26 /// gitbay serves raw bytes at `/{owner}/{repo}/raw/{ref}/…` and the file page at
27 /// `/blob/…`, so relative images resolve against `raw` and links against `blob`.
28 private var orgOptions: OrgRenderOptions {
29 let parts = repoPath.split(separator: "/", maxSplits: 1).map(String.init)
30 guard parts.count == 2, !host.isEmpty else {
31 return OrgRenderOptions()
32 }
33 return OrgRenderOptions(
34 host: host,
35 owner: parts[0],
36 repositoryName: parts[1],
37 ref: "HEAD",
38 readmePath: name,
39 imagePathSegment: "raw",
40 linkPathSegment: "blob"
41 )
42 }
43}