Created
February 20, 2023 20:59
-
-
Save eliakorkmaz/781d65ed27ed0a70cdacea07fc36e564 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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