feat: render nowhere links inline as branded cards
Nowhere (https://github.com/5t34k/nowhere) encodes whole mini-sites in a URL fragment, so the OpenGraph preview path returns nothing useful and hostednowhere.com 403s scrapers. Detect URLs on hostednowhere.com and nowhr.xyz that carry a fragment as a NowhereLinkSegment and render them through a NowhereLinkCard that labels the tool type (Event, Store, Message, ...) and opens the URL in the browser on click.
This commit is contained in:
+27
@@ -332,6 +332,10 @@ class RichTextParser {
|
||||
}
|
||||
}
|
||||
|
||||
if (urls.withoutScheme.contains(word) || urls.withScheme.contains(word)) {
|
||||
parseNowhereLink(word)?.let { return it }
|
||||
}
|
||||
|
||||
if (urls.withoutScheme.contains(word)) return SchemelessUrlSegment(word)
|
||||
|
||||
if (urls.withScheme.contains(word)) return LinkSegment(word)
|
||||
@@ -529,6 +533,29 @@ class RichTextParser {
|
||||
}
|
||||
|
||||
fun isUrlWithoutScheme(url: String) = noProtocolUrlValidator.matches(url)
|
||||
|
||||
// Nowhere links (https://github.com/5t34k/nowhere) encode an entire mini-site in the URL
|
||||
// fragment. Servers never see the fragment, so OpenGraph previews return nothing useful
|
||||
// (and hostednowhere.com 403s scrapers). Detect them by host + presence of a fragment
|
||||
// so the renderer can show a branded card instead of falling through to LoadUrlPreview.
|
||||
private val nowhereHosts = listOf("nowhr.xyz", "hostednowhere.com")
|
||||
|
||||
fun parseNowhereLink(word: String): NowhereLinkSegment? {
|
||||
val afterScheme =
|
||||
when {
|
||||
word.startsWith("https://", ignoreCase = true) -> word.substring(8)
|
||||
word.startsWith("http://", ignoreCase = true) -> word.substring(7)
|
||||
else -> word
|
||||
}
|
||||
val slash = afterScheme.indexOf('/')
|
||||
if (slash < 0) return null
|
||||
val host = afterScheme.substring(0, slash).lowercase()
|
||||
if (host !in nowhereHosts) return null
|
||||
val hash = afterScheme.indexOf('#', slash)
|
||||
if (hash < 0) return null
|
||||
val pathSegment = afterScheme.substring(slash + 1, hash).substringBefore('/').takeIf { it.isNotEmpty() }
|
||||
return NowhereLinkSegment(word, host, pathSegment)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
@@ -158,6 +158,13 @@ class SchemelessUrlSegment(
|
||||
segment: String,
|
||||
) : Segment(segment)
|
||||
|
||||
@Immutable
|
||||
class NowhereLinkSegment(
|
||||
segment: String,
|
||||
val host: String,
|
||||
val tool: String?,
|
||||
) : Segment(segment)
|
||||
|
||||
@Immutable
|
||||
class RegularTextSegment(
|
||||
segment: String,
|
||||
|
||||
+29
@@ -4400,6 +4400,35 @@ class RichTextParserTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testNowhereLinkInTheMiddleOfAPost() {
|
||||
val text = "Check out my store https://nowhr.xyz/s#bYxNC4JAEEDRX7LbpcsirR9F1MW0jRDLPsEOMZaHinJTCzOl right now"
|
||||
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||
val nowhere = state.paragraphs.flatMap { it.words }.firstOrNull { it is NowhereLinkSegment } as? NowhereLinkSegment
|
||||
assertEquals("https://nowhr.xyz/s#bYxNC4JAEEDRX7LbpcsirR9F1MW0jRDLPsEOMZaHinJTCzOl", nowhere?.segmentText)
|
||||
assertEquals("nowhr.xyz", nowhere?.host)
|
||||
assertEquals("s", nowhere?.tool)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHostedNowhereSchemelessLink() {
|
||||
val text = "see hostednowhere.com/m#bYxNAB end"
|
||||
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||
val nowhere = state.paragraphs.flatMap { it.words }.firstOrNull { it is NowhereLinkSegment } as? NowhereLinkSegment
|
||||
assertEquals("hostednowhere.com/m#bYxNAB", nowhere?.segmentText)
|
||||
assertEquals("hostednowhere.com", nowhere?.host)
|
||||
assertEquals("m", nowhere?.tool)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPlainNowhereHostWithoutFragmentStaysAsLink() {
|
||||
// No fragment => not a nowhere site, fall through to LinkSegment.
|
||||
val text = "Visit https://hostednowhere.com/manage to manage"
|
||||
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||
val nowhere = state.paragraphs.flatMap { it.words }.firstOrNull { it is NowhereLinkSegment }
|
||||
assertEquals(null, nowhere)
|
||||
}
|
||||
|
||||
private fun printStateForDebug(state: RichTextViewerState) {
|
||||
state.paragraphs.forEach { paragraph ->
|
||||
paragraph.words.forEach { seg ->
|
||||
|
||||
Reference in New Issue
Block a user