Search in sources :

Example 1 with AbstractMarkwonPlugin

use of io.noties.markwon.AbstractMarkwonPlugin 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();
}
Also used : HtmlBlock(org.commonmark.node.HtmlBlock) Context(android.content.Context) StrikethroughPlugin(io.noties.markwon.ext.strikethrough.StrikethroughPlugin) MarkwonVisitor(io.noties.markwon.MarkwonVisitor) NonNull(androidx.annotation.NonNull) Prism4jThemeDefault(io.noties.markwon.syntax.Prism4jThemeDefault) Markwon(io.noties.markwon.Markwon) SyntaxHighlightPlugin(io.noties.markwon.syntax.SyntaxHighlightPlugin) Prism4j(io.noties.prism4j.Prism4j) Visitor(org.commonmark.node.Visitor) TableEntryPlugin(io.noties.markwon.recycler.table.TableEntryPlugin) TaskListPlugin(io.noties.markwon.ext.tasklist.TaskListPlugin) FencedCodeBlock(org.commonmark.node.FencedCodeBlock) Parser(org.commonmark.parser.Parser) TablePlugin(io.noties.markwon.ext.tables.TablePlugin) AbstractMarkwonPlugin(io.noties.markwon.AbstractMarkwonPlugin) PrismBundle(io.noties.prism4j.annotations.PrismBundle) Node(org.commonmark.node.Node) HtmlPlugin(io.noties.markwon.html.HtmlPlugin) AbstractVisitor(org.commonmark.node.AbstractVisitor) PostProcessor(org.commonmark.parser.PostProcessor) GlideImagesPlugin(io.noties.markwon.image.glide.GlideImagesPlugin) AbstractVisitor(org.commonmark.node.AbstractVisitor) MarkwonVisitor(io.noties.markwon.MarkwonVisitor) Visitor(org.commonmark.node.Visitor) AbstractVisitor(org.commonmark.node.AbstractVisitor) Node(org.commonmark.node.Node) HtmlPlugin(io.noties.markwon.html.HtmlPlugin) HtmlBlock(org.commonmark.node.HtmlBlock) Prism4j(io.noties.prism4j.Prism4j) NonNull(androidx.annotation.NonNull) AbstractMarkwonPlugin(io.noties.markwon.AbstractMarkwonPlugin) PostProcessor(org.commonmark.parser.PostProcessor)

Example 2 with AbstractMarkwonPlugin

use of io.noties.markwon.AbstractMarkwonPlugin 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);
}
Also used : Context(android.content.Context) DeviceInfo(com.nextcloud.client.device.DeviceInfo) StrikethroughPlugin(io.noties.markwon.ext.strikethrough.StrikethroughPlugin) Bundle(android.os.Bundle) Spanned(android.text.Spanned) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) OCFile(com.owncloud.android.datamodel.OCFile) Markwon(io.noties.markwon.Markwon) Intent(android.content.Intent) SyntaxHighlightPlugin(io.noties.markwon.syntax.SyntaxHighlightPlugin) LinkMovementMethod(android.text.method.LinkMovementMethod) Prism4j(io.noties.prism4j.Prism4j) UserAccountManager(com.nextcloud.client.account.UserAccountManager) MarkwonConfiguration(io.noties.markwon.MarkwonConfiguration) Inject(javax.inject.Inject) TaskListPlugin(io.noties.markwon.ext.tasklist.TaskListPlugin) TablePlugin(io.noties.markwon.ext.tables.TablePlugin) TaskListDrawable(io.noties.markwon.ext.tasklist.TaskListDrawable) MarkwonTheme(io.noties.markwon.core.MarkwonTheme) Handler(android.os.Handler) View(android.view.View) DisplayUtils(com.owncloud.android.utils.DisplayUtils) LayoutInflater(android.view.LayoutInflater) Prism4jThemeDefault(io.noties.markwon.syntax.Prism4jThemeDefault) Prism4jTheme(io.noties.markwon.syntax.Prism4jTheme) TextUtils(android.text.TextUtils) SearchView(androidx.appcompat.widget.SearchView) PorterDuff(android.graphics.PorterDuff) ViewGroup(android.view.ViewGroup) Color(android.graphics.Color) Log_OC(com.owncloud.android.lib.common.utils.Log_OC) TextView(android.widget.TextView) ThemeColorUtils(com.owncloud.android.utils.theme.ThemeColorUtils) Nullable(androidx.annotation.Nullable) AbstractMarkwonPlugin(io.noties.markwon.AbstractMarkwonPlugin) PrismBundle(io.noties.prism4j.annotations.PrismBundle) FileFragment(com.owncloud.android.ui.fragment.FileFragment) StringUtils(com.owncloud.android.utils.StringUtils) Html(android.text.Html) HtmlPlugin(io.noties.markwon.html.HtmlPlugin) MimeTypeUtil(com.owncloud.android.utils.MimeTypeUtil) Injectable(com.nextcloud.client.di.Injectable) FileDisplayActivity(com.owncloud.android.ui.activity.FileDisplayActivity) Activity(android.app.Activity) R(com.owncloud.android.R) Resources(android.content.res.Resources) TextFilePreviewBinding(com.owncloud.android.databinding.TextFilePreviewBinding) TaskListDrawable(io.noties.markwon.ext.tasklist.TaskListDrawable) MarkwonTheme(io.noties.markwon.core.MarkwonTheme) Intent(android.content.Intent) MarkwonConfiguration(io.noties.markwon.MarkwonConfiguration) Prism4j(io.noties.prism4j.Prism4j) Markwon(io.noties.markwon.Markwon) AbstractMarkwonPlugin(io.noties.markwon.AbstractMarkwonPlugin) Prism4jTheme(io.noties.markwon.syntax.Prism4jTheme)

Aggregations

Context (android.content.Context)2 NonNull (androidx.annotation.NonNull)2 AbstractMarkwonPlugin (io.noties.markwon.AbstractMarkwonPlugin)2 Markwon (io.noties.markwon.Markwon)2 StrikethroughPlugin (io.noties.markwon.ext.strikethrough.StrikethroughPlugin)2 TablePlugin (io.noties.markwon.ext.tables.TablePlugin)2 Activity (android.app.Activity)1 Intent (android.content.Intent)1 Resources (android.content.res.Resources)1 Color (android.graphics.Color)1 PorterDuff (android.graphics.PorterDuff)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Html (android.text.Html)1 Spanned (android.text.Spanned)1 TextUtils (android.text.TextUtils)1 LinkMovementMethod (android.text.method.LinkMovementMethod)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1