Giter Site home page Giter Site logo

Comments (6)

ciouti avatar ciouti commented on August 28, 2024 1

it's working well. Thanks a lot!

Glad I could help.

from markwon.

ciouti avatar ciouti commented on August 28, 2024

use "font", example:

<font color="#7093DB">your text</font>
<font color="#7093DB" size="5">your text</font>

from markwon.

emrememil avatar emrememil commented on August 28, 2024

@ciouti I tried this but it didn't work.

This is my configuration for Markdown. Is there something missing?

        return Markwon.builder(context)
            .usePlugin(HtmlPlugin.create { plugin -> plugin.excludeDefaults(true) })
            .usePlugin(CorePlugin.create())
            .usePlugin(CoilImagesPlugin.create(context, coilImageLoader))
            .usePlugin(MarkwonInlineParserPlugin.create())
            .usePlugin(JLatexMathPlugin.create(binding.markdownTextView.textSize) { mathPluginBuilder ->
                mathPluginBuilder.inlinesEnabled(true)
            })
            .usePlugin(StrikethroughPlugin.create())
            .usePlugin(TablePlugin.create(context))
            .usePlugin(TaskListPlugin.create(context))
            .usePlugin(SimpleExtPlugin.create())
            .usePlugin(LinkifyPlugin.create(linkifyMask))
            .usePlugin(object : AbstractMarkwonPlugin() {
                override fun configureConfiguration(builder: MarkwonConfiguration.Builder) {
                }
            })
            .build()

from markwon.

ciouti avatar ciouti commented on August 28, 2024

Sorry, I forgot this is my own extension of the code. Here is the code :

                //HTML
                .usePlugin(HtmlPlugin.create(plugin -> {
                    plugin.addHandler(new FontTagHandler());
                }))

FontTagHandler Class :

package utils.markwon;

import static utils.tool.StrUtils.patternStrArr;

import android.graphics.Color;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

import io.noties.markwon.MarkwonVisitor;
import io.noties.markwon.html.HtmlTag;
import io.noties.markwon.html.MarkwonHtmlRenderer;
import io.noties.markwon.html.TagHandler;

public class FontTagHandler extends TagHandler {

    private final int fontTextSize;

    FontTagHandler() {
        this.fontTextSize = 15;
    }

    private static double parsePositionSize(@Nullable String value) {
        double position;
        if (!TextUtils.isEmpty(value)) {
            try {
                position = Double.parseDouble(Objects.requireNonNull(value));
            } catch (NumberFormatException e) {
                e.printStackTrace();
                position = 3;
            }
        } else {
            position = 3;
        }
        return position;
    }

    private static String parsePositionColor(@Nullable String value) {
        String position = "#000000";
        if (!TextUtils.isEmpty(value)) {
            try {
                String[] positions = patternStrArr(Objects.requireNonNull(value), "^#{0,1}[a-fA-F\\d]{6}$");
                if (positions != null) {
                    position = positions[0];
                    if (!position.startsWith("#")) {
                        position = String.format("%s%s", "#", position);
                    }
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return position;
    }

    @Override
    public void handle(
            @NonNull MarkwonVisitor visitor,
            @NonNull MarkwonHtmlRenderer renderer,
            @NonNull HtmlTag tag) {

        //自定义文本大小 <font size="3">This is some text!</font>
        final double size = parsePositionSize(tag.attributes().get("size"));
        int setTextSize;
        try {
            if (size > 0) {
                setTextSize = Double.valueOf(size * fontTextSize).intValue();
            } else {
                setTextSize = 3 * fontTextSize;
            }
        } catch (Exception exception) {
            Log.e("MarkdownTAG", String.valueOf(exception));
            setTextSize = 3 * fontTextSize;
        }
        visitor.builder().setSpan(
                new AbsoluteSizeSpan(setTextSize),
                tag.start(),
                tag.end()
        );

        //自定义文本颜色 <font color="#000000">This is some text!</font>
        final String color = parsePositionColor(tag.attributes().get("color"));
        visitor.builder().setSpan(
                new ForegroundColorSpan(Color.parseColor(color)),
                tag.start(),
                tag.end()
        );

    }

    @NonNull
    @NotNull
    @Override
    public Collection<String> supportedTags() {
        return Collections.singleton("font");
    }
}

patternStrArr code :

    //正则匹配字符串
    public static String[] patternStrArr(@NotNull String str, @NonNull String pattern_split) {
        try {
            final Pattern p = Pattern.compile(pattern_split);
            final Matcher m = p.matcher(str);
            ArrayList<String> tempList = new ArrayList<>();
            String[] strArr;
            while (m.find()) {
                tempList.add(m.group());
            }
            if (tempList.size() > 0) {
                strArr = arrListToStr(tempList);//匹配结果保存到strArr
                return strArr;
            } else {
                return null;
            }
        } catch (Exception exception) {
            Log.e("PatternTAG", String.valueOf(exception));
            return null;
        }
    }

from markwon.

ciouti avatar ciouti commented on August 28, 2024

size = 3 This is closest to the size of normal text.
My code allows numeric values with fractional parts, but Typora or the browser doesn't support them.

from markwon.

emrememil avatar emrememil commented on August 28, 2024

it's working well. Thanks a lot!

from markwon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.