Skip to content

Commit 58c3629

Browse files
committed
fix: Create singleton instances of Markdown Parser and Renderer and add some tests
1 parent 3ff7ca7 commit 58c3629

3 files changed

Lines changed: 109 additions & 10 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
3+
* This program and the accompanying materials are made
4+
* available under the terms of the Eclipse Public License 2.0
5+
* which is available at https://www.eclipse.org/legal/epl-2.0/
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* See git history
11+
*******************************************************************************/
12+
package org.eclipse.lsp4e.test.internal;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
16+
import java.util.stream.Stream;
17+
18+
import org.eclipse.lsp4e.internal.MarkdownUtil;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.Arguments;
21+
import org.junit.jupiter.params.provider.MethodSource;
22+
23+
public class MarkdownUtilTest {
24+
25+
public static Stream<Arguments> renderHtml() {
26+
return Stream.of( //
27+
Arguments.argumentSet("Simple", """
28+
# Heading 1
29+
- this is a test""", """
30+
<h1>Heading 1</h1>
31+
<ul>
32+
<li>this is a test</li>
33+
</ul>
34+
"""), //
35+
Arguments.argumentSet("With table", """
36+
| Header | Another Header |
37+
|---------|----------------|
38+
| field 1 | value one |""", """
39+
<table>
40+
<thead>
41+
<tr>
42+
<th>Header</th>
43+
<th>Another Header</th>
44+
</tr>
45+
</thead>
46+
<tbody>
47+
<tr>
48+
<td>field 1</td>
49+
<td>value one</td>
50+
</tr>
51+
</tbody>
52+
</table>
53+
""") //
54+
);
55+
}
56+
57+
@ParameterizedTest
58+
@MethodSource
59+
void renderToHtml(String markdown, String expectedHtml) throws Exception {
60+
// Simple test to make sure we configured CommonMark correctly.
61+
assertEquals(expectedHtml, MarkdownUtil.renderToHtml(markdown));
62+
}
63+
64+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
3+
* This program and the accompanying materials are made
4+
* available under the terms of the Eclipse Public License 2.0
5+
* which is available at https://www.eclipse.org/legal/epl-2.0/
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* See git history
11+
*******************************************************************************/
12+
package org.eclipse.lsp4e.internal;
13+
14+
import java.util.List;
15+
16+
import org.commonmark.Extension;
17+
import org.commonmark.ext.gfm.tables.TablesExtension;
18+
import org.commonmark.parser.Parser;
19+
import org.commonmark.renderer.Renderer;
20+
import org.commonmark.renderer.html.HtmlRenderer;
21+
22+
public class MarkdownUtil {
23+
24+
/**
25+
* Used commonmark extensions
26+
*/
27+
private static final List<Extension> EXTENSIONS = List.of(TablesExtension.create());
28+
29+
/**
30+
* Singleton instance, as both classes are thread-safe, see
31+
* https://github.com/commonmark/commonmark-java?tab=readme-ov-file#thread-safety
32+
*/
33+
private static final Parser PARSER = Parser.builder().extensions(EXTENSIONS).build();
34+
private static final Renderer RENDERER = HtmlRenderer.builder().extensions(EXTENSIONS).build();
35+
36+
/**
37+
* Renders the given markdown content to HTML.
38+
*/
39+
public static String renderToHtml(String markdown) {
40+
return RENDERER.render(PARSER.parse(markdown));
41+
}
42+
43+
}

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/hover/LSPTextHover.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
import java.util.function.Predicate;
3030
import java.util.stream.Collectors;
3131

32-
import org.commonmark.Extension;
33-
import org.commonmark.ext.gfm.tables.TablesExtension;
34-
import org.commonmark.node.Node;
35-
import org.commonmark.parser.Parser;
36-
import org.commonmark.renderer.html.HtmlRenderer;
3732
import org.eclipse.jdt.annotation.Nullable;
3833
import org.eclipse.jface.internal.text.html.BrowserInformationControl;
3934
import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
@@ -53,6 +48,7 @@
5348
import org.eclipse.lsp4e.LanguageServers;
5449
import org.eclipse.lsp4e.internal.CancellationUtil;
5550
import org.eclipse.lsp4e.internal.IdentifierUtil;
51+
import org.eclipse.lsp4e.internal.MarkdownUtil;
5652
import org.eclipse.lsp4j.Hover;
5753
import org.eclipse.lsp4j.HoverParams;
5854
import org.eclipse.lsp4j.MarkedString;
@@ -113,11 +109,7 @@ public class LSPTextHover implements ITextHover, ITextHoverExtension, ITextHover
113109
.collect(Collectors.joining("\n\n")) //$NON-NLS-1$
114110
.trim();
115111
if (!result.isEmpty()) {
116-
List<Extension> extensions = List.of(TablesExtension.create());
117-
Parser parser = Parser.builder().extensions(extensions).build();
118-
Node document = parser.parse(result);
119-
HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
120-
return renderer.render(document);
112+
return MarkdownUtil.renderToHtml(result);
121113
} else {
122114
return null;
123115
}

0 commit comments

Comments
 (0)