import type { EntityType, PageType } from "@src/shared"; import type { ClassifierHints } from "@src/observers"; import type { SocialPlatformAdapter, UrlEntityRule } from "./adapter.ts"; /** Reddit adapter — Phase 1 platform (new web UI, `shreddit-*` custom elements, GraphQL + `.json` payloads). */ const classifierHints: ClassifierHints = { platform: "reddit", idKeys: { postId: "post", commentId: "comment", subredditId: "community", authorId: "profile" }, urlForId: (type: EntityType, id: string) => { if (type === "post" && /^t3_/.test(id)) return `https://www.reddit.com/comments/${id.slice(3)}`; if (type === "post" && /^[a-z0-9]{5,8}$/.test(id)) return `https://www.reddit.com/comments/${id}`; return undefined; }, }; const urlEntityRules: UrlEntityRule[] = [ { pattern: /^\/r\/([\w]+)\/comments\/([a-z0-9]+)\/[^/]*\/([a-z0-9]+)/, type: "comment", id: (m) => `t1_${m[3]}`, confidence: 0.9 }, { pattern: /^\/r\/([\w]+)\/comments\/([a-z0-9]+)/, type: "post", id: (m) => `t3_${m[2]}`, confidence: 0.95 }, { pattern: /^\/comments\/([a-z0-9]+)/, type: "post", id: (m) => `t3_${m[1]}`, confidence: 0.9 }, { pattern: /^\/(?:user|u)\/([\w-]+)\/?(?:\?|$)/, type: "profile", id: (m) => m[1]?.toLowerCase(), confidence: 0.92 }, { pattern: /^\/r\/([\w]+)\/?(?:\?|$)/, type: "community", id: (m) => m[1]?.toLowerCase(), confidence: 0.92 }, ]; export const redditAdapter: SocialPlatformAdapter = { platform: "reddit", homeUrl: "https://www.reddit.com/", hosts: /(^|\.)reddit\.com$|^redd\.it$/, pageTypeHints: [ { pattern: /^\/search\/?\?/, page_type: "SEARCH_RESULTS", confidence: 0.95 }, { pattern: /\/comments\//, page_type: "POST_DETAIL", confidence: 0.95 }, { pattern: /^\/(user|u)\//, page_type: "PROFILE", confidence: 0.92 }, { pattern: /^\/r\/[\w]+\/?(\?|$|\/(hot|new|top|rising))/, page_type: "GROUP", confidence: 0.9 }, { pattern: /^\/?(\?|$)|^\/(popular|all|best)/, page_type: "HOME_FEED", confidence: 0.9 }, ], urlEntityRules, classifierHints, searchUrl: (q) => `https://www.reddit.com/search/?q=${encodeURIComponent(q)}`, isNoiseLink: (href, text) => /^\/(settings|premium|coins|policies|help|topics|best\/communities|answers)/.test(href.pathname) || /^(Home|Popular|All|Accueil|Populaire)$/i.test(text), entityLabel: (t) => ({ post: "post", comment: "comment", profile: "user", community: "subreddit" } as Partial>)[t] ?? t, expectedEntities: (t: PageType) => ({ HOME_FEED: 6, SEARCH_RESULTS: 5, POST_DETAIL: 3, GROUP: 6, PROFILE: 3 } as Partial>)[t] ?? 0, };