-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-block-diff.ts
More file actions
45 lines (38 loc) · 1.16 KB
/
code-block-diff.ts
File metadata and controls
45 lines (38 loc) · 1.16 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
/**
* This module provides utilities for processing and styling lines of a text diff.
*/
const DIFF_STYLES = {
added: {
backgroundColor: "var(--color-diff-added-bg)",
borderLeft: "2px solid",
borderLeftColor: "var(--color-diff-added-border)",
indicator: "+",
},
removed: {
backgroundColor: "var(--color-diff-removed-bg)",
borderLeft: "2px solid",
borderLeftColor: "var(--color-diff-removed-border)",
indicator: "-",
},
normal: {
backgroundColor: "transparent",
borderLeft: "none",
borderLeftColor: "transparent",
indicator: "",
},
} as const
type DiffType = keyof typeof DIFF_STYLES
const DIFF_PATTERNS = {
"+ ": "added",
"- ": "removed",
} as const
type DiffPatternPrefix = keyof typeof DIFF_PATTERNS
const isDiffPatternPrefix = (prefix: string): prefix is DiffPatternPrefix => {
return prefix in DIFF_PATTERNS
}
export const getDiffType = (line: string): DiffType => {
const prefix = line.trimStart().slice(0, 2)
return isDiffPatternPrefix(prefix) ? DIFF_PATTERNS[prefix] : "normal"
}
export const cleanDiffLine = (line: string) => line.replace(/^(\s*)[+-] /, "$1")
export const getDiffStyles = (diffType: DiffType) => DIFF_STYLES[diffType]