-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExampleView.swift
More file actions
168 lines (131 loc) · 4.31 KB
/
ExampleView.swift
File metadata and controls
168 lines (131 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import SwiftUI
import Charts
struct ExampleView: View {
@State var markdown = try! MarkdownDocument(rawMarkdown)
var body: some View {
ScrollView(.vertical) {
Markdown(markdown, lazy: false, customView: { customView in
switch customView.tag {
case "myChart":
MyChart(title: customView.parameters["title"] ?? "Chart")
case "dataChart":
DataChart(customView: customView)
default:
EmptyView()
}
})
.markdownStyle(MarkdownStyle())
.tint(.blue)
.padding()
}
}
}
struct MyChart: View {
var title: String = "Chart"
var body: some View {
if #available(iOS 16.0, macOS 13.0, *) {
Chart {
BarMark(
x: .value("Color", "Red"),
y: .value("Value", 10)
)
.foregroundStyle(.red)
BarMark(
x: .value("Color", "Green"),
y: .value("Value", 12)
)
.foregroundStyle(.green)
BarMark(
x: .value("Color", "Blue"),
y: .value("Value", 8)
)
.foregroundStyle(.blue)
}
.frame(height: 200)
.padding(.horizontal)
}
}
}
struct DataChart: View {
var customView: MarkdownBlock.CustomView
struct Bar: Decodable {
let label: String
let value: Double
}
var bars: [Bar] {
guard let data = customView.content else { return [] }
return (try? JSONDecoder().decode([Bar].self, from: data)) ?? []
}
var body: some View {
if #available(iOS 16.0, macOS 13.0, *) {
Chart(bars, id: \.label) { bar in
BarMark(
x: .value("Label", bar.label),
y: .value("Value", bar.value)
)
}
.frame(height: 200)
.padding(.horizontal)
}
}
}
// MARK: - Preview
#Preview {
ExampleView()
}
// MARK: - Markdown Text
let attributedMarkdown = try! AttributedString(markdown: rawMarkdown)
let rawMarkdown = #"""
# Header 1
## Header 2
Here is a link: [Apple](https://apple.com). We should also try **bold** to see if it works. Not to mention _italic_ and I guess one more __bold__.
- This is a list item.
- This is a nested list item.
- This is also a nested list item. It is a bit longer, to see what happens if a list element occupies multiple lines.
- Here's yet another nested item.
- Twice as nested.
- This too, is a list item.
- Nested list items are fun.
---
1. First of all, let's try a list item.
1. Secondly, this is also a list item. It is a bit longer, to see what happens if a list element occupies multiple lines.
1. This is a nested item.
2. Also a nested item.
- Mixed unordered list here
- One more item
1. Thirdly, here's another list item.
### Header 3
Let's try some `inline code` here, and maybe some ~~strikethrough~~ while we are at it.
> This is a blockquote. A blockquote is a typographic element used to set apart and visually distinguish a longer quotation or excerpt from the main text.
```swift
// Here's a code block
func fibonacci(n: Int) -> Int {
var num1 = 0
var num2 = 1
for _ in 0 ..< n {
let num = num1 + num2
num1 = num2
num2 = num
}
return num2
}
```
This text is before the separator.
---
This text is after the separator.
| Fruit | Color | Is it good? |
|-----------|--------|------------:|
| 🍌 Banana | Yellow | Yes |
| 🍏 Apple | Green | Yes |
| 🍋 Lemon | Yellow | Not really |
This paragraph is after the table.
Here's a remote image:

And here's a local image:

Here's a custom view:
<view tag="myChart" title="Sales Data" />
Here's a data-driven chart with a JSON payload:
<view tag="dataChart">[{"label": "Q1", "value": 42}, {"label": "Q2", "value": 58}, {"label": "Q3", "value": 35}, {"label": "Q4", "value": 71}]</view>
This is just some text after the custom views.
"""#