Merge pull request #2971 from vitorpamplona/claude/add-nowhere-links-LPMnM
Add Nowhere link detection and branded card rendering
This commit is contained in:
+31
-2
@@ -332,9 +332,15 @@ class RichTextParser {
|
||||
}
|
||||
}
|
||||
|
||||
if (urls.withoutScheme.contains(word)) return SchemelessUrlSegment(word)
|
||||
if (urls.withoutScheme.contains(word)) {
|
||||
parseNowhereLink(word)?.let { return it }
|
||||
return SchemelessUrlSegment(word)
|
||||
}
|
||||
|
||||
if (urls.withScheme.contains(word)) return LinkSegment(word)
|
||||
if (urls.withScheme.contains(word)) {
|
||||
parseNowhereLink(word)?.let { return it }
|
||||
return LinkSegment(word)
|
||||
}
|
||||
|
||||
if (urls.emails.contains(word)) return EmailSegment(word)
|
||||
|
||||
@@ -529,6 +535,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,
|
||||
|
||||
+46
@@ -4400,6 +4400,52 @@ 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 testNowhereLinkWithLargeBase64UrlFragment() {
|
||||
// Real nowhere links can be kilobytes long (a full store catalogue or forum thread
|
||||
// base64url-encoded). Verify both UrlDetector capture and our classifier survive a
|
||||
// ~5KB fragment containing every base64url-legal character (A-Z, a-z, 0-9, -, _).
|
||||
val alphabet = ('A'..'Z').joinToString("") + ('a'..'z').joinToString("") + ('0'..'9').joinToString("") + "-_"
|
||||
val payload = (0 until 80).joinToString("") { alphabet }
|
||||
val url = "https://nowhr.xyz/s#$payload"
|
||||
val text = "look at this big store $url and reply"
|
||||
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||
val nowhere = state.paragraphs.flatMap { it.words }.firstOrNull { it is NowhereLinkSegment } as? NowhereLinkSegment
|
||||
assertEquals(url, nowhere?.segmentText)
|
||||
assertEquals(payload.length, nowhere?.segmentText?.substringAfter('#')?.length)
|
||||
assertEquals("nowhr.xyz", nowhere?.host)
|
||||
assertEquals("s", 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 ->
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.richtext
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.model.EmptyTagList
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class RichTextParserNowhereJvmTest {
|
||||
@Test
|
||||
fun realWorldNowhereStoreLinkRoundtripsThroughTheParser() {
|
||||
// Verbatim from a real Nostr note ("Works, thanks!\n\nhttps://nowhr.xyz/s#..."): a
|
||||
// published nowhr.xyz store catalogue with a ~5KB base64url fragment. Kept as a
|
||||
// resource so the URL isn't subject to source-editor truncation.
|
||||
val url =
|
||||
this::class.java
|
||||
.classLoader!!
|
||||
.getResourceAsStream("richtext/realNowhereStoreUrl.txt")!!
|
||||
.bufferedReader()
|
||||
.readText()
|
||||
.trim()
|
||||
|
||||
assertTrue(url.startsWith("https://nowhr.xyz/s#"), "resource lost its prefix")
|
||||
assertTrue(url.length > 5000, "resource shorter than expected: ${url.length}")
|
||||
|
||||
val text = "Works, thanks!\n\n$url"
|
||||
|
||||
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||
val nowhere = state.paragraphs.flatMap { it.words }.firstOrNull { it is NowhereLinkSegment } as? NowhereLinkSegment
|
||||
|
||||
assertEquals(url, nowhere?.segmentText)
|
||||
assertEquals("nowhr.xyz", nowhere?.host)
|
||||
assertEquals("s", nowhere?.tool)
|
||||
assertTrue(state.urlSet.withScheme.contains(url), "url not in detected withScheme set")
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user