Markup
Markup copied to clipboard
Some idea to allow extending MarkupNode with own cases?
As in the title 😄
That depends on what you want to add 😄. Could you please be more specific?
I.e. add case superscript([MarkupNode]) and many more.
Ok, so you want to add more delimiters. Implementing the superscript would involve the following:
- Add the character
^
inMarkupTokenizer
- Add a
superscript([MarkupNode])
case toMarkupNode
- Customize the
MarkupNode
constructor to handle the^
delimiter. - Customize the
MarkupRenderer
to handlesuperscript
nodes appropriately. - Add the corresponding tests here and here.
After completing those steps you should be able to take text like Hello^*world*^
and obtain Helloworld.
I've done it in the same way, but I was wondering how to do it more flexible without modifying your code and add more cases by i.e. extensions for your classes :)
To be honest, I did not think about a generic way to extend the parser or renderer. Doing so will probably mean not using an enum for the nodes, as swift enums cannot be extended with additional cases. In any case, it is out of the scope and was not my intention when I published this library.
If you find an elegant and generic way to extend it, please do let me know. It is an interesting exercise.
@gonzalezreal how can I add an rule which looks like "TEXT" where "**" is the new delimiter. The focus on this question is that I use two characters as delimiter.
Hi @Kurt57,
That would involve substantial changes in MarkupTokenizer.
- Refactor the
delimiters
constant into an array of strings (currently is aCharacterSet
) - Refactor all the code relying in that constant.
Also it would require changing MarkupToken to accept strings as associated values instead of UnicodeScalar
.
Finally, refactor all the code that depends on the MarkupToken
(mainly the parser).
As you can see, a lot of work 😅