Skip to main content
PreprocessedContent is a nested class within MarkdownTextView that encapsulates preprocessed markdown content including parsed block nodes, rendered math equations, syntax-highlighted code, and image sources.

Class Definition

public final class MarkdownTextView.PreprocessedContent

Properties

blocks
[MarkdownBlockNode]
Array of parsed markdown block nodes representing the document structure.
rendered
RenderedTextContent.Map
Map of rendered math content keyed by replacement text identifiers. Type alias for [String: RenderedTextContent].
highlightMaps
[Int: CodeHighlighter.HighlightMap]
Map of syntax highlighting color ranges for code blocks, keyed by content hash.

Initializers

Empty Initializer

Creates an empty preprocessed content instance.
public init()

Manual Initializer

Creates preprocessed content with manually provided components.
public init(
    blocks: [MarkdownBlockNode],
    rendered: RenderedTextContent.Map,
    highlightMaps: [Int: CodeHighlighter.HighlightMap],
    imageSources: Set<String> = []
)
blocks
[MarkdownBlockNode]
required
Array of parsed markdown block nodes.
rendered
RenderedTextContent.Map
required
Map of rendered math content.
highlightMaps
[Int: CodeHighlighter.HighlightMap]
required
Map of syntax highlighting data for code blocks.
imageSources
Set<String>
default:"[]"
Set of image source URLs to preload.

Parser Result Initializer

Creates preprocessed content from a parser result. Performs both code highlighting and math rendering on the calling thread.
public init(parserResult: MarkdownParser.ParseResult, theme: MarkdownTheme)
parserResult
MarkdownParser.ParseResult
required
The parser result containing the markdown document.
theme
MarkdownTheme
required
The theme to use for rendering and highlighting.
This initializer performs math rendering which requires UIKit trait access. Use the backgroundSafe initializer variant for background queue initialization.

Background-Safe Initializer

Creates preprocessed content with code highlighting done on the calling thread and math rendering deferred. Use this from background queues where UIKit trait access is unavailable.
public init(
    parserResult: MarkdownParser.ParseResult,
    theme: MarkdownTheme,
    backgroundSafe: Bool
)
parserResult
MarkdownParser.ParseResult
required
The parser result containing the markdown document.
theme
MarkdownTheme
required
The theme to use for rendering and highlighting.
backgroundSafe
Bool
required
If true, defers math rendering for later completion on the main thread. Code highlighting is performed immediately as it’s thread-safe.
When backgroundSafe is true, you must call completeMathRendering(parserResult:theme:) on the main thread to finish math rendering.

Methods

completeMathRendering

Fills in math-rendered content from the main thread after background initialization. Use this to complete preprocessing started with the backgroundSafe initializer.
public func completeMathRendering(
    parserResult: MarkdownParser.ParseResult,
    theme: MarkdownTheme
) -> PreprocessedContent
parserResult
MarkdownParser.ParseResult
required
The parser result used during initial preprocessing.
theme
MarkdownTheme
required
The theme to use for math rendering.
content
PreprocessedContent
A new PreprocessedContent instance with math rendering completed.
This method must be called on the main thread as it performs math rendering that requires UIKit trait access.

Type Aliases

RenderedTextContent.Map

public typealias RenderedTextContent.Map = [String: RenderedTextContent]
A dictionary mapping replacement text identifiers to rendered math content.

CodeHighlighter.HighlightMap

public typealias CodeHighlighter.HighlightMap = [NSRange: PlatformColor]
A dictionary mapping text ranges to syntax highlighting colors.

Usage Patterns

Simple Synchronous Usage

let parser = MarkdownParser()
let result = parser.parse("# Hello\n\nThis is **markdown**.")
let content = PreprocessedContent(parserResult: result, theme: .default)
markdownView.setMarkdown(content)

Background Processing with Deferred Math Rendering

DispatchQueue.global(qos: .userInitiated).async {
    let parser = MarkdownParser()
    let result = parser.parse(markdown)
    let partial = PreprocessedContent(
        parserResult: result,
        theme: theme,
        backgroundSafe: true
    )
    
    DispatchQueue.main.async {
        let complete = partial.completeMathRendering(
            parserResult: result,
            theme: theme
        )
        markdownView.setMarkdown(complete)
    }
}

Manual Construction

let content = PreprocessedContent(
    blocks: blocks,
    rendered: renderedMath,
    highlightMaps: codeHighlights,
    imageSources: ["https://example.com/image.png"]
)

Performance Considerations

  • Code highlighting is thread-safe and can be performed on background queues
  • Math rendering requires main thread access for UIKit trait collection
  • Images are automatically preloaded asynchronously when PreprocessedContent is created
  • The backgroundSafe initializer pattern enables optimal background processing

Build docs developers (and LLMs) love