mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-03 21:36:36 +00:00
Markdown.ToHtml does not provide argument to turn on ImplicitParagraph option #81
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 @KirkMunro on GitHub (Jan 16, 2017).
I'd like to see an option on ToHtml that allows me to specify whether or not ImplicitParagraph should be set on the HtmlRenderer that is used within that method.
I was thinking about making this change with an optional argument, but before submitting a PR I thought I should ask, are there other HtmlRenderer options that would be useful to see exposed in the ToHtml method? These could be combined in an enumerated value argument so that whatever options are needed could be set in one additional argument. I haven't needed to look at the other options though so I don't know whether or not they should be exposed in ToHtml. Thoughts?
@xoofx commented on GitHub (Jan 26, 2017):
Hm I would rather like to not expose specific/duplicate options to the top level
ToHtmlfunction...If you need this kind of customization, you can always instantiate the
HtmlRendereryourself and do whatToHtmlis doing (this is a very tiny wrapper around it)...@KirkMunro commented on GitHub (Jan 27, 2017):
I took a closer look at this, and discovered that the problem I'm facing isn't really that ToHtml doesn't expose options to turn ImplicitParagraph on or off. The problem is that Markdig's conversion from markdown to HTML assumes you are converting an entire markdown document to a new HTML document. It makes sense to automatically wrap paragraphs in paragraph tags when you are performing such a conversion, so having that as the default in ToHtml is the right thing to do; however, I'm facing a scenario where I'm converting portions of a markdown document to HTML format, and when I perform those conversions I absolutely do not want paragraphs automatically wrapped in paragraph tags in the HTML output because in this specific case, that negatively impacts the UX where the HTML that is being generated is used. Instead, what I'm after is to separate empty-line-delimited paragraph blocks in markdown with two line break tags on separate lines.
e.g. I want this:
to convert to this:
In order to get that to work with Markdig, I had to write my own paragraph renderer, as follows:
I also had to duplicate the ToHtml methods and the CheckForSelfPipeline method, and insert the following lines right after the HtmlRenderer is created:
With all of that in place I could get Markdig to convert paragraphs the way I need them converted.
I'm happy the results give me what I needed, but I don't think that having to duplicate so much logic should be necessary. Is there something I'm missing that would eliminate the unnecessary duplication of code that seems to be required the way this is currently set up?
Maybe the HtmlRenderer constructor and the ToHtml method should have an optional parameter that accepts a list of custom renderers that you want to use. In the HtmlRenderer constructor, it could skip any default renderers that are provided in the optional custom renderer list. This approach would greatly simplify the code I had to write to make this work, eliminating most if not all of the duplication that is otherwise required when you need to convert markdown to HTML using something other than the default renderers. If this was in place, all I would have to do is create the custom renderers that I need and then pass them in as a collection to ToHtml to render the markdown as HTML using the logic that is already written inside of Markdig.
@xoofx commented on GitHub (Jan 27, 2017):
You don't need to modify the markdig to plug your own
MyParagraphRendererJust build a a plugin and register to the pipeline builder:
and add it to the builder
builder.Use<MyParagraphExtension>();But your solution doesn't look right. What you want is the
ParagraphRenderernot to render top level paragraphs, but it should be able to render nested paragraphs. It needs a depth level information accessible from the renderer which is not provided today but easy to add (readonly integer propertyCurrentDepththat returns the current depth level, incremented, decremented everytime we visit an element inHtmlRenderer).@KirkMunro commented on GitHub (Jan 27, 2017):
Thank you for the fast reply. I'm starting to understand how the extensions work now. With the additional information you provided, I was able to remove all of my ToHtml wrappers and simplify my logic, resulting in this:
Regarding your last comment about a CurrentDepth property, is that something you could add? Or point me to where it should be incremented/decremented and I can add it and submit a PR. Then I could modify my MyParagraphRenderer to do what it does if CurrentDepth is 0, or invoke the base class Write method if CurrentDepth is > 0.
@KirkMunro commented on GitHub (Feb 2, 2017):
FYI, I discovered that I can use obj.Parent inside of my Write override method to identify whether or not I'm dealing with a top-level paragraph, so CurrentDepth isn't really that necessary. It's not a perfect solution, but it's pretty good. Here's the Write method I have now for my MyParagraphRenderer:
The one piece I'm not loving with this is how I determine when I want to insert double
<br/>tags. There are situations where I don't really want them that this doesn't cover (e.g. if I follow a paragraph by a<hr/>tag or a line with* * *). I'm still thinking about that one.@xoofx commented on GitHub (Feb 3, 2017):
Good catch, indeed forgot that we have of course access to the Parent!
@xoofx commented on GitHub (Feb 3, 2017):
So Is this issue closable or?
@KirkMunro commented on GitHub (Feb 3, 2017):
Yes, I'll close it. I have what I need for now.