Skip to content

Commit

Permalink
Merge pull request #9 from petrpavlik/master
Browse files Browse the repository at this point in the history
Expose markdown parsing options
  • Loading branch information
0xTim committed Apr 12, 2019
2 parents 1e34264 + 6866aae commit d9a43ac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
14 changes: 12 additions & 2 deletions Sources/LeafMarkdown/Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ public final class Markdown: TagRenderer {

public let name = "markdown"

public init() {}
private let options: MarkdownOptions?

public init(options: MarkdownOptions? = nil) {
self.options = options
}

public func render(tag: TagContext) throws -> Future<TemplateData> {

Expand All @@ -23,7 +27,13 @@ public final class Markdown: TagRenderer {
markdown = markdownArgumentValue
}

let markdownHTML = try markdownToHTML(markdown)
let markdownHTML: String = try {
if let options = options {
return try markdownToHTML(markdown, options: options)
} else {
return try markdownToHTML(markdown)
}
}()

return Future.map(on: tag) {
.string(markdownHTML)
Expand Down
35 changes: 34 additions & 1 deletion Tests/LeafMarkdownTests/LeafTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import LeafMarkdown
class LeafTests: XCTestCase {
static var allTests = [
("testRunTag", testRunTag),
("testNilParameterDoesNotCrashLeaf", testNilParameterDoesNotCrashLeaf)
("testNilParameterDoesNotCrashLeaf", testNilParameterDoesNotCrashLeaf),
("testStripHtml", testStripHtml),
("testDoNotStripHtml", testDoNotStripHtml)
]

var renderer: LeafRenderer!
Expand Down Expand Up @@ -41,4 +43,35 @@ class LeafTests: XCTestCase {
let resultString = String(data: result.data, encoding: .utf8)!
XCTAssertEqual(resultString, expectedHtml)
}

func testStripHtml() throws {
let inputMarkdown = "<br>"
let data = TemplateData.dictionary(["data": .string(inputMarkdown)])
let expectedHtml = "<!-- raw HTML omitted -->\n"

let result = try renderer.render(template: template.data(using: .utf8)!, data).wait()
let resultString = String(data: result.data, encoding: .utf8)!
XCTAssertEqual(resultString, expectedHtml)

}

func testDoNotStripHtml() throws {

let queue = EmbeddedEventLoop()
let container = BasicContainer(config: .init(), environment: .testing, services: .init(), on: queue)
let tag = Markdown(options: [])
var leafTagConfig = LeafTagConfig.default()
leafTagConfig.use(tag, as: tag.name)
let renderer = LeafRenderer(config: LeafConfig(tags: leafTagConfig, viewsDir: "", shouldCache: false),
using: container)

let inputMarkdown = "<br>"
let data = TemplateData.dictionary(["data": .string(inputMarkdown)])
let expectedHtml = "<br>\n"

let result = try renderer.render(template: template.data(using: .utf8)!, data).wait()
let resultString = String(data: result.data, encoding: .utf8)!
XCTAssertEqual(resultString, expectedHtml)

}
}

0 comments on commit d9a43ac

Please sign in to comment.