Markwon

Additional

Language
Java
Version
v1.1.0 (Jul 30, 2018)
Created
May 18, 2017
Updated
Jul 30, 2018
Owner
Dimitry (noties)
Contributor
Dimitry (noties)
1
Activity
Badge
Generate
Download
Source code
APK file

Show card

Markwon

Markwon is a library for Android that renders markdown as system-native Spannables. It gives ability to display markdown in all TextView widgets (TextView, Button, Switch, CheckBox, etc), Notifications, Toasts, etc. No WebView is required. Library provides reasonable defaults for display style of markdown but also gives all the means to tweak the appearance if desired. All markdown features are supported (including limited support for inlined HTML code, markdown tables and images).

*This file is displayed by default in the sample-apk application. Which is a generic markdown viewer with support to display markdown via http, https & file schemes and 2 themes included: Light & Dark

Installation

implementation 'ru.noties:markwon:1.1.0'
implementation 'ru.noties:markwon-image-loader:1.1.0' // optional
implementation 'ru.noties:markwon-syntax:1.1.0' // optional
implementation 'ru.noties:markwon-view:1.1.0' // optional

Snapshot

In order to use latest SNAPSHOT version add snapshot repository to your root project's build.gradle file:

allprojects {
    repositories {
        jcenter()
        google()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    }
}

and then in your module build.gradle:

implementation 'ru.noties:markwon:1.1.0-SNAPSHOT'

Please note that markwon-image-loader, markwon-syntax and markwon-view are also present in SNAPSHOT repository and share the same version as main markwon artifact.

Supported markdown features:

  • Emphasis (*, _)
  • Strong emphasis (**, __)
  • Strike-through (~~)
  • Headers (#{1,6})
  • Links ([]() && [][])
  • Images (requires special handling)
  • Thematic break (---, ***, ___)
  • Quotes & nested quotes (>{1,})
  • Ordered & non-ordered lists & nested ones
  • Inline code
  • Code blocks
  • Tables (with limitations)
  • Small subset of inline-html (which is rendered by this library):
    • Emphasis (<i>, <em>, <cite>, <dfn>)
    • Strong emphasis (<b>, <strong>)
    • SuperScript (<sup>)
    • SubScript (<sub>)
    • Underline (<u>)
    • Strike-through (<s>, <strike>, <del>)
    • other inline html is rendered via (Html.fromHtml(...))
  • Task lists:
  • Not
    • Done with X
    done
  • andor small x


It's just a helper method, that does underneath:

  • constructs a Parser (see: commonmark-java) and parses markdown
  • constructs a SpannableConfiguration
  • renders parsed markdown to Spannable (via SpannableRenderer)
  • prepares TextView to display images, tables and links
  • sets text

This flow answers the most simple usage of displaying markdown: one shot parsing & configuration of relatively small markdown chunks. If your markdown contains a lot of text or you plan to display multiple UI widgets with markdown you might consider stepping in and taking control of this flow.

The candidate requirements to step in:

  • parsing and processing of parsed markdown in background thread
  • reusing Parser and/or SpannableConfiguration between multiple calls
  • ignore images and tables specific logic (you know that markdown won't contain them)

So, if we expand Markwon.setMarkdown(textView, markdown) method we will see the following:

// create a Parser instance (can be done manually)
// internally creates default Parser instance & registers `strike-through` & `tables` extension
final Parser parser = Markwon.createParser();

// core class to display markdown, can be obtained via this method,
// which creates default instance (no images handling though),
// or via `builder` method, which lets you to configure this instance
//
// `this` refers to a Context instance
final SpannableConfiguration configuration = SpannableConfiguration.create(this);

// it's better **not** to re-use this class between multiple calls
final SpannableRenderer renderer = new SpannableRenderer();

final Node node = parser.parse(markdown);
final CharSequence text = renderer.render(configuration, node);

// for links in markdown to be clickable
textView.setMovementMethod(LinkMovementMethod.getInstance());

// we need these due to the limited nature of Spannables to invalidate TextView
Markwon.unscheduleDrawables(textView);
Markwon.unscheduleTableRows(textView);

textView.setText(text);

Markwon.scheduleDrawables(textView);
Markwon.scheduleTableRows(textView);

Please note that if you are having trouble with LinkMovementMethod you can use Markwon.setText(textView, markdown, movementMethod) method (@since 1.0.6) to specify no movement method (aka null) or own implementation. As an alternative to the system LinkMovementMethod you can use Better-Link-Movement-Method.

Please refer to SpannableConfiguration document for more info

Syntax highlight

Starting with version 1.1.0 there is an artifact (markwon-syntax) that allows you to have syntax highlight functionality. It is based on Prism4j project. It contains 2 builtin themes: Default (light, Prism4jThemeDefault) and Darkula (dark, Prism4jThemeDarkula).

library-syntax