Skip to content

Commit c1831de

Browse files
committed
Add simple snippets support
1 parent 52563fc commit c1831de

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

Snippets/Parsing/Explanation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Examples on how to use Swift Markdown Framework to parse the Markdown String into Swift object

Snippets/Parsing/ParseFile.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
import Markdown
3+
import Foundation
4+
5+
guard let url = Bundle.main.url(forResource: "Test", withExtension: "md") else {
6+
fatalError("Unable to locate Test.md resource file")
7+
}
8+
guard let source = try? String(contentsOf: url) else {
9+
fatalError("Unable to get the contents of the Test.md resource file")
10+
}
11+
let everythingDocument = Document(parsing: source)
12+
let document = Document(parsing: source)
13+
print(document.debugDescription(options: [.printEverything]))

Snippets/Parsing/ParseString.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
import Markdown
3+
4+
let source = "Hello world"
5+
let document = Document(parsing: source)
6+
print(document.debugDescription(options: [.printEverything]))

Snippets/Parsing/Resources/Test.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1. Hello
2+
2. World

Snippets/Visiting/Explanation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Examples on how to use Swift Markdown Framework to visit the Markdown object

Snippets/Visiting/MarkupCounter.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! Define a MarkupCounter struct to count the Text and UnorderedList count of the Markup Object
2+
import Markdown
3+
struct MarkupCounter: MarkupWalker {
4+
private var textCount = 0
5+
private var unorderedListCount = 0
6+
private var unorderedListItemsCount = 0
7+
8+
// MARK: Hide
9+
var result: String {
10+
"""
11+
The Markup has:
12+
\(textCount) Text elements
13+
\(unorderedListCount) UnorderedList elements with a total of \(unorderedListItemsCount) items
14+
"""
15+
}
16+
// MARK: Show
17+
18+
mutating func visitText(_ text: Text) -> () {
19+
print("Visiting Text Element: \(text.string)\n")
20+
textCount += 1
21+
}
22+
mutating func visitUnorderedList(_ unorderedList: UnorderedList) -> () {
23+
print("Visiting UnorderedList Element: \(unorderedList.format())\n")
24+
unorderedListCount += 1
25+
unorderedListItemsCount += unorderedList.childCount
26+
}
27+
}
28+
29+
let source = """
30+
Hello
31+
32+
- Item 1
33+
- Item 2
34+
35+
world
36+
37+
- Item 3
38+
"""
39+
let document = Document(parsing: source)
40+
41+
var dumper = MarkupCounter()
42+
dumper.visit(document)
43+
print(dumper.result)

0 commit comments

Comments
 (0)