Add inline processor to custom extension sample
This commit is contained in:
parent
36089699d4
commit
d2e4730179
@ -8,6 +8,9 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.commonmark.node.Link;
|
||||
import org.commonmark.node.Text;
|
||||
import org.commonmark.parser.InlineParserFactory;
|
||||
import org.commonmark.parser.Parser;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@ -20,6 +23,8 @@ import io.noties.markwon.RenderProps;
|
||||
import io.noties.markwon.SpannableBuilder;
|
||||
import io.noties.markwon.core.CorePlugin;
|
||||
import io.noties.markwon.core.CoreProps;
|
||||
import io.noties.markwon.inlineparser.InlineProcessor;
|
||||
import io.noties.markwon.inlineparser.MarkwonInlineParser;
|
||||
import io.noties.markwon.sample.R;
|
||||
|
||||
public class CustomExtensionActivity2 extends Activity {
|
||||
@ -35,6 +40,20 @@ public class CustomExtensionActivity2 extends Activity {
|
||||
// * `#1` - an issue or a pull request
|
||||
// * `@user` link to a user
|
||||
|
||||
|
||||
final String md = "# Custom Extension 2\n" +
|
||||
"\n" +
|
||||
"This is an issue #1\n" +
|
||||
"Done by @noties";
|
||||
|
||||
|
||||
// inline_parsing(textView, md);
|
||||
|
||||
text_added(textView, md);
|
||||
}
|
||||
|
||||
private void text_added(@NonNull TextView textView, @NonNull String md) {
|
||||
|
||||
final Markwon markwon = Markwon.builder(this)
|
||||
.usePlugin(new AbstractMarkwonPlugin() {
|
||||
@Override
|
||||
@ -45,14 +64,84 @@ public class CustomExtensionActivity2 extends Activity {
|
||||
})
|
||||
.build();
|
||||
|
||||
final String md = "# Custom Extension 2\n" +
|
||||
"\n" +
|
||||
"This is an issue #1\n" +
|
||||
"Done by @noties";
|
||||
markwon.setMarkdown(textView, md);
|
||||
}
|
||||
|
||||
private void inline_parsing(@NonNull TextView textView, @NonNull String md) {
|
||||
|
||||
final InlineParserFactory inlineParserFactory = MarkwonInlineParser.factoryBuilder()
|
||||
// include all current defaults (otherwise will be empty - contain only our inline-processors)
|
||||
.includeDefaults()
|
||||
.addInlineProcessor(new IssueInlineProcessor())
|
||||
.addInlineProcessor(new UserInlineProcessor())
|
||||
.build();
|
||||
|
||||
final Markwon markwon = Markwon.builder(this)
|
||||
.usePlugin(new AbstractMarkwonPlugin() {
|
||||
@Override
|
||||
public void configureParser(@NonNull Parser.Builder builder) {
|
||||
builder.inlineParserFactory(inlineParserFactory);
|
||||
}
|
||||
})
|
||||
.build();
|
||||
|
||||
markwon.setMarkdown(textView, md);
|
||||
}
|
||||
|
||||
private static class IssueInlineProcessor extends InlineProcessor {
|
||||
|
||||
private static final Pattern RE = Pattern.compile("\\d+");
|
||||
|
||||
@Override
|
||||
public char specialCharacter() {
|
||||
return '#';
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean parse() {
|
||||
final String id = match(RE);
|
||||
if (id != null) {
|
||||
final Link link = new Link(createIssueOrPullRequestLinkDestination(id), null);
|
||||
link.appendChild(new Text("#" + id));
|
||||
appendNode(link);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static String createIssueOrPullRequestLinkDestination(@NonNull String id) {
|
||||
return "https://github.com/noties/Markwon/issues/" + id;
|
||||
}
|
||||
}
|
||||
|
||||
private static class UserInlineProcessor extends InlineProcessor {
|
||||
|
||||
private static final Pattern RE = Pattern.compile("\\w+");
|
||||
|
||||
@Override
|
||||
public char specialCharacter() {
|
||||
return '@';
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean parse() {
|
||||
final String user = match(RE);
|
||||
if (user != null) {
|
||||
final Link link = new Link(createUserLinkDestination(user), null);
|
||||
link.appendChild(new Text("@" + user));
|
||||
appendNode(link);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static String createUserLinkDestination(@NonNull String user) {
|
||||
return "https://github.com/" + user;
|
||||
}
|
||||
}
|
||||
|
||||
private static class GithubLinkifyRegexTextAddedListener implements CorePlugin.OnTextAddedListener {
|
||||
|
||||
private static final Pattern PATTERN = Pattern.compile("((#\\d+)|(@\\w+))", Pattern.MULTILINE);
|
||||
|
Loading…
x
Reference in New Issue
Block a user