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 , type GitHubSpotType } from './githubSpotTypes'
5
6
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
17
-
18
- export type GitHubSpotType = ( typeof GITHUB_SPOT_TYPES ) [ number ]
19
-
20
- export interface GitHubAddCommentSpot extends CommentSpot {
7
+ interface GitHubPRAddCommentSpot extends CommentSpot {
21
8
type : GitHubSpotType // Override to narrow from string to specific union
22
9
domain : string
23
10
slug : string // owner/repo
24
11
number : number // issue/PR number, undefined for new issues and PRs
25
12
}
26
13
27
- export class GitHubAddCommentEnhancer implements CommentEnhancer < GitHubAddCommentSpot > {
14
+ export class GitHubPRAddCommentEnhancer implements CommentEnhancer < GitHubPRAddCommentSpot > {
28
15
forSpotTypes ( ) : string [ ] {
29
16
return [ ...GITHUB_SPOT_TYPES ]
30
17
}
31
18
32
- tryToEnhance ( _textarea : HTMLTextAreaElement ) : GitHubAddCommentSpot | null {
19
+ tryToEnhance ( _textarea : HTMLTextAreaElement ) : GitHubPRAddCommentSpot | null {
33
20
// Only handle github.com domains TODO: identify GitHub Enterprise somehow
34
21
if ( window . location . hostname !== 'github.com' ) {
35
22
return null
@@ -55,10 +42,10 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
55
42
}
56
43
57
44
prepareForFirstEnhancement ( ) : void {
58
- OverType . setCodeHighlighter ( hljsHighlighter )
45
+ OverType . setCodeHighlighter ( githubHighlighter )
59
46
}
60
47
61
- enhance ( textArea : HTMLTextAreaElement , _spot : GitHubAddCommentSpot ) : OverTypeInstance {
48
+ enhance ( textArea : HTMLTextAreaElement , _spot : GitHubPRAddCommentSpot ) : OverTypeInstance {
62
49
const overtypeContainer = this . modifyDOM ( textArea )
63
50
return new OverType ( overtypeContainer , {
64
51
autoResize : true ,
@@ -81,31 +68,16 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
81
68
return overtypeContainer . parentElement ! . closest ( 'div' ) !
82
69
}
83
70
84
- tableTitle ( spot : GitHubAddCommentSpot ) : string {
71
+ tableTitle ( spot : GitHubPRAddCommentSpot ) : string {
85
72
const { slug, number } = spot
86
73
return `${ slug } PR #${ number } `
87
74
}
88
75
89
- tableIcon ( _ : GitHubAddCommentSpot ) : string {
76
+ tableIcon ( _ : GitHubPRAddCommentSpot ) : string {
90
77
return '🔄' // PR icon TODO: icon urls in /public
91
78
}
92
79
93
- buildUrl ( spot : GitHubAddCommentSpot ) : string {
80
+ buildUrl ( spot : GitHubPRAddCommentSpot ) : string {
94
81
return `https://${ spot . domain } /${ spot . slug } /pull/${ spot . number } `
95
82
}
96
83
}
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
- }
0 commit comments