Skip to content

Commit

Permalink
Render markdown with commonmark instead of wikitext
Browse files Browse the repository at this point in the history
commonmark is the library used internally by the JDK and by JDT for
markdown rendering. It's more standard and leaner than wikitext.
  • Loading branch information
mickaelistria committed Sep 20, 2024
1 parent 4c0e997 commit 8d03914
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
5 changes: 2 additions & 3 deletions org.eclipse.lsp4e/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.12.0",
org.eclipse.ui.navigator;bundle-version="3.6.100",
org.eclipse.lsp4j;bundle-version="[0.23.0,0.24.0)",
org.eclipse.lsp4j.jsonrpc;bundle-version="[0.23.0,0.24.0)",
org.eclipse.mylyn.wikitext;bundle-version="4.3.0",
org.eclipse.mylyn.wikitext.markdown;bundle-version="4.3.0",
org.eclipse.ui.console,
org.eclipse.ltk.core.refactoring,
org.eclipse.core.expressions;bundle-version="3.5.0",
Expand All @@ -42,7 +40,8 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.12.0",
com.google.guava;bundle-version="30.1.0",
org.eclipse.e4.core.commands,
org.eclipse.compare.core,
org.eclipse.compare
org.eclipse.compare,
org.commonmark;bundle-version="0.23.0"
Bundle-ClassPath: .
Bundle-Localization: plugin
Bundle-ActivationPolicy: lazy
Expand Down
11 changes: 7 additions & 4 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.IFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBuffer;
Expand Down Expand Up @@ -138,8 +141,6 @@
import org.eclipse.ltk.core.refactoring.resource.RenameResourceChange;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.mylyn.wikitext.markdown.MarkdownLanguage;
import org.eclipse.mylyn.wikitext.parser.MarkupParser;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.RGBA;
import org.eclipse.text.edits.MalformedTreeException;
Expand Down Expand Up @@ -184,7 +185,6 @@ public final class LSPEclipseUtils {
private static final String MARKDOWN = "markdown"; //$NON-NLS-1$
private static final String MD = "md"; //$NON-NLS-1$
private static final int MAX_BROWSER_NAME_LENGTH = 30;
private static final MarkupParser MARKDOWN_PARSER = new MarkupParser(new MarkdownLanguage());

private LSPEclipseUtils() {
// this class shouldn't be instantiated
Expand Down Expand Up @@ -1434,7 +1434,10 @@ public static List<IContentType> getDocumentContentTypes(IDocument document) {
String kind = markupContent.getKind();
if (MARKDOWN.equalsIgnoreCase(kind) || MD.equalsIgnoreCase(kind)) {
try {
return MARKDOWN_PARSER.parseToHtml(text);
Parser parser = Parser.builder().build();
Node document = parser.parse(text);
HtmlRenderer renderer = HtmlRenderer.builder().build();
return renderer.render(document);
} catch (Exception e) {
LanguageServerPlugin.logError(e);
return htmlParagraph(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.internal.text.html.BrowserInformationControl;
import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
Expand All @@ -48,8 +51,6 @@
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.mylyn.wikitext.markdown.MarkdownLanguage;
import org.eclipse.mylyn.wikitext.parser.MarkupParser;
import org.eclipse.swt.widgets.Shell;

/**
Expand All @@ -59,7 +60,6 @@
@SuppressWarnings("restriction")
public class LSPTextHover implements ITextHover, ITextHoverExtension {

private static final MarkupParser MARKDOWN_PARSER = new MarkupParser(new MarkdownLanguage(true));
private static final int GET_TIMEOUT_MS = 1000;

private @Nullable IRegion lastRegion;
Expand Down Expand Up @@ -94,7 +94,10 @@ public class LSPTextHover implements ITextHover, ITextHoverExtension {
.collect(Collectors.joining("\n\n")) //$NON-NLS-1$
.trim();
if (!result.isEmpty()) {
return MARKDOWN_PARSER.parseToHtml(result);
Parser parser = Parser.builder().build();
Node document = parser.parse(result);
HtmlRenderer renderer = HtmlRenderer.builder().build();
return renderer.render(document);
} else {
return null;
}
Expand Down
6 changes: 6 additions & 0 deletions repository/category.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@
<bundle id="com.google.gson">
<category name="deps"/>
</bundle>
<bundle id="org.commonmark">
<category name="deps"/>
</bundle>
<bundle id="org.commonmark-gfm-tables">
<category name="deps"/>
</bundle>
</site>
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
<?pde version="3.8"?>
<target name="LSP4E Target-platform" sequenceNumber="1473749310">
<locations>
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.mylyn.wikitext" version="0.0.0"/>
<unit id="org.eclipse.mylyn.wikitext.markdown" version="0.0.0"/>
<unit id="org.eclipse.mylyn.wikitext.markdown.ui" version="0.0.0"/>
<repository location="https://download.eclipse.org/mylyn/updates/release/latest/"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.lsp4j" version="0.0.0"/>
<unit id="org.eclipse.lsp4j.jsonrpc" version="0.0.0"/>
Expand All @@ -21,6 +15,11 @@
<unit id="org.eclipse.sdk.ide" version="0.0.0"/>
<repository location="https://download.eclipse.org/eclipse/updates/latest/"/>
</location>
<location type="InstallableUnit" includeConfigurePhase="true" includeMode="planner" includeSource="true">
<repository location="https://download.eclipse.org/tools/orbit/simrel/orbit-aggregation/milestone/S202409180633"/>
<unit id="org.commonmark" version="0.0.0"/>
<unit id="org.commonmark-gfm-tables" version="0.0.0"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.tm4e.feature.feature.group" version="0.0.0"/>
<repository location="https://download.eclipse.org/tm4e/releases/latest/"/>
Expand Down

0 comments on commit 8d03914

Please sign in to comment.