Skip to main content
MarkdownInlineNode is an enum that represents all inline-level elements in a parsed markdown document. Inline nodes are used within block elements to represent formatted text, links, images, code spans, and other inline content.

Definition

public enum MarkdownInlineNode: Hashable, Sendable, Equatable, Codable

Enum Cases

text

Plain text content.
case text(String)
String
String
required
The raw text content

Example

// Markdown: "Hello world"
.text("Hello world")

code

Inline code span.
case code(String)
String
String
required
The code content (without the surrounding backticks)

Example

// Markdown: "`let x = 42`"
.code("let x = 42")

emphasis

Emphasized (italic) text.
case emphasis(children: [MarkdownInlineNode])
children
[MarkdownInlineNode]
required
Nested inline nodes that are emphasized

Example

// Markdown: "*italic text*" or "_italic text_"
.emphasis(children: [.text("italic text")])

// Markdown: "*italic with **bold** inside*"
.emphasis(children: [
    .text("italic with "),
    .strong(children: [.text("bold")]),
    .text(" inside")
])

strong

Strong (bold) text.
case strong(children: [MarkdownInlineNode])
children
[MarkdownInlineNode]
required
Nested inline nodes that are bold

Example

// Markdown: "**bold text**" or "__bold text__"
.strong(children: [.text("bold text")])

strikethrough

Strikethrough text (GitHub Flavored Markdown extension).
case strikethrough(children: [MarkdownInlineNode])
children
[MarkdownInlineNode]
required
Nested inline nodes that have strikethrough formatting

Example

// Markdown: "~~deleted text~~"
.strikethrough(children: [.text("deleted text")])
A hyperlink.
case link(destination: String, children: [MarkdownInlineNode])
destination
String
required
The URL or reference the link points to
children
[MarkdownInlineNode]
required
Inline nodes representing the link text

Example

// Markdown: "[Click here](https://example.com)"
.link(
    destination: "https://example.com",
    children: [.text("Click here")]
)

// Markdown: "[**Bold link**](https://example.com)"
.link(
    destination: "https://example.com",
    children: [.strong(children: [.text("Bold link")])]
)

image

An embedded image.
case image(source: String, children: [MarkdownInlineNode])
source
String
required
The URL or path to the image file
children
[MarkdownInlineNode]
required
Inline nodes representing the alt text

Example

// Markdown: "![Alt text](image.png)"
.image(
    source: "image.png",
    children: [.text("Alt text")]
)

html

Raw HTML content.
case html(String)
String
String
required
Raw HTML string

Example

// Markdown: "Text with <span class='highlight'>HTML</span>"
.html("<span class='highlight'>")
.text("HTML")
.html("</span>")

softBreak

A soft line break (newline in source that doesn’t create a <br>).
case softBreak

Example

// Markdown: "Line 1\nLine 2" (single newline)
.text("Line 1")
.softBreak
.text("Line 2")

lineBreak

A hard line break (creates a <br> in HTML).
case lineBreak

Example

// Markdown: "Line 1  \nLine 2" (two spaces before newline)
.text("Line 1")
.lineBreak
.text("Line 2")

math

A mathematical expression in LaTeX format.
case math(content: String, replacementIdentifier: String)
content
String
required
The LaTeX math expression
replacementIdentifier
String
required
Unique identifier used to look up the original expression in the ParseResult’s mathContext

Example

// Markdown: "$E = mc^2$"
.math(
    content: "E = mc^2",
    replacementIdentifier: "<<MATH_0>>"
)

// The original LaTeX is also stored in ParseResult.mathContext:
// mathContext[0] = "E = mc^2"

Properties

children

Gets or sets the child nodes for container inline nodes.
var children: [MarkdownInlineNode] { get set }
Returns child nodes for .emphasis, .strong, .strikethrough, .link, and .image. Returns empty array for leaf nodes. Setting this property updates the children for container nodes.

Example

var node = MarkdownInlineNode.emphasis(children: [.text("old")])
node.children = [.text("new")]  // Now: .emphasis(children: [.text("new")])

Usage Examples

Basic Text Formatting

let parser = MarkdownParser()
let result = parser.parse("This is **bold** and *italic* text.")

if case .paragraph(let inlines) = result.document.first {
    for inline in inlines {
        switch inline {
        case .text(let str):
            print("Text: \(str)")
        case .strong(let children):
            print("Bold: \(children)")
        case .emphasis(let children):
            print("Italic: \(children)")
        default:
            break
        }
    }
}
let result = parser.parse("Visit [our website](https://example.com) for more.")

if case .paragraph(let inlines) = result.document.first {
    for inline in inlines {
        if case .link(let destination, let children) = inline {
            print("Link to: \(destination)")
            print("Link text: \(children)")
        }
    }
}

Traversing Nested Inline Nodes

func collectText(from inlines: [MarkdownInlineNode]) -> String {
    var text = ""
    for inline in inlines {
        switch inline {
        case .text(let str):
            text += str
        case .emphasis(let children),
             .strong(let children),
             .strikethrough(let children),
             .link(_, let children),
             .image(_, let children):
            text += collectText(from: children)
        case .code(let str):
            text += str
        case .softBreak, .lineBreak:
            text += " "
        default:
            break
        }
    }
    return text
}

let result = parser.parse("**Bold** and *italic*")
if case .paragraph(let content) = result.document.first {
    let plainText = collectText(from: content)
    print(plainText)  // "Bold and italic"
}

Build docs developers (and LLMs) love