diff --git a/commonmark/src/main/java/org/commonmark/renderer/html/CoreHtmlNodeRenderer.java b/commonmark/src/main/java/org/commonmark/renderer/html/CoreHtmlNodeRenderer.java index 115d553f..0603aa01 100644 --- a/commonmark/src/main/java/org/commonmark/renderer/html/CoreHtmlNodeRenderer.java +++ b/commonmark/src/main/java/org/commonmark/renderer/html/CoreHtmlNodeRenderer.java @@ -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(); } diff --git a/commonmark/src/main/java/org/commonmark/renderer/html/HtmlNodeRendererContext.java b/commonmark/src/main/java/org/commonmark/renderer/html/HtmlNodeRendererContext.java index eb950ffa..eecff0f4 100644 --- a/commonmark/src/main/java/org/commonmark/renderer/html/HtmlNodeRendererContext.java +++ b/commonmark/src/main/java/org/commonmark/renderer/html/HtmlNodeRendererContext.java @@ -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 */ @@ -47,6 +47,11 @@ public interface HtmlNodeRendererContext { */ boolean shouldEscapeHtml(); + /** + * @return whether documents that only contain a single paragraph should be rendered without the {@code
} tag
+ */
+ boolean shouldOmitSingleParagraphP();
+
/**
* @return true if the {@link UrlSanitizer} should be used.
* @since 0.14.0
diff --git a/commonmark/src/main/java/org/commonmark/renderer/html/HtmlRenderer.java b/commonmark/src/main/java/org/commonmark/renderer/html/HtmlRenderer.java
index 35db46a6..386abebf 100644
--- a/commonmark/src/main/java/org/commonmark/renderer/html/HtmlRenderer.java
+++ b/commonmark/src/main/java/org/commonmark/renderer/html/HtmlRenderer.java
@@ -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 } 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.
*
@@ -242,6 +256,11 @@ public boolean shouldEscapeHtml() {
return escapeHtml;
}
+ @Override
+ public boolean shouldOmitSingleParagraphP() {
+ return omitSingleParagraphP;
+ }
+
@Override
public boolean shouldSanitizeUrls() {
return sanitizeUrls;
diff --git a/commonmark/src/test/java/org/commonmark/test/HtmlRendererTest.java b/commonmark/src/test/java/org/commonmark/test/HtmlRendererTest.java
index 7cc0b036..018b9e45 100644
--- a/commonmark/src/test/java/org/commonmark/test/HtmlRendererTest.java
+++ b/commonmark/src/test/java/org/commonmark/test/HtmlRendererTest.java
@@ -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;
@@ -308,6 +310,12 @@ public void canRenderContentsOfSingleParagraph() {
defaultRenderer().render(document));
}
+ @Test
+ public void omitSingleParagraphP() {
+ var renderer = HtmlRenderer.builder().omitSingleParagraphP(true).build();
+ assertEquals("hi there", renderer.render(parse("hi *there*")));
+ }
+
@Test
public void threading() throws Exception {
Parser parser = Parser.builder().build();