From 34f63787109b39bb6a115b76a3d592485dabc474 Mon Sep 17 00:00:00 2001 From: Petr Pavlik Date: Tue, 2 Apr 2019 23:44:17 +0200 Subject: [PATCH 1/3] expose markdown parsing options --- Sources/LeafMarkdown/Tag.swift | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Sources/LeafMarkdown/Tag.swift b/Sources/LeafMarkdown/Tag.swift index b5c3004..c98245a 100644 --- a/Sources/LeafMarkdown/Tag.swift +++ b/Sources/LeafMarkdown/Tag.swift @@ -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 { @@ -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) From 1b783acaab157a3191f4c4c7f9947591c568cdb9 Mon Sep 17 00:00:00 2001 From: Petr Pavlik Date: Tue, 9 Apr 2019 23:39:50 +0200 Subject: [PATCH 2/3] add tests --- Tests/LeafMarkdownTests/LeafTests.swift | 35 ++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Tests/LeafMarkdownTests/LeafTests.swift b/Tests/LeafMarkdownTests/LeafTests.swift index c41daae..0f7061d 100644 --- a/Tests/LeafMarkdownTests/LeafTests.swift +++ b/Tests/LeafMarkdownTests/LeafTests.swift @@ -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! @@ -41,4 +43,35 @@ class LeafTests: XCTestCase { let resultString = String(data: result.data, encoding: .utf8)! XCTAssertEqual(resultString, expectedHtml) } + + func testStripHtml() throws { + let inputMarkdown = "
" + let data = TemplateData.dictionary(["data": .string(inputMarkdown)]) + let expectedHtml = "\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 = "
" + let data = TemplateData.dictionary(["data": .string(inputMarkdown)]) + let expectedHtml = "
\n" + + let result = try renderer.render(template: template.data(using: .utf8)!, data).wait() + let resultString = String(data: result.data, encoding: .utf8)! + XCTAssertEqual(resultString, expectedHtml) + + } } From 6866aae0d3634a3c40eba600a5973f75c900632c Mon Sep 17 00:00:00 2001 From: Petr Pavlik Date: Tue, 9 Apr 2019 23:42:15 +0200 Subject: [PATCH 3/3] fix a typo --- Tests/LeafMarkdownTests/LeafTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/LeafMarkdownTests/LeafTests.swift b/Tests/LeafMarkdownTests/LeafTests.swift index 0f7061d..db388a7 100644 --- a/Tests/LeafMarkdownTests/LeafTests.swift +++ b/Tests/LeafMarkdownTests/LeafTests.swift @@ -9,7 +9,7 @@ class LeafTests: XCTestCase { ("testRunTag", testRunTag), ("testNilParameterDoesNotCrashLeaf", testNilParameterDoesNotCrashLeaf), ("testStripHtml", testStripHtml), - ("testDonotStripHtml", testDonotStripHtml) + ("testDoNotStripHtml", testDoNotStripHtml) ] var renderer: LeafRenderer! @@ -55,7 +55,7 @@ class LeafTests: XCTestCase { } - func testDonotStripHtml() throws { + func testDoNotStripHtml() throws { let queue = EmbeddedEventLoop() let container = BasicContainer(config: .init(), environment: .testing, services: .init(), on: queue)