### Convert Markup to XML with MarkupVisitor Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Demonstrates using MarkupVisitor to transform a Markup tree into a nested XML structure. This is an example of converting to a different nested format. ```swift import Markdown struct XMLConverter: MarkupVisitor { func visitDocument(_ document: Document) -> String { return "\n\(visitChildren(document))\n" } func visitText(_ text: Text) -> String { return "\(text.string)" } // Add other visit methods as needed for different element types } let document = Document(parsing: "Hello") var converter = XMLConverter() let xmlString = converter.visit(document) print(xmlString) ``` -------------------------------- ### Basic Block Directive Example Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Markdown/BlockDirectives.md A block directive with a name, arguments, and a list as its content. ```markdown @Directive(x: 1, y: 2 z: 3) { - A - B - C } ``` -------------------------------- ### Name-Value Arguments Example Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Markdown/BlockDirectives.md A block directive using the name-value argument parser with string literals. ```markdown @Directive(x: 1, y: "2") ``` -------------------------------- ### Parsing Doxygen Commands and Block Directives Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Markdown/DoxygenCommands.md This example demonstrates how Swift Markdown parses multiple Doxygen commands and a block directive when parsing is enabled. It shows the structure of parsed commands and their descriptions. ```markdown \param thing The thing. This is the thing that is modified. \param otherThing The other thing. \returns A thing that has been modified. @Comment { This is not part of the `\returns` command. } ``` -------------------------------- ### Ordered List Numerals Formatting Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Formats ordered lists to use a consistent numeral style, typically starting with '1.'. This ensures predictable numbering. ```swift import Markdown let document = Document(parsing: "1. First item\n2. Second item") let orderedListFormatter = FormatOrderedListNumerals(preferredStyle: .one) let formattedDocument = document.formatted(using: orderedListFormatter) print(formattedDocument.debugDescription) ``` -------------------------------- ### Counting Specific Links with MarkupWalker Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Visitors-Walkers-and-Rewriters.md Implement MarkupWalker to count occurrences of a specific link destination within a document. This example counts links to 'https://swift.org'. ```swift import Markdown /// Counts `Link`s in a `Document`. struct LinkCounter: MarkupWalker { var count = 0 mutating func visitLink(_ link: Link) { if link.destination == "https://swift.org" { count += 1 } descendInto(link) } } let source = "There are [two](https://swift.org) links to here." let document = Document(parsing: source) print(document.debugDescription()) var linkCounter = LinkCounter() linkCounter.visit(document) print(linkCounter.count) // 2 ``` -------------------------------- ### Handling Indented Descriptions in Doxygen Commands Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Markdown/DoxygenCommands.md This example illustrates how Swift Markdown parses indented trailing lines within a Doxygen command's description. The indented lines are correctly parsed as part of the description's paragraph. ```markdown \param thing The thing. This is the thing that is modified. ``` -------------------------------- ### Deleting Strong Elements with MarkupRewriter Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Visitors-Walkers-and-Rewriters.md Implement MarkupRewriter to remove specific elements from a markup tree. This example deletes all **strong** elements by returning nil from visitStrong. ```swift import Markdown /// Delete all **strong** elements in a markup tree. struct StrongDeleter: MarkupRewriter { mutating func visitStrong(_ strong: Strong) -> Markup? { return nil } } let source = "Now you see me, **now you don't" let document = Document(parsing: source) var strongDeleter = StrongDeleter() let newDocument = strongDeleter.visit(document) print(newDocument!.debugDescription()) // Document // └─ Paragraph // └─ Text "Now you see me, " ``` -------------------------------- ### Parse Markdown String to Document Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Parsing-Building-and-Modifying Markup-Trees.md Create a Document by parsing a String. Shows the resulting tree structure. ```swift import Markdown let source = "This is a markup *document*." let document = Document(parsing: source) print(document.debugDescription()) // Document // └─ Paragraph // ├─ Text "This is a markup " // ├─ Emphasis // │ └─ Text "document" // └─ Text "." ``` -------------------------------- ### Build Markup Tree Declaratively Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Parsing-Building-and-Modifying Markup-Trees.md Construct a Document and its elements programmatically using initializers. Useful for inserting content from other data sources. ```swift import Markdown let document = Document( Paragraph( Text("This is a "), Emphasis( Text("paragraph.")))) ``` -------------------------------- ### Nested Block Directives Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Markdown/BlockDirectives.md Demonstrates nesting of block directives, showing how content can contain other directives. ```markdown @Outer { @Inner { - A - B - C } } ``` -------------------------------- ### Basic Swift Function Source: https://github.com/swiftlang/swift-markdown/blob/main/Tests/MarkdownTests/Visitors/Everything.md A simple Swift function declaration. ```swift func foo() { let x = 1 } ``` -------------------------------- ### Custom Line Prefix Formatting Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Formats a Markdown document using a custom prefix for each line. This is often used for specific output formats or custom list styles. ```swift import Markdown let document = Document(parsing: "Line 1\nLine 2") let customPrefixFormatter = FormatLinePrefix(prefix: "> ") let formattedDocument = document.formatted(using: customPrefixFormatter) print(formattedDocument.debugDescription) ``` -------------------------------- ### Parse a Markdown Document Source: https://github.com/swiftlang/swift-markdown/blob/main/README.md Use the Document(parsing:) initializer to parse a String or URL into a Markdown document. The debugDescription provides a hierarchical view of the parsed document. ```swift import Markdown let source = "This is a markup *document*." let document = Document(parsing: source) print(document.debugDescription()) // Document // └─ Paragraph // ├─ Text "This is a markup " // ├─ Emphasis // │ └─ Text "document" // └─ Text "." ``` -------------------------------- ### Preferred Heading Style Formatting Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Formats headings to use a preferred style, either Setext (underline) or Atx (hash marks). This ensures consistent heading representation. ```swift import Markdown let document = Document(parsing: "Title\n====\n\nAnother Title\n------") let headingFormatter = FormatPreferredHeadingStyle(preferredStyle: .atx) let formattedDocument = document.formatted(using: headingFormatter) print(formattedDocument.debugDescription) ``` -------------------------------- ### Enabling Block Directive Syntax in Swift Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Markdown/BlockDirectives.md Shows how to enable block directive parsing by passing the `.parseBlockDirectives` option to the `Document` initializer. ```swift let document = Document(parsing: source, options: .parseBlockDirectives) ``` -------------------------------- ### Format Markdown with Maximum Width Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Formats a Markdown document while respecting a maximum line width. Lines will be wrapped to fit within the specified limit. ```swift import Markdown let longText = "This is a very long line of text that needs to be wrapped to fit within a specific width." let document = Document(parsing: longText) let maxWidthFormatter = FormatWidth(maxWidth: 40) let formattedDocument = document.formatted(using: maxWidthFormatter) print(formattedDocument.debugDescription) ``` -------------------------------- ### Default Markdown Formatting Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Applies default formatting rules to a Markdown document to ensure a consistent style. This is the most common formatting scenario. ```swift import Markdown let document = Document(parsing: "# Title\n\n Indented paragraph.") let formattedDocument = document.formatted() print(formattedDocument.debugDescription) ``` -------------------------------- ### Use Code Fence Formatting Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Formats code blocks to use a specific type of code fence, typically triple backticks (```). This ensures consistent syntax highlighting indication. ```swift import Markdown let document = Document(parsing: "```swift\nprint(\"Hello\")\n```") let codeFenceFormatter = FormatUseCodeFence(preferredFence: .tripleBackticks) let formattedDocument = document.formatted(using: codeFenceFormatter) print(formattedDocument.debugDescription) ``` -------------------------------- ### Declare Swift Markdown Product Dependency in Target Source: https://github.com/swiftlang/swift-markdown/blob/main/README.md Include the 'Markdown' product from the Swift Markdown package in your target's dependencies. ```swift .target( name: "MyTarget", dependencies: [ .product(name: "Markdown", package: "swift-markdown"), ] ), ``` -------------------------------- ### MarkupVisitor Protocol Definition Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Visitors-Walkers-and-Rewriters.md The core MarkupVisitor protocol defines the structure for transforming, walking, or rewriting a markup tree. It includes an associated Result type for customization. ```swift public protocol MarkupVisitor { associatedtype Result } ``` -------------------------------- ### Parse Markdown File Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Parses the content of a Markdown file on disk into a structured Markup tree. Ensure the file path is correct and accessible. ```swift import Foundation import Markdown // Assuming "MyDocument.md" is in the same directory or a specified path let fileURL = URL(fileURLWithPath: "MyDocument.md") let document = try Document(contentsOf: fileURL) print(document.debugDescription) ``` -------------------------------- ### Unordered List Marker Formatting Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Formats unordered lists to use a consistent marker, such as '-', '+', or '*'. This standardizes list appearance. ```swift import Markdown let document = Document(parsing: "* Item 1\n- Item 2") let unorderedListFormatter = FormatUnorderedListMarker(preferredMarker: "-") let formattedDocument = document.formatted(using: unorderedListFormatter) print(formattedDocument.debugDescription) ``` -------------------------------- ### Emphasis Markers Formatting Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Applies formatting rules to standardize the markers used for emphasis (bold and italics). This ensures consistent use of `*` or `_`. ```swift import Markdown let document = Document(parsing: "*italic* or _italic_") let emphasisFormatter = FormatEmphasisMarkers(preferredMarker: "*") let formattedDocument = document.formatted(using: emphasisFormatter) print(formattedDocument.debugDescription) ``` -------------------------------- ### Indented Nested Block Directives Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Markdown/BlockDirectives.md Illustrates how indentation is handled for nested block directives, affecting both structure and parsing of content. ```markdown @Outer { @Inner { - A - B } } ``` -------------------------------- ### Add Swift Markdown Dependency to Package.swift Source: https://github.com/swiftlang/swift-markdown/blob/main/README.md Add the Swift Markdown package as a dependency in your Swift Package Manager manifest. ```swift .package(url: "https://github.com/swiftlang/swift-markdown.git", branch: "main"), ``` -------------------------------- ### Condense Autolinks Formatting Source: https://github.com/swiftlang/swift-markdown/blob/main/Sources/Markdown/Markdown.docc/Snippets.md Applies formatting rules to condense autolinks (like URLs) in a Markdown document. This can make the Markdown source cleaner. ```swift import Markdown let document = Document(parsing: "Visit .") 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\n

This 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.