Skip to content

Commit 3b61953

Browse files
committed
Refactor all the gihub highlighters to use oncePerRefresh.
1 parent 387f330 commit 3b61953

File tree

6 files changed

+19
-26
lines changed

6 files changed

+19
-26
lines changed

src/lib/enhancers/github/githubEditComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CommentEnhancer, CommentSpot } from '@/lib/enhancer'
44
import { logger } from '@/lib/logger'
55
import { modifyDOM } from '../modifyDOM'
66
import { commonGithubOptions } from './ghOptions'
7-
import { githubHighlighter } from './githubHighlighter'
7+
import { prepareGitHubHighlighter } from './githubHighlighter'
88

99
export interface GitHubEditCommentSpot extends CommentSpot {
1010
type: 'GH_EDIT_COMMENT'
@@ -48,11 +48,8 @@ export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditComm
4848
}
4949
}
5050

51-
prepareForFirstEnhancement(): void {
52-
OverType.setCodeHighlighter(githubHighlighter)
53-
}
54-
5551
enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditCommentSpot): OverTypeInstance {
52+
prepareGitHubHighlighter()
5653
const overtypeContainer = modifyDOM(textArea)
5754
return new OverType(overtypeContainer, {
5855
...commonGithubOptions,

src/lib/enhancers/github/githubHighlighter.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import hljs from 'highlight.js'
2+
import OverType from 'overtype'
3+
import { oncePerRefresh } from '@/lib/once-per-refresh'
24

3-
export function githubHighlighter(code: string, language?: string) {
5+
export function prepareGitHubHighlighter() {
6+
oncePerRefresh('github-highlighter', () => {
7+
OverType.setCodeHighlighter(githubHighlighter)
8+
})
9+
}
10+
11+
function githubHighlighter(code: string, language?: string) {
412
try {
513
if (language && hljs.getLanguage(language)) {
614
const result = hljs.highlight(code, { language })

src/lib/enhancers/github/githubIssueAddComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhan
55
import { logger } from '@/lib/logger'
66
import { modifyDOM } from '../modifyDOM'
77
import { commonGithubOptions } from './ghOptions'
8-
import { githubHighlighter } from './githubHighlighter'
8+
import { prepareGitHubHighlighter } from './githubHighlighter'
99

1010
export interface GitHubIssueAddCommentSpot extends CommentSpot {
1111
type: 'GH_ISSUE_ADD_COMMENT'
@@ -53,11 +53,8 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
5353
}
5454
}
5555

56-
prepareForFirstEnhancement(): void {
57-
OverType.setCodeHighlighter(githubHighlighter)
58-
}
59-
6056
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAddCommentSpot): OverTypeInstance {
57+
prepareGitHubHighlighter()
6158
const overtypeContainer = modifyDOM(textArea)
6259
return new OverType(overtypeContainer, {
6360
...commonGithubOptions,

src/lib/enhancers/github/githubIssueNewComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhan
33
import { logger } from '../../logger'
44
import { modifyDOM } from '../modifyDOM'
55
import { commonGithubOptions } from './ghOptions'
6-
import { githubHighlighter } from './githubHighlighter'
6+
import { prepareGitHubHighlighter } from './githubHighlighter'
77

88
interface GitHubIssueNewCommentSpot extends CommentSpot {
99
type: 'GH_ISSUE_NEW_COMMENT'
@@ -42,11 +42,8 @@ export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssu
4242
}
4343
}
4444

45-
prepareForFirstEnhancement(): void {
46-
OverType.setCodeHighlighter(githubHighlighter)
47-
}
48-
4945
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueNewCommentSpot): OverTypeInstance {
46+
prepareGitHubHighlighter()
5047
const overtypeContainer = modifyDOM(textArea)
5148
return new OverType(overtypeContainer, {
5249
...commonGithubOptions,

src/lib/enhancers/github/githubPRAddComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhan
44
import { logger } from '@/lib/logger'
55
import { modifyDOM } from '../modifyDOM'
66
import { commonGithubOptions } from './ghOptions'
7-
import { githubHighlighter } from './githubHighlighter'
7+
import { prepareGitHubHighlighter } from './githubHighlighter'
88

99
export interface GitHubPRAddCommentSpot extends CommentSpot {
1010
type: 'GH_PR_ADD_COMMENT' // Override to narrow from string to specific union
@@ -49,11 +49,8 @@ export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCo
4949
}
5050
}
5151

52-
prepareForFirstEnhancement(): void {
53-
OverType.setCodeHighlighter(githubHighlighter)
54-
}
55-
5652
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRAddCommentSpot): OverTypeInstance {
53+
prepareGitHubHighlighter()
5754
const overtypeContainer = modifyDOM(textArea)
5855
return new OverType(overtypeContainer, {
5956
...commonGithubOptions,

src/lib/enhancers/github/githubPRNewComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhan
33
import { logger } from '../../logger'
44
import { modifyDOM } from '../modifyDOM'
55
import { commonGithubOptions } from './ghOptions'
6-
import { githubHighlighter } from './githubHighlighter'
6+
import { prepareGitHubHighlighter } from './githubHighlighter'
77

88
interface GitHubPRNewCommentSpot extends CommentSpot {
99
type: 'GH_PR_NEW_COMMENT'
@@ -50,11 +50,8 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
5050
}
5151
}
5252

53-
prepareForFirstEnhancement(): void {
54-
OverType.setCodeHighlighter(githubHighlighter)
55-
}
56-
5753
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRNewCommentSpot): OverTypeInstance {
54+
prepareGitHubHighlighter()
5855
const overtypeContainer = modifyDOM(textArea)
5956
return new OverType(overtypeContainer, {
6057
...commonGithubOptions,

0 commit comments

Comments
 (0)