Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/google

Google helpers for Gmail, Calendar, Drive, People, YouTube (including upload), and Analytics.

FollowFork

Files

gmail-url.ts

6 lines · 3.0 KB · TypeScript
/**
 * Gmail web URL parsing. Kept free of runtime imports so it can be unit tested.
 */
export const GMAIL_HEX_ID = /^[a-f0-9]{8,}$/i
export type GmailUrlInfo = { kind: string; id: string | null; threadId: string | null; messageId: string | null; folder: string | null; authuser: string | null; userIndex: number | null; url: string }
export function parseGmailUrl(input: string): GmailUrlInfo { const base: GmailUrlInfo = { kind: 'unknown', id: null, threadId: null, messageId: null, folder: null, authuser: null, userIndex: null, url: input }; let parsed: URL; try { parsed = new URL(input) } catch { return base } if (!(parsed.hostname === 'mail.google.com' || parsed.hostname.endsWith('.mail.google.com'))) return base; const authuser = parsed.searchParams.get('authuser'); const userIndexMatch = parsed.pathname.match(new RegExp('/mail/u/(\d+)')); const userIndex = userIndexMatch ? Number(userIndexMatch[1]) : null; const hash = (parsed.hash || '').replace(/^#/, ''); const segments = hash.split('/').filter(Boolean).map(s => decodeURIComponent(s)); if (segments.length === 0) return { ...base, folder: null, authuser, userIndex }; const folder = segments[0]; const tail = segments.slice(1); const out = { ...base, folder, authuser, userIndex }; if (folder === 'compose') { const draftId = tail[0] && tail[0] !== 'new' ? tail[0] : null; return { ...out, kind: 'compose', id: draftId } } if (folder === 'label') { const label = tail[0] || null; const threadCandidate = tail.find((seg, i) => i > 0 && GMAIL_HEX_ID.test(seg)) || null; const afterThread = threadCandidate ? tail[tail.indexOf(threadCandidate) + 1] : null; const messageCandidate = afterThread && GMAIL_HEX_ID.test(afterThread) ? afterThread : null; if (messageCandidate) return { ...out, kind: 'message', id: messageCandidate, threadId: threadCandidate, messageId: messageCandidate }; if (threadCandidate) return { ...out, kind: 'thread', id: threadCandidate, threadId: threadCandidate }; return { ...out, kind: 'label', id: label } } if (folder === 'search') { const query = tail[0] || null; const threadCandidate = tail.find((seg, i) => i > 0 && GMAIL_HEX_ID.test(seg)) || null; const afterThread = threadCandidate ? tail[tail.indexOf(threadCandidate) + 1] : null; const messageCandidate = afterThread && GMAIL_HEX_ID.test(afterThread) ? afterThread : null; if (messageCandidate) return { ...out, kind: 'message', id: messageCandidate, threadId: threadCandidate, messageId: messageCandidate }; if (threadCandidate) return { ...out, kind: 'thread', id: threadCandidate, threadId: threadCandidate }; return { ...out, kind: 'search', id: query } } if (tail.length === 0) return { ...out, kind: 'folder' }; const threadCandidate = tail[0] && GMAIL_HEX_ID.test(tail[0]) ? tail[0] : null; const messageCandidate = tail[1] && GMAIL_HEX_ID.test(tail[1]) ? tail[1] : null; if (threadCandidate && messageCandidate) return { ...out, kind: 'message', id: messageCandidate, threadId: threadCandidate, messageId: messageCandidate }; if (threadCandidate) return { ...out, kind: 'thread', id: threadCandidate, threadId: threadCandidate }; return out }