gonzalezreal / swift-markdown-ui

Display and customize Markdown text in SwiftUI
MIT License
2.25k stars 268 forks source link

image-related markdown will hang app #101

Closed tsfischer closed 1 year ago

tsfischer commented 2 years ago

If I try to render the following in a Markdown view, my app hangs:

![alt text](image.jpg)

This was originally part of a large block of markdown our QA threw at the app, and I whittled it down to this single line.

Note that there is no actual image image.jpg anywhere, but I wouldn't expect malformed markdown to crash/hang the app. This will be displaying arbitrary markdown that the user inputs as a message so I have little control over it.

Additional info:

I have done some more narrowing down of this issue and found that when placed in a simple view, the markdown above works (doesn't display anything, but doesn't hang the app either). But if I embed the view into, say, a TabView, then the app hangs.

I am attaching a sample project with 3 tabs: 2 dummy tabs, and one containing the markdown snippet above. The app hangs on launch trying to render the view w/ markdown.

MarkdownUITest.zip

In addition, here's the relevant view code which triggers the issue:

struct ContentView: View {
    @Binding var selected: Int
    var body: some View {
        TabView(selection: $selected) {
            // Home View
            Text("Home")
            .tabItem {
                Image(systemName: "house")
            }

            // Browse View
            Text("Browse")
                .tabItem {
                    Image(systemName: "folder")
            }

            // Notifications View
           MarkdownTestView()
            .tabItem {
                Image(systemName: "bell")
            }
        }
    }
}

struct MarkdownTestView: View {
    var body: some View {
        Markdown("![alt text](image.jpg)")
    }
}
tsfischer commented 2 years ago

Anything that I can provide to help reproduce/resolve this let me know. If I can't find a fix or workaround for this I'm going to need to change directions and try to find a different markdown parser, but I really like the SwiftUI simplicity of this one.

tsfischer commented 2 years ago

I have done some more narrowing down of this issue and found that when placed in a simple view, the markdown above works (doesn't display anything, but doesn't hang the app either). But if I embed the view into, say, a TabView, then the app hangs. I will edit my original issue above to add more detail.

I have made some attempts to try to narrow down what is apparently infinitely looping in the code, but being relatively new to SwiftUI, along with not having a full understanding of how this MarkdownUI code is set up, is hindering that effort.

tsfischer commented 2 years ago

I think I have found a hack workaround. If I give it a (bogus) starting URL on the Markdown initializer, the hang goes away:

Markdown(<markdown including the image tag shown above>, baseURL: URL(string: "http://")!)

Not thrilled with it, but at least it pushes us forward.

Hext123 commented 2 years ago

我也遇到了这个问题, 在列表中展示相对路径的图片时, APP会无响应, 并且内存持续增加. 而如果是绝对路径, 哪怕图片其实不存在也不会有问题. 最后的解决方案跟tsfischer一样, 给定一个baseURL就能避免这个问题了

I also encountered this problem, when displaying pictures with relative paths in the list, the APP would become unresponsive, and the memory continued to increase. And if it is an absolute path, there will be no problem even if the image does not actually exist. The final solution is the same as tsfischer, given a baseURL to avoid this problem

gonzalezreal commented 2 years ago

Hi @tsfischer, Sorry for the late response. Thanks for all the info to reproduce this issue. I will look into it.

gonzalezreal commented 1 year ago

Hi @tsfischer,

The fix for this issue will be included in the next release. Thanks again for providing the sample code to reproduce the problem, it was instrumental to find the cause.

Cheers!