Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not load local image and link to local .md file #40

Closed
Cu-Toof opened this issue Aug 7, 2017 · 6 comments
Closed

Can not load local image and link to local .md file #40

Cu-Toof opened this issue Aug 7, 2017 · 6 comments

Comments

@Cu-Toof
Copy link

Cu-Toof commented Aug 7, 2017

I want to load local image and link to local .md file in .md file. So, I want to know subclasses that I have to edit. Could you please tell me?, thank you

@ladislas
Copy link
Contributor

ladislas commented Aug 17, 2017

@Cu-Toof here is my solution.

Just drop a text file containing your content anywhere in your project and use the following in your code:

import UIKit
import Down

class ViewController: UIViewController {

    var downView: DownView?

    override func viewDidLoad() {

        super.viewDidLoad()


        let path = Bundle.main.path(forResource: "NameOfMyFile", ofType: "txt")
        let content = try? String(contentsOfFile: path!, encoding: String.Encoding.utf8)

        print(content!)

        downView = try? DownView(frame: self.view.bounds, markdownString: content) {
            self.view.addSubview(self.downView!)
        }

    }

}

@Cu-Toof
Copy link
Author

Cu-Toof commented Aug 18, 2017

I want to click a link in the content of WKWebView to show a .md file that is saved in local and my solution is WKScriptMessageHandler. But I have an issue, I can't set WKWebViewConfiguration because DownView class has inited WKWebView with WKWebViewConfiguration default. So, I have not used DowView and custom a new WKWebView. Could you please edit DownView class with init method that can set WKWebViewConfiguration. WKWebViewConfiguration will help developer can handle javascript in WKWebView.

@ladislas
Copy link
Contributor

ladislas commented Aug 18, 2017

@Cu-Toof and as for the images not loading, the problem comes from the baseURL used when loadHTMLString is called, it resolves to the DownView.bundle url inside the pod and therefore cannot find pictures that are in your own project.

The solution I found was to:

  • copy the content of the DownView.bundle
    screenshot 2017-08-18 10 19 30
  • and put it inside my own resources group in my project and add them as groups and NOT as folder references
    screenshot 2017-08-18 10 21 40
  • now you just need to instantiate DownView with your own templateBundle, now being Bundle.main like this:
downView = try? DownView(frame: self.view.bounds, markdownString: content, templateBundle: Bundle.main) {
    self.view.addSubview(self.downView!)
}

or in the whole ViewController.swift:

import UIKit
import Down

class ViewController: UIViewController {

    var downView: DownView?

    override func viewDidLoad() {

        super.viewDidLoad()

        let path = Bundle.main.path(forResource: "content", ofType: "md")
        let content = try! String(contentsOfFile: path!, encoding: String.Encoding.utf8)

        downView = try? DownView(frame: self.view.bounds, markdownString: content, templateBundle: Bundle.main) {
            self.view.addSubview(self.downView!)
        }

    }

}
  • finally, you have to edit the content of index.html to resolve to the right files by removing css/ and js/ where needed:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=640"/>
    <link charset="utf-8" href="down.min.css" rel="stylesheet">
    <script charset="utf-8" src="highlight.min.js" type="text/javascript"></script>
    <script charset="utf-8" src="down.js" type="text/javascript"></script>
    <title></title>
</head>
<body>
    DOWN_HTML
</body>
</html>

You can now add your own images in the img group and reference them in your markdown as ![](imageName.jpg) without the group prefix, e.g. ![](doggy.jpg). It will look like this:

img_1673

You can also add your own css and js as long as you reference them in index.html.

@ladislas
Copy link
Contributor

@Cu-Toof said:

Could you please edit DownView class with init method that can set WKWebViewConfiguration. WKWebViewConfiguration will help developer can handle javascript in WKWebView.

Try my solution and if it doesn't work, why not make a pull request yourself to add this feature?

@iwasrobbed
Copy link
Collaborator

Thanks for your help @ladislas 💯 I'm happy to review any pull requests to improve upon this

@iwasrobbed
Copy link
Collaborator

Closing this for now; feel free to re-open for any PRs or further help needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants