Skip to content

Add simple snippets support #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/*.xcodeproj
.swiftpm
Package.resolved
.vscode
1 change: 1 addition & 0 deletions Snippets/Parsing/Explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Examples on how to use Swift Markdown Framework to parse the Markdown String into Swift object
19 changes: 19 additions & 0 deletions Snippets/Parsing/ParseFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Example on how to parse Markdown from file
import Markdown
import Foundation

// MARK: Hide
let tempDirecotry = FileManager.default.temporaryDirectory
let testFile = tempDirecotry.appendingPathComponent("Test.md")
let testData = """
1. Hello
2. World
""".data(using: .utf8)!
try testData.write(to: testFile)
// MARK: Show
guard let source = try? String(contentsOf: testFile) else {
fatalError("Unable to get the contents of the Test.md file")
}
let everythingDocument = Document(parsing: source)
let document = Document(parsing: source)
print(document.debugDescription(options: [.printEverything]))
6 changes: 6 additions & 0 deletions Snippets/Parsing/ParseString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Example on how to parse Markdown from string
import Markdown

let source = "Hello world"
let document = Document(parsing: source)
print(document.debugDescription(options: [.printEverything]))
1 change: 1 addition & 0 deletions Snippets/Visiting/Explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Examples on how to use Swift Markdown Framework to visit the Markdown object
43 changes: 43 additions & 0 deletions Snippets/Visiting/MarkupCounter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! Define a MarkupCounter struct to count the Text and UnorderedList count of the Markup Object
import Markdown
struct MarkupCounter: MarkupWalker {
private var textCount = 0
private var unorderedListCount = 0
private var unorderedListItemsCount = 0

// MARK: Hide
var result: String {
"""
The Markup has:
\(textCount) Text elements
\(unorderedListCount) UnorderedList elements with a total of \(unorderedListItemsCount) items
"""
}
// MARK: Show

mutating func visitText(_ text: Text) -> () {
print("Visiting Text Element: \(text.string)\n")
textCount += 1
}
mutating func visitUnorderedList(_ unorderedList: UnorderedList) -> () {
print("Visiting UnorderedList Element: \(unorderedList.format())\n")
unorderedListCount += 1
unorderedListItemsCount += unorderedList.childCount
}
}

let source = """
Hello

- Item 1
- Item 2

world

- Item 3
"""
let document = Document(parsing: source)

var dumper = MarkupCounter()
dumper.visit(document)
print(dumper.result)