Merge branch 'develop' of https://github.com/noties/Markwon into develop
This commit is contained in:
commit
20d2bebd2b
@ -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,41 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param useCompat If true, use {@link LinkifyCompat} to handle links.
|
||||||
|
* Note that the {@link LinkifyCompat} depends on androidx.core:core,
|
||||||
|
* the dependency must be added on a client side explicitly.
|
||||||
|
*/
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param useCompat If true, use {@link LinkifyCompat} to handle links.
|
||||||
|
* Note that the {@link LinkifyCompat} depends on androidx.core:core,
|
||||||
|
* the dependency must be added on a client side explicitly.
|
||||||
|
*/
|
||||||
|
@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 +77,13 @@ 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));
|
final LinkifyTextAddedListener listener;
|
||||||
|
if (useCompat) {
|
||||||
|
listener = new LinkifyCompatTextAddedListener(mask);
|
||||||
|
} else {
|
||||||
|
listener = new LinkifyTextAddedListener(mask);
|
||||||
|
}
|
||||||
|
corePlugin.addOnTextAddedListener(listener);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -80,7 +110,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 +131,21 @@ public class LinkifyPlugin extends AbstractMarkwonPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask) {
|
||||||
|
return Linkify.addLinks(text, mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class LinkifyCompatTextAddedListener extends LinkifyTextAddedListener {
|
||||||
|
|
||||||
|
LinkifyCompatTextAddedListener(int mask) {
|
||||||
|
super(mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask) {
|
||||||
|
return LinkifyCompat.addLinks(text, mask);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user