Skip to content
Merged
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
10 changes: 10 additions & 0 deletions CDMarkdownKitTests/TestCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ final class TestCode: XCTestCase {
XCTAssertTrue(TestHelpers.isMonospaced(testString: parsed, at: 6))
}

func testEmojiInsideCode() throws {
let parser = getParser()

let parsed = parser.parse("`test 👋 abc`")
XCTAssertEqual(parsed.string, "test 👋 abc")
XCTAssertTrue(TestHelpers.isMonospaced(testString: parsed, at: 1))
XCTAssertTrue(TestHelpers.isMonospaced(testString: parsed, at: 9))
XCTAssertTrue(TestHelpers.isMonospaced(testString: parsed, at: 10))
}

/*
// Expected to fail currently, because the CodeEscaping does not differentiate between code and syntax and always allows new lines
func testCodeMultiline() throws {
Expand Down
11 changes: 11 additions & 0 deletions CDMarkdownKitTests/TestSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,15 @@ final class TestSyntax: XCTestCase {
XCTAssertTrue(TestHelpers.isMonospaced(testString: parsed, at: 4))
}

func testEmojiInsideSyntax() throws {
let parser = getParser()

let parsed = parser.parse("```\nsyntax\n👍ABC\n```")
XCTAssertEqual(parsed.string, "syntax\n👍ABC\n")
XCTAssertTrue(TestHelpers.isMonospaced(testString: parsed, at: 4))
XCTAssertTrue(TestHelpers.isMonospaced(testString: parsed, at: 10))
XCTAssertTrue(TestHelpers.isMonospaced(testString: parsed, at: 11))
XCTAssertTrue(TestHelpers.isMonospaced(testString: parsed, at: 12))
}

}
5 changes: 4 additions & 1 deletion Source/CDMarkdownCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@
guard let unescapedString = matchString.fromBase64() else { return }
attributedString.replaceCharacters(in: range,
with: unescapedString)

// Since we use NSRange here, we need to count based on UTF16, alternatively could use NSString length property
let range = NSRange(location: range.location,
length: unescapedString.characterCount())
length: unescapedString.utf16.count)
attributedString.addAttributes(attributes,
range: range)

Check warning on line 77 in Source/CDMarkdownCode.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
let mutableString = attributedString.mutableString
// Remove \n if in string, not valid in Code element
// Use Syntax element for \n to parse in string
Expand Down
3 changes: 2 additions & 1 deletion Source/CDMarkdownSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ open class CDMarkdownSyntax: CDMarkdownCommonElement {
attributedString.replaceCharacters(in: range,
with: unescapedString)

// Since we use NSRange here, we need to count based on UTF16, alternatively could use NSString length property
let range = NSRange(location: range.location,
length: unescapedString.characterCount())
length: unescapedString.utf16.count)
attributedString.addAttributes(attributes,
range: range)
// If the previous character was a newline then parser doesn't have to worry about
Expand Down
4 changes: 0 additions & 4 deletions Source/String+CDMarkdownKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ internal extension String {
return from ..< to
}

func characterCount() -> Int {
return self.count
}

func sizeWithAttributes(_ attributes: [CDAttributedStringKey: Any]? = nil) -> CGSize {
return self.size(withAttributes: attributes)
}
Expand Down
Loading