Skip to content

Commit

Permalink
Merge pull request #340 from commonmark/issue-150-omit-single-paragra…
Browse files Browse the repository at this point in the history
…ph-p

Add omitSingleParagraphP option to HtmlRenderer.Builder
  • Loading branch information
robinst authored Sep 16, 2024
2 parents db57101 + 8c819ab commit 0fb7a16
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ public void visit(Heading heading) {

@Override
public void visit(Paragraph paragraph) {
boolean inTightList = isInTightList(paragraph);
if (!inTightList) {
boolean omitP = isInTightList(paragraph) || //
(context.shouldOmitSingleParagraphP() && paragraph.getParent() instanceof Document && //
paragraph.getPrevious() == null && paragraph.getNext() == null);
if (!omitP) {
html.line();
html.tag("p", getAttrs(paragraph, "p"));
}
visitChildren(paragraph);
if (!inTightList) {
if (!omitP) {
html.tag("/p");
html.line();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public interface HtmlNodeRendererContext {
/**
* Let extensions modify the HTML tag attributes.
*
* @param node the node for which the attributes are applied
* @param tagName the HTML tag name that these attributes are for (e.g. {@code h1}, {@code pre}, {@code code}).
* @param node the node for which the attributes are applied
* @param tagName the HTML tag name that these attributes are for (e.g. {@code h1}, {@code pre}, {@code code}).
* @param attributes the attributes that were calculated by the renderer
* @return the extended attributes with added/updated/removed entries
*/
Expand Down Expand Up @@ -47,6 +47,11 @@ public interface HtmlNodeRendererContext {
*/
boolean shouldEscapeHtml();

/**
* @return whether documents that only contain a single paragraph should be rendered without the {@code <p>} tag
*/
boolean shouldOmitSingleParagraphP();

/**
* @return true if the {@link UrlSanitizer} should be used.
* @since 0.14.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ public class HtmlRenderer implements Renderer {

private final String softbreak;
private final boolean escapeHtml;
private final boolean percentEncodeUrls;
private final boolean omitSingleParagraphP;
private final boolean sanitizeUrls;
private final UrlSanitizer urlSanitizer;
private final boolean percentEncodeUrls;
private final List<AttributeProviderFactory> attributeProviderFactories;
private final List<HtmlNodeRendererFactory> nodeRendererFactories;

private HtmlRenderer(Builder builder) {
this.softbreak = builder.softbreak;
this.escapeHtml = builder.escapeHtml;
this.sanitizeUrls = builder.sanitizeUrls;
this.percentEncodeUrls = builder.percentEncodeUrls;
this.omitSingleParagraphP = builder.omitSingleParagraphP;
this.sanitizeUrls = builder.sanitizeUrls;
this.urlSanitizer = builder.urlSanitizer;
this.attributeProviderFactories = new ArrayList<>(builder.attributeProviderFactories);

Expand Down Expand Up @@ -83,6 +85,7 @@ public static class Builder {
private boolean sanitizeUrls = false;
private UrlSanitizer urlSanitizer = new DefaultUrlSanitizer();
private boolean percentEncodeUrls = false;
private boolean omitSingleParagraphP = false;
private List<AttributeProviderFactory> attributeProviderFactories = new ArrayList<>();
private List<HtmlNodeRendererFactory> nodeRendererFactories = new ArrayList<>();

Expand Down Expand Up @@ -166,6 +169,17 @@ public Builder percentEncodeUrls(boolean percentEncodeUrls) {
return this;
}

/**
* Whether documents that only contain a single paragraph should be rendered without the {@code <p>} tag. Set to
* {@code true} to render without the tag; the default of {@code false} always renders the tag.
*
* @return {@code this}
*/
public Builder omitSingleParagraphP(boolean omitSingleParagraphP) {
this.omitSingleParagraphP = omitSingleParagraphP;
return this;
}

/**
* Add a factory for an attribute provider for adding/changing HTML attributes to the rendered tags.
*
Expand Down Expand Up @@ -242,6 +256,11 @@ public boolean shouldEscapeHtml() {
return escapeHtml;
}

@Override
public boolean shouldOmitSingleParagraphP() {
return omitSingleParagraphP;
}

@Override
public boolean shouldSanitizeUrls() {
return sanitizeUrls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.commonmark.parser.Parser;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.html.*;
import org.commonmark.testutil.Asserts;
import org.commonmark.testutil.RenderingTestCase;
import org.commonmark.testutil.TestResources;
import org.junit.Test;

Expand Down Expand Up @@ -308,6 +310,12 @@ public void canRenderContentsOfSingleParagraph() {
defaultRenderer().render(document));
}

@Test
public void omitSingleParagraphP() {
var renderer = HtmlRenderer.builder().omitSingleParagraphP(true).build();
assertEquals("hi <em>there</em>", renderer.render(parse("hi *there*")));
}

@Test
public void threading() throws Exception {
Parser parser = Parser.builder().build();
Expand Down

0 comments on commit 0fb7a16

Please sign in to comment.