7.6 KiB
Configuration
SpannableConfiguration is the core component that controls how markdown is parsed and rendered.
It can be obtained via factory methods:
// creates default implementation
final SpannableConfiguration configuration = SpannableConfiguration.create(context);
// creates configurablable instance via `#builder` method
final SpannableConfiguration configuration = SpannableConfiguration.builder(context)
.asyncDrawableLoader(AsyncDrawableLoader.create())
.build();
:::tip Note
If #builder factory method is used, you do not need to specify default
values as they will be applied automatically
:::
:::warning Images
If you plan on using images inside your markdown/HTML, you will have to explicitly
register an implementation of AsyncDrawable.Loader via #asyncDrawableLoader builder method.
Markwon comes with ready implementation for that and it can be cound in
markwon-image-loader module. Refer to module documentation
:::
Theme
SpannableTheme controls how markdown is rendered. It has pretty extensive number of
options that can be found here
SpannableConfiguration.builder(context)
.theme(SpannableTheme)
.build();
If SpannableTheme is not provided explicitly, SpannableTheme.create(context) will be used
Images
Async loader
AsyncDrawable.Loader handles images in your markdown and HTML
SpannableConfiguration.builder(context)
.asyncDrawableLoader(AsyncDrawable.Loader)
.build();
If AsyncDrawable.Loader is not provided explicitly, default no-op implementation will be used.
:::tip Implementation
There are no restrictions on what implementation to use, but Markwon has artifact that can
answer the most common needs of displaying SVG, GIF and other image formats. It can be found here
:::
Size resolver
ImageSizeResolver controls the size of an image to be displayed. Currently it
handles only HTML images (specified via img tag).
SpannableConfiguration.builder(context)
.imageSizeResolver(ImageSizeResolver)
.build();
If not provided explicitly, default ImageSizeResolverDef implementation will be used.
It handles 3 dimention units:
%(percent)em(relative to text size)px(absolute size, every dimention that is not%oremis considered to be absolute)
<img width="100%">
<img width="2em" height="10px">
<img style="{width: 100%; height: 8em;}">
ImageSizeResolverDef keeps the ratio of original image if one of the dimentions is missing.
:::warning Height%
There is no support for % units for height dimention. This is due to the fact that
height of an TextView in which markdown is displayed is non-stable and changes with time
(for example when image is loaded and applied to a TextView it will increase TextView's height),
so we will have no point-of-refence from which to calculate image height.
:::
Syntax highlight
SyntaxHighlight controls the syntax highlight for code blocks (in markdown).
SpannableConfiguration.builder(context)
.syntaxHighlight(SyntaxHighlight)
.build();
If not provided explicitly, default no-op implementation will be used.
:::tip Syntax highlight
Although SyntaxHighlight interface was included with the very first version
of Markwon there were no ready-to-use implementations. But starting with
Markwon provides one. It can be found in markwon-syntax-highlight artifact. Refer
to module documentation
:::
Link resolver
LinkSpan.Resolver is triggered when a link is clicked in markdown/HTML.
SpannableConfiguration.builder(context)
.linkResolver(LinkSpan.Resolver)
.build();
If not provided explicitly, default LinkResolverDef implementation will be used.
Underneath it constructs an Intent and tries to start an Activity associated with it.
It no Activity is found, it will silently fail (no runtime exceptions)
URL processor
UrlProcessor is used to process found URLs in markdown/HTML.
SpannableConfiguration.builder(context)
.urlProcessor(UrlProcessor)
.build();
If not provided explicitly, default no-op implementation will be used.
Markwon provides 2 implementations of UrlProcessor:
UrlProcessorRelativeToAbsoluteUrlProcessorAndroidAssets
UrlProcessorRelativeToAbsolute
UrlProcessorRelativeToAbsolute can be used to make relative URL absolute. For example if an image is
defined like this:  and UrlProcessorRelativeToAbsolute
is created with https://github.com/noties/Markwon/raw/master/ as the base:
new UrlProcessorRelativeToAbsolute("https://github.com/noties/Markwon/raw/master/"),
then final image will have https://github.com/noties/Markwon/raw/master/art/image.JPG
as the destination.
UrlProcessorAndroidAssets
UrlProcessorAndroidAssets can be used to make processed links to point to Android assets folder.
So an image:  will have file:///android_asset/art/image.JPG as the
destination
Factory
SpannableFactory is used to control what span implementations to be used
SpannableConfiguration.builder(context)
.factory(SpannableFactory)
.build();
If not provided explicitly, default SpannableFactoryDef implementation will be used. It is documented
in this section
Soft line break
softBreakAddsNewLine option controls how soft breaks are treated in the final result.
If true -> soft break will add a new line, else it will add a (space) char.
SpannableConfiguration.builder(context)
.softBreakAddsNewLine(boolean)
.build();
If not provided explicitly, default false value will be used.
Trim white space from end
trimWhiteSpaceEnd option controls whether or not to trim white spaces from the
end of a document.
SpannableConfiguration.builder(context)
.trimWhiteSpaceEnd(boolean)
.build();
If not provided explicitly, default true value will be used.
:::tip Before
Before 2.0.0 version this functionality was implicitly included in
SpannableBuilder#text method. This is no longer true and now SpannableBuilder
does not trim white spaces (which was by default and non-configurable)
:::
HTML
Parser
MarkwonHtmlParser is used to parse HTML content
SpannableConfiguration.builder(context)
.htmlParser(MarkwonHtmlParser)
.build();
if not provided explicitly, default MarkwonHtmlParserImpl will be used
if it can be found in classpath, otherwise default no-op implementation
wiil be used. Refer to HTML document for more information about this behavior.
Renderer
MarkwonHtmlRenderer controls how parsed HTML content will be rendered.
SpannableConfiguration.builder(context)
.htmlRenderer(MarkwonHtmlRenderer)
.build();
If not provided explicitly, default MarkwonHtmlRenderer implementation will be used.
It is documented here
HTML ignore non-closed tags
htmlIgnoreNonClosedTags option is used to control whether or not to
render non-closed HTML tags
SpannableConfiguration.builder(context)
.htmlIgnoreNonClosedTags(boolean)
.build();
If not provided explicitly, default value true will be used (non-closed tags won't be rendered).