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

Allow Modifiers for plain text #45

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions Sources/Ink/API/Modifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ public extension Modifier {
case links
case lists
case paragraphs
case text
}
}
7 changes: 6 additions & 1 deletion Sources/Ink/Internal/FormattedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ internal struct FormattedText: Readable, HTMLConvertible, PlainTextConvertible {
case .linebreak:
string.append("<br>")
case .text(let text):
string.append(String(text))
let html = text.html(
usingURLs: urls,
rawString: text,
applyingModifiers: modifiers
)
string.append(html)
case .styleMarker(let marker):
let html = marker.html(usingURLs: urls, modifiers: modifiers)
string.append(html)
Expand Down
22 changes: 22 additions & 0 deletions Sources/Ink/Internal/Substring+Modifiable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Substring+Modifiable.swift
//
//
// Created by Ben Syverson on 2020/01/29.
//

extension Substring: HTMLConvertible {
func html(usingURLs urls: NamedURLCollection, modifiers: ModifierCollection) -> String {
return String(self)
}
}

extension Substring: PlainTextConvertible {
func plainText() -> String {
return String(self)
}
}

extension Substring: Modifiable {
var modifierTarget: Modifier.Target { .text }
}
41 changes: 40 additions & 1 deletion Tests/InkTests/ModifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,42 @@ final class ModifierTests: XCTestCase {

XCTAssertEqual(html, "<p>Code is cool:</p><pre><code>Code\n</code></pre>")
}

func testTextModifier() {
var parser = MarkdownParser()

parser.addModifier(Modifier(target: .text) {
$0.html.uppercased()
})

let html = parser.html(from: "foo αγω éö")

XCTAssertEqual(html, "<p>FOO ΑΓΩ ÉÖ</p>")
}

func testTextModifierAffectsLinkText() {
var parser = MarkdownParser()

parser.addModifier(Modifier(target: .text) {
$0.html.uppercased()
})

let html = parser.html(from: "foo [αγω](test) éö")

XCTAssertEqual(html, #"<p>FOO <a href="test">ΑΓΩ</a> ÉÖ</p>"#)
}

func testTextModifierProtectsCodeAndHTML() {
var parser = MarkdownParser()

parser.addModifier(Modifier(target: .text) {
$0.html.uppercased()
})

let html = parser.html(from: "foo `αγω` éö <p>foo `αγω` éö</p>")

XCTAssertEqual(html, #"<p>FOO <code>αγω</code> ÉÖ</p><p>foo `αγω` éö</p>"#)
}
}

extension ModifierTests {
Expand All @@ -84,7 +120,10 @@ extension ModifierTests {
("testModifierInput", testModifierInput),
("testInitializingParserWithModifiers", testInitializingParserWithModifiers),
("testAddingModifiers", testAddingModifiers),
("testMultipleModifiersForSameTarget", testMultipleModifiersForSameTarget)
("testMultipleModifiersForSameTarget", testMultipleModifiersForSameTarget),
("testTextModifier", testTextModifier),
("testTextModifierAffectsLinkText", testTextModifierAffectsLinkText),
("testTextModifierProtectsCodeAndHTML", testTextModifierProtectsCodeAndHTML),
]
}
}