Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doxia-modules/doxia-module-markdown/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ under the License.
<artifactId>flexmark-ext-yaml-front-matter</artifactId>
<version>${flexmarkVersion}</version>
</dependency>

<dependency>
<!-- for converting html to xhtml -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.21.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.util.HtmlTools;
import org.codehaus.plexus.util.xml.pull.XmlPullParser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
* <p>
Expand Down Expand Up @@ -172,14 +174,14 @@ public class MarkdownParser extends AbstractTextParser implements TextMarkup {
public void parse(Reader source, Sink sink, String reference) throws ParseException {
try {
// Markdown to HTML (using flexmark-java library)
String html = toHtml(source);
String xhtml = toXhtml(source);

// TODO: add locator for the markdown source (not the intermediate HTML format)
// this requires writing a custom renderer not leveraging the XHTML parser

// then HTML to Sink API
parser.setEmitComments(isEmitComments());
parser.parse(html, getWrappedSink(sink), "Intermediate HTML from " + reference);
parser.parse(xhtml, getWrappedSink(sink), "Intermediate HTML from " + reference);
} catch (IOException e) {
throw new ParseException("Failed reading Markdown source document", e);
}
Expand Down Expand Up @@ -272,7 +274,7 @@ private boolean writeHtmlMetadata(StringBuilder html, String key, List<String> v
* @return HTML content generated by flexmark-java
* @throws IOException passed through
*/
String toHtml(Reader source) throws IOException {
String toXhtml(Reader source) throws IOException {
// Read the source
StringBuilder markdownText = new StringBuilder(IOUtils.toString(source));

Expand Down Expand Up @@ -314,7 +316,13 @@ String toHtml(Reader source) throws IOException {
html.append("</body>");
html.append("</html>");

return html.toString();
return toXhtml(html.toString());
}

private String toXhtml(String html) {
final Document document = Jsoup.parse(html);
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml).prettyPrint(false);
return document.html();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ public void testHtmlContent() throws Exception {
"table",
"tableRows",
"text",
"unknown", // tbody start
"tableRow",
"tableHeaderCell",
"text",
Expand All @@ -640,6 +641,7 @@ public void testHtmlContent() throws Exception {
"tableCell_",
"tableRow_",
"text",
"unknown", // tbody end
"tableRows_",
"table_",
"text",
Expand Down Expand Up @@ -669,7 +671,7 @@ protected SinkEventTestingSink parseFileToEventTestingSink(String file) throws P

protected String parseFileToHtml(String file) throws ParseException, IOException {
try (Reader reader = getTestReader(file)) {
return parser.toHtml(reader).toString();
return parser.toXhtml(reader).toString();
}
}

Expand Down Expand Up @@ -860,6 +862,11 @@ public void testCommentsRemovedWithEmitCommentsFalse() throws ParseException, IO
assertSinkDoesNotContain(eventList.iterator(), "comment", "comment_");
}

@Test
public void testHtmlInMarkdown() throws ParseException, IOException {
parseFileToEventTestingSink("html");
}

protected static void assertComment(ListIterator<SinkEventElement> it, String comment) {
assertSinkEquals(it.next(), "comment", comment);
// every comment ends with a line break in the emitted html which leads to an additional text event containing a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# HTML in Markdown

This is a <p> test.
Loading