Skip to content

Commit

Permalink
fix: Fix open links in browsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 11, 2022
1 parent 107d057 commit d74ddfd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
name: "Markdown",
platforms: [
.iOS(.v13),
.macOS(.v10_15)
.macOS(.v11)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
Expand Down
16 changes: 14 additions & 2 deletions Sources/Markdown/MarkdownWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ private struct JavascriptFunction {
}
}

public class MarkdownWebView: CustomView {
public class MarkdownWebView: CustomView, WKNavigationDelegate {
@Environment(\.openURL) private var openURL
private struct Constants {
static let mdPreviewDidReady = "mdPreviewDidReady"
static let mdPreviewDidChanged = "mdPreviewDidChanged"
Expand All @@ -43,6 +44,7 @@ public class MarkdownWebView: CustomView {
configuration.preferences = preferences
configuration.userContentController = userController
let webView = WKWebView(frame: bounds, configuration: configuration)
webView.navigationDelegate = self

#if os(OSX)
webView.setValue(true, forKey: "drawsTransparentBackground") // Prevent white flick
Expand Down Expand Up @@ -117,8 +119,18 @@ public class MarkdownWebView: CustomView {
func setPaddingRight(_ right: Int) {
callJavascript(javascriptString: "__markdown_preview__.style.paddingRight = '\(right)px';")
}
}

public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url {
if url.isFileURL == false {
openURL(url)
decisionHandler(.cancel)
return
}
}
decisionHandler(.allow)
}
}


extension MarkdownWebView {
Expand Down

0 comments on commit d74ddfd

Please sign in to comment.