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'
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
17-
18- export type GitHubSpotType = ( typeof GITHUB_SPOT_TYPES ) [ number ]
19-
20- export interface GitHubAddCommentSpot extends CommentSpot {
7+ interface GitHubPRAddCommentSpot extends CommentSpot {
218 type : GitHubSpotType // Override to narrow from string to specific union
229 domain : string
2310 slug : string // owner/repo
2411 number : number // issue/PR number, undefined for new issues and PRs
2512}
2613
27- export class GitHubAddCommentEnhancer implements CommentEnhancer < GitHubAddCommentSpot > {
14+ export class GitHubPRAddCommentEnhancer implements CommentEnhancer < GitHubPRAddCommentSpot > {
2815 forSpotTypes ( ) : string [ ] {
2916 return [ ...GITHUB_SPOT_TYPES ]
3017 }
3118
32- tryToEnhance ( _textarea : HTMLTextAreaElement ) : GitHubAddCommentSpot | null {
19+ tryToEnhance ( _textarea : HTMLTextAreaElement ) : GitHubPRAddCommentSpot | null {
3320 // Only handle github.com domains TODO: identify GitHub Enterprise somehow
3421 if ( window . location . hostname !== 'github.com' ) {
3522 return null
@@ -55,10 +42,10 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
5542 }
5643
5744 prepareForFirstEnhancement ( ) : void {
58- OverType . setCodeHighlighter ( hljsHighlighter )
45+ OverType . setCodeHighlighter ( githubHighlighter )
5946 }
6047
61- enhance ( textArea : HTMLTextAreaElement , _spot : GitHubAddCommentSpot ) : OverTypeInstance {
48+ enhance ( textArea : HTMLTextAreaElement , _spot : GitHubPRAddCommentSpot ) : OverTypeInstance {
6249 const overtypeContainer = this . modifyDOM ( textArea )
6350 return new OverType ( overtypeContainer , {
6451 autoResize : true ,
@@ -81,31 +68,16 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
8168 return overtypeContainer . parentElement ! . closest ( 'div' ) !
8269 }
8370
84- tableTitle ( spot : GitHubAddCommentSpot ) : string {
71+ tableTitle ( spot : GitHubPRAddCommentSpot ) : string {
8572 const { slug, number } = spot
8673 return `${ slug } PR #${ number } `
8774 }
8875
89- tableIcon ( _ : GitHubAddCommentSpot ) : string {
76+ tableIcon ( _ : GitHubPRAddCommentSpot ) : string {
9077 return '🔄' // PR icon TODO: icon urls in /public
9178 }
9279
93- buildUrl ( spot : GitHubAddCommentSpot ) : string {
80+ buildUrl ( spot : GitHubPRAddCommentSpot ) : string {
9481 return `https://${ spot . domain } /${ spot . slug } /pull/${ spot . number } `
9582 }
9683}
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