|
| 1 | +// |
| 2 | +// ViewComponetMarkdown.swift |
| 3 | +// SwiftPamphletApp |
| 4 | +// |
| 5 | +// Created by Ming Dai on 2024/4/7. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import SwiftUI |
| 10 | +import MarkdownUI |
| 11 | + |
| 12 | +// MARK: - MarkdownUI |
| 13 | +struct MarkdownView: View { |
| 14 | + var s: String |
| 15 | + var body: some View { |
| 16 | + Markdown(s) |
| 17 | + .markdownTheme(.gitHubCustom) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +extension Theme { |
| 22 | + /// A theme that mimics the GitHub style. |
| 23 | + /// |
| 24 | + /// Style | Preview |
| 25 | + /// --- | --- |
| 26 | + /// Inline text |  |
| 27 | + /// Headings |  |
| 28 | + /// Blockquote |  |
| 29 | + /// Code block |  |
| 30 | + /// Image |  |
| 31 | + /// Task list |  |
| 32 | + /// Bulleted list |  |
| 33 | + /// Numbered list |  |
| 34 | + /// Table |  |
| 35 | + public static let gitHubCustom = Theme() |
| 36 | + .text { |
| 37 | + ForegroundColor(.text) |
| 38 | + BackgroundColor(.background) |
| 39 | + FontSize(16) |
| 40 | + } |
| 41 | + .code { |
| 42 | + FontFamilyVariant(.monospaced) |
| 43 | + FontSize(.em(0.85)) |
| 44 | + BackgroundColor(.secondaryBackground) |
| 45 | + } |
| 46 | + .strong { |
| 47 | + FontWeight(.semibold) |
| 48 | + } |
| 49 | + .link { |
| 50 | + ForegroundColor(.link) |
| 51 | + } |
| 52 | + .heading1 { configuration in |
| 53 | + VStack(alignment: .leading, spacing: 0) { |
| 54 | + configuration.label |
| 55 | + .relativePadding(.bottom, length: .em(0.3)) |
| 56 | + .relativeLineSpacing(.em(0.125)) |
| 57 | + .markdownMargin(top: 24, bottom: 16) |
| 58 | + .markdownTextStyle { |
| 59 | + FontWeight(.semibold) |
| 60 | + FontSize(.em(2)) |
| 61 | + } |
| 62 | + Divider().overlay(Color.divider) |
| 63 | + } |
| 64 | + } |
| 65 | + .heading2 { configuration in |
| 66 | + VStack(alignment: .leading, spacing: 0) { |
| 67 | + configuration.label |
| 68 | + .relativePadding(.bottom, length: .em(0.3)) |
| 69 | + .relativeLineSpacing(.em(0.125)) |
| 70 | + .markdownMargin(top: 24, bottom: 16) |
| 71 | + .markdownTextStyle { |
| 72 | + FontWeight(.semibold) |
| 73 | + FontSize(.em(1.5)) |
| 74 | + } |
| 75 | + Divider().overlay(Color.divider) |
| 76 | + } |
| 77 | + } |
| 78 | + .heading3 { configuration in |
| 79 | + configuration.label |
| 80 | + .relativeLineSpacing(.em(0.125)) |
| 81 | + .markdownMargin(top: 24, bottom: 16) |
| 82 | + .markdownTextStyle { |
| 83 | + FontWeight(.semibold) |
| 84 | + FontSize(.em(1.25)) |
| 85 | + } |
| 86 | + } |
| 87 | + .heading4 { configuration in |
| 88 | + configuration.label |
| 89 | + .relativeLineSpacing(.em(0.125)) |
| 90 | + .markdownMargin(top: 24, bottom: 16) |
| 91 | + .markdownTextStyle { |
| 92 | + FontWeight(.semibold) |
| 93 | + } |
| 94 | + } |
| 95 | + .heading5 { configuration in |
| 96 | + configuration.label |
| 97 | + .relativeLineSpacing(.em(0.125)) |
| 98 | + .markdownMargin(top: 24, bottom: 16) |
| 99 | + .markdownTextStyle { |
| 100 | + FontWeight(.semibold) |
| 101 | + FontSize(.em(0.875)) |
| 102 | + } |
| 103 | + } |
| 104 | + .heading6 { configuration in |
| 105 | + configuration.label |
| 106 | + .relativeLineSpacing(.em(0.125)) |
| 107 | + .markdownMargin(top: 24, bottom: 16) |
| 108 | + .markdownTextStyle { |
| 109 | + FontWeight(.semibold) |
| 110 | + FontSize(.em(0.85)) |
| 111 | + ForegroundColor(.tertiaryText) |
| 112 | + } |
| 113 | + } |
| 114 | + .paragraph { configuration in |
| 115 | + configuration.label |
| 116 | + .fixedSize(horizontal: false, vertical: true) |
| 117 | + .relativeLineSpacing(.em(0.25)) |
| 118 | + .markdownMargin(top: 0, bottom: 16) |
| 119 | + } |
| 120 | + .blockquote { configuration in |
| 121 | + HStack(spacing: 0) { |
| 122 | + RoundedRectangle(cornerRadius: 6) |
| 123 | + .fill(Color.border) |
| 124 | + .relativeFrame(width: .em(0.2)) |
| 125 | + configuration.label |
| 126 | + .markdownTextStyle { ForegroundColor(.secondaryText) } |
| 127 | + .relativePadding(.horizontal, length: .em(1)) |
| 128 | + } |
| 129 | + .fixedSize(horizontal: false, vertical: true) |
| 130 | + } |
| 131 | + .codeBlock { configuration in |
| 132 | + configuration.label |
| 133 | + .fixedSize(horizontal: false, vertical: true) |
| 134 | + .relativeLineSpacing(.em(0.225)) |
| 135 | + .markdownTextStyle { |
| 136 | + FontFamilyVariant(.monospaced) |
| 137 | + FontSize(.em(0.85)) |
| 138 | + } |
| 139 | + .padding(16) |
| 140 | + .background(Color.secondaryBackground) |
| 141 | + .clipShape(RoundedRectangle(cornerRadius: 6)) |
| 142 | + .markdownMargin(top: 0, bottom: 16) |
| 143 | + } |
| 144 | + .listItem { configuration in |
| 145 | + configuration.label |
| 146 | + .markdownMargin(top: .em(0.25)) |
| 147 | + } |
| 148 | + .taskListMarker { configuration in |
| 149 | + Image(systemName: configuration.isCompleted ? "checkmark.square.fill" : "square") |
| 150 | + .symbolRenderingMode(.hierarchical) |
| 151 | + .foregroundStyle(Color.checkbox, Color.checkboxBackground) |
| 152 | + .imageScale(.small) |
| 153 | + .relativeFrame(minWidth: .em(1.5), alignment: .trailing) |
| 154 | + } |
| 155 | + .table { configuration in |
| 156 | + configuration.label |
| 157 | + .fixedSize(horizontal: false, vertical: true) |
| 158 | + .markdownTableBorderStyle(.init(color: .border)) |
| 159 | + .markdownTableBackgroundStyle( |
| 160 | + .alternatingRows(Color.background, Color.secondaryBackground) |
| 161 | + ) |
| 162 | + .markdownMargin(top: 0, bottom: 16) |
| 163 | + } |
| 164 | + .tableCell { configuration in |
| 165 | + configuration.label |
| 166 | + .markdownTextStyle { |
| 167 | + if configuration.row == 0 { |
| 168 | + FontWeight(.semibold) |
| 169 | + } |
| 170 | + BackgroundColor(nil) |
| 171 | + } |
| 172 | + .fixedSize(horizontal: false, vertical: true) |
| 173 | + .padding(.vertical, 6) |
| 174 | + .padding(.horizontal, 13) |
| 175 | + .relativeLineSpacing(.em(0.25)) |
| 176 | + } |
| 177 | + .thematicBreak { |
| 178 | + Divider() |
| 179 | + .relativeFrame(height: .em(0.25)) |
| 180 | + .overlay(Color.border) |
| 181 | + .markdownMargin(top: 24, bottom: 24) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +extension Color { |
| 186 | + fileprivate static let text = Color( |
| 187 | + light: Color(rgba: 0x0606_06ff), dark: Color(rgba: 0xfbfb_fcff) |
| 188 | + ) |
| 189 | + fileprivate static let secondaryText = Color( |
| 190 | + light: Color(rgba: 0x6b6e_7bff), dark: Color(rgba: 0x9294_a0ff) |
| 191 | + ) |
| 192 | + fileprivate static let tertiaryText = Color( |
| 193 | + light: Color(rgba: 0x6b6e_7bff), dark: Color(rgba: 0x6d70_7dff) |
| 194 | + ) |
| 195 | + fileprivate static let background = Color( |
| 196 | + light: .white, dark: Color(rgba: 0x1819_1dff) |
| 197 | + ) |
| 198 | + fileprivate static let secondaryBackground = Color( |
| 199 | + light: Color(rgba: 0xf7f7_f9ff), dark: Color(rgba: 0x2526_2aff) |
| 200 | + ) |
| 201 | + fileprivate static let link = Color( |
| 202 | + light: Color(rgba: 0x2c65_cfff), dark: Color(rgba: 0x4c8e_f8ff) |
| 203 | + ) |
| 204 | + fileprivate static let border = Color( |
| 205 | + light: Color(rgba: 0xe4e4_e8ff), dark: Color(rgba: 0x4244_4eff) |
| 206 | + ) |
| 207 | + fileprivate static let divider = Color( |
| 208 | + light: Color(rgba: 0xd0d0_d3ff), dark: Color(rgba: 0x3334_38ff) |
| 209 | + ) |
| 210 | + fileprivate static let checkbox = Color(rgba: 0xb9b9_bbff) |
| 211 | + fileprivate static let checkboxBackground = Color(rgba: 0xeeee_efff) |
| 212 | +} |
0 commit comments