.")
let condenseAutolinksFormatter = FormatCondenseAutolinks()
let formattedDocument = document.formatted(using: condenseAutolinksFormatter)
print(formattedDocument.debugDescription)
```
--------------------------------
### Collect Links with MarkupWalker
Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md
Uses MarkupWalker to traverse a Markup tree and collect all link elements. This is suitable for read-only analysis of the document structure.
```swift
import Markdown
struct LinkCollector: MarkupWalker {
var links: [Link] = []
mutating func visitLink(_ link: Link) -> ActionResult {
links.append(link)
return .continueWalk
}
}
let document = Document(parsing: "[Swift](https://swift.org)")
var collector = LinkCollector()
document.walk(&collector)
print(collector.links)
```
--------------------------------
### Query Child Elements
Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md
Retrieves specific child elements from a Markup node. This snippet demonstrates how to access children by their index.
```swift
import Markdown
let document = Document(parsing: "# Title\nSome text.")
// Access the first child element (the heading)
if let firstChild = document.child(at: 0) {
print(firstChild)
}
```
--------------------------------
### Modify Markup Tree and Observe Persistence
Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Parsing-Building-and-Modifying Markup-Trees.md
Demonstrates modifying a Text element within a Document. Swift-Markdown creates copies of necessary substructure, leaving the original tree unchanged.
```swift
import Markdown
let source = "This is *emphasized.*"
let document = Document(parsing: source)
print(document.debugDescription())
// Document
// └─ Paragraph
// ├─ Text "This is "
// └─ Emphasis
// └─ Text "emphasized."
var text = document.child(through:
0, // Paragraph
1, // Emphasis
0) as! Text // Text
text.string = "really emphasized!"
print(text.root.debugDescription())
// Document
// └─ Paragraph
// ├─ Text "This is "
// └─ Emphasis
// └─ Text "really emphasized!"
// The original document is unchanged:
print(document.debugDescription())
// Document
// └─ Paragraph
// ├─ Text "This is "
// └─ Emphasis
// └─ Text "emphasized."
```
--------------------------------
### Parse Markdown String
Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md
Parses a Markdown string into a structured Markup tree in memory. This is useful for processing Markdown content directly from a string variable.
```swift
import Foundation
import Markdown
let markdownString = "# Hello, World!\n\nThis is a paragraph."
let document = Document(parsing: markdownString)
print(document.debugDescription)
```
--------------------------------
### Block Directive Without Arguments
Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Markdown/BlockDirectives.md
A block directive that omits parentheses for argument text when none is needed.
```markdown
@Directive {
- A
- B
- C
}
```
--------------------------------
### Replace Text with MarkupRewriter
Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md
Utilizes MarkupRewriter to find and replace specific text within a Markup tree. This is useful for bulk text updates across a document.
```swift
import Markdown
let document = Document(parsing: "Hello world, hello Swift!")
let rewriter = ReplaceText(old: "hello", new: "hi")
let newDocument = document.rewriting(with: rewriter)
print(newDocument.html)
// Output: hi world, hi Swift!
```
--------------------------------
### Block Directive Without Content
Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Markdown/BlockDirectives.md
A block directive that omits curly braces for content when none is present.
```markdown
@TOC
# Title
...
```
--------------------------------
### Remove Elements by Kind with MarkupRewriter
Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md
Employs MarkupRewriter to remove all elements of a specific kind, such as all paragraphs, from a Markup tree. This modifies the document structure.
```swift
import Markdown
let document = Document(parsing: "# Title\n\nThis paragraph will be removed.
")
let rewriter = RemoveElementKind(kind: Paragraph.self)
let newDocument = document. rewriting(with: rewriter)
print(newDocument.debugDescription)
```
--------------------------------
### Thematic Break Character Formatting
Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md
Formats thematic breaks (horizontal rules) to use a preferred character, such as '-', '_', or '*'. This standardizes the appearance of separators.
```swift
import Markdown
let document = Document(parsing: "---\n***")
let thematicBreakFormatter = FormatThematicBreakCharacter(preferredCharacter: "-")
let formattedDocument = document.formatted(using: thematicBreakFormatter)
print(formattedDocument.debugDescription)
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.