Markwon Documentation

repository·master·Indexed 25 days ago

https://github.com/noties/markwon

A high-performance Android library that renders Markdown directly into native Android Spannables without using a WebView. It is extensible and compatible with any Android widget that accepts Spanned content. Features include support for LaTeX formulas via JLatexMathPlugin, strikethrough functionality, GFM task-lists, HTML rendering via HtmlPlugin, and advanced table rendering using TablePlugin or TableEntryPlugin for RecyclerView integration.

Tokens
56.7K
Snippets
182
Records
242
Agent score
84%

What's inside Markwon

  1. Overview of Markwon features

    master

    Markwon is an Android markdown library that parses markdown following the [commonmark-spec] and renders it as native Android Spannables.

    Key characteristics:

    • No WebView required: It does not use HTML as an intermediate step, making it extremely fast.
    • Native Rendering: Renders directly to Android-native Spannables.
    • Wide Compatibility: Works with any widget that accepts Spanned content, including TextView, Button, Switch, CheckBox, and Toasts.
    • Extensible: Supports features like syntax highlighting, tables, images, and LaTeX formulas via extensions.
    • Editor Support: Since version 4.2.0, it includes an editor (markwon-editor) to highlight markdown input in real-time (e.g., in an EditText).
  2. Overview of Markwon

    master

    Markwon is a markdown library for Android that parses markdown following the CommonMark spec using the commonmark-java library. It renders the result as native Android Spannables without using HTML or a WebView as an intermediate step, making it extremely fast and lightweight.

    Key capabilities:

    • Displays markdown in any widget that accepts Spanned content (e.g., TextView, Button, Switch, CheckBox, Toasts).
    • Supports standard markdown features including headers, emphasis, links, images, lists, code blocks, and tables.
    • Includes an editor (since v4.2.0) to highlight markdown input in real-time (e.g., in an EditText).
    • Highly extensible via plugins/extensions.
  3. Use MarkwonView to display Markdown in Android

    master

    Markwon provides two view types for displaying Markdown in Android applications. Both implement the IMarkwonView interface and support layout previews in Android Studio.

    • MarkwonView: Extends android.view.TextView.
    • MarkwonViewCompat: Extends android.support.v7.widget.AppCompatTextView.

    Warning: These views parse Markdown on the main thread. Use them only for relatively small portions of Markdown to avoid UI blocking.

  4. Install Markwon via Maven/Gradle

    master

    To use Markwon in your Android project, you can include the stable artifacts via Maven Central. For the latest development features, you can use the SNAPSHOT version by adding the Sonatype snapshot repository to your root project's build.gradle file.

    allprojects {
        repositories {
            jcenter()
            google()
            // Add this for SNAPSHOT versions
            maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
        }
    }
  5. Run tests in the Markwon sample app

    master

    The sample app uses Robolectric (v3.8), which is incompatible with JDK versions greater than 1.8. If you are using an IDEA-bundled JDK, you must specify the JDK path explicitly when running tests from the command line.

    Run tests using the following command:

    ./gradlew :app-s:testDe -Dorg.gradle.java.home="{INSERT BUNDLED JDK PATH HERE}"

    To find your bundled JDK path in IntelliJ IDEA:

    1. Open Project Structure...
    2. Open SDK Location
    3. Copy the path from the JDK Location field.
    ./gradlew :app-s:testDe -Dorg.gradle.java.home="{INSERT BUNDLED JDK PATH HERE}"
  6. Enable LaTeX inline parsing

    master

    To use inline LaTeX, you must explicitly enable inlines in the JLatexMathPlugin and you must also include the MarkwonInlineParserPlugin in your Markwon builder.

    final Markwon markwon = Markwon.builder(this)
            // required plugin to support inline parsing
            .usePlugin(MarkwonInlineParserPlugin.create())
            .usePlugin(JLatexMathPlugin.create(textView.getTextSize(), new JLatexMathPlugin.BuilderConfigure() {
                @Override
                public void configureBuilder(@NonNull JLatexMathPlugin.Builder builder) {
                    // ENABLE inlines
                    builder.inlinesEnabled(true);
                }
            }))
            .build();
  7. Add SVG image support to Markwon

    master

    To support SVG images within your markdown, you must use the SvgPlugin. This plugin relies on the androidsvg library. When building your Markwon instance, you must register both the ImagesPlugin and the SvgPlugin. The SvgPlugin requires a Resources object to correctly scale SVG media based on the device's display density.

    final Markwon markwon = Markwon.builder(context)
            // it's required to register ImagesPlugin
            .usePlugin(ImagesPlugin.create(context))
            .usePlugin(SvgPlugin.create(context.getResources()))
            .build();
  8. Make tables scrollable with HorizontalScrollView

    master

    To allow table columns to stretch to the screen width or to enable horizontal scrolling when table content exceeds the screen width, wrap the TableLayout inside a HorizontalScrollView in your layout XML.

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:paddingLeft="16dip"
        android:paddingTop="8dip"
        android:paddingRight="16dip"
        android:paddingBottom="8dip"
        android:scrollbarStyle="outsideInset">
    
        <TableLayout
            android:id="@+id/table_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:stretchColumns="*" />
    
    </HorizontalScrollView>
  9. Force new lines on softbreak nodes

    master

    To ensure a new line is added whenever a softbreak is encountered in the markdown, register a visitor for the SoftLineBreak.class node that calls visitor.forceNewLine().

    final Markwon markwon = Markwon.builder(context)
            .usePlugin(new AbstractMarkwonPlugin() {
                @Override
                public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) {
                    builder.on(SoftLineBreak.class, new MarkwonVisitor.NodeVisitor<SoftLineBreak>() {
                        @Override
                        public void visit(@NonNull MarkwonVisitor visitor, @NonNull SoftLineBreak softLineBreak) {
                            visitor.forceNewLine();
                        }
                    });
                }
            })
            .build();