|
| 1 | +package com.swmansion.enriched.markdown.input.autolink |
| 2 | + |
| 3 | +import android.text.Spannable |
| 4 | +import com.swmansion.enriched.markdown.input.detection.TextDetector |
| 5 | +import com.swmansion.enriched.markdown.input.detection.WordResult |
| 6 | +import com.swmansion.enriched.markdown.input.formatting.FormattingStore |
| 7 | +import com.swmansion.enriched.markdown.input.model.FormattingRange |
| 8 | +import com.swmansion.enriched.markdown.input.model.InputFormatterStyle |
| 9 | +import com.swmansion.enriched.markdown.input.model.StyleType |
| 10 | +import java.util.regex.Pattern |
| 11 | + |
| 12 | +typealias OnLinkDetectedCallback = (text: String, url: String, start: Int, end: Int) -> Unit |
| 13 | + |
| 14 | +class AutoLinkDetector( |
| 15 | + private val formattingStore: FormattingStore, |
| 16 | +) : TextDetector { |
| 17 | + private var config: LinkRegexConfig? = null |
| 18 | + private var compiledPattern: Pattern? = null |
| 19 | + var style: InputFormatterStyle? = null |
| 20 | + var onLinkDetected: OnLinkDetectedCallback? = null |
| 21 | + |
| 22 | + fun setRegexConfig(newConfig: LinkRegexConfig) { |
| 23 | + if (newConfig == config) return |
| 24 | + config = newConfig |
| 25 | + compiledPattern = null |
| 26 | + |
| 27 | + if (!newConfig.isDefault && !newConfig.isDisabled && newConfig.pattern.isNotEmpty()) { |
| 28 | + var flags = 0 |
| 29 | + if (newConfig.caseInsensitive) flags = flags or Pattern.CASE_INSENSITIVE |
| 30 | + if (newConfig.dotAll) flags = flags or Pattern.DOTALL |
| 31 | + compiledPattern = |
| 32 | + try { |
| 33 | + Pattern.compile(newConfig.pattern, flags) |
| 34 | + } catch (_: Exception) { |
| 35 | + null |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + override fun processWord( |
| 41 | + spannable: Spannable, |
| 42 | + wordResult: WordResult, |
| 43 | + ) { |
| 44 | + val detectedUrl = detectNewLinkInWord(spannable, wordResult) |
| 45 | + if (detectedUrl != null) { |
| 46 | + onLinkDetected?.invoke(wordResult.word, detectedUrl, wordResult.start, wordResult.end) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + override fun refreshStyling(spannable: Spannable) { |
| 51 | + val currentStyle = style ?: return |
| 52 | + val markers = spannable.getSpans(0, spannable.length, AutoDetectedLinkMarkerSpan::class.java) |
| 53 | + for (marker in markers) { |
| 54 | + val start = spannable.getSpanStart(marker) |
| 55 | + val end = spannable.getSpanEnd(marker) |
| 56 | + applyVisualStyling(spannable, start, end, currentStyle) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + override fun transientFormattingRanges(spannable: Spannable): List<FormattingRange> { |
| 61 | + val markers = spannable.getSpans(0, spannable.length, AutoDetectedLinkMarkerSpan::class.java) |
| 62 | + return markers.map { marker -> |
| 63 | + FormattingRange( |
| 64 | + StyleType.LINK, |
| 65 | + spannable.getSpanStart(marker), |
| 66 | + spannable.getSpanEnd(marker), |
| 67 | + marker.url, |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + fun clearAutoLinkInRange( |
| 73 | + spannable: Spannable, |
| 74 | + start: Int, |
| 75 | + end: Int, |
| 76 | + ) { |
| 77 | + removeAutoLinkSpans(spannable, start, end) |
| 78 | + } |
| 79 | + |
| 80 | + private fun detectNewLinkInWord( |
| 81 | + spannable: Spannable, |
| 82 | + wordResult: WordResult, |
| 83 | + ): String? { |
| 84 | + if (config?.isDisabled == true) return null |
| 85 | + |
| 86 | + val (word, wordStart, wordEnd) = wordResult |
| 87 | + |
| 88 | + val hasManualLink = formattingStore.rangeOfType(StyleType.LINK, wordStart) != null |
| 89 | + if (hasManualLink) { |
| 90 | + removeAutoLinkSpans(spannable, wordStart, wordEnd) |
| 91 | + return null |
| 92 | + } |
| 93 | + |
| 94 | + val matchedUrl = matchWord(word) |
| 95 | + val existingMarker = |
| 96 | + spannable |
| 97 | + .getSpans(wordStart, wordEnd, AutoDetectedLinkMarkerSpan::class.java) |
| 98 | + .firstOrNull { spannable.getSpanStart(it) == wordStart && spannable.getSpanEnd(it) == wordEnd } |
| 99 | + |
| 100 | + if (matchedUrl != null) { |
| 101 | + if (existingMarker?.url == matchedUrl) return null |
| 102 | + |
| 103 | + removeAutoLinkSpans(spannable, wordStart, wordEnd) |
| 104 | + spannable.setSpan( |
| 105 | + AutoDetectedLinkMarkerSpan(matchedUrl), |
| 106 | + wordStart, |
| 107 | + wordEnd, |
| 108 | + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE, |
| 109 | + ) |
| 110 | + val currentStyle = style |
| 111 | + if (currentStyle != null) { |
| 112 | + applyVisualStyling(spannable, wordStart, wordEnd, currentStyle) |
| 113 | + } |
| 114 | + return matchedUrl |
| 115 | + } else { |
| 116 | + if (existingMarker != null) { |
| 117 | + removeAutoLinkSpans(spannable, wordStart, wordEnd) |
| 118 | + } |
| 119 | + return null |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private fun matchWord(word: String): String? { |
| 124 | + if (word.isEmpty()) return null |
| 125 | + |
| 126 | + val custom = compiledPattern |
| 127 | + if (custom != null) { |
| 128 | + return if (custom.matcher(word).matches()) normalizeUrl(word) else null |
| 129 | + } |
| 130 | + |
| 131 | + if (DEFAULT_PATTERN.matcher(word).matches()) return normalizeUrl(word) |
| 132 | + |
| 133 | + return null |
| 134 | + } |
| 135 | + |
| 136 | + private fun removeAutoLinkSpans( |
| 137 | + spannable: Spannable, |
| 138 | + start: Int, |
| 139 | + end: Int, |
| 140 | + ) { |
| 141 | + for (span in spannable.getSpans(start, end, AutoDetectedLinkMarkerSpan::class.java)) { |
| 142 | + spannable.removeSpan(span) |
| 143 | + } |
| 144 | + for (span in spannable.getSpans(start, end, AutoDetectedLinkColorSpan::class.java)) { |
| 145 | + spannable.removeSpan(span) |
| 146 | + } |
| 147 | + for (span in spannable.getSpans(start, end, AutoDetectedLinkUnderlineSpan::class.java)) { |
| 148 | + spannable.removeSpan(span) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private fun applyVisualStyling( |
| 153 | + spannable: Spannable, |
| 154 | + start: Int, |
| 155 | + end: Int, |
| 156 | + style: InputFormatterStyle, |
| 157 | + ) { |
| 158 | + spannable.setSpan( |
| 159 | + AutoDetectedLinkColorSpan(style.linkColor), |
| 160 | + start, |
| 161 | + end, |
| 162 | + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE, |
| 163 | + ) |
| 164 | + if (style.linkUnderline) { |
| 165 | + spannable.setSpan( |
| 166 | + AutoDetectedLinkUnderlineSpan(), |
| 167 | + start, |
| 168 | + end, |
| 169 | + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE, |
| 170 | + ) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + companion object { |
| 175 | + private val DEFAULT_PATTERN: Pattern = |
| 176 | + Pattern.compile( |
| 177 | + "(?:https?://[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-z]{2,6}\\b[-a-zA-Z0-9@:%_+.~#?&//=]*" + |
| 178 | + "|www\\.[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-z]{2,6}\\b[-a-zA-Z0-9@:%_+.~#?&//=]*" + |
| 179 | + "|[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-z]{2,6}\\b[-a-zA-Z0-9@:%_+.~#?&//=]*)", |
| 180 | + Pattern.CASE_INSENSITIVE, |
| 181 | + ) |
| 182 | + |
| 183 | + private fun normalizeUrl(url: String): String { |
| 184 | + if (url.startsWith("http://") || url.startsWith("https://")) return url |
| 185 | + return "https://$url" |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments