Skip to content

Commit 0d04722

Browse files
committed
Example usage of snippets for Swift Evolution
1 parent 7dd6c52 commit 0d04722

File tree

9 files changed

+143
-1
lines changed

9 files changed

+143
-1
lines changed

[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
5858
// SwiftPM command plugins are only supported by Swift version 5.6 and later.
5959
#if swift(>=5.6)
6060
package.dependencies += [
61-
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
61+
.package(url: "https://github.com/apple/swift-docc-plugin", .branch("main")),
6262
]
6363
#endif
6464
} else {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! Format a parsed Markdown document with default settings.
2+
3+
import Markdown
4+
5+
let source = """
6+
|a|b|c|
7+
|-|-|-|
8+
|*Some text*||<https://swift.org>|
9+
"""
10+
11+
let document = Document(parsing: source)
12+
let formattedSource = document.format()
13+
14+
// MARK: Hide
15+
16+
print("""
17+
## Original source:
18+
```
19+
\(source)
20+
```
21+
""")
22+
23+
print("""
24+
## Formatted source:
25+
```
26+
\(formattedSource)
27+
```
28+
""")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! Keep lines under a certain length.
2+
import Markdown
3+
4+
let source = """
5+
This is a really, really, really, really, really, really, really, really, really, really, really long line.
6+
"""
7+
8+
let document = Document(parsing: source)
9+
let lineLimit = MarkupFormatter.Options.PreferredLineLimit(maxLength: 80, breakWith: .softBreak)
10+
let formattingOptions = MarkupFormatter.Options(preferredLineLimit: lineLimit)
11+
let newSource = document.format(options: formattingOptions)
12+
13+
// MARK: Hide
14+
15+
print("## Original source:")
16+
print("```")
17+
print(source)
18+
print("```")
19+
print()
20+
21+
print("## Formatted source:")
22+
print("```")
23+
print(newSource)
24+
print("```")
25+
print()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Parse the contents of a file by its ``URL`` without having to read
2+
//! its contents yourself.
3+
4+
import Foundation
5+
import Markdown
6+
7+
let file = URL(fileURLWithPath: "test.md")
8+
let document = try Document(parsing: file)
9+
10+
// MARK: HIDE
11+
12+
print("Parsed \(file.path)")
13+
print("## Parsed document structure")
14+
print(document.debugDescription())
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! Parse a ``String`` as Markdown
2+
3+
import Markdown
4+
5+
let source = "Some *Markdown* source"
6+
let document = Document(parsing: source)
7+
8+
// MARK: HIDE
9+
10+
print("Parsed \(source.debugDescription)")
11+
print("## Parsed document structure")
12+
print(document.debugDescription())

Snippets/Parsing/test.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Sample document
2+
3+
This is a *sample document*.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//! Remove all instances of a kind of element using a `MarkupRewriter`.
2+
import Markdown
3+
4+
// MARK: Hide
5+
6+
let source = """
7+
The strong emphasis element is **going to be** deleted.
8+
"""
9+
10+
// MARK: Show
11+
12+
struct StrongDeleter: MarkupRewriter {
13+
func visitStrong(_ strong: Strong) -> Markup? {
14+
return nil
15+
}
16+
}
17+
18+
let document = Document(parsing: source)
19+
var deleter = StrongDeleter()
20+
let newDocument = deleter.visit(document) as! Document
21+
22+
// MARK: Hide
23+
24+
print("## Original source:")
25+
print("```")
26+
print(source)
27+
print("```")
28+
print()
29+
30+
print("## Original Markdown tree:")
31+
print(document.debugDescription())
32+
print()
33+
34+
print("## New Markdown tree:")
35+
print(newDocument.debugDescription())
36+
print()
37+
38+
print("## New source:")
39+
print("```")
40+
print(newDocument.format())
41+
print("```")

Sources/Markdown/Markdown.docc/Markdown.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ The markup tree provided by this package is comprised of immutable/persistent, t
1010

1111
## Topics
1212

13+
### Snippets
14+
15+
- <doc:snippets>
16+
1317
### Getting Started
1418

1519
- <doc:Parsing-Building-and-Modifying-Markup-Trees>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Snippets
2+
3+
## Parsing
4+
5+
@Snippet(path: "swift-markdown/snippets/ParseDocumentString")
6+
@Snippet(path: "swift-markdown/snippets/ParseDocumentFile")
7+
8+
## Formatting
9+
10+
@Snippet(path: "swift-markdown/snippets/DefaultFormatting"
11+
@Snippet(path: "swift-markdown/snippets/MaximumWidth")
12+
13+
## Rewriters
14+
15+
@Snippet(path: "swift-markdown/snippets/RemoveElementKind")

0 commit comments

Comments
 (0)