1+ import { parseGitHubRepository } from '@/lib/oauth/github-repository'
12import { atlassianSiteId } from '@/lib/sim-search/live/atlassian'
23import { linearIssueReference } from '@/lib/sim-search/live/linear'
34import { notionResource } from '@/lib/sim-search/live/notion-mcp'
@@ -46,7 +47,6 @@ const GOOGLE_EDITOR_KINDS: Record<string, string> = {
4647}
4748const JIRA_KEY = / ^ [ A - Z ] [ A - Z \d _ ] * - \d + $ /
4849const NUMERIC_ID = / ^ \d + $ /
49- const GITHUB_NAME = / ^ [ \w . - ] + $ /
5050/** First path segments on github.com that are site pages, not repository owners. */
5151const GITHUB_RESERVED_OWNERS = new Set ( [
5252 'apps' ,
@@ -128,17 +128,33 @@ function slackLink(url: URL): DocumentLink | undefined {
128128 } ,
129129 }
130130 }
131- const file = parts [ 0 ] === 'files' ? parts [ 2 ] : parts [ 0 ] === 'docs' ? parts [ 2 ] : undefined
131+ const file = parts [ 0 ] === 'files' || parts [ 0 ] === 'docs' ? parts [ 2 ] : undefined
132132 if ( ! file || ! / ^ F [ A - Z \d ] + $ / . test ( file ) ) return undefined
133133 return { provider : 'slack' , reference : { id : file , kind : 'file' } }
134134}
135135
136+ /** A repository file at a revision: the first segment after `blob` names it, the rest the path. */
137+ function blobReference ( rest : string [ ] ) : LinkedReference | undefined {
138+ const [ revision , ...path ] = rest
139+ if ( ! revision || ! path . length || path . some ( ( part ) => part === '.' || part === '..' ) )
140+ return undefined
141+ return { id : path . join ( '/' ) , kind : 'code' , revision }
142+ }
143+
144+ function githubRepository ( owner : string , name : string ) : string | undefined {
145+ try {
146+ const { owner : parsedOwner , repo } = parseGitHubRepository ( `${ owner } /${ name } ` )
147+ return `${ parsedOwner } /${ repo } `
148+ } catch {
149+ return undefined
150+ }
151+ }
152+
136153function githubLink ( url : URL ) : DocumentLink | undefined {
137154 const [ owner , name , section , ...rest ] = pathSegments ( url )
138155 if ( ! owner || ! name || GITHUB_RESERVED_OWNERS . has ( owner . toLowerCase ( ) ) ) return undefined
139- const repository = `${ owner } /${ name . replace ( / \. g i t $ / , '' ) } `
140- if ( ! GITHUB_NAME . test ( owner ) || ! GITHUB_NAME . test ( repository . split ( '/' ) [ 1 ] ?? '' ) )
141- return undefined
156+ const repository = githubRepository ( owner , name )
157+ if ( ! repository ) return undefined
142158 const link = ( reference : LinkedReference ) : DocumentLink => ( {
143159 provider : 'github' ,
144160 reference : { container : repository , ...reference } ,
@@ -149,10 +165,9 @@ function githubLink(url: URL): DocumentLink | undefined {
149165 return commit ? githubCommit ( commit , link ) : link ( { id : rest [ 0 ] ?? '' , kind : 'issues' } )
150166 }
151167 if ( section === 'commit' ) return githubCommit ( rest [ 0 ] , link )
152- if ( section === 'blob' && rest . length >= 2 ) {
153- const [ revision , ...path ] = rest
154- if ( ! revision || path . some ( ( part ) => part === '.' || part === '..' ) ) return undefined
155- return link ( { id : path . join ( '/' ) , kind : 'code' , revision } )
168+ if ( section === 'blob' ) {
169+ const blob = blobReference ( rest )
170+ return blob && link ( blob )
156171 }
157172 return undefined
158173}
@@ -176,16 +191,14 @@ function gitlabLink(url: URL): DocumentLink | undefined {
176191 host : url . host ,
177192 reference : { container : project , ...reference } ,
178193 } )
179- if ( section === 'issues' || section === 'work_items' )
180- return NUMERIC_ID . test ( rest [ 0 ] ?? '' ) ? link ( { id : rest [ 0 ] ?? '' , kind : 'issues' } ) : undefined
181- if ( section === 'merge_requests' )
182- return NUMERIC_ID . test ( rest [ 0 ] ?? '' )
183- ? link ( { id : rest [ 0 ] ?? '' , kind : 'merge_requests' } )
194+ const iid = rest [ 0 ]
195+ if ( section === 'issues' || section === 'work_items' || section === 'merge_requests' )
196+ return iid && NUMERIC_ID . test ( iid )
197+ ? link ( { id : iid , kind : section === 'merge_requests' ? 'merge_requests' : 'issues' } )
184198 : undefined
185- if ( section === 'blob' && rest . length >= 2 ) {
186- const [ revision , ...path ] = rest
187- if ( ! revision || path . some ( ( part ) => part === '.' || part === '..' ) ) return undefined
188- return link ( { id : path . join ( '/' ) , kind : 'code' , revision } )
199+ if ( section === 'blob' ) {
200+ const blob = blobReference ( rest )
201+ return blob && link ( blob )
189202 }
190203 if ( section === 'wikis' && rest . length ) return link ( { id : rest . join ( '/' ) , kind : 'wiki' } )
191204 return undefined
@@ -250,6 +263,19 @@ function notionLink(value: string): DocumentLink | undefined {
250263 return resource && { provider : 'notion' , reference : { id : resource . id , kind : 'mcp' } }
251264}
252265
266+ /** Parsers for hosts that serve one provider's documents. */
267+ const HOST_PARSERS = new Map < string , ( url : URL ) => DocumentLink | undefined > ( [
268+ [ 'docs.google.com' , googleDriveLink ] ,
269+ [ 'drive.google.com' , googleDriveLink ] ,
270+ [ 'mail.google.com' , gmailLink ] ,
271+ [ 'calendar.google.com' , calendarLink ] ,
272+ [ 'www.google.com' , calendarLink ] ,
273+ [ 'github.com' , githubLink ] ,
274+ [ 'www.github.com' , githubLink ] ,
275+ [ 'linear.app' , linearLink ] ,
276+ [ 'app.fireflies.ai' , firefliesLink ] ,
277+ ] )
278+
253279/**
254280 * The provider documents a pasted link may name, or undefined when `value` is not a web link.
255281 * An empty list means the link names nothing Sim can read directly, such as a folder, channel or
@@ -266,17 +292,9 @@ export function parseDocumentLinks(value: string): DocumentLink[] | undefined {
266292 return [ ]
267293 }
268294 if ( url . protocol !== 'https:' || url . username || url . password ) return [ ]
269- const host = url . hostname . toLowerCase ( )
270- if ( host === 'docs.google.com' || host === 'drive.google.com' )
271- return [ googleDriveLink ( url ) ] . filter ( ( link ) => link !== undefined )
272- if ( host === 'mail.google.com' ) return [ gmailLink ( url ) ] . filter ( ( link ) => link !== undefined )
273- if ( host === 'calendar.google.com' || host === 'www.google.com' )
274- return [ calendarLink ( url ) ] . filter ( ( link ) => link !== undefined )
275- if ( host . endsWith ( '.slack.com' ) ) return [ slackLink ( url ) ] . filter ( ( link ) => link !== undefined )
276- if ( host === 'github.com' || host === 'www.github.com' )
277- return [ githubLink ( url ) ] . filter ( ( link ) => link !== undefined )
278- if ( host === 'linear.app' ) return [ linearLink ( url ) ] . filter ( ( link ) => link !== undefined )
279- if ( host === 'app.fireflies.ai' ) return [ firefliesLink ( url ) ] . filter ( ( link ) => link !== undefined )
295+ const parse =
296+ HOST_PARSERS . get ( url . hostname ) ?? ( url . hostname . endsWith ( '.slack.com' ) ? slackLink : undefined )
297+ if ( parse ) return [ parse ( url ) ] . filter ( ( link ) => link !== undefined )
280298 const notion = notionLink ( value )
281299 if ( notion ) return [ notion ]
282300 return [ jiraLink ( url ) , confluenceLink ( url ) , gitlabLink ( url ) ] . filter ( ( link ) => link !== undefined )
0 commit comments