-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
111 lines (96 loc) · 3.65 KB
/
github.ts
File metadata and controls
111 lines (96 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import hljs from 'highlight.js'
import { logger } from '../../lib/logger'
import OverType, { type OverTypeInstance } from '../../overtype/overtype'
import type { CommentEnhancer, CommentSpot } from '../enhancer'
const GITHUB_SPOT_TYPES = [
'GH_PR_ADD_COMMENT',
/* TODO
'GH_ISSUE_NEW',
'GH_PR_NEW',
'GH_ISSUE_ADD_COMMENT',
'GH_ISSUE_EDIT_COMMENT',
'GH_PR_EDIT_COMMENT',
'GH_PR_CODE_COMMENT',
*/
] as const
export type GitHubSpotType = (typeof GITHUB_SPOT_TYPES)[number]
export interface GitHubAddCommentSpot extends CommentSpot {
type: GitHubSpotType // Override to narrow from string to specific union
domain: string
slug: string // owner/repo
number: number // issue/PR number, undefined for new issues and PRs
}
export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommentSpot> {
forSpotTypes(): string[] {
return [...GITHUB_SPOT_TYPES]
}
tryToEnhance(textarea: HTMLTextAreaElement): [OverTypeInstance, GitHubAddCommentSpot] | null {
// Only handle github.com domains TODO: identify GitHub Enterprise somehow
if (window.location.hostname !== 'github.com') {
return null
}
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
logger.debug(`${this.constructor.name} examing url`, window.location.pathname)
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/pull\/(\d+))/)
logger.debug(`${this.constructor.name} found match`, window.location.pathname)
if (!match) return null
const [, owner, repo, numberStr] = match
const slug = `${owner}/${repo}`
const number = parseInt(numberStr!, 10)
const unique_key = `github.com:${slug}:${number}`
const spot: GitHubAddCommentSpot = {
domain: 'github.com',
number,
slug,
type: 'GH_PR_ADD_COMMENT',
unique_key,
}
return [this.createOvertypeFor(textarea), spot]
}
private createOvertypeFor(ghCommentBox: HTMLTextAreaElement): OverTypeInstance {
OverType.setCodeHighlighter(hljsHighlighter)
const overtypeContainer = this.modifyDOM(ghCommentBox)
return new OverType(overtypeContainer, {
autoResize: true,
minHeight: '102px',
padding: 'var(--base-size-8)',
placeholder: 'Add your comment here...',
})[0]!
}
private modifyDOM(overtypeInput: HTMLTextAreaElement): HTMLElement {
overtypeInput.classList.add('overtype-input')
const overtypePreview = document.createElement('div')
overtypePreview.classList.add('overtype-preview')
overtypeInput.insertAdjacentElement('afterend', overtypePreview)
const overtypeWrapper = overtypeInput.parentElement!.closest('div')!
overtypeWrapper.classList.add('overtype-wrapper')
overtypeInput.placeholder = 'Add your comment here...'
const overtypeContainer = overtypeWrapper.parentElement!.closest('div')!
overtypeContainer.classList.add('overtype-container')
return overtypeContainer.parentElement!.closest('div')!
}
tableTitle(spot: GitHubAddCommentSpot): string {
const { slug, number } = spot
return `${slug} PR #${number}`
}
tableIcon(_: GitHubAddCommentSpot): string {
return '🔄' // PR icon TODO: icon urls in /public
}
buildUrl(spot: GitHubAddCommentSpot): string {
return `https://${spot.domain}/${spot.slug}/pull/${spot.number}`
}
}
function hljsHighlighter(code: string, language: string) {
try {
if (language && hljs.getLanguage(language)) {
const result = hljs.highlight(code, { language })
return result.value
} else {
const result = hljs.highlightAuto(code)
return result.value
}
} catch (error) {
console.warn('highlight.js highlighting failed:', error)
return code
}
}