-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubIssueAddComment.ts
More file actions
69 lines (59 loc) · 2.3 KB
/
githubIssueAddComment.ts
File metadata and controls
69 lines (59 loc) · 2.3 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
import OverType, { type OverTypeInstance } from 'overtype'
import type { CommentEnhancer, CommentSpot } from '../../enhancer'
import { logger } from '../../logger'
import { modifyDOM } from '../modifyDOM'
import { commonGithubOptions } from './ghOptions'
import { githubHighlighter } from './githubHighlighter'
interface GitHubIssueAddCommentSpot extends CommentSpot {
type: 'GH_ISSUE_ADD_COMMENT'
domain: string
slug: string // owner/repo
number: number // issue number, undefined for new issues
}
export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssueAddCommentSpot> {
forSpotTypes(): string[] {
return ['GH_ISSUE_ADD_COMMENT']
}
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubIssueAddCommentSpot | null {
if (document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== '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(/^\/([^/]+)\/([^/]+)(?:\/issues\/(\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}`
return {
domain: 'github.com',
number,
slug,
type: 'GH_ISSUE_ADD_COMMENT',
unique_key,
}
}
prepareForFirstEnhancement(): void {
OverType.setCodeHighlighter(githubHighlighter)
}
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAddCommentSpot): OverTypeInstance {
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
minHeight: '100px',
placeholder: 'Use Markdown to format your comment',
})[0]!
}
tableTitle(spot: GitHubIssueAddCommentSpot): string {
const { slug, number } = spot
return `${slug} Issue #${number}`
}
tableIcon(_: GitHubIssueAddCommentSpot): string {
return '🔄' // PR icon TODO: icon urls in /public
}
buildUrl(spot: GitHubIssueAddCommentSpot): string {
return `https://${spot.domain}/${spot.slug}/issue/${spot.number}`
}
}