6.1 KiB
Configuration
MarkwonConfiguration class holds common Markwon functionality.
These are configurable properties:
AsyncDrawableLoader(back here since )SyntaxHighlightLinkSpan.ResolverUrlProcessorImageSizeResolver
:::tip
Additionally MarkwonConfiguration holds:
MarkwonThemeMarkwonSpansFactory
Please note that these values can be retrieved from MarkwonConfiguration
instance, but their configuration must be done by a Plugin by overriding
one of the methods:
Plugin#configureThemePlugin#configureSpansFactory:::
AsyncDrawableLoader
Allows loading and displaying of images in markdown. Please note that if one is not specified directly (or via plugin) no images will be displayed.
final Markwon markwon = Markwon.builder(context)
.usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.asyncDrawableLoader(AsyncDrawableLoader.noOp());
}
})
.build();
Currently Markwon provides 3 implementations for loading images:
- own implementation with SVG, GIF, data uri and android_assets support
- based on Picasso
- based on Glide
SyntaxHighlight
final Markwon markwon = Markwon.builder(this)
.usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.syntaxHighlight(new SyntaxHighlightNoOp());
}
})
.build();
:::tip Use syntax-highlight to add syntax highlighting to your application :::
LinkSpan.Resolver
React to a link click event. By default LinkResolverDef is used,
which tries to start an Activity given the link argument. If no
Activity can handle link LinkResolverDef silently ignores click event
final Markwon markwon = Markwon.builder(this)
.usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.linkResolver(new LinkSpan.Resolver() {
@Override
public void resolve(View view, @NonNull String link) {
// react to link click here
}
});
}
})
.build();
:::tip
Please note that Markwon will apply LinkMovementMethod to a resulting TextView
if there is none registered. if you wish to register own instance of a MovementMethod
apply it directly to a TextView or use MovementMethodPlugin
:::
UrlProcessor
Process URLs in your markdown (for links and images). If not provided explicitly, default no-op implementation will be used, which does not modify URLs (keeping them as-is).
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.
:::tip
Please note that UrlProcessorAndroidAssets will process only URLs that have no scheme information,
so a ./art/image.png will become file:///android_asset/art/image.JPG whilst https://so.me/where.png
will be kept as-is.
:::
ImageSizeResolver
ImageSizeResolver controls the size of an image to be displayed. Currently it
handles only HTML images (specified via img tag).
final Markwon markwon = Markwon.builder(this)
.usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.imageSizeResolver(new ImageSizeResolver() {
@NonNull
@Override
public Rect resolveImageSize(
@Nullable ImageSize imageSize,
@NonNull Rect imageBounds,
int canvasWidth,
float textSize) {
return null;
}
});
}
})
.build();
If not provided explicitly, default ImageSizeResolverDef implementation will be used.
It handles 3 dimension units:
%(percent, relative to Canvas width)em(relative to text size)px(absolute size, every dimension 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 dimensions is missing.
:::warning Height%
There is no support for % units for height dimension. 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-reference from which to calculate image height.
:::
:::tip
ImageSizeResolverDef also takes care for an image to not exceed
canvas width. If an image has greater width than a TextView Canvas, then
image will be scaled-down to fit the canvas. Please note that this rule
applies only if image has no absolute sizes (for example width is specified
in pixels).
:::