This is a Markdown => NSAttributedString parser built on top of a flex parser. It takes an NSString and returns an NSAttributedString with markdown tags replaced by CoreText formatting attributes.
This project is a fork of NSAttributedMarkdownParser by NimbusKit: https://github.com/NimbusKit/markdown
Add pod 'XNGMarkdownParser'
to your Podfile and do a pod install
.
- Drag all of the files from the src/ directory into your project.
- Import XNGMarkdownParser.h in your project.
- Create an instance of the parser object and pass it the string you wish to parse.
- Plug the resulting NSAttributedString into your favorite NSAttributedString label implementation (like an UITextView)
*italics*
**bold**
***bold italic***
~~strikethrough~~
# Header 1
## Header 2
### Header 3
#### Header 4
##### Header 5
###### Header 6
Header 1
========
Header 2
--------
https://google.com urls
[Text] (https://google.com "alt text") urls
[email protected]
- UTF-8 support
- Vastly improved speed
- Extended formatting for paragraphs
- Support different link fonts
- Tests and example project
- Support for CocoaPods
###Simplest example
XNGMarkdownParser *parser = [[XNGMarkdownParser alloc] init];
NSAttributedString *string = [parser attributedStringFromMarkdownString:@"This is __rad__."];
// this parser initializes only once and customizes fonts, line height and link color
+ (XNGMarkdownParser *)titleMarkdownParser {
static dispatch_once_t onceToken;
static XNGMarkdownParser *parser;
dispatch_once(&onceToken, ^{
parser = [[XNGMarkdownParser alloc] init];
parser.paragraphFont = [UIFont xng_14Font];
parser.boldFontName = [UIFont xng_14Font].fontName;
parser.linkFontName = [UIFont xng_14Font].fontName;
const CGFloat lineHeight = 18;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = lineHeight;
parser.topAttributes = @{
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName: self.textColor
};
});
return parser;
}
See the included Example project and the tests to check for further options.
If you would like to collaborate with the project by adding features or fixing bugs, you can do it like this:
- If you need to change the grammar we use to parse Markdown test, you have to change the Lex file
grammar/markdown.grammar
, used to generate the lexical analizer. If you need to add new tokens, you will need to add them to the filesXNGMarkdownTokens.h
andXNGMarkdownTokens.cpp
, in order to be able to execute actions later when they are detected. - If you change the grammar, you need to run the script
generate.sh
to generate the code for the lexical analyzer. - In order to convert the detected text to an
NSAttributedString
, you will need to perform changes in the filesrc/XNGMarkdownParser.m
, more concisely in the method-consumeToken:text:
.
After you have fixed a bug or added a feature, please check the tests and extend them if needed, and you will be ready to create a pull request. Thanks!