|
| 1 | +const MEMBER_PATTERNS = [ |
| 2 | + /([\d.,]+(?:\s?[KMB千萬万亿])?)\s*members?/i, |
| 3 | + /([\d.,]+(?:\s?[KMB千萬万亿])?)\s*位成员/, |
| 4 | +]; |
| 5 | +const FOLLOWER_PATTERNS = [ |
| 6 | + /([\d.,]+(?:\s?[KMB千萬万亿])?)\s*followers?/i, |
| 7 | + /([\d.,]+(?:\s?[KMB千萬万亿])?)\s*位关注者/, |
| 8 | +]; |
| 9 | +const PRIVATE_PATTERNS = [/\bprivate\b/i, /锁定列表/]; |
| 10 | +const EMPTY_STATE_PATTERNS = [ |
| 11 | + /hasn't created any lists/i, |
| 12 | + /has not created any lists/i, |
| 13 | + /no lists yet/i, |
| 14 | + /没有创建任何列表/, |
| 15 | + /还没有创建任何列表/, |
| 16 | +]; |
| 17 | +function normalizeText(text) { |
| 18 | + return String(text || '').replace(/\s+/g, ' ').trim(); |
| 19 | +} |
| 20 | +function matchMetric(text, patterns) { |
| 21 | + for (const pattern of patterns) { |
| 22 | + const match = text.match(pattern); |
| 23 | + if (match) |
| 24 | + return normalizeText(match[1]); |
| 25 | + } |
| 26 | + return '0'; |
| 27 | +} |
| 28 | +function looksLikeMetadata(line) { |
| 29 | + const text = normalizeText(line); |
| 30 | + if (!text) |
| 31 | + return true; |
| 32 | + if (text.startsWith('@')) |
| 33 | + return true; |
| 34 | + if (MEMBER_PATTERNS.some((pattern) => pattern.test(text))) |
| 35 | + return true; |
| 36 | + if (FOLLOWER_PATTERNS.some((pattern) => pattern.test(text))) |
| 37 | + return true; |
| 38 | + if (PRIVATE_PATTERNS.some((pattern) => pattern.test(text))) |
| 39 | + return true; |
| 40 | + if (/^(public|pinned)$/i.test(text)) |
| 41 | + return true; |
| 42 | + if (/^(lists?|你的列表)$/i.test(text)) |
| 43 | + return true; |
| 44 | + return false; |
| 45 | +} |
| 46 | +export function parseListCards(cards) { |
| 47 | + const seen = new Set(); |
| 48 | + const results = []; |
| 49 | + for (const card of cards || []) { |
| 50 | + const href = normalizeText(card?.href); |
| 51 | + const rawText = String(card?.text || ''); |
| 52 | + if (!href || seen.has(href)) |
| 53 | + continue; |
| 54 | + seen.add(href); |
| 55 | + const text = normalizeText(rawText); |
| 56 | + if (!text) |
| 57 | + continue; |
| 58 | + const lines = rawText |
| 59 | + .split('\n') |
| 60 | + .map((line) => normalizeText(line)) |
| 61 | + .filter(Boolean); |
| 62 | + const name = lines.find((line) => !looksLikeMetadata(line)); |
| 63 | + if (!name) |
| 64 | + continue; |
| 65 | + results.push({ |
| 66 | + name, |
| 67 | + members: matchMetric(text, MEMBER_PATTERNS), |
| 68 | + followers: matchMetric(text, FOLLOWER_PATTERNS), |
| 69 | + mode: PRIVATE_PATTERNS.some((pattern) => pattern.test(text)) ? 'private' : 'public', |
| 70 | + }); |
| 71 | + } |
| 72 | + return results; |
| 73 | +} |
| 74 | +export function isEmptyListsState(text) { |
| 75 | + const normalized = normalizeText(text); |
| 76 | + return EMPTY_STATE_PATTERNS.some((pattern) => pattern.test(normalized)); |
| 77 | +} |
0 commit comments