diff --git a/Sources/Ink/API/Modifier.swift b/Sources/Ink/API/Modifier.swift index 24ecfa1..f365403 100644 --- a/Sources/Ink/API/Modifier.swift +++ b/Sources/Ink/API/Modifier.swift @@ -52,5 +52,6 @@ public extension Modifier { case links case lists case paragraphs + case text } } diff --git a/Sources/Ink/Internal/FormattedText.swift b/Sources/Ink/Internal/FormattedText.swift index 0be2e39..a74b7d3 100644 --- a/Sources/Ink/Internal/FormattedText.swift +++ b/Sources/Ink/Internal/FormattedText.swift @@ -32,7 +32,12 @@ internal struct FormattedText: Readable, HTMLConvertible, PlainTextConvertible { case .linebreak: string.append("
") 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) diff --git a/Sources/Ink/Internal/Substring+Modifiable.swift b/Sources/Ink/Internal/Substring+Modifiable.swift new file mode 100644 index 0000000..08e4bc7 --- /dev/null +++ b/Sources/Ink/Internal/Substring+Modifiable.swift @@ -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 } +} diff --git a/Tests/InkTests/ModifierTests.swift b/Tests/InkTests/ModifierTests.swift index e5f97dc..cd4fb3e 100644 --- a/Tests/InkTests/ModifierTests.swift +++ b/Tests/InkTests/ModifierTests.swift @@ -76,6 +76,42 @@ final class ModifierTests: XCTestCase { XCTAssertEqual(html, "

Code is cool:

Code\n
") } + + func testTextModifier() { + var parser = MarkdownParser() + + parser.addModifier(Modifier(target: .text) { + $0.html.uppercased() + }) + + let html = parser.html(from: "foo αγω éö") + + XCTAssertEqual(html, "

FOO ΑΓΩ ÉÖ

") + } + + func testTextModifierAffectsLinkText() { + var parser = MarkdownParser() + + parser.addModifier(Modifier(target: .text) { + $0.html.uppercased() + }) + + let html = parser.html(from: "foo [αγω](test) éö") + + XCTAssertEqual(html, #"

FOO ΑΓΩ ÉÖ

"#) + } + + func testTextModifierProtectsCodeAndHTML() { + var parser = MarkdownParser() + + parser.addModifier(Modifier(target: .text) { + $0.html.uppercased() + }) + + let html = parser.html(from: "foo `αγω` éö

foo `αγω` éö

") + + XCTAssertEqual(html, #"

FOO αγω ÉÖ

foo `αγω` éö

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