Closed yohannes closed 7 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.
@loiclec cool. Thanks. I look forward to your sample text view.
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:
String.UTF16View
DefaultReferenceDefinitionStore()
. You don’t need to care about this unless you want to handle links and images (referred to in Apodimark as references) in a special way.UTF16MarkdownCodec.self
which tells Apodimark how to read the text. This is needed because I plan to allow more customization through codecs in the future, and it was convenient from an implementation point of view.Why do I use String.UTF16View
? Because…
source
needs to be a BidirectionalCollection
String.CharacterView
would be slower and more difficult to work with. (Note, however, that you can use it and you would get more correct string parsing)Range<String.UTF16View.Index>
can be easily translated into NSRange
, making syntax highlighting easierYou’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.
@loiclec thanks for the elaborate explanation w/ the accompanying sample. I'll take a look at it.
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.