-
Notifications
You must be signed in to change notification settings - Fork 293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow disabling wrapping <p> tags in one liners. #150
Comments
Hey. Can you go into a bit more detail why you want that? I'm not sure the option is the best way to solve it. E.g., for markdown like this:
There's no |
I guess the real question is why would I want the wrapping However, in the example you've mentioned, the text should definitely be wrapped with a For backwards compatability though, I think adding a multiline vs single line flag on HtmlRenderer would be a good option. |
Note that you can already achieve this with the current API. What you have to do is after parsing, change the nodes from a structure like this:
to this:
I've committed a test case that shows how to do that: 8b0fd7c I'm pretty convinced that adding an option to Maybe what we could add is support for parsing a single paragraph only (no block-level elements). But even that has a couple of edge cases depending on what you want to use it for. |
Are you saying that libraries/apps that consume CommonMark should be aware of the HTML/DOM that is being generated and manipulate it accordingly? I couldn't disagree with that assessment more. That's extremely brittle and breaks every time HtmlRenderer changes. Also, why use markdown in the first place if I have to manipulate the DOM after the fact? I could also just use HTML to begin with. The problem here is that CommonMark makes a bad assumption that a single line is a paragraph in the first place. If this were a new library, I'd say it's a bad design decision that you change up front by default by not adding the |
They have to be aware of the AST (abstract syntax tree, node structure), yeah.
No, it doesn't. To be clear, what I'm proposing is that you do this:
The middle step is not dependent on how HtmlRenderer behaves. Not sure why you would think that? We could add a utility method for this middle step, but I'm not sure about it yet. The method name would be something like
It's consistent with what the reference implementations commonmark.js and cmark do. I had a quick look and there's an open issue for the JS implementation here, but no replies yet: commonmark/commonmark.js#152 |
I had the same concerns as OP, but after thinking about DOM manipulation as well as the unit test code, this was easy enough to figure out. |
Again, consumers should not be aware of or rely on any specific HTML or structure that is generated by this library. I’m not arguing that it isn’t easy...it is incredibly easy. However, it’s not future proof and it’s brittle. It also completely defeats the purpose of using any rendering library (MD or otherwise) to have to post process and manipulate the results. Think of it like a translator library. If I call you to translate English to Spanish, should I manipulate the output or ask you for a specific dialect? It’s no different for MD to HTML. I’m not sure how else to explain that this is a bad design decision. Coding around it is almost as painful as having to explain why. |
Reviving this thread. Taking your example @dustindclark , thinking about the translation context, I expect the translator does the translation without any extra step as you mean. So, bringing this example to Let's assume that we implement this extension as you suggested. So, we would have something like: HtmlRenderer htmlRenderer = HtmlRenderer.builder().noParagraph().build();
Document document = new Document();
document.appendChild(parse("First Line [link](http://test.com)"));
document.appendChild(parse("Second Line [link](http://foo.com)"));
return htmlRenderer.render(document)); Generating an HTML such as First Line <a href="http://test.com">link1</a>Second Line <a href="http://foo.com">link2</a> In the end, we do have some HTML content that is not following the good practices because of this extra new configuration. If this is your needs, it seems so specialized that you can't skip some HTML conventions. if this is the case that would be where the @robinst example could be helpful. I hope I added something to the discussion. :) |
@samukce , first, there's absolutely nothing wrong with the generated HTML you've posted, and there's no HTML "good practice" that would indicate otherwise. It's perfectly valid syntactically, and that's exactly how I would craft HTML if I didn't want a line break/or space between links. Second, while your example isn't really relevant because this feature request concerns one-liners, I don't think the example is a stretch of a use case. However, the rendered HTML that you presented is not what I would expect the result to look like. Your example has a newline in the document being processed, so I would expect that to be translated to HTML as a
Third, I would disagree that this is a specialized use case. It's simply a use case, and again, none of the above HTML "skips HTML conventions". |
Gotcha, thanks for share @dustindclark . So, assuming that you do have this valid html block, where would you inputs this html block? In a |
@dustindclark How about you raise a PR to implement it? I'm curious what it would look like, and if we have code with tests it's much easier to talk about. I'm not opposed to adding something for this. My main question is, with the new option enabled, how would this be rendered?:
|
You have: |
This is useful for rendering single lines where wrapping in a <p> might be undesirable. Fixes #150.
I've raised a PR to add an |
This has been released in 0.23.0, see full changelog: https://github.com/commonmark/commonmark-java/blob/main/CHANGELOG.md#0230---2024-09-16 |
Steps to reproduce the problem (provide example input)
Expected behavior:
Actual behavior:
I understand that this may be a breaking change, so an option on HtmlRenderer on this would be sufficient.
The text was updated successfully, but these errors were encountered: