File tree Expand file tree Collapse file tree 5 files changed +70
-0
lines changed Expand file tree Collapse file tree 5 files changed +70
-0
lines changed Original file line number Diff line number Diff line change
1
+ Examples on how to use Swift Markdown Framework to parse the Markdown String into Swift object
Original file line number Diff line number Diff line change
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] ) )
Original file line number Diff line number Diff line change
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] ) )
Original file line number Diff line number Diff line change
1
+ Examples on how to use Swift Markdown Framework to visit the Markdown object
Original file line number Diff line number Diff line change
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)
You can’t perform that action at this time.
0 commit comments