-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dmcarth/master
Added options and removed warning
- Loading branch information
Showing
2 changed files
with
30 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,37 @@ | ||
import ccmark | ||
|
||
public enum MarkdownError: Error { | ||
case conversionFailed | ||
case conversionFailed | ||
} | ||
|
||
public func markdownToHTML(_ str: String) throws -> String { | ||
public struct MarkdownOptions: OptionSet { | ||
public let rawValue: Int32 | ||
|
||
public init(rawValue: Int32) { | ||
self.rawValue = rawValue | ||
} | ||
|
||
static public let sourcePosition = MarkdownOptions(rawValue: 1 << 1) | ||
static public let hardBreaks = MarkdownOptions(rawValue: 1 << 2) | ||
static public let safe = MarkdownOptions(rawValue: 1 << 3) | ||
static public let noBreaks = MarkdownOptions(rawValue: 1 << 4) | ||
static public let normalize = MarkdownOptions(rawValue: 1 << 8) | ||
static public let validateUTF8 = MarkdownOptions(rawValue: 1 << 9) | ||
static public let smartQuotes = MarkdownOptions(rawValue: 1 << 10) | ||
} | ||
|
||
public func markdownToHTML(_ str: String, options: MarkdownOptions = []) throws -> String { | ||
var buffer: String? | ||
try str.withCString { | ||
//TODO: add options, right now passing 0 | ||
guard let buf = cmark_markdown_to_html($0, Int(strlen($0)), 0) else { | ||
throw MarkdownError.conversionFailed | ||
} | ||
buffer = String(cString: buf) | ||
free(buf) | ||
} | ||
guard let output = buffer else { | ||
throw MarkdownError.conversionFailed | ||
} | ||
return output | ||
try str.withCString { | ||
guard let buf = cmark_markdown_to_html($0, Int(strlen($0)), options.rawValue) else { | ||
throw MarkdownError.conversionFailed | ||
} | ||
buffer = String(cString: buf) | ||
free(buf) | ||
} | ||
guard let output = buffer else { | ||
throw MarkdownError.conversionFailed | ||
} | ||
return output | ||
} | ||
|