Skip to content

Commit 45f18ef

Browse files
committed
Add simple snippets support
1 parent ecb12ae commit 45f18ef

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Example on how to parse Markdown from file
2+
import Markdown
3+
import Foundation
4+
5+
// MARK: Hide
6+
let tempDirecotry = FileManager.default.temporaryDirectory
7+
let testFile = tempDirecotry.appendingPathComponent("Test.md")
8+
let testData = """
9+
1. Hello
10+
2. World
11+
""".data(using: .utf8)!
12+
try testData.write(to: testFile)
13+
// MARK: Show
14+
guard let source = try? String(contentsOf: testFile) else {
15+
fatalError("Unable to get the contents of the Test.md file")
16+
}
17+
let everythingDocument = Document(parsing: source)
18+
let document = Document(parsing: source)
19+
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+
//! Example on how to parse Markdown from string
2+
import Markdown
3+
4+
let source = "Hello world"
5+
let document = Document(parsing: source)
6+
print(document.debugDescription(options: [.printEverything]))

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)