Skip to content

Issues with Code Highlighting when Highlighting needs Async #338

Open
@erik-aie

Description

@erik-aie

Description
I am trying to use a from-source version of HighlightSwift to perform my code highlighting as I want a macOS / iOS / visionOS unified solution for highlighting, which this library seems closest to providing. It's based in HighlightJS though, and the calls to the JS Core are done via async, which means it's root level call to get attributed text for given code is async. CodeSyntaxHighlighter from MarkdownUI only provides a synchronous highlightCode method.

I was able to make this kind of work, it works on macOS, but it freezes on iOS and visionOS. I ripped this down to a sample project (See SyntaxHighlightFreezeDemo ) and I think I've narrowed it down to the code that interfaces with MarkdownUI, not the underlying highlighting code, which seems to work cross platform as expected.

The offending code seems to be my highlightCode function, which I had to finagle to work with the async call inside the synchronous function. Maybe there's a much better way to do this? I am getting a lot of warnings regarding Swift 6 Concurrency but I don't know of a better way to do this without getting an async call from MarkdownUI's protocol.

func highlightCode(_ content: String, language: String?) -> Text {
        guard language != nil else {
            return Text(content)
        }

        let semaphore = DisSemaphore(value: 0)
        var result: AttributedString?


        var detectedLanguage: HighlightLanguage? = nil
        if language == "swift" {
            detectedLanguage = .swift
        } else if language == "csharp" {
            detectedLanguage = .cSharp
        }

        Task {
            do {
                if detectedLanguage != nil {
                        result = try await syntaxHighlighter.attributedText(content, language: detectedLanguage!, colors: isDarkMode ? .dark(.) : .light(.))
                } else {
                    result = try await syntaxHighlighter.attributedText(content, colors: isDarkMode ? .dark(.) : .light(.))
                }
                print("finished")
            } catch {
                // Handle error
                print(error)
            }
            semaphore.signal()
        }
        semaphore.wait()

        return Text(result ?? AttributedString(""))
    }

What's weirder is that the function executes, and it gets to the Highlight.swift function call to create NSMutableAttributedString from the HTML data generated by HighlightJS, but my debug execution stops after that call:

        let mutableString = try NSMutableAttributedString(
            data: data,
            options: [
                .documentType: NSAttributedString.DocumentType.html,
                .characterEncoding: String.Encoding.utf8.rawValue
            ],
            documentAttributes: nil
        )

Checklist

  • I can reproduce this issue with a vanilla SwiftUI project.
  • I can reproduce this issue using the main branch of this package.
  • This bug hasn't been addressed in an existing issue.

Steps to reproduce
See SyntaxHighlightFreezeDemo

Expected behavior
MarkdownUI performing syntax highlighting cross platform

Resulting Behavior
Syntax highlighting on macOS, but a freeze on iOS and visionOS

Version information

  • MarkdownUI: 2.3.1
  • OS: iOS 17, macOS 18
  • Xcode: 16.0 Beta 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions