use of io.noties.prism4j.Prism4j in project PocketHub by pockethub.
the class MarkwonUtils method createMarkwon.
public static Markwon createMarkwon(Context context, String baseUrl) {
final Prism4j prism4j = new Prism4j(new GrammarLocatorDef());
return Markwon.builder(context).usePlugin(StrikethroughPlugin.create()).usePlugin(TaskListPlugin.create(context)).usePlugin(HtmlPlugin.create()).usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) {
builder.on(FencedCodeBlock.class, (visitor, fencedCodeBlock) -> {
// We actually won't be applying code spans here, as our custom view will
// draw background and apply mono typeface
//
// NB the `trim` operation on literal (as code will have a new line at the end)
final CharSequence code = visitor.configuration().syntaxHighlight().highlight(fencedCodeBlock.getInfo(), fencedCodeBlock.getLiteral().trim());
visitor.builder().append(code);
});
}
@Override
public void configureParser(@NonNull Parser.Builder builder) {
super.configureParser(builder);
builder.postProcessor(new PostProcessor() {
@Override
public Node process(Node node) {
Visitor t = new AbstractVisitor() {
@Override
public void visit(HtmlBlock htmlBlock) {
String literal = htmlBlock.getLiteral();
if (literal.startsWith("<!--")) {
htmlBlock.unlink();
} else {
super.visit(htmlBlock);
}
}
};
node.accept(t);
return node;
}
});
}
}).usePlugin(GlideImagesPlugin.create(new GifAwareGlideStore(context))).usePlugin(new SpanLinkPlugin(baseUrl)).usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configure(@NonNull Registry registry) {
registry.require(HtmlPlugin.class, htmlPlugin -> htmlPlugin.addHandler(new AlignHandler()));
}
}).usePlugin(TableEntryPlugin.create(TablePlugin.create(context))).usePlugin(SyntaxHighlightPlugin.create(prism4j, Prism4jThemeDefault.create())).usePlugin(new AsyncDrawableSchedulerPlugin()).build();
}
use of io.noties.prism4j.Prism4j in project android by nextcloud.
the class PreviewTextFragment method getRenderedMarkdownText.
protected static Spanned getRenderedMarkdownText(Activity activity, String markdown) {
Prism4j prism4j = new Prism4j(new MarkwonGrammarLocator());
Prism4jTheme prism4jTheme = Prism4jThemeDefault.create();
TaskListDrawable drawable = new TaskListDrawable(Color.GRAY, Color.GRAY, Color.WHITE);
drawable.setColorFilter(ThemeColorUtils.primaryColor(activity, true), PorterDuff.Mode.SRC_ATOP);
final Markwon markwon = Markwon.builder(activity).usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configureTheme(@NonNull MarkwonTheme.Builder builder) {
builder.linkColor(ThemeColorUtils.primaryColor(activity, true));
builder.headingBreakHeight(0);
}
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.linkResolver((view, link) -> {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
DisplayUtils.startIntentIfAppAvailable(intent, activity, R.string.no_browser_available);
});
}
}).usePlugin(TablePlugin.create(activity)).usePlugin(TaskListPlugin.create(drawable)).usePlugin(StrikethroughPlugin.create()).usePlugin(HtmlPlugin.create()).usePlugin(SyntaxHighlightPlugin.create(prism4j, prism4jTheme)).build();
return markwon.toMarkdown(markdown);
}
Aggregations