Git Product home page Git Product logo

tsmarkdownparser's Introduction

TSMarkdownParser

Build Status Carthage compatible Version Platform Licence

TSMarkdownParser is a markdown to NSAttributedString parser for iOS implemented using NSRegularExpressions. It supports many of the standard tags layed out by John Gruber on his site Daring Fireball. It is also very extendable via Regular Expressions making it easy to add your own custom tags or a totally different parsing syntax if you like.

#Supported tags Below is a list of tags supported by the parser out of the box, to add your own tags see "Adding custom parsing"

Escaping
\`
`code`
``code``

Headings
# H1
## H2
### H3
#### H4
##### H5
###### H6

Lists
* item
** item
+ item
++ item
- item
-- item

Quotes
> text
>> text

Images
![Alternative text](image.png)

URL
[Link text](https://www.example.net)

Autodetection
https://www.example.net

Emphasis
*Em*
_Em_
**Strong**
__Strong__

#Requirements TSMarkdownParser 2.x requires Xcode 7 or newer.

#Installation TSMarkdownParser is distributed via CocoaPods

pod 'TSMarkdownParser'

alternativly you can clone the project and build the static library setup in the project, or drag the source files into you project.

#Usage The standardParser class method provides a new instance of the parser configured to parse the tags listed above. You can also just create a new instance of TSMarkdownParser and add your own parsing. See "Adding custom parsing" for information on how to do this.

NSAttributedString *string = [[TSMarkdownParser standardParser] attributedStringFromMarkdown:markdown];

#Customizing appearance You can configure how the markdown is to be displayed by changing the different properties on a TSMarkdownParser instance. Alternatively you could implement the parsing yourself and add custom attributes to the attributed string. You can also alter the attributed string returned from the parser.

#Adding custom parsing Below is an example of how parsing of the bold tag is implemented. You can add your own parsing using the same addParsingRuleWithRegularExpression:block: method. You can add a parsing rule to the standardParser or to your own instance of the parser. If you want to use any of the configuration properties within makesure you use a weak reference to the parser so you don't create a retain cycle.

NSRegularExpression *boldParsing = [NSRegularExpression regularExpressionWithPattern:@"(\\*\\*|__)(.+?)(\\1)" options:kNilOptions error:nil];
__weak TSMarkdownParser *weakSelf = self;
[self addParsingRuleWithRegularExpression:boldParsing block:^(NSTextCheckingResult *match, NSMutableAttributedString *attributedString) {
    [attributedString deleteCharactersInRange:[match rangeAtIndex:3]];
    [attributedString addAttributes:weakSelf.strongAttributes range:[match rangeAtIndex:2];
    [attributedString deleteCharactersInRange:[match rangeAtIndex:1]];
}];

#License TSMarkdownParser is distributed under a MIT licence, see the licence file for more info.

tsmarkdownparser's People

Contributors

laptobbe avatar coeur avatar groomsy avatar kompozer avatar baldurh avatar oblador avatar marcopompei avatar k06a avatar cokile avatar darrarski avatar matt-oakes avatar plaetzchen avatar invlid avatar tilmans avatar eyeplum avatar radex avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.