Skip to content

Commit

Permalink
Merge pull request #22 from iwasrobbed/downview
Browse files Browse the repository at this point in the history
v0.3.1: Add the ability to update DownView content
  • Loading branch information
iwasrobbed authored Feb 10, 2017
2 parents 3131e3b + 761e903 commit 11e757b
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 10 deletions.
19 changes: 19 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Please help prevent duplicate issues before submitting a new one:

* [ ] I've searched other open/closed issues for duplicates before opening up this new issue.

# Report

## What did you do?

ℹ Please replace this with what you did.

## What did you expect to happen?

ℹ Please replace this with what you expected to happen.

## What happened instead?

ℹ Please replace this with what happened instead (e.g. the issue).


62 changes: 62 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Change Log

## [Unreleased](https://github.com/iwasrobbed/Down/tree/HEAD)

[Full Changelog](https://github.com/iwasrobbed/Down/compare/v0.3.1...HEAD)

**Closed issues:**

- How to update DownView content and keep the style? [\#19](https://github.com/iwasrobbed/Down/issues/19)

## [v0.3.1](https://github.com/iwasrobbed/Down/tree/v0.3.1) (2017-02-09)
[Full Changelog](https://github.com/iwasrobbed/Down/compare/v0.3...v0.3.1)

**Closed issues:**

- How to keep UITextView font style [\#21](https://github.com/iwasrobbed/Down/issues/21)
- Define custom fonts [\#18](https://github.com/iwasrobbed/Down/issues/18)
- Render progress [\#17](https://github.com/iwasrobbed/Down/issues/17)
- Disable zoom WebView [\#16](https://github.com/iwasrobbed/Down/issues/16)
- Text Size of DownView too small [\#15](https://github.com/iwasrobbed/Down/issues/15)
- How to customize the Font? [\#14](https://github.com/iwasrobbed/Down/issues/14)
- Support Images [\#13](https://github.com/iwasrobbed/Down/issues/13)
- How to manually install \(w/o Carthage or CocoaPods\) [\#12](https://github.com/iwasrobbed/Down/issues/12)
- Add support for Carthage [\#8](https://github.com/iwasrobbed/Down/issues/8)

## [v0.3](https://github.com/iwasrobbed/Down/tree/v0.3) (2016-10-12)
[Full Changelog](https://github.com/iwasrobbed/Down/compare/v0.2...v0.3)

**Implemented enhancements:**

- Swift 3.0 support [\#10](https://github.com/iwasrobbed/Down/issues/10)
- Add web view for rendering output [\#3](https://github.com/iwasrobbed/Down/issues/3)

**Closed issues:**

- Can you give an example how to custom the parser? [\#9](https://github.com/iwasrobbed/Down/issues/9)

**Merged pull requests:**

- Swift 3 [\#11](https://github.com/iwasrobbed/Down/pull/11) ([azeff](https://github.com/azeff))
- Adds a few extra tests where possible [\#7](https://github.com/iwasrobbed/Down/pull/7) ([iwasrobbed](https://github.com/iwasrobbed))
- DownView rendering to close \#3 [\#5](https://github.com/iwasrobbed/Down/pull/5) ([iwasrobbed](https://github.com/iwasrobbed))

## [v0.2](https://github.com/iwasrobbed/Down/tree/v0.2) (2016-06-02)
[Full Changelog](https://github.com/iwasrobbed/Down/compare/v0.1.1...v0.2)

**Implemented enhancements:**

- Add attributed string support [\#2](https://github.com/iwasrobbed/Down/issues/2)
- Adds attributed string rendering [\#4](https://github.com/iwasrobbed/Down/pull/4) ([iwasrobbed](https://github.com/iwasrobbed))

## [v0.1.1](https://github.com/iwasrobbed/Down/tree/v0.1.1) (2016-06-01)
[Full Changelog](https://github.com/iwasrobbed/Down/compare/v0.1...v0.1.1)

**Implemented enhancements:**

- Add CocoaPods support & cmark license [\#1](https://github.com/iwasrobbed/Down/pull/1) ([iwasrobbed](https://github.com/iwasrobbed))

## [v0.1](https://github.com/iwasrobbed/Down/tree/v0.1) (2016-06-01)


\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
2 changes: 1 addition & 1 deletion Down.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|
spec.name = "Down"
spec.summary = "Blazing fast Markdown rendering in Swift, built upon cmark."
spec.version = "0.3"
spec.version = "0.3.1"
spec.homepage = "https://github.com/iwasrobbed/Down"
spec.license = { :type => "MIT", :file => "LICENSE" }
spec.authors = { "Rob Phillips" => "rob@desideratalabs.co" }
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ Or manually install:
The `DownView` class offers a very simple way to parse a UTF-8 encoded string with Markdown and convert it to a web view that can be added to any view:

```swift
let downView = try? DownView(frame: self.view.bounds, markdownString: "**Oh Hai**")
let downView = try? DownView(frame: self.view.bounds, markdownString: "**Oh Hai**") {
// Optional callback for loading finished
}
// Now add to view or constrain w/ Autolayout
// Or you could optionally update the contents at some point:
try? downView?.update(markdownString: "## [Google](https://google.com)") {
// Optional callback for loading finished
}
```

Meta example of rendering this README:
Expand Down
41 changes: 35 additions & 6 deletions Source/Views/DownView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,51 @@ import WebKit

// MARK: - Public API

public typealias DownViewClosure = () -> ()

open class DownView: WKWebView {

/**
Initializes a web view with the results of rendering a CommonMark Markdown string

- parameter frame: The frame size of the web view
- parameter markdownString: A string containing CommonMark Markdown
- parameter openLinksInBrowser: Whether or not to open links using an external browser
- parameter frame: The frame size of the web view
- parameter markdownString: A string containing CommonMark Markdown
- parameter openLinksInBrowser: Whether or not to open links using an external browser
- parameter didLoadSuccessfully: Optional callback for when the web content has loaded successfully

- returns: An instance of Self
*/

public init(frame: CGRect, markdownString: String, openLinksInBrowser: Bool = true) throws {
public init(frame: CGRect, markdownString: String, openLinksInBrowser: Bool = true, didLoadSuccessfully: DownViewClosure? = nil) throws {
self.didLoadSuccessfully = didLoadSuccessfully
super.init(frame: frame, configuration: WKWebViewConfiguration())

if openLinksInBrowser { navigationDelegate = self }
if openLinksInBrowser || didLoadSuccessfully != nil { navigationDelegate = self }
try loadHTMLView(markdownString)
}

required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - API

/**
Renders the given CommonMark Markdown string into HTML and updates the DownView while keeping the style intact

- parameter markdownString: A string containing CommonMark Markdown
- parameter didLoadSuccessfully: Optional callback for when the web content has loaded successfully

- throws: `DownErrors` depending on the scenario
*/
public func update(markdownString: String, didLoadSuccessfully: DownViewClosure? = nil) throws {
// Note: As the init method takes this callback already, we only overwrite it here if
// a non-nil value is passed in
if let didLoadSuccessfully = didLoadSuccessfully {
self.didLoadSuccessfully = didLoadSuccessfully
}

try loadHTMLView(markdownString)
}

// MARK: - Private Properties

Expand All @@ -44,6 +67,8 @@ open class DownView: WKWebView {
fileprivate lazy var baseURL: URL = {
return self.bundle.url(forResource: "index", withExtension: "html")!
}()

fileprivate var didLoadSuccessfully: DownViewClosure?
}

// MARK: - Private API
Expand Down Expand Up @@ -79,4 +104,8 @@ extension DownView: WKNavigationDelegate {
}
}

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
didLoadSuccessfully?()
}

}
63 changes: 61 additions & 2 deletions Tests/DownViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,67 @@ import XCTest
class DownViewTests: XCTestCase {

func testInstantiation() {
let downView = try? DownView(frame: .zero, markdownString: "## [Down](https://github.com/iwasrobbed/Down)")
XCTAssertNotNil(downView)
let expect1 = expectation(description: "DownView sets the html and validates the html is correct")
var downView: DownView?
downView = try? DownView(frame: .zero, markdownString: "## [Down](https://github.com/iwasrobbed/Down)", didLoadSuccessfully: {
self._pageContents(for: downView!) { (htmlString) in
XCTAssertTrue(htmlString!.contains("css/down.min.css"))
XCTAssertTrue(htmlString!.contains("https://github.com/iwasrobbed/Down"))

expect1.fulfill()
}
})

waitForExpectations(timeout: 10) { (error: Error?) in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
}
}

func testUpdatingMarkdown() {
let expect1 = expectation(description: "DownView sets the html and validates the html is correct")
var downView: DownView?
downView = try? DownView(frame: .zero, markdownString: "## [Down](https://github.com/iwasrobbed/Down)") {
self._pageContents(for: downView!) { (htmlString) in
XCTAssertTrue(htmlString!.contains("css/down.min.css"))
XCTAssertTrue(htmlString!.contains("https://github.com/iwasrobbed/Down"))

expect1.fulfill()
}
}

waitForExpectations(timeout: 10) { (error: Error?) in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
}

let expect2 = expectation(description: "DownView sets the html and validates the html is correct")
try? downView?.update(markdownString: "## [Google](https://google.com)") {
self._pageContents(for: downView!) { (htmlString) in
XCTAssertTrue(htmlString!.contains("css/down.min.css"))
XCTAssertTrue(htmlString!.contains("https://google.com"))

expect2.fulfill()
}
}

waitForExpectations(timeout: 10) { (error: Error?) in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
}
}

}

fileprivate extension DownViewTests {

func _pageContents(for downView: DownView, completion: @escaping (_ htmlString: String?) -> ()) {
downView.evaluateJavaScript("document.documentElement.outerHTML.toString()") { (html: Any?, _) in
completion(html as? String)
}
}

}

0 comments on commit 11e757b

Please sign in to comment.