Skip to content

Commit a7a5e97

Browse files
committed
Adopt more snippets
Add snippets for the following areas: - Parsing - Querying - Walkers, Rewriters, and Visitors - Formatting Add a link to the top-level page. Add an article which inlines all of the snippets in one place for quick reference. rdar://103054461
1 parent cca62f7 commit a7a5e97

20 files changed

+570
-72
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Format links that use URLs as their link text into autolinks.
2+
3+
import Markdown
4+
5+
let source = """
6+
This [https://swift.org](https://swift.org) link will become <https://swift.org>
7+
"""
8+
9+
let document = Document(parsing: source)
10+
let formattingOptions = MarkupFormatter.Options(condenseAutolinks: true)
11+
let formattedSource = document.format(options: formattingOptions)
12+
13+
print("""
14+
## Original source:
15+
\(source)
16+
17+
## Formatted source:
18+
\(formattedSource)
19+
""")
20+
// snippet.hide
21+
/*
22+
This source file is part of the Swift.org open source project
23+
24+
Copyright (c) 2022 Apple Inc. and the Swift project authors
25+
Licensed under Apache License v2.0 with Runtime Library Exception
26+
27+
See https://swift.org/LICENSE.txt for license information
28+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
29+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Format a Markdown document to have a custom line prefix, such as a comment prefix for use in source code.
2+
3+
import Markdown
4+
5+
let source = """
6+
This document's lines
7+
will be prefixed with `//`.
8+
"""
9+
10+
let document = Document(parsing: source)
11+
let formattingOptions = MarkupFormatter.Options(customLinePrefix: "// ")
12+
let formattedSource = document.format(options: formattingOptions)
13+
14+
print("""
15+
## Original source:
16+
\(source)
17+
18+
## Formatted source:
19+
\(formattedSource)
20+
""")
21+
// snippet.hide
22+
/*
23+
This source file is part of the Swift.org open source project
24+
25+
Copyright (c) 2022 Apple Inc. and the Swift project authors
26+
Licensed under Apache License v2.0 with Runtime Library Exception
27+
28+
See https://swift.org/LICENSE.txt for license information
29+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
30+
*/
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1-
// Format a parsed Markdown document with default settings.
1+
// Format Markdown with the default settings.
22

33
import Markdown
44

55
let source = """
66
|a|b|c|
7-
|-|-|-|
8-
|*Some text*||<https://swift.org>|
7+
|-|:-|-:|
8+
|*Some text*|x|<https://swift.org>|
99
"""
1010

11+
// There is not an option for formatting tables per se but is useful to show the behavior for tables.
12+
// Table columns are automatically expanded to fit the column's largest
13+
// cell, making the table easier to read in the Markdown source.
14+
1115
let document = Document(parsing: source)
1216
let formattedSource = document.format()
1317

14-
// MARK: Hide
15-
1618
print("""
1719
## Original source:
18-
```
1920
\(source)
20-
```
21-
""")
2221
23-
print("""
2422
## Formatted source:
25-
```
2623
\(formattedSource)
27-
```
2824
""")
25+
// snippet.hide
26+
/*
27+
This source file is part of the Swift.org open source project
28+
29+
Copyright (c) 2022 Apple Inc. and the Swift project authors
30+
Licensed under Apache License v2.0 with Runtime Library Exception
31+
32+
See https://swift.org/LICENSE.txt for license information
33+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
34+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Format a consistent style for emphasis markers.
2+
3+
import Markdown
4+
5+
let source = """
6+
This document uses a mix of *star* and _underbar_ emphasized elements.
7+
"""
8+
9+
let document = Document(parsing: source)
10+
// Use only * for emphasis markers.
11+
let emphasisMarker = MarkupFormatter.Options.EmphasisMarker.star
12+
let formattedSource = document.format(options: .init(emphasisMarker: emphasisMarker))
13+
14+
print("""
15+
## Original source:
16+
\(source)
17+
18+
## Formatted source:
19+
\(formattedSource)
20+
""")
21+
// snippet.hide
22+
/*
23+
This source file is part of the Swift.org open source project
24+
25+
Copyright (c) 2022 Apple Inc. and the Swift project authors
26+
Licensed under Apache License v2.0 with Runtime Library Exception
27+
28+
See https://swift.org/LICENSE.txt for license information
29+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
30+
*/
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
// Keep lines under a certain length.
1+
// Format lines to stay under a certain length.
2+
23
import Markdown
34

45
let source = """
56
This is a really, really, really, really, really, really, really, really, really, really, really long line.
67
"""
78

89
let document = Document(parsing: source)
10+
// Break lines longer than 80 characters in width with a soft break.
911
let lineLimit = MarkupFormatter.Options.PreferredLineLimit(maxLength: 80, breakWith: .softBreak)
1012
let formattingOptions = MarkupFormatter.Options(preferredLineLimit: lineLimit)
11-
let newSource = document.format(options: formattingOptions)
13+
let formattedSource = document.format(options: formattingOptions)
14+
15+
print("""
16+
## Original source:
17+
\(source)
1218
13-
// MARK: Hide
19+
## Formatted source:
20+
\(formattedSource)
21+
""")
22+
// snippet.hide
23+
/*
24+
This source file is part of the Swift.org open source project
1425

15-
print("## Original source:")
16-
print("```")
17-
print(source)
18-
print("```")
19-
print()
26+
Copyright (c) 2022 Apple Inc. and the Swift project authors
27+
Licensed under Apache License v2.0 with Runtime Library Exception
2028

21-
print("## Formatted source:")
22-
print("```")
23-
print(newSource)
24-
print("```")
25-
print()
29+
See https://swift.org/LICENSE.txt for license information
30+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
31+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Format the counting behavior of ordered lists.
2+
3+
import Markdown
4+
5+
let source = """
6+
1. An
7+
2. ordered
8+
3. list
9+
"""
10+
11+
let document = Document(parsing: source)
12+
// Use all 0. markers to allow easily reordering ordered list items.
13+
let formattingOptions = MarkupFormatter.Options(orderedListNumerals: .allSame(1))
14+
let formattedSource = document.format(options: formattingOptions)
15+
16+
print("""
17+
## Original source:
18+
\(source)
19+
20+
## Formatted source:
21+
\(formattedSource)
22+
""")
23+
// snippet.hide
24+
/*
25+
This source file is part of the Swift.org open source project
26+
27+
Copyright (c) 2022 Apple Inc. and the Swift project authors
28+
Licensed under Apache License v2.0 with Runtime Library Exception
29+
30+
See https://swift.org/LICENSE.txt for license information
31+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
32+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Format a Markdown document to use ATX style headings throughout.
2+
3+
import Markdown
4+
5+
let source = """
6+
# Title
7+
8+
## Second-level Heading
9+
10+
Another Second-level Heading
11+
----------------------------
12+
13+
The above heading will be converted to ATX style, using hashes.
14+
"""
15+
16+
let document = Document(parsing: source)
17+
let headingStyle = MarkupFormatter.Options.PreferredHeadingStyle.atx
18+
let formattingOptions = MarkupFormatter.Options(preferredHeadingStyle: headingStyle)
19+
let formattedSource = document.format(options: formattingOptions)
20+
21+
print("""
22+
## Original source:
23+
\(source)
24+
25+
## Formatted source:
26+
\(formattedSource)
27+
""")
28+
// snippet.hide
29+
/*
30+
This source file is part of the Swift.org open source project
31+
32+
Copyright (c) 2022 Apple Inc. and the Swift project authors
33+
Licensed under Apache License v2.0 with Runtime Library Exception
34+
35+
See https://swift.org/LICENSE.txt for license information
36+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
37+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Format a consistent style for thematic breaks.
2+
3+
import Markdown
4+
5+
let source = """
6+
First paragraph.
7+
8+
-----
9+
10+
Second paragraph.
11+
12+
*****
13+
"""
14+
15+
let document = Document(parsing: source)
16+
let thematicBreakCharacter = MarkupFormatter.Options.ThematicBreakCharacter.dash
17+
// Make all thematic breaks 10 dash `-` characters.
18+
let formattingOptions = MarkupFormatter.Options(thematicBreakCharacter: thematicBreakCharacter, thematicBreakLength: 10)
19+
let formattedSource = document.format(options: formattingOptions)
20+
21+
print("""
22+
## Original source:
23+
\(source)
24+
25+
## Formatted source:
26+
\(formattedSource)
27+
""")
28+
// snippet.hide
29+
/*
30+
This source file is part of the Swift.org open source project
31+
32+
Copyright (c) 2022 Apple Inc. and the Swift project authors
33+
Licensed under Apache License v2.0 with Runtime Library Exception
34+
35+
See https://swift.org/LICENSE.txt for license information
36+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
37+
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Format the unordered list marker.
2+
3+
import Markdown
4+
5+
let source = """
6+
- An
7+
- unordered
8+
- list
9+
"""
10+
11+
let document = Document(parsing: source)
12+
// Use an star or asterisk `*` as the unordered list marker.
13+
let formattedSource = document.format(options: .init(unorderedListMarker: .star))
14+
15+
print("""
16+
## Original source:
17+
\(source)
18+
19+
## Formatted source:
20+
\(formattedSource)
21+
""")
22+
// snippet.hide
23+
/*
24+
This source file is part of the Swift.org open source project
25+
26+
Copyright (c) 2022 Apple Inc. and the Swift project authors
27+
Licensed under Apache License v2.0 with Runtime Library Exception
28+
29+
See https://swift.org/LICENSE.txt for license information
30+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
31+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Format all code blocks to use a consistent style for code blocks,
2+
// optionally setting the default info string to declare that they
3+
// have a particular syntax.
4+
5+
import Markdown
6+
7+
let source = """
8+
This document contains a mix of indented and fenced code blocks.
9+
10+
A code block.
11+
12+
```
13+
func foo() {}
14+
```
15+
"""
16+
17+
let document = Document(parsing: source)
18+
// Always fenced code blocks.
19+
let fencedCodeBlock = MarkupFormatter.Options.UseCodeFence.always
20+
// Use `swift` as the info string on all fenced code blocks.
21+
let defaultCodeBlockLanguage = "swift"
22+
let formattedSource = document.format(options: .init(useCodeFence: fencedCodeBlock, defaultCodeBlockLanguage: defaultCodeBlockLanguage))
23+
24+
print("""
25+
## Original source:
26+
\(source)
27+
28+
## Formatted source:
29+
\(formattedSource)
30+
""")
31+
// snippet.hide
32+
/*
33+
This source file is part of the Swift.org open source project
34+
35+
Copyright (c) 2022 Apple Inc. and the Swift project authors
36+
Licensed under Apache License v2.0 with Runtime Library Exception
37+
38+
See https://swift.org/LICENSE.txt for license information
39+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
40+
*/

Snippets/Parsing/ParseDocumentFile.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
import Foundation
44
import Markdown
55

6-
let file = URL(fileURLWithPath: "test.md")
7-
let document = try Document(parsing: file)
6+
// snippet.hide
7+
let inputFileURL = URL(fileURLWithPath: #file).deletingLastPathComponent().appendingPathComponent("test.md")
8+
// snippet.show
9+
let document = try Document(parsing: inputFileURL)
810

9-
// MARK: HIDE
10-
11-
print("Parsed \(file.path)")
12-
print("## Parsed document structure")
1311
print(document.debugDescription())
12+
// snippet.hide
13+
/*
14+
This source file is part of the Swift.org open source project
15+
16+
Copyright (c) 2022 Apple Inc. and the Swift project authors
17+
Licensed under Apache License v2.0 with Runtime Library Exception
18+
19+
See https://swift.org/LICENSE.txt for license information
20+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
21+
*/

0 commit comments

Comments
 (0)