Files
mkstack/.agents/skills/note-content/files/lib/customEmoji.ts
T
Alex Gleason 0b220f250a Upgrade note-content skill with advancements from Ditto
Replaces the minimal NoteContent component with the full-featured
version from ~/Projects/ditto, and bundles the helper modules and
minimal companion stubs needed to make it work standalone.

New capabilities:
- BOLT11 lightning-invoice detection (renders LightningInvoiceCard)
- Inline image / video / audio / webxdc embeds with MIME detection
- Consecutive images grouped into an ImageGallery with shared Lightbox
- NIP-94 imeta-declared media appended when absent from content
- Embedded nevent / naddr cards, with relay and author hints pulled
  from q tags; supports naddr1 fragments embedded in external URLs
- Bare and @-prefixed NIP-19 identifiers (no nostr: prefix required)
- Line-end-aware link rendering: URLs at the end of a line become
  preview cards, mid-sentence URLs stay inline
- Trailing-punctuation stripping on URLs so 'see https://x.com.'
  links correctly
- wss:// relay URLs routed to /r/<encoded>
- Unicode-aware hashtag regex (matches non-ASCII letters)
- NIP-30 custom emoji via :shortcode: with per-event and viewer-wide
  fallback collections (useCustomEmojis)
- Flag emoji linkified to /i/iso3166:<CC>
- 'Emoji-only' posts auto-rendered at large size
- dir="auto" for RTL content
- Whitespace normalization around block embeds
- disableEmbeds / disableMediaEmbeds / disableNoteEmbeds /
  hideEmbedImages props for recursive embed cards and preview
  contexts

New bundled files:
- lib/: mediaUrls.ts, sanitizeUrl.ts, imeta.ts, customEmoji.ts,
  getDisplayName.ts, avatarShape.ts, countries.ts
- hooks/: useCustomEmojis.ts (stub), useBlossomFallback.ts (stub),
  useProfileUrl.ts (stub)
- components/: LinkEmbed, EmbeddedNote, EmbeddedNaddr,
  LightningInvoiceCard, VideoPlayer, AudioVisualizer, WebxdcEmbed,
  ImageGallery + Lightbox, ProfileHoverCard, CustomEmoji \u2014 all
  minimal but functional stubs; each file documents how to upgrade
  to a richer implementation

SKILL.md rewritten with full file list, upgrade paths for each
stub, routing expectations, and a patterns-to-render table.

Validated: tsc --noEmit clean, vitest run passes all 5 NoteContent
tests, vite build succeeds \u2014 when all skill files are staged
into src/ alongside the component.
2026-04-26 19:38:31 -05:00

78 lines
2.5 KiB
TypeScript

import type { NostrEvent } from '@nostrify/nostrify';
/**
* Checks if a string is a NIP-30 custom emoji shortcode (`:shortcode:` format).
*/
export function isCustomEmoji(content: string): boolean {
return /^:[a-zA-Z0-9_-]+:$/.test(content);
}
/**
* Extracts the custom emoji URL from a NostrEvent's tags for a given shortcode.
* The shortcode should include the colons (e.g., `:soapbox:`).
*/
export function getCustomEmojiUrl(shortcode: string, tags: string[][]): string | undefined {
const name = shortcode.slice(1, -1); // Remove surrounding colons
const emojiTag = tags.find(([tagName, tagShortcode]) => tagName === 'emoji' && tagShortcode === name);
return emojiTag?.[2];
}
/**
* Builds a map of shortcode -> URL from an event's emoji tags.
*/
export function buildEmojiMap(tags: string[][]): Map<string, string> {
const map = new Map<string, string>();
for (const tag of tags) {
if (tag[0] === 'emoji' && tag[1] && tag[2]) {
map.set(tag[1], tag[2]);
}
}
return map;
}
/**
* Represents a resolved reaction emoji that can be rendered.
* For custom emojis, includes the URL; for unicode, just the content string.
*/
export interface ResolvedEmoji {
/** The display content — unicode emoji string or `:shortcode:` */
content: string;
/** For custom emojis, the image URL. Undefined for unicode emojis. */
url?: string;
/** For custom emojis, the shortcode name (without colons). */
name?: string;
}
/**
* Checks whether a kind 7 reaction event is valid.
* Custom emoji reactions (`:shortcode:` content) are invalid without a matching `emoji` tag.
*/
export function isValidReaction(event: NostrEvent): boolean {
const content = event.content.trim();
const emoji = (content === '+' || content === '') ? '+' : content;
if (isCustomEmoji(emoji)) {
return getCustomEmojiUrl(emoji, event.tags) !== undefined;
}
return true;
}
/**
* Resolves a reaction emoji from a kind 7 event into a ResolvedEmoji.
* Returns `undefined` for malformed custom emoji reactions (missing emoji tag).
*/
export function resolveReactionEmoji(event: NostrEvent): ResolvedEmoji | undefined {
const content = event.content.trim();
const emoji = (content === '+' || content === '') ? '👍' : content === '-' ? '👎' : content;
if (isCustomEmoji(emoji)) {
const url = getCustomEmojiUrl(emoji, event.tags);
if (url) {
return { content: emoji, url, name: emoji.slice(1, -1) };
}
// Malformed: custom emoji shortcode without a matching emoji tag
return undefined;
}
return { content: emoji };
}