2026-09-09 22:31:04 +00:00
|
|
|
import {ParsedType, parse, renderAsText, summarize} from "@welshman/content"
|
|
|
|
|
import type {Parsed, SummaryName} from "@welshman/content"
|
2026-09-09 16:59:54 +00:00
|
|
|
import type {TrustedEvent} from "@welshman/util"
|
|
|
|
|
import {profiles} from "@app/core"
|
|
|
|
|
|
2026-09-09 22:31:04 +00:00
|
|
|
const summaryName: SummaryName = parsed =>
|
|
|
|
|
parsed.type === ParsedType.Profile ? profiles.get().display(parsed.value.pubkey).get() : undefined
|
2026-09-09 16:59:54 +00:00
|
|
|
|
2026-09-09 22:31:04 +00:00
|
|
|
const renderSummary = (parsed: Parsed[]) =>
|
|
|
|
|
renderAsText(summarize(parsed, summaryName)).toString().trim()
|
2026-09-09 16:59:54 +00:00
|
|
|
|
2026-09-09 22:31:04 +00:00
|
|
|
export const renderEventAsText = (event: TrustedEvent) => renderSummary(parse(event))
|
2026-09-09 16:59:54 +00:00
|
|
|
|
|
|
|
|
const splitLines = (parsed: Parsed[]) => {
|
|
|
|
|
const lines: Parsed[][] = [[]]
|
|
|
|
|
|
|
|
|
|
for (const node of parsed) {
|
|
|
|
|
if (node.type === ParsedType.Newline) {
|
|
|
|
|
lines.push([])
|
|
|
|
|
} else {
|
|
|
|
|
lines[lines.length - 1].push(node)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lines.filter(line => line.length > 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hasProse = (line: Parsed[]) =>
|
|
|
|
|
line.some(node => node.type === ParsedType.Text && node.value.trim().length > 0)
|
|
|
|
|
|
|
|
|
|
// A reply prepends the message it answers, so the first line of one describes the quote and says
|
|
|
|
|
// nothing about the reply. The first line carrying words of its own is what the message is about.
|
|
|
|
|
export const renderEventAsSummary = (event: TrustedEvent) => {
|
|
|
|
|
const lines = splitLines(parse(event))
|
|
|
|
|
|
2026-09-09 22:31:04 +00:00
|
|
|
return renderSummary(lines.find(hasProse) || lines[0] || [])
|
2026-09-09 16:59:54 +00:00
|
|
|
}
|