mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-04 05:44:50 +00:00
StreamReader #412
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Feofilakt on GitHub (Nov 2, 2020).
Hello!
Is it make sense to use StreamReader for markdown file parsing? I've noticed that Markdig has only method Parse that takes a string. I didn't deal with markdown parsing before, maybe I miss something.
@MihaZupan commented on GitHub (Nov 2, 2020):
Short answer: no.
If we were to accept a
StreamReader, the implementation would 100% be to just callReadToEnd(allocating a new string for the entire content), then delegate to the existing parsing logic.As such I don't think it adds any value.
Markdig's
ToHtmlhas 2 phases:Parsetakes in the entire markdown text and parses it to an abstract syntax tree.Rendertakes the AST and spits out the corresponding HTMLThis approach gives Markdig a lot of customization and extensibility opportunities.
Parsing and the resulting AST are making heavy use of the string type all over the place (albeit in an efficient way). While it could technically be changed to something like
Memory<char>, it would likely be slower than just allocating a new string once at the start.Markdown is complex, so parsing can't be implemented in a single-pass non-seeking manner - therefore we don't really benefit from Streaming APIs (for either source or destination).
Markdig is also very fast and the single string allocation at the start shouldn't make or break the performance of Markdown processing.
@ondrejpialek commented on GitHub (Jan 7, 2024):
A nice use case these days is feeding streamed md from LLMs, such as what OpenAI expose in their API (I have an
IAsyncEnumerable<string>as a response from a particular library).It would be cool to be able to render HTML that markdown parser could render so far, but I understand it's not trivial, especially since tags would need to closed and then that being undone as more data comes in.