Skip to content

Instantly share code, notes, and snippets.

@eliakorkmaz
Created February 20, 2023 20:59
Show Gist options
  • Save eliakorkmaz/781d65ed27ed0a70cdacea07fc36e564 to your computer and use it in GitHub Desktop.
Save eliakorkmaz/781d65ed27ed0a70cdacea07fc36e564 to your computer and use it in GitHub Desktop.
import UIKit
class MarkdownPreviewViewController: UIViewController {
@IBOutlet weak var markdownTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
Task {
await prepareMarkdown(from: "https://www.awesomemarkdownsource.com")
}
}
}
extension MarkdownPreviewViewController {
func prepareMarkdown(from url: String) async {
do {
let markdownFromWebService = try await fetchMarkdownData(url: url)
let parsedMarkdown = parseMarkdownData(rawMarkdown: markdownFromWebService)
let cachedMarkdown = cacheMarkdownData(parsedMarkdown: parsedMarkdown)
showMarkdownData(in: markdownTextView, markdown: cachedMarkdown)
} catch {
print(error.localizedDescription)
}
}
}
private extension MarkdownPreviewViewController {
struct MarkdownParser {
static func parse(markdown: String) -> String { markdown }
}
struct MarkdownCacher {
@discardableResult static func cache(markdown: String) -> String { markdown }
}
func fetchMarkdownData(url urlString: String) async throws -> String {
let url = URL(string: urlString)!
let (data, _) = try await URLSession.shared.data(from: url)
let downloadedMarkdownData = String(data: data, encoding: .utf8) ?? ""
return downloadedMarkdownData
}
func parseMarkdownData(rawMarkdown markdown: String) -> String {
print(".......parsed")
return MarkdownParser.parse(markdown: markdown)
}
func cacheMarkdownData(parsedMarkdown markdown: String) -> String {
print(".......cached")
return MarkdownCacher.cache(markdown: markdown)
}
func showMarkdownData(in uiElement: UITextView, markdown markdownToShow: String) {
uiElement.text = markdownToShow
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment