Skip to content

Commit c1665e5

Browse files
committed
Break up github.ts into a more organized folder/file structure.
1 parent 9306a5b commit c1665e5

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

browser-extension/src/lib/enhancers/github.ts renamed to browser-extension/src/lib/enhancers/github/githubAddComment.ts

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
import hljs from 'highlight.js'
2-
import { logger } from '../../lib/logger'
3-
import OverType, { type OverTypeInstance } from '../../overtype/overtype'
4-
import type { CommentEnhancer, CommentSpot } from '../enhancer'
1+
import OverType, { type OverTypeInstance } from '../../../overtype/overtype'
2+
import type { CommentEnhancer, CommentSpot } from '../../enhancer'
3+
import { logger } from '../../logger'
4+
import { githubHighlighter } from './githubHighlighter'
5+
import { GITHUB_SPOT_TYPES } from './githubSpotTypes'
56

6-
const GITHUB_SPOT_TYPES = [
7-
'GH_PR_ADD_COMMENT',
8-
/* TODO
9-
'GH_ISSUE_NEW',
10-
'GH_PR_NEW',
11-
'GH_ISSUE_ADD_COMMENT',
12-
'GH_ISSUE_EDIT_COMMENT',
13-
'GH_PR_EDIT_COMMENT',
14-
'GH_PR_CODE_COMMENT',
15-
*/
16-
] as const
7+
type GitHubSpotType = (typeof GITHUB_SPOT_TYPES)[number]
178

18-
export type GitHubSpotType = (typeof GITHUB_SPOT_TYPES)[number]
19-
20-
export interface GitHubAddCommentSpot extends CommentSpot {
9+
interface GitHubAddCommentSpot extends CommentSpot {
2110
type: GitHubSpotType // Override to narrow from string to specific union
2211
domain: string
2312
slug: string // owner/repo
@@ -55,7 +44,7 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
5544
}
5645

5746
prepareForFirstEnhancement(): void {
58-
OverType.setCodeHighlighter(hljsHighlighter)
47+
OverType.setCodeHighlighter(githubHighlighter)
5948
}
6049

6150
enhance(textArea: HTMLTextAreaElement, _spot: GitHubAddCommentSpot): OverTypeInstance {
@@ -94,18 +83,3 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
9483
return `https://${spot.domain}/${spot.slug}/pull/${spot.number}`
9584
}
9685
}
97-
98-
function hljsHighlighter(code: string, language: string) {
99-
try {
100-
if (language && hljs.getLanguage(language)) {
101-
const result = hljs.highlight(code, { language })
102-
return result.value
103-
} else {
104-
const result = hljs.highlightAuto(code)
105-
return result.value
106-
}
107-
} catch (error) {
108-
console.warn('highlight.js highlighting failed:', error)
109-
return code
110-
}
111-
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import hljs from 'highlight.js'
2+
3+
export function githubHighlighter(code: string, language: string) {
4+
try {
5+
if (language && hljs.getLanguage(language)) {
6+
const result = hljs.highlight(code, { language })
7+
return result.value
8+
} else {
9+
const result = hljs.highlightAuto(code)
10+
return result.value
11+
}
12+
} catch (error) {
13+
console.warn('highlight.js highlighting failed:', error)
14+
return code
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const GITHUB_SPOT_TYPES = [
2+
'GH_PR_ADD_COMMENT',
3+
/* TODO
4+
'GH_ISSUE_NEW',
5+
'GH_PR_NEW',
6+
'GH_ISSUE_ADD_COMMENT',
7+
'GH_ISSUE_EDIT_COMMENT',
8+
'GH_PR_EDIT_COMMENT',
9+
'GH_PR_CODE_COMMENT',
10+
*/
11+
] as const

browser-extension/src/lib/registries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OverTypeInstance } from '../overtype/overtype'
22
import type { CommentEnhancer, CommentSpot } from './enhancer'
3-
import { GitHubAddCommentEnhancer } from './enhancers/github'
3+
import { GitHubAddCommentEnhancer } from './enhancers/github/githubAddComment'
44

55
export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
66
textarea: HTMLTextAreaElement

0 commit comments

Comments
 (0)