- Part 10, OrbitView Control
- Part 11, PullToRefreshListView Control
- Part 12, Working with Headings
- Part 13, the Loading… Control
- Part 14, RSS Parser
Intro
As I mentioned in my last post, Part 14, the UWP Community Toolkit includes a pair of helper classes for parsing, one for parsing RSS data and the other for parsing markdown. Let's take a look into the Markdown Parser helper this time.Markdown Parser
The Markdown parser includes a helper class to take a markdown string, parse it into a Markdown Document and then render that document into your UWP controls with a Markdown Renderer.(Tip: The toolkit's MarkdownTextBlock also uses the MarkdownDocument and MarkdownRenderer classes. When you use the control, you can use this default renderer or set your own that overrides the MarkdownRendererBase.)The UWP Community Toolkit Sample App includes this simple example of the Markdown Parser in action.
In this example, you see the raw markdown "This is **Markdown**". That text was parsed into a MarkdownDocument. The document was then serialized to JSON and displayed in a TextBlock. Here is that code from the sample app:
private void UpdateMDResult() { var document = new MarkdownDocument(); document.Parse(RawMarkdown.Text); var json = JsonConvert.SerializeObject(document, Formatting.Indented, new StringEnumConverter()); MarkdownResult.Text = json; }You can see that creating the document is as simple as instantiating a new MarkdownDocument and calling Parse with your raw text. Once you have a MarkdownDocument, you can very easily manipulate it to add, modify or remove individual blocks or elements. The document is essentially a list of markdown blocks. In fact, the sole property you need to be concerned with on MarkdownDocument is Blocks (an IList<MarkdownBlock>).
Objects that derive from MarkdownBlock are MarkdownDocument or one of the following block types:
- CodeBlock
- HeaderBlock
- HorizontalRuleBlock
- LinkReferenceBlock
- ListBlock
- ParagraphBlock
- QuoteBlock
- TableBlock
- Id - A unique string id to identify the reference link.
- Tooltip - The link's tooltip to be displayed when rendered.
- Url - The target.
- Value - The text value to display for the link.
- Type - This is inherited from the MarkdownBlock base and would return the type of this particular block.
Wrap-Up
I encourage you to explore the Markdown Parser on MS Docs and GitHub when you have a chance. If you have any ideas to enhance this parser or any part of the toolkit, submit an issue or submit a pull request.Happy coding!
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.