2.6 KiB
2.6 KiB
Registry
Registry
allows to pre-configure other plugins and/or declare a dependency on a plugin,
which also will modify internal order of plugins inside a Markwon
instance.
For example, you have a configurable plugin:
public class MyPlugin extends AbstractMarkwonPlugin {
private boolean enabled;
public boolean enabled() {
return enabled;
}
@NonNull
public MyPlugin enabled(boolean enabled) {
this.enabled = enabled;
return this;
}
{...}
}
and other plugin that needs to access MyPlugin
or modify/configure it:
public class MyOtherPlugin extends AbstractMarkwonPlugin {
@Override
public void configure(@NonNull Registry registry) {
registry.require(MyPlugin.class, new Action<MyPlugin>() {
@Override
public void apply(@NonNull MyPlugin myPlugin) {
myPlugin.enabled(false);
}
});
}
}
final Markwon markwon = Markwon.builder(context)
.usePlugin(new MyOtherPlugin())
.usePlugin(new MyPlugin())
.build();
Internal plugins order (in this case) will be:
CorePlugin
(added automatically and always the first one)MyPlugin
(was required byMyOtherPlugin
)MyOtherPlugin
:::tip
There is no need to require CorePlugin
as it will be the first one inside
Markwon
instance.
:::
The order matters if you want to override some plugin. For example, CoolPlugin
adds a SpanFactory
for a Cool
markdown node. Other NotCoolPlugin
wants to
use a different SpanFactory
, then:
final Markwon markwon = Markwon.builder(context)
.usePlugin(CoolPlugin.create())
.usePlugin(new NotCoolPlugin() {
@Override
public void configure(@NonNull MarkwonPlugin.Registry registry) {
registry.require(CoolPlugin.class);
}
@Override
public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) {
builder.setFactory(Cool.class, new NotCoolSpanFactory());
}
})
.build();
All require
calls to the Registry
will also validate at runtime that
required plugins are registered.
final Markwon markwon = Markwon.builder(context)
.usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configure(@NonNull Registry registry) {
// will throw an exception if `NotPresentPlugin` is not present
registry.require(NotPresentPlugin.class);
}
})
.build();