Markdown presentation as stylized text in plugins

Hello Community.

How would you recommend parsing and presenting Markdown formatted text in IDE using a plugin?

Potential flow:

  1. get markdown formatted text from API,
  2. convert it to HTML with org.jetbrains:markdown and show using JBHtmlPanel
  3. show code fragments in markdown using com.intellij.openapi.editor.ex.EditorEx to get syntax highlighting.

On the markdown plugin usage -

Sample code from GitHub - JetBrains/markdown: Markdown parser written in kotlin :

final String src = "Some *Markdown*";
final MarkdownFlavourDescriptor flavour = new GFMFlavourDescriptor();
final ASTNode parsedTree = new MarkdownParser(flavour).buildMarkdownTreeFromString(text);
final String html = new HtmlGenerator(src, parsedTree, flavour, false).generateHtml();

generateHtml requires ASTNode, ASTNode, Iterable<CharSequence?>.
Is there a minimal working Java code?

Thank you!

If you haven’t already, you could explore how the Markdown plugin works - the source is on GitHub: intellij-community/plugins/markdown at master · JetBrains/intellij-community · GitHub

You are in luck; there’s an API that does exactly this:

import com.intellij.markdown.utils.doc.DocMarkdownToHtmlConverter

val html = DocMarkdownToHtmlConverter.convert(project, someMarkdownString)

(This is in Kotlin, but the equivalent Java code should be similar.)

It adds syntax highlighting and such on top of org.jetbrains:markdown. There’s a small bug, but it should be good enough for the vast majority of cases.

3 Likes