Fix truncating content based on links that aren't displayed

This commit is contained in:
Jonathan Staab 2023-06-20 13:47:07 -07:00
parent 4420284e64
commit 06405d5cc3
6 changed files with 22 additions and 24 deletions

View File

@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import {without, last} from "ramda" import {without} from "ramda"
import { import {
parseContent, parseContent,
getLinks, getLinks,
@ -28,8 +28,7 @@
const links = getLinks(shortContent) const links = getLinks(shortContent)
const extraLinks = without(links, getLinks(fullContent)) const extraLinks = without(links, getLinks(fullContent))
export const isNewline = i => export const isNewline = i => !shortContent[i] || shortContent[i].type === NEWLINE
!shortContent[i] || shortContent[i].type === NEWLINE
export const isStartOrEnd = i => isNewline(i - 1) || isNewline(i + 1) export const isStartOrEnd = i => isNewline(i - 1) || isNewline(i + 1)
</script> </script>
@ -46,7 +45,7 @@
<QRCode fullWidth onClick="copy" code={value} /> <QRCode fullWidth onClick="copy" code={value} />
</div> </div>
{:else if type === LINK} {:else if type === LINK}
<NoteContentLink {value} showMedia={showMedia && isStartOrEnd(i) && last(value.url.split('://')).includes('/')} /> <NoteContentLink {value} showMedia={showMedia && isStartOrEnd(i)} />
{:else if type.match(/^nostr:np(rofile|ub)$/)} {:else if type.match(/^nostr:np(rofile|ub)$/)}
<NoteContentPerson {value} /> <NoteContentPerson {value} />
{:else if type.startsWith("nostr:") && showMedia && isStartOrEnd(i) && value.id !== anchorId} {:else if type.startsWith("nostr:") && showMedia && isStartOrEnd(i) && value.id !== anchorId}

View File

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import {Tags} from "src/util/nostr" import {Tags} from "src/util/nostr"
import {canDisplayUrl} from "src/util/notes" import {urlIsMedia} from "src/util/notes"
import NoteContentLink from "src/app/shared/NoteContentLink.svelte" import NoteContentLink from "src/app/shared/NoteContentLink.svelte"
export let note, showMedia export let note, showMedia
@ -9,5 +9,5 @@
</script> </script>
{#if url} {#if url}
<NoteContentLink value={{url, canDisplay: canDisplayUrl(url)}} {showMedia} /> <NoteContentLink value={{url, isMedia: urlIsMedia(url)}} {showMedia} />
{/if} {/if}

View File

@ -1,10 +1,10 @@
<script lang="ts"> <script lang="ts">
import {marked} from 'marked' import {marked} from "marked"
import insane from 'insane' import insane from "insane"
import {Tags} from 'src/util/nostr' import {Tags} from "src/util/nostr"
import {canDisplayUrl} from 'src/util/notes' import {urlIsMedia} from "src/util/notes"
import {modal} from "src/partials/state" import {modal} from "src/partials/state"
import Chip from 'src/partials/Chip.svelte' import Chip from "src/partials/Chip.svelte"
import NoteContentLink from "src/app/shared/NoteContentLink.svelte" import NoteContentLink from "src/app/shared/NoteContentLink.svelte"
export let note, showEntire export let note, showEntire
@ -19,18 +19,16 @@
</script> </script>
<div class="flex flex-col gap-2 overflow-hidden text-ellipsis"> <div class="flex flex-col gap-2 overflow-hidden text-ellipsis">
<h3 class="text-2xl staatliches">{title}</h3> <h3 class="staatliches text-2xl">{title}</h3>
{#if summary && !showEntire} {#if summary && !showEntire}
<p>{summary}</p> <p>{summary}</p>
{/if} {/if}
{#if showMedia && image && canDisplayUrl(image)} {#if showMedia && image && urlIsMedia(image)}
<NoteContentLink value={{url: image, canDisplay: true}} showMedia /> <NoteContentLink value={{url: image, canDisplay: true}} showMedia />
{/if} {/if}
<div> <div>
{#each tags.topics() as topic} {#each tags.topics() as topic}
<Chip <Chip class="mr-2 mb-2 inline-block cursor-pointer" on:click={() => openTopic(topic)}>
class="mr-2 mb-2 inline-block cursor-pointer"
on:click={() => openTopic(topic)}>
#{topic} #{topic}
</Chip> </Chip>
{/each} {/each}

View File

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import {Tags} from "src/util/nostr" import {Tags} from "src/util/nostr"
import {canDisplayUrl} from "src/util/notes" import {urlIsMedia} from "src/util/notes"
import NoteContentKind1 from "src/app/shared/NoteContentKind1.svelte" import NoteContentKind1 from "src/app/shared/NoteContentKind1.svelte"
import NoteContentLink from "src/app/shared/NoteContentLink.svelte" import NoteContentLink from "src/app/shared/NoteContentLink.svelte"
@ -16,5 +16,5 @@
</div> </div>
{#if ref} {#if ref}
<NoteContentLink {showMedia} value={{url: ref, canDisplay: canDisplayUrl(ref)}} /> <NoteContentLink {showMedia} value={{url: ref, isMedia: urlIsMedia(ref)}} />
{/if} {/if}

View File

@ -13,7 +13,7 @@
let hidden = false let hidden = false
</script> </script>
{#if showMedia && value.canDisplay} {#if showMedia && value.isMedia}
<div class="py-2"> <div class="py-2">
<Media link={annotateMedia(value.url)} onClose={close} /> <Media link={annotateMedia(value.url)} onClose={close} />
</div> </div>

View File

@ -14,7 +14,8 @@ export const NOSTR_NPUB = "nostr:npub"
export const NOSTR_NPROFILE = "nostr:nprofile" export const NOSTR_NPROFILE = "nostr:nprofile"
export const NOSTR_NADDR = "nostr:naddr" export const NOSTR_NADDR = "nostr:naddr"
export const canDisplayUrl = url => !url.match(/\.(apk|docx|xlsx|csv|dmg)/) export const urlIsMedia = url =>
!url.match(/\.(apk|docx|xlsx|csv|dmg)/) && last(url.split("://")).includes("/")
export const parseContent = ({content, tags = []}) => { export const parseContent = ({content, tags = []}) => {
const result = [] const result = []
@ -120,7 +121,7 @@ export const parseContent = ({content, tags = []}) => {
url = "https://" + url url = "https://" + url
} }
return [LINK, raw, {url, canDisplay: canDisplayUrl(url)}] return [LINK, raw, {url, isMedia: urlIsMedia(url)}]
} }
} }
@ -170,8 +171,8 @@ export const truncateContent = (content, {showEntire, maxLength, showMedia = fal
const truncateAt = maxLength * 0.6 const truncateAt = maxLength * 0.6
content.every((part, i) => { content.every((part, i) => {
const isText = [TOPIC, TEXT].includes(part.type) const isText = [TOPIC, TEXT].includes(part.type) || (part.type === LINK && !part.value.isMedia)
const isMedia = [LINK, INVOICE].includes(part.type) || part.type.startsWith("nostr:") const isMedia = part.type === INVOICE || part.type.startsWith("nostr:") || part.value.isMedia
if (isText) { if (isText) {
length += part.value.length length += part.value.length
@ -200,5 +201,5 @@ export const truncateContent = (content, {showEntire, maxLength, showMedia = fal
export const getLinks = parts => export const getLinks = parts =>
pluck( pluck(
"value", "value",
parts.filter(x => x.type === LINK && x.canDisplay) parts.filter(x => x.type === LINK && x.isMedia)
) )