Skip to content
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

Pass plain text to the modifier closure where available #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Sources/Ink/API/Modifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct Modifier {
/// contains the HTML that was generated for a fragment, and
/// its raw Markdown representation. Note that for metadata
/// targets, the two input arguments will be equivalent.
public typealias Input = (html: String, markdown: Substring)
public typealias Input = (html: String, plainText: String?, markdown: Substring)
/// The type of closure that Modifiers are based on. Each
/// modifier is given a set of input, and is expected to return
/// an HTML string after performing its modifications.
Expand Down
7 changes: 6 additions & 1 deletion Sources/Ink/Internal/HTMLConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ extension HTMLConvertible where Self: Modifiable {
applyingModifiers modifiers: ModifierCollection) -> String {
var html = self.html(usingURLs: urls, modifiers: modifiers)

var plainText: String? = nil
if let plainTextConvertible = self as? PlainTextConvertible {
plainText = plainTextConvertible.plainText()
}

modifiers.applyModifiers(for: modifierTarget) { modifier in
html = modifier.closure((html, rawString))
html = modifier.closure((html, plainText, rawString))
}

return html
Expand Down
4 changes: 2 additions & 2 deletions Sources/Ink/Internal/Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ internal struct Metadata: Readable {

modifiers.applyModifiers(for: .metadataKeys) { modifier in
for (key, value) in modified.values {
let newKey = modifier.closure((key, Substring(key)))
let newKey = modifier.closure((key, key, Substring(key)))
modified.values[key] = nil
modified.values[newKey] = value
}
}

modifiers.applyModifiers(for: .metadataValues) { modifier in
modified.values = modified.values.mapValues { value in
modifier.closure((value, Substring(value)))
modifier.closure((value, value, Substring(value)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/InkTests/MarkdownTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ final class MarkdownTests: XCTestCase {

func testMetadataModifiers() {
let parser = MarkdownParser(modifiers: [
Modifier(target: .metadataKeys) { key, _ in
Modifier(target: .metadataKeys) { key, _, _ in
"ModifiedKey-" + key
},
Modifier(target: .metadataValues) { value, _ in
Modifier(target: .metadataValues) { value, _, _ in
"ModifiedValue-" + value
}
])
Expand Down
7 changes: 6 additions & 1 deletion Tests/InkTests/ModifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import Ink
final class ModifierTests: XCTestCase {
func testModifierInput() {
var allHTML = [String]()
var allPlainText = [String]()
var allMarkdown = [Substring]()

let parser = MarkdownParser(modifiers: [
Modifier(target: .paragraphs) { html, markdown in
Modifier(target: .paragraphs) { html, plainText, markdown in
allHTML.append(html)
if let plainText = plainText {
allPlainText.append(plainText)
}
allMarkdown.append(markdown)
return html
}
Expand All @@ -23,6 +27,7 @@ final class ModifierTests: XCTestCase {
let html = parser.html(from: "One\n\nTwo\n\nThree")
XCTAssertEqual(html, "<p>One</p><p>Two</p><p>Three</p>")
XCTAssertEqual(allHTML, ["<p>One</p>", "<p>Two</p>", "<p>Three</p>"])
XCTAssertEqual(allPlainText, ["One", "Two", "Three"])
XCTAssertEqual(allMarkdown, ["One", "Two", "Three"])
}

Expand Down