diff --git a/MatrixKit.podspec b/MatrixKit.podspec index 6d026c9bf..344deeab6 100644 --- a/MatrixKit.podspec +++ b/MatrixKit.podspec @@ -27,6 +27,6 @@ Pod::Spec.new do |s| s.dependency 'HPGrowingTextView', '~> 1.1' s.dependency 'libPhoneNumber-iOS', '~> 0.8.14' s.dependency 'DTCoreText', '~> 1.6.17' - s.dependency 'GHMarkdownParser', '~> 0.1.2' + s.dependency 'cmark', '~> 0.24.1' end diff --git a/MatrixKit/Utils/MXKEventFormatter.m b/MatrixKit/Utils/MXKEventFormatter.m index a1a142595..c33666840 100644 --- a/MatrixKit/Utils/MXKEventFormatter.m +++ b/MatrixKit/Utils/MXKEventFormatter.m @@ -22,7 +22,7 @@ #import "MXKTools.h" #import "DTCoreText.h" -#import "GHMarkdownParser.h" +#import "cmark.h" #import "MXDecryptionResult.h" @@ -33,11 +33,6 @@ @interface MXKEventFormatter () */ MXSession *mxSession; - /** - The Markdown to HTML parser. - */ - GHMarkdownParser *markdownParser; - /** The default CSS converted in DTCoreText object. */ @@ -64,10 +59,6 @@ - (instancetype)initWithMatrixSession:(MXSession *)matrixSession [self initDateTimeFormatters]; - markdownParser = [[GHMarkdownParser alloc] init]; - markdownParser.options = kGHMarkdownAutoLink | kGHMarkdownNoSmartQuotes; - markdownParser.githubFlavored = YES; // This is the Markdown flavor we use in Matrix apps - // Use the same list as matrix-react-sdk ( https://github.com/matrix-org/matrix-react-sdk/blob/24223ae2b69debb33fa22fcda5aeba6fa93c93eb/src/HtmlUtils.js#L25 ) _allowedHTMLTags = @[ @"font", // custom to matrix for IRC-style font coloring @@ -1448,15 +1439,16 @@ - (UIFont*)fontForEvent:(MXEvent*)event - (NSString *)htmlStringFromMarkdownString:(NSString *)markdownString { - // In GitHub Flavored Mardown, the '#' sign creates an HTML header only if it is - // followed by the space char. - // But GHMarkdownParser creates an HTML header everytime it finds a '#' sign which - // kills room aliases (like #matrix:matrix.org). - // So, escape them if they are not followed by a space - NSString *str = [markdownString stringByReplacingOccurrencesOfString:@"(#+)[^( |#)]" withString:@"\\\\$0" options:NSRegularExpressionSearch range:NSMakeRange(0, markdownString.length)]; - - NSString *htmlString = [markdownParser HTMLStringFromMarkdownString:str]; + const char *cstr = [markdownString cStringUsingEncoding: NSUTF8StringEncoding]; + const char *htmlCString = cmark_markdown_to_html(cstr, strlen(cstr), CMARK_OPT_HARDBREAKS); + NSString *htmlString = [[NSString alloc] initWithCString:htmlCString encoding:NSUTF8StringEncoding]; + // Strip off the trailing newline, if it exists. + if ([htmlString hasSuffix:@"\n"]) + { + htmlString = [htmlString substringToIndex:htmlString.length - 1]; + } + // Strip start and end
tags else you get 'orrible spacing if ([htmlString hasPrefix:@"
"])
{
diff --git a/MatrixKitTests/MXKEventFormatterTests.m b/MatrixKitTests/MXKEventFormatterTests.m
index 897b72cc7..b479bbd0e 100644
--- a/MatrixKitTests/MXKEventFormatterTests.m
+++ b/MatrixKitTests/MXKEventFormatterTests.m
@@ -71,6 +71,19 @@ - (void)testRenderHTMLStringWithPreCode
}];
}
+
+- (void)testMarkdownFormatting
+{
+ NSString *html = [eventFormatter htmlStringFromMarkdownString:@"Line One.\nLine Two."];
+
+ BOOL hardBreakExists = [html rangeOfString:@"
"].location != NSNotFound;
+ BOOL openParagraphExists = [html rangeOfString:@"
"].location != NSNotFound; + BOOL closeParagraphExists = [html rangeOfString:@"
"].location != NSNotFound; + + // Check for some known error cases + XCTAssert(hardBreakExists, "The soft break (\\n) must be converted to a hard break (