Skip to content

Commit

Permalink
add markdown to html parser for pull request description
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsymhoven committed May 29, 2021
1 parent 05d945c commit e057afd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<json-schema.version>1.5.1</json-schema.version>
<forensics-api.version>1.0.0</forensics-api.version>
<j2html.version>1.4.0</j2html.version>
<commonmark.version>0.17.2</commonmark.version>
</properties>

<licenses>
Expand Down Expand Up @@ -107,6 +108,11 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>${commonmark.version}</version>
</dependency>

<!-- Workflow dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import hudson.model.Run;
import io.jenkins.plugins.forensics.reference.ReferenceFinder;
import j2html.tags.DomContent;
import j2html.tags.UnescapedText;
import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import jenkins.model.RunAction2;
import jenkins.scm.api.metadata.ContributorMetadataAction;
import jenkins.scm.api.metadata.ObjectMetadataAction;
Expand All @@ -25,8 +27,6 @@
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static j2html.TagCreator.*;
Expand Down Expand Up @@ -180,39 +180,11 @@ public String getPullRequestMetadataBody() {
return span(i("No description provided.")).render();
}

Pattern markdownImage = Pattern.compile("\\[?!\\[(.*?)\\]\\((.*?)\\)\\]?\\(?(.*)\\)?");
Matcher imageMatcher = markdownImage.matcher(description);
while (imageMatcher.find()) {
String text = imageMatcher.group(1);
String link = imageMatcher.group(2);
description = description.replace(imageMatcher.group(), img().withSrc(link).withAlt(text).render());
}

Pattern markdownUrl = Pattern.compile("\\[(.+)\\]\\(([^ ]+)( \"(.+)\")?\\)");
Matcher urlMatcher = markdownUrl.matcher(description);
while (urlMatcher.find()) {
String text = urlMatcher.group(1);
String link = urlMatcher.group(2);
description = description.replace(urlMatcher.group(), a().withHref(link).withText(text).render());
}

Pattern codeBlock = Pattern.compile("```[a-z]*\\r?\\n?([\\s\\S]*?)\\r?\\n?```");
Matcher codeBlockMatcher = codeBlock.matcher(description);
while (codeBlockMatcher.find()) {
String code = codeBlockMatcher.group(1);
description = description.replace(codeBlockMatcher.group(), code().withText(code).render());
}

Pattern codeLine = Pattern.compile("`([\\s\\S]*?)`");
Matcher codeLineMatcher = codeLine.matcher(description);
while (codeLineMatcher.find()) {
String code = codeLineMatcher.group(1);
description = description.replace(codeLineMatcher.group(), code().withText(code).render());
}

description = description.replaceAll("---", hr().render());
Parser parser = Parser.builder().build();
Node document = parser.parse(description);
HtmlRenderer renderer = HtmlRenderer.builder().build();

return span(join(description)).withStyle("white-space: pre-line;").render();
return renderer.render(document);
}

/**
Expand Down

0 comments on commit e057afd

Please sign in to comment.