Sample, add block handler sample

This commit is contained in:
Dimitry Ivanov 2020-07-29 12:12:33 +03:00
parent aafbd8585c
commit 0a6c5df0f2
2 changed files with 78 additions and 0 deletions

View File

@ -1,4 +1,16 @@
[ [
{
"javaClassName": "io.noties.markwon.app.samples.BlockHandlerSample",
"id": "202007211090524",
"title": "Block handler",
"description": "Custom block delimiters that control new lines after block nodes",
"artifacts": [
"CORE"
],
"tags": [
"rendering"
]
},
{ {
"javaClassName": "io.noties.markwon.app.samples.CacheMarkwonSample", "javaClassName": "io.noties.markwon.app.samples.CacheMarkwonSample",
"id": "202007189102458", "id": "202007189102458",

View File

@ -0,0 +1,66 @@
package io.noties.markwon.app.samples;
import androidx.annotation.NonNull;
import org.commonmark.node.Node;
import io.noties.markwon.AbstractMarkwonPlugin;
import io.noties.markwon.Markwon;
import io.noties.markwon.MarkwonVisitor;
import io.noties.markwon.app.sample.Tags;
import io.noties.markwon.app.sample.ui.MarkwonTextViewSample;
import io.noties.markwon.sample.annotations.MarkwonArtifact;
import io.noties.markwon.sample.annotations.MarkwonSampleInfo;
@MarkwonSampleInfo(
id = "202007211090524",
title = "Block handler",
description = "Custom block delimiters that control new lines after block nodes",
artifacts = MarkwonArtifact.CORE,
tags = Tags.rendering
)
public class BlockHandlerSample extends MarkwonTextViewSample {
@Override
public void render() {
final String md = "" +
"# Heading\n" +
"* one\n" +
"* two\n" +
"* three\n" +
"---\n" +
"> a quote\n\n" +
"```\n" +
"code\n" +
"```\n" +
"some text after";
final Markwon markwon = Markwon.builder(context)
.usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) {
builder.blockHandler(new BlockHandlerNoAdditionalNewLines());
}
})
.build();
markwon.setMarkdown(textView, md);
}
}
class BlockHandlerNoAdditionalNewLines implements MarkwonVisitor.BlockHandler {
@Override
public void blockStart(@NonNull MarkwonVisitor visitor, @NonNull Node node) {
// ensure that content rendered on a new line
visitor.ensureNewLine();
}
@Override
public void blockEnd(@NonNull MarkwonVisitor visitor, @NonNull Node node) {
if (visitor.hasNext(node)) {
visitor.ensureNewLine();
// by default markwon here also has:
// visitor.forceNewLine();
}
}
}