Visitor
WARNING
This is documentation for legacy versions. For the most current version click here.
Starting with 3.0.0 visiting of parsed markdown
nodes does not require creating own instance of commonmark-java Visitor,
instead a composable/configurable MarkwonVisitor is used.
Visitor.Builder
There is no need to create own instance of MarkwonVisitor.Builder as
it is done by Markwon itself. One still can configure it as one wishes:
final Markwon markwon = Markwon.builder(contex)
.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();
}
});
}
});
MarkwonVisitor encapsulates most of the functionality of rendering parsed markdown.
It holds rendering configuration:
MarkwonVisitor#configuration- getter for current MarkwonConfigurationMarkwonVisitor#renderProps- getter for current RenderPropsMarkwonVisitor#builder- getter for currentSpannableBuilder
It contains also a number of utility functions:
visitChildren(Node)- will visit all children of supplied NodehasNext(Node)- utility function to check if supplied Node has a Node after it (useful for white-space management, so there should be no blank new line after last BlockNode)ensureNewLine- will insert a new line at currentSpannableBuilderposition only if current (last) character is not a new-lineforceNewLine- will insert a new line character without any condition checkinglength- helper function to callvisitor.builder().length(), returns current length ofSpannableBuilderclear- will clear state forRenderPropsandSpannableBuilder, this is done byMarkwonautomatically after each render call
And some utility functions to control the spans:
setSpans(int start, Object spans)- will apply suppliedspansonSpannableBuilderstarting atstartposition and ending atSpannableBuilder#length.spanscan benull(no spans will be applied) or an array of spans (each span of this array will be applied)setSpansForNodeOptional(N node, int start)- helper method to set spans for specifiednode(internally obtainsSpanFactoryfor that node and uses it to apply spans)setSpansForNode(N node, int start)- almost the same assetSpansForNodeOptionalbut instead of silently ignoring call if noneSpanFactoryis registered, this method will throw an exception.
@Override
public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) {
builder.on(Heading.class, new MarkwonVisitor.NodeVisitor<Heading>() {
@Override
public void visit(@NonNull MarkwonVisitor visitor, @NonNull Heading heading) {
// or just `visitor.length()`
final int start = visitor.builder().length();
visitor.visitChildren(heading);
// or just `visitor.setSpansForNodeOptional(heading, start)`
final SpanFactory factory = visitor.configuration().spansFactory().get(heading.getClass());
if (factory != null) {
visitor.setSpans(start, factory.getSpans(visitor.configuration(), visitor.renderProps()));
}
if (visitor.hasNext(heading)) {
visitor.ensureNewLine();
visitor.forceNewLine();
}
}
});
}