Allow LinkifyPlugin to use LinkifyCompat through config
This commit is contained in:
parent
8d3f0e908d
commit
d7f52607ab
@ -14,6 +14,12 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
deps.with {
|
||||||
|
// To use LinkifyCompat
|
||||||
|
// note that this dependency must be added on a client side explicitly
|
||||||
|
compileOnly it['x-core']
|
||||||
|
}
|
||||||
|
|
||||||
api project(':markwon-core')
|
api project(':markwon-core')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package io.noties.markwon.linkify;
|
package io.noties.markwon.linkify;
|
||||||
|
|
||||||
|
import android.text.Spannable;
|
||||||
import android.text.SpannableStringBuilder;
|
import android.text.SpannableStringBuilder;
|
||||||
import android.text.style.URLSpan;
|
import android.text.style.URLSpan;
|
||||||
import android.text.util.Linkify;
|
import android.text.util.Linkify;
|
||||||
|
|
||||||
import androidx.annotation.IntDef;
|
import androidx.annotation.IntDef;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.core.text.util.LinkifyCompat;
|
||||||
|
|
||||||
import org.commonmark.node.Link;
|
import org.commonmark.node.Link;
|
||||||
|
|
||||||
@ -33,19 +35,31 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin {
|
|||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public static LinkifyPlugin create() {
|
public static LinkifyPlugin create() {
|
||||||
return create(Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);
|
return create(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static LinkifyPlugin create(boolean useCompat) {
|
||||||
|
return create(Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS | Linkify.WEB_URLS, useCompat);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public static LinkifyPlugin create(@LinkifyMask int mask) {
|
public static LinkifyPlugin create(@LinkifyMask int mask) {
|
||||||
return new LinkifyPlugin(mask);
|
return new LinkifyPlugin(mask, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static LinkifyPlugin create(@LinkifyMask int mask, boolean useCompat) {
|
||||||
|
return new LinkifyPlugin(mask, useCompat);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final int mask;
|
private final int mask;
|
||||||
|
private final boolean useCompat;
|
||||||
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
@SuppressWarnings("WeakerAccess")
|
||||||
LinkifyPlugin(@LinkifyMask int mask) {
|
LinkifyPlugin(@LinkifyMask int mask, boolean useCompat) {
|
||||||
this.mask = mask;
|
this.mask = mask;
|
||||||
|
this.useCompat = useCompat;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -53,7 +67,7 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin {
|
|||||||
registry.require(CorePlugin.class, new Action<CorePlugin>() {
|
registry.require(CorePlugin.class, new Action<CorePlugin>() {
|
||||||
@Override
|
@Override
|
||||||
public void apply(@NonNull CorePlugin corePlugin) {
|
public void apply(@NonNull CorePlugin corePlugin) {
|
||||||
corePlugin.addOnTextAddedListener(new LinkifyTextAddedListener(mask));
|
corePlugin.addOnTextAddedListener(new LinkifyTextAddedListener(mask, useCompat));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -61,9 +75,11 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin {
|
|||||||
private static class LinkifyTextAddedListener implements CorePlugin.OnTextAddedListener {
|
private static class LinkifyTextAddedListener implements CorePlugin.OnTextAddedListener {
|
||||||
|
|
||||||
private final int mask;
|
private final int mask;
|
||||||
|
private final boolean useCompat;
|
||||||
|
|
||||||
LinkifyTextAddedListener(int mask) {
|
LinkifyTextAddedListener(int mask, boolean useCompat) {
|
||||||
this.mask = mask;
|
this.mask = mask;
|
||||||
|
this.useCompat = useCompat;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -80,7 +96,7 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin {
|
|||||||
// render calls from different threads and ... better performance)
|
// render calls from different threads and ... better performance)
|
||||||
final SpannableStringBuilder builder = new SpannableStringBuilder(text);
|
final SpannableStringBuilder builder = new SpannableStringBuilder(text);
|
||||||
|
|
||||||
if (Linkify.addLinks(builder, mask)) {
|
if (addLinks(builder, mask)) {
|
||||||
// target URL span specifically
|
// target URL span specifically
|
||||||
final URLSpan[] spans = builder.getSpans(0, builder.length(), URLSpan.class);
|
final URLSpan[] spans = builder.getSpans(0, builder.length(), URLSpan.class);
|
||||||
if (spans != null
|
if (spans != null
|
||||||
@ -101,5 +117,13 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask) {
|
||||||
|
if (useCompat) {
|
||||||
|
return LinkifyCompat.addLinks(text, mask);
|
||||||
|
} else {
|
||||||
|
return Linkify.addLinks(text, mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user