Skip to content

Commit 3dd00d1

Browse files
fbriconangelozerr
authored andcommitted
build: replace remark with flexmark
Signed-off-by: Fred Bricon <[email protected]>
1 parent 0d3437e commit 3dd00d1

File tree

6 files changed

+135
-89
lines changed

6 files changed

+135
-89
lines changed

build.gradle.kts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ sourceSets {
7373

7474
dependencies {
7575
implementation("org.zeroturnaround:zt-zip:1.14")
76-
implementation("com.kotcrab.remark:remark:1.2.0") //FIXME use lsp4ij's flexmark instead
77-
implementation("org.jsoup:jsoup:1.14.2") //FIXME use lsp4ij's jsoup instead
78-
implementation("com.google.code.gson:gson:2.10.1")
76+
implementation("org.jsoup:jsoup:1.17.1")
77+
implementation("com.vladsch.flexmark:flexmark-html2md-converter:0.64.8") {
78+
exclude(group="com.vladsch.flexmark", module= "flexmark-jira-converter")
79+
}
80+
implementation("com.google.code.gson:gson:2.10.1") //Need to ensure we don't get telemetry's old gson version
7981
implementation("io.quarkus:quarkus-core:$quarkusVersion") {
8082
isTransitive = false
8183
}

src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/core/ls/PsiUtilsLSImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public String getJavadoc(PsiMethod method, DocumentFormat documentFormat) {
118118

119119
@Override
120120
public String getJavadoc(PsiMember method, com.redhat.qute.commons.DocumentFormat documentFormat) {
121-
boolean markdown = DocumentFormat.Markdown.equals(documentFormat);
121+
boolean markdown = DocumentFormat.Markdown.name().toLowerCase().equals(documentFormat.name().toLowerCase());
122122
Reader reader = markdown ? JavadocContentAccess.getMarkdownContentReader(method)
123123
: JavadocContentAccess.getPlainTextContentReader(method);
124124
return reader != null ? toString(reader) : null;

src/main/java/com/redhat/devtools/intellij/quarkus/javadoc/JavaDoc2MarkdownConverter.java

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313

1414
import java.io.Reader;
15-
import java.lang.reflect.Field;
16-
17-
import org.jsoup.safety.Cleaner;
18-
import org.jsoup.safety.Safelist;
19-
import com.overzealous.remark.Options;
20-
import com.overzealous.remark.Options.Tables;
21-
import com.overzealous.remark.Remark;
15+
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
16+
import com.vladsch.flexmark.parser.PegdownExtensions;
17+
import com.vladsch.flexmark.util.data.DataKey;
18+
import org.jsoup.Jsoup;
19+
import org.jsoup.nodes.Document;
20+
import org.jsoup.nodes.Element;
21+
import org.jsoup.select.Elements;
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
2424

@@ -30,34 +30,29 @@
3030
public class JavaDoc2MarkdownConverter extends AbstractJavaDocConverter {
3131
private static final Logger LOGGER = LoggerFactory.getLogger(JavaDoc2MarkdownConverter.class);
3232

33-
private static Remark remark;
34-
35-
static {
36-
Options options = new Options();
37-
options.tables = Tables.MULTI_MARKDOWN;
38-
options.hardwraps = true;
39-
options.inlineLinks = true;
40-
options.autoLinks = true;
41-
options.reverseHtmlSmartPunctuation = true;
42-
remark = new Remark(options);
43-
//Stop remark from stripping file and jdt protocols in an href
44-
try {
45-
Field cleanerField = Remark.class.getDeclaredField("cleaner");
46-
cleanerField.setAccessible(true);
47-
48-
Cleaner c = (Cleaner) cleanerField.get(remark);
49-
50-
Field safelistField = Cleaner.class.getDeclaredField("safelist");
51-
safelistField.setAccessible(true);
33+
private static final String LINE_SEPARATOR = "\n";
5234

53-
Safelist s = (Safelist) safelistField.get(c);
54-
55-
s.addProtocols("a", "href", "file", "jdt");
56-
s.addProtocols("img", "src", "file");
57-
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
58-
LOGGER.error("Unable to modify jsoup to include file and jdt protocols", e);
59-
}
60-
}
35+
final static public DataKey<Integer> HTML_EXTENSIONS = new DataKey<>("HTML_EXTENSIONS", 0
36+
//| Extensions.ABBREVIATIONS
37+
//| Extensions.EXTANCHORLINKS /*| Extensions.EXTANCHORLINKS_WRAP*/
38+
//| Extensions.AUTOLINKS
39+
//| Extensions.DEFINITIONS
40+
| PegdownExtensions.FENCED_CODE_BLOCKS
41+
//| Extensions.FORCELISTITEMPARA
42+
//| Extensions.HARDWRAPS
43+
//| Extensions.ATXHEADERSPACE
44+
//| Extensions.QUOTES
45+
//| Extensions.SMARTS
46+
//| Extensions.RELAXEDHRULES
47+
//| Extensions.STRIKETHROUGH
48+
//| Extensions.SUPPRESS_HTML_BLOCKS
49+
//| Extensions.SUPPRESS_INLINE_HTML
50+
//| Extensions.TABLES
51+
//| Extensions.TASKLISTITEMS
52+
//| Extensions.WIKILINKS
53+
//| Extensions.TRACE_PARSER
54+
);
55+
private static final FlexmarkHtmlConverter CONVERTER = FlexmarkHtmlConverter.builder().build();
6156

6257
public JavaDoc2MarkdownConverter(Reader reader) {
6358
super(reader);
@@ -68,7 +63,53 @@ public JavaDoc2MarkdownConverter(String javadoc) {
6863
}
6964

7065
@Override
71-
String convert(String rawHtml) {
72-
return remark.convert(rawHtml);
66+
public String convert(String html) {
67+
Document document = Jsoup.parse(html);
68+
//Add missing table headers if necessary, else most Markdown renderers will crap out
69+
document.select("table").forEach(JavaDoc2MarkdownConverter::addMissingTableHeaders);
70+
71+
String markdown = CONVERTER.convert(document);
72+
if (markdown.endsWith(LINE_SEPARATOR)) {// FlexmarkHtmlConverter keeps adding an extra line
73+
markdown = markdown.substring(0, markdown.length() - LINE_SEPARATOR.length());
74+
}
75+
76+
return markdown;
77+
}
78+
79+
/**
80+
* Adds a new row header if the given table doesn't have any.
81+
*
82+
* @param table
83+
* the HTML table to check for a header
84+
*/
85+
private static void addMissingTableHeaders(Element table) {
86+
int numCols = 0;
87+
for (Element child : table.children()) {
88+
if ("thead".equals(child.nodeName())) {
89+
// Table already has a header, nothing else to do
90+
return;
91+
}
92+
if ("tbody".equals(child.nodeName())) {
93+
Elements rows = child.getElementsByTag("tr");
94+
if (!rows.isEmpty()) {
95+
for (Element row : rows) {
96+
int colSize = row.getElementsByTag("td").size();
97+
//Keep the biggest column size
98+
if (colSize > numCols) {
99+
numCols = colSize;
100+
}
101+
}
102+
}
103+
}
104+
}
105+
if (numCols > 0) {
106+
//Create a new header row based on the number of columns already found
107+
Element newHeader = new Element("tr");
108+
for (int i = 0; i < numCols; i++) {
109+
newHeader.appendChild(new Element("th"));
110+
}
111+
//Insert header row in 1st position in the table
112+
table.insertChildren(0, newHeader);
113+
}
73114
}
74115
}

src/main/java/com/redhat/microprofile/psi/internal/quarkus/kubernetes/properties/QuarkusKubernetesProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ protected void processClass(PsiClass configType, String className, SearchContext
8989
// see
9090
// https://github.com/quarkusio/quarkus/blob/44e5e2e3a642d1fa7af9ddea44b6ff8d37e862b8/extensions/kubernetes/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesProcessor.java#L94
9191
super.addItemMetadata(collector, "kubernetes.deployment.target", "java.lang.String", //
92-
"To enable the generation of OpenShift resources, you need to include OpenShift in the target platforms: `kubernetes.deployment.target=openshift`."
93-
+ System.lineSeparator()
94-
+ "If you need to generate resources for both platforms (vanilla Kubernetes and OpenShift), then you need to include both (coma separated)."
95-
+ System.lineSeparator() + "`kubernetes.deployment.target=kubernetes, openshift`.",
92+
"""
93+
To enable the generation of OpenShift resources, you need to include OpenShift in the target platforms: `kubernetes.deployment.target=openshift`.
94+
If you need to generate resources for both platforms (vanilla Kubernetes and OpenShift), then you need to include both (comma-separated).
95+
`kubernetes.deployment.target=kubernetes, openshift`.""",
9696
null, null, null, KUBERNETES_PREFIX, null, true);
9797
// kubernetes.registry
9898
// see

src/test/java/com/redhat/devtools/intellij/qute/psi/template/TemplateGetJavadocTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void testgetFieldJavadoc() throws Exception {
4141
DocumentFormat.Markdown);
4242

4343
String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new EmptyProgressIndicator());
44-
String expected = " The name of the item ";
44+
String expected = "The name of the item";
4545
assertEquals(expected, actual);
4646
}
4747

@@ -57,9 +57,11 @@ public void testgetMethodJavadoc() throws Exception {
5757
DocumentFormat.Markdown);
5858

5959
String actual = QuteSupportForTemplate.getInstance().getJavadoc(params, getJDTUtils(), new EmptyProgressIndicator());
60-
String expected = " Returns the derived items. \n" +
61-
" * Returns:\n" +
62-
" - the derived items";
60+
String expected = """
61+
Returns the derived items.
62+
63+
* **Returns:**
64+
* the derived items""";
6365
assertEquals(expected, actual);
6466
}
6567

0 commit comments

Comments
 (0)