Class Markflow
Note that Markflow does not provide any caching for parsing and rendering. Each call
to parse(java.lang.CharSequence)
and render(org.commonmark.node.Node)
will create an instance for tracking internal
states. Therefore they are also thread-safe and can be done in any background thread.
To display a Markdown document on the screen, you need to follow the following steps:
- Call
parse(CharSequence)
with the raw Markdown input (treated as plain text) to obtain a tree of nodes. - Call
render(Node)
with the tree of nodes to obtain a text with styling spans, which is also known as rendered Markdown. - Call
setRenderedMarkdown(TextView, Spanned)
to install the rendered Markdown to a TextView on UI thread. Then TextView will then be responsible for creating text layout and the render node for multi-frame rendering.
convert(CharSequence)
can be used to parse invalid input: '&' render Markdown,
this method can be called from any thread. setMarkdown(TextView, CharSequence)
can be used to parse invalid input: '&' render invalid input: '&' install Markdown, but it can only be called from UI thread.
Note that: Once a TextView installs Markdown text through Markflow, it must also be uninstalled using the same Markflow.
- Since:
- 3.12
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic Markflow.Builder
Create a new instance ofMarkflow.Builder
with core plugin registered.static Markflow.Builder
Create a new instance ofMarkflow.Builder
, optionally with core plugin registered.convert
(CharSequence input) This is a helper method, calling this method is the same as callingparse(CharSequence)
and thenrender(Node)
.static Markflow
Create a minimal Markflow instance with the default theme and the core plugin.@UnmodifiableView List
<? extends MarkflowPlugin> Returns an immutable view of all registered plugins, sorted by dependencies.<P extends MarkflowPlugin>
PRequests information if certain plugin has been registered.org.commonmark.node.Node
parse
(CharSequence input) Parse the input Markdown text into AST.render
(org.commonmark.node.Node document) Render the tree of nodes to a Spanned text, which can be handled by framework's text layout engine.<P extends MarkflowPlugin>
PrequirePlugin
(Class<P> type) Requests information for certain plugin.void
setMarkdown
(TextView textView, CharSequence markdown) This is a helper method, calling this method is the same as callingparse(CharSequence)
thenrender(Node)
, and thensetRenderedMarkdown(TextView, Spanned)
.void
setRenderedMarkdown
(TextView textView, Spanned markdown) Install a rendered Markdown to the givenTextView
.
-
Method Details
-
create
Create a minimal Markflow instance with the default theme and the core plugin. Only core Markdown features are enabled, and images will not be resolved.- Parameters:
context
- view context- Returns:
- a new Markflow instance with only core plugin registered
-
builder
Create a new instance ofMarkflow.Builder
with core plugin registered.- Parameters:
context
- view context- Returns:
- a new Builder instance with core plugin registered
-
builder
Create a new instance ofMarkflow.Builder
, optionally with core plugin registered.- Parameters:
context
- view contextwithCore
- whether to register core plugin- Returns:
- a new Builder instance
-
parse
Parse the input Markdown text into AST.This method is thread-safe and allows multiple threads to start parsings for different source text concurrently.
- Parameters:
input
- the Markdown text to parse- Returns:
- the root document, can be cast to
Document
-
render
Render the tree of nodes to a Spanned text, which can be handled by framework's text layout engine.This method is thread-safe and allows multiple threads to start renderings for different documents concurrently.
Note that returned Spanned has few limitations. For example, images, tables and ordered lists require TextView to be properly displayed. Ordered list items require text layout based on TextView's parameters. Whenever possible use
setMarkdown(TextView, CharSequence)
orsetRenderedMarkdown(TextView, Spanned)
as these methods will additionally call specificMarkflowPlugin
methods to prepare proper display.- Parameters:
document
- the root node- Returns:
- the rendered Markdown
-
convert
This is a helper method, calling this method is the same as callingparse(CharSequence)
and thenrender(Node)
. Seerender(Node)
for details.- Parameters:
input
- the raw Markdown text- Returns:
- the rendered Markdown
-
setMarkdown
This is a helper method, calling this method is the same as callingparse(CharSequence)
thenrender(Node)
, and thensetRenderedMarkdown(TextView, Spanned)
.In most cases, you just need this to display Markdown, when Markdown is static and small. Note that you should not change TextView typography attributes after calling this.
This method can be called only from UI thread.
-
setRenderedMarkdown
Install a rendered Markdown to the givenTextView
.This method can be called only from UI thread.
- Parameters:
textView
- the target text view used to display the Markdownmarkdown
- the rendered Markdown to display
-
getPlugin
Requests information if certain plugin has been registered. -
requirePlugin
Requests information for certain plugin. Throws an exception if the plugin is not registered.- See Also:
-
getAllPlugins
Returns an immutable view of all registered plugins, sorted by dependencies. -
getConfig
-