import { searchPostsForUrl, getCurrentNetwork, type SearchPost } from '../lib/api' const DISCUSSION_POST_LIMIT = 10 function escapeHtml(str: string): string { return str .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') } function formatDate(dateStr: string): string { const date = new Date(dateStr) return date.toLocaleDateString('ja-JP', { year: 'numeric', month: '2-digit', day: '2-digit', }) } function getPostUrl(uri: string, appUrl: string): string { // at://did:plc:xxx/app.bsky.feed.post/rkey -> {appUrl}/profile/did:plc:xxx/post/rkey const parts = uri.replace('at://', '').split('/') if (parts.length >= 3) { return `${appUrl}/profile/${parts[0]}/post/${parts[2]}` } return '#' } export function renderDiscussion(postUrl: string, appUrl: string = 'https://bsky.app'): string { // Build search URL with host/@username only let searchQuery = postUrl try { const urlObj = new URL(postUrl) const pathParts = urlObj.pathname.split('/').filter(Boolean) // pathParts[0] = @username.domain (e.g., @syui.syui.ai) // Extract just @username if (pathParts[0]?.startsWith('@')) { const handlePart = pathParts[0].slice(1) // remove @ const username = handlePart.split('.')[0] // get first part before . searchQuery = `${urlObj.host}/@${username}` } else { searchQuery = urlObj.host } } catch { // Keep original } const searchUrl = `${appUrl}/search?q=${encodeURIComponent(searchQuery)}` return `
` } export async function loadDiscussionPosts(container: HTMLElement, postUrl: string, appUrl: string = 'https://bsky.app'): Promise