mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-04 05:44:50 +00:00
Ability to cache and reuse HtmlRenderer instances #431
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 @AndreyChechel on GitHub (Feb 1, 2021).
There are some scenarios requiring ability to implement a custom cache of
HtmlRendererobjects.One scenario is when a
MarkdownDocumentneeds to be parsed and processed first before rendering it into a string:Another scenario includes ability to create and cache
HtmlRendererobjects having custom settings. Like the ones stripping Html from inlines implemented here.The problem with caching is that a
HtmlRendererobject needs to be reset after usage, but theTextRendererBase.Resetmethod is declared asinternal, what makes reuse impossible. Do you think accessibility of the method can be revised to allowprotected, or evenpublicaccess?@xoofx commented on GitHub (Feb 2, 2021):
Could be. I don't have a look at its usage, but if it looks reasonable, PR welcome.
@MihaZupan commented on GitHub (Feb 2, 2021):
I think it's something that could be exposed. I'd prefer to change it to protected rather than public, as it can't always be used (depending on the writer) and custom renderer implementations must take care to reset any state they might have added.
@liciniomendes commented on GitHub (Feb 22, 2021):
Shouldn't we reuse and cache the render defined in the pipeline? If we set a value in
BaseUrlwe can't use theToHtmlstatic method.@AndreyChechel commented on GitHub (Feb 23, 2021):
@liciniomendes if I understand your question right, this issue makes it possible to implement a custom cache similar to how it's done here. Static
ToHtmlmethod works with internal renderers only (they're cached too).@MihaZupan commented on GitHub (Feb 23, 2021):
@liciniomendes Markdown gives you a lot of extensibility with parsing and rendering, so you can potentially change a lot of things. This is why we can't try to cache custom renderers unless we're sure they are reset properly.
If you're using a custom renderer, you can't use the static
ToHtmlhelper.In your example, you'd have to do what
ToHtmldoes internally:As @AndreyChechel mentioned, if you wanted to cache things, you would need a custom Renderer implementation (can just be a tiny wrapper over HtmlRenderer) to be able to call the protected Reset method.
The only way I can think of that Markdig could help with caching is if you had factory callbacks for the renderer
@liciniomendes commented on GitHub (Feb 23, 2021):
Thank you both for the answers.
@MihaZupan that was what I ended up implementing using an extension method. Your solution for the static method is what I was thinking about. It simplifies/removes the boilerplate code that we will need to do if we have a scenario like this.