loiclec / Apodimark

Fast, flexible markdown parser written in Swift
MIT License
0 stars 0 forks source link

Example on how to use your library? #6

Closed yohannes closed 7 years ago

yohannes commented 8 years ago

Hi Loic,

Can you push a simple example app where your library is used? I am having difficulty understanding on how to implement this to a project of mine that has a uitextview with text for instance.

loiclec commented 8 years ago

Yes, I can 😊 There is a bit of boilerplate when using the library because of its genericity, but once you get past that, it is pretty conventional. I'll try to push a sample text view very soon. If you have a specific question, you can ask it here or send me an email.

yohannes commented 8 years ago

@loiclec cool. Thanks. I look forward to your sample text view.

loiclec commented 8 years ago

Here it is: https://github.com/loiclec/TestApodimark

It uses Cocoapods, and you’ll need to run pod update to download Apodimark. Tell me if that’s an issue for you. Also, don’t read too much into any of the TextView code, I just wrote the minimum necessary to demo Apodimark.

It is a simple Mac app consisting of one NSTextView with a delegate. Every time the content of the text view changes, its text is parsed as Markdown. Then I use the parsed document to provide some basic syntax highlighting (only text, emphasis, paragraph, and header are supported).

It works by using the parsedMarkdown function with these arguments:

Why do I use String.UTF16View? Because…

You’ll also notice that I have this weird empty Mark enum. This is not important, I just use it to define typealiases and static functions and keep the code clean.

The parsedMarkdown function returns an abstract syntax tree defined by an array of MarkdownBlock. You can look at the file Apodimark.swift to see what a MarkdownBlock or MarkdownInline can be. I think it’s straightforward, but if you have a question, don't hesitate to ask me.

Then I look at all the nodes of the AST (abstract syntax tree) and create a list of attributes and ranges from that.

Finally, I apply this list of attributes to the text view’s text storage.

yohannes commented 8 years ago

@loiclec thanks for the elaborate explanation w/ the accompanying sample. I'll take a look at it.