File tree Expand file tree Collapse file tree 6 files changed +66
-0
lines changed Expand file tree Collapse file tree 6 files changed +66
-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
+
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] ) )
Original file line number Diff line number Diff line change
1
+
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
+ 1 . Hello
2
+ 2 . World
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