Skip to content

Commit

Permalink
Linkify notes and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Staab committed Nov 27, 2022
1 parent 4f313a3 commit 7fbbfb1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Bugs

- [ ] Fix scrolling on note detail
- [ ] Ellipsize and hyperlink links
- [ ] Be sure to deduplicate all events if needed
- [ ] Format text with line breaks - pre, or split/br
- [ ] Remove dexie, or use it instead of localstorage for cached data
Expand Down
11 changes: 9 additions & 2 deletions src/partials/Note.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {fly} from 'svelte/transition'
import {navigate} from 'svelte-routing'
import {ellipsize} from 'hurdak/src/core'
import {hasParent} from 'src/util/html'
import {hasParent, toHtml} from 'src/util/html'
import Anchor from 'src/partials/Anchor.svelte'
import {dispatch} from "src/state/dispatch"
import {accounts, modal} from "src/state/app"
Expand All @@ -14,6 +14,7 @@
export let note
export let isReply = false
export let showEntire = false
export let interactive = false
export let invertColors = false
Expand Down Expand Up @@ -105,7 +106,13 @@
Reply to <Anchor on:click={showParent}>{parentId.slice(0, 8)}</Anchor>
</small>
{/if}
<p>{ellipsize(note.content, 240)}</p>
<p>
{#if note.content.length > 240 && !showEntire}
{ellipsize(note.content, 240)}
{:else}
{@html toHtml(note.content)}
{/if}
</p>
<div class="flex gap-6 text-light">
<div>
<i
Expand Down
2 changes: 1 addition & 1 deletion src/partials/NoteDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</script>

{#if note.pubkey}
<Note note={note} />
<Note showEntire note={note} />
{#each note.replies as r (r.id)}
<div class="ml-4 border-l border-solid border-medium">
<Note interactive invertColors isReply note={r} />
Expand Down
3 changes: 2 additions & 1 deletion src/routes/ChatRoom.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import {prop, last} from 'ramda'
import {switcherFn} from 'hurdak/src/core'
import {formatTimestamp} from 'src/util/misc'
import {toHtml} from 'src/util/html'
import UserBadge from 'src/partials/UserBadge.svelte'
import {channels} from 'src/state/nostr'
import {rooms, accounts, ensureAccounts} from 'src/state/app'
Expand Down Expand Up @@ -117,7 +118,7 @@
<p class="text-sm text-light">{formatTimestamp(m.created_at)}</p>
</div>
{/if}
<div class="ml-6">{m.content}</div>
<div class="ml-6">{@html toHtml(m.content)}</div>
</li>
{/each}
</ul>
Expand Down
16 changes: 16 additions & 0 deletions src/util/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,19 @@ export const hasParent = (tagOrClass, $el) => {

return false
}

export const escapeHtml = html => {
const div = document.createElement("div")

div.innerText = html

return div.innerHTML
}

export const toHtml = content => {
return escapeHtml(content)
.replace(/\n/g, '<br />')
.replace(/https?:\/\/([\w\.-]+)[^ ]*/g, (url, domain) => {
return `<a href="${url}" target="_blank noopener" class="underline">${domain}</a>`
})
}

0 comments on commit 7fbbfb1

Please sign in to comment.