Skip to content

Commit 76cfb11

Browse files
improve
1 parent d14e4d3 commit 76cfb11

File tree

11 files changed

+410
-100
lines changed

11 files changed

+410
-100
lines changed

packages/core/index.d.ts

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ declare class Cache$1<K, V> extends Map<K, V> {
99
}
1010
declare class File$1 {
1111

12-
readonly raw: string;
12+
raw: string;
1313
readonly lang: DiffHighlighterLang | string;
1414
readonly fileName?: string;
1515
ast?: DiffAST;
@@ -84,11 +84,13 @@ export declare class DiffFile {
8484
buildUnifiedDiffLines(): void;
8585
getSplitLeftLine: (index: number) => SplitLineItem;
8686
getSplitLineByLineNumber: (lineNumber: number, side: SplitSide) => SplitLineItem;
87+
getSplitLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
8788
getSplitRightLine: (index: number) => SplitLineItem;
8889
getSplitHunkLine: (index: number) => DiffHunkItem;
8990
onSplitHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
9091
getUnifiedLine: (index: number) => UnifiedLineItem;
9192
getUnifiedLineByLineNumber: (lienNumber: number, side: SplitSide) => UnifiedLineItem;
93+
getUnifiedLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
9294
getUnifiedHunkLine: (index: number) => DiffHunkItem;
9395
onUnifiedHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
9496
onAllExpand: (mode: "split" | "unified") => void;
@@ -412,8 +414,6 @@ export declare const checkCurrentLineIsHidden: (diffFile: DiffFile, lineNumber:
412414
};
413415
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
414416
export declare const disableCache: () => void;
415-
export declare const doAfterTransform: (content: string) => string;
416-
export declare const doPreTransform: (content: string) => string;
417417
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, getAdditionSyntax, getDeletionSyntax, }: {
418418
getAdditionRaw: (lineNumber: number) => string;
419419
getDeletionRaw: (lineNumber: number) => string;
@@ -438,22 +438,68 @@ export declare const getSyntaxLineTemplate: (line: SyntaxLine) => string;
438438
export declare const getUnifiedContentLine: (diffFile: DiffFile) => DiffUnifiedContentLineItem[];
439439
export declare const getUnifiedLines: (diffFile: DiffFile) => DiffUnifiedLineItem[];
440440
export declare const highlighter: DiffHighlighter;
441+
/**
442+
* Checks whether content transformation is currently enabled.
443+
*
444+
* @returns {boolean} True if transformation is enabled, false otherwise
445+
*
446+
* @example
447+
* ```typescript
448+
* if (isTransformEnabled()) {
449+
* console.log('Transformations are active');
450+
* }
451+
* ```
452+
*/
441453
export declare const isTransformEnabled: () => boolean;
442454
export declare const numIterator: <T>(num: number, cb: (index: number) => T) => T[];
443455
export declare const parseInstance: DiffParser;
444456
export declare const processAST: (ast: DiffAST) => {
445457
syntaxFileObject: Record<number, SyntaxLine>;
446458
syntaxFileLineNumber: number;
447459
};
460+
/**
461+
* Applies the transformation function to the provided content if transformation is enabled.
462+
*
463+
* @param content - The content string to transform
464+
* @returns {string} The transformed content if transformation is enabled and configured, otherwise the original content
465+
*
466+
* @example
467+
* ```typescript
468+
* const transformed = processTransformContent(' hello world ');
469+
* ```
470+
*/
471+
export declare const processTransformContent: (content: string) => string;
472+
/**
473+
* Applies the file transformation function to the provided content if transformation is enabled.
474+
*
475+
* @param content - The content string to transform
476+
* @returns {string} The transformed content if transformation is enabled and configured, otherwise the original content
477+
*
478+
* @example
479+
* ```typescript
480+
* const transformed = doTransformFile('some file content');
481+
* ```
482+
*/
483+
export declare const processTransformForFile: (content: string) => string;
448484
export declare const resetDefaultComposeLength: () => void;
485+
/**
486+
* Resets all transformation functions to their default state and disables transformation.
487+
* This clears any previously set pre-transform and after-transform functions.
488+
*
489+
* @example
490+
* ```typescript
491+
* resetTransform(); // Clears all transformations
492+
* ```
493+
*/
449494
export declare const resetTransform: () => void;
450-
export declare const setAfterTransform: (fn: (content: string) => string) => void;
451495
/**
452496
* ⚠️ **WARNING: DANGEROUS OPERATION** ⚠️
453497
*
454498
* Sets a pre-transformation function that will be applied to content before processing.
455499
* This is a global state modification that affects all subsequent operations.
456500
*
501+
* if your set a transform content function, you may also need call `escapeHtml` function to escape html characters.
502+
*
457503
* **CAUTION**:
458504
* - This function modifies global state and may cause unexpected side effects
459505
* - The transformation will be applied to ALL content processing operations
@@ -466,10 +512,32 @@ export declare const setAfterTransform: (fn: (content: string) => string) => voi
466512
* @example
467513
* ```typescript
468514
* // Use with caution - this affects global behavior
469-
* setPreTransform((content) => content.trim());
515+
* setTransformForContent((content) => content.trim());
516+
* ```
517+
*/
518+
export declare const setTransformForContent: (fn: (content: string) => string) => void;
519+
/**
520+
* ⚠️ **WARNING: DANGEROUS OPERATION** ⚠️
521+
*
522+
* Sets a transformation function that will be applied to the file content.
523+
* This is a global state modification that affects all subsequent file operations.
524+
*
525+
* **CAUTION**:
526+
* - This function modifies global state and may cause unexpected side effects
527+
* - The transformation will be applied to ALL file content processing operations
528+
* - Multiple calls will overwrite the previous transform function
529+
* - Ensure proper error handling in your transform function to avoid breaking the entire pipeline
530+
*
531+
* @param fn - The transformation function to apply to file content
532+
* @throws {Error} Throws an error if the provided parameter is not a function
533+
*
534+
* @example
535+
* ```typescript
536+
* // Use with caution - this affects global behavior
537+
* setTransformFile((content) => content.toUpperCase());
470538
* ```
471539
*/
472-
export declare const setPreTransform: (fn: (content: string) => string) => void;
540+
export declare const setTransformForFile: (fn: (content: string) => string) => void;
473541
export declare const versions: string;
474542
export declare enum DiffFileLineType {
475543
hunk = 1,

packages/core/src/diff-file.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -642,18 +642,10 @@ export class DiffFile {
642642
return this.#oldFileLines?.[lineNumber];
643643
}
644644

645-
#getOldPlainLine(lineNumber: number) {
646-
return this.#oldFilePlainLines?.[lineNumber];
647-
}
648-
649645
#getNewRawLine(lineNumber: number) {
650646
return this.#newFileLines?.[lineNumber];
651647
}
652648

653-
#getNewPlainLine(lineNumber: number) {
654-
return this.#newFilePlainLines?.[lineNumber];
655-
}
656-
657649
#getOldSyntaxLine(lineNumber: number) {
658650
return this.#oldFileSyntaxLines?.[lineNumber];
659651
}
@@ -1088,6 +1080,14 @@ export class DiffFile {
10881080
}
10891081
};
10901082

1083+
getSplitLineIndexByLineNumber = (lineNumber: number, side: SplitSide) => {
1084+
if (side === SplitSide.old) {
1085+
return this.#splitLeftLines?.findIndex((item) => item.lineNumber === lineNumber);
1086+
} else {
1087+
return this.#splitRightLines?.findIndex((item) => item.lineNumber === lineNumber);
1088+
}
1089+
}
1090+
10911091
getSplitRightLine = (index: number) => {
10921092
return this.#splitRightLines[index];
10931093
};
@@ -1183,6 +1183,14 @@ export class DiffFile {
11831183
}
11841184
};
11851185

1186+
getUnifiedLineIndexByLineNumber = (lineNumber: number, side: SplitSide) => {
1187+
if (side === SplitSide.old) {
1188+
return this.#unifiedLines?.findIndex((item) => item.oldLineNumber === lineNumber);
1189+
} else {
1190+
return this.#unifiedLines?.findIndex((item) => item.newLineNumber === lineNumber);
1191+
}
1192+
}
1193+
11861194
getUnifiedHunkLine = (index: number) => {
11871195
return this.#unifiedHunksLines?.[index];
11881196
};

packages/core/src/file.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { highlighter } from "@git-diff-view/lowlight";
22

33
import { Cache } from "./cache";
4-
import { getPlainLineTemplate, getSyntaxLineTemplate } from "./parse";
4+
import { getPlainLineTemplate, getSyntaxLineTemplate, processTransformForFile } from "./parse";
55

66
import type { DiffAST, DiffHighlighter, DiffHighlighterLang, SyntaxLine } from "@git-diff-view/lowlight";
77

@@ -85,10 +85,12 @@ export class File {
8585
constructor(row: string, lang: DiffHighlighterLang, fileName?: string);
8686
constructor(row: string, lang: string, fileName?: string);
8787
constructor(
88-
readonly raw: string,
88+
public raw: string,
8989
readonly lang: DiffHighlighterLang | string,
9090
readonly fileName?: string
9191
) {
92+
this.raw = processTransformForFile(raw);
93+
9294
Object.defineProperty(this, "__v_skip", { value: true });
9395
}
9496

packages/core/src/parse/template.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { addContentHighlightBGName, delContentHighlightBGName, getSymbol } from
22

33
import { escapeHtml } from "../escape-html";
44

5-
import { doPreTransform, doAfterTransform } from "./transform";
5+
import { processTransformContent, isTransformEnabled } from "./transform";
66

77
import type { SyntaxLineWithTemplate } from "../file";
88
import type { DiffLine } from "./diff-line";
@@ -23,6 +23,8 @@ export const getPlainDiffTemplate = ({
2323

2424
if (!changes || !changes.hasLineChange || !rawLine) return;
2525

26+
const transform = isTransformEnabled() ? processTransformContent : escapeHtml;
27+
2628
const range = changes.range;
2729
const str1 = rawLine.slice(0, range.location);
2830
const str2 = rawLine.slice(range.location, range.location + range.length);
@@ -33,11 +35,11 @@ export const getPlainDiffTemplate = ({
3335

3436
const template = `<span data-range-start="${range.location}" data-range-end="${
3537
range.location + range.length
36-
}">${doAfterTransform(escapeHtml(doPreTransform(str1)))}<span data-diff-highlight style="background-color: var(${operator === "add" ? addContentHighlightBGName : delContentHighlightBGName});border-radius: 0.2em;">${
38+
}">${transform(str1)}<span data-diff-highlight style="background-color: var(${operator === "add" ? addContentHighlightBGName : delContentHighlightBGName});border-radius: 0.2em;">${
3739
isLast
38-
? `${doAfterTransform(escapeHtml(doPreTransform(_str2)))}<span data-newline-symbol>${getSymbol(isNewLineSymbolChanged)}</span>`
39-
: doAfterTransform(escapeHtml(doPreTransform(str2)))
40-
}</span>${doAfterTransform(escapeHtml(doPreTransform(str3)))}</span>`;
40+
? `${transform(_str2)}<span data-newline-symbol>${getSymbol(isNewLineSymbolChanged)}</span>`
41+
: transform(str2)
42+
}</span>${transform(str3)}</span>`;
4143

4244
diffLine.plainTemplate = template;
4345
};
@@ -57,6 +59,8 @@ export const getSyntaxDiffTemplate = ({
5759

5860
if (!changes || !changes.hasLineChange) return;
5961

62+
const transform = isTransformEnabled() ? processTransformContent : escapeHtml;
63+
6064
const range = changes.range;
6165

6266
let template = `<span data-range-start="${range.location}" data-range-end="${range.location + range.length}">`;
@@ -65,7 +69,7 @@ export const getSyntaxDiffTemplate = ({
6569
if (node.endIndex < range.location || range.location + range.length < node.startIndex) {
6670
template += `<span data-start="${node.startIndex}" data-end="${node.endIndex}" class="${(
6771
wrapper?.properties?.className || []
68-
)?.join(" ")}" style="${wrapper?.properties?.style || ""}">${doAfterTransform(escapeHtml(doPreTransform(node.value)))}</span>`;
72+
)?.join(" ")}" style="${wrapper?.properties?.style || ""}">${transform(node.value)}</span>`;
6973
} else {
7074
const index1 = range.location - node.startIndex;
7175
const index2 = index1 < 0 ? 0 : index1;
@@ -80,11 +84,11 @@ export const getSyntaxDiffTemplate = ({
8084
wrapper?.properties?.className || []
8185
)?.join(
8286
" "
83-
)}" style="${wrapper?.properties?.style || ""}">${doAfterTransform(escapeHtml(doPreTransform(str1)))}<span data-diff-highlight style="background-color: var(${operator === "add" ? addContentHighlightBGName : delContentHighlightBGName});border-top-left-radius: ${isStart ? "0.2em" : "0"};border-bottom-left-radius: ${isStart ? "0.2em" : "0"};border-top-right-radius: ${isEnd || isLast ? "0.2em" : "0"};border-bottom-right-radius: ${isEnd || isLast ? "0.2em" : "0"}">${
87+
)}" style="${wrapper?.properties?.style || ""}">${transform(str1)}<span data-diff-highlight style="background-color: var(${operator === "add" ? addContentHighlightBGName : delContentHighlightBGName});border-top-left-radius: ${isStart ? "0.2em" : "0"};border-bottom-left-radius: ${isStart ? "0.2em" : "0"};border-top-right-radius: ${isEnd || isLast ? "0.2em" : "0"};border-bottom-right-radius: ${isEnd || isLast ? "0.2em" : "0"}">${
8488
isLast
85-
? `${doAfterTransform(escapeHtml(doPreTransform(_str2)))}<span data-newline-symbol>${getSymbol(changes.newLineSymbol)}</span>`
86-
: doAfterTransform(escapeHtml(doPreTransform(str2)))
87-
}</span>${doAfterTransform(escapeHtml(doPreTransform(str3)))}</span>`;
89+
? `${transform(_str2)}<span data-newline-symbol>${getSymbol(changes.newLineSymbol)}</span>`
90+
: transform(str2)
91+
}</span>${transform(str3)}</span>`;
8892
}
8993
});
9094

@@ -96,10 +100,12 @@ export const getSyntaxDiffTemplate = ({
96100
export const getSyntaxLineTemplate = (line: SyntaxLine) => {
97101
let template = "";
98102

103+
const transform = isTransformEnabled() ? processTransformContent : escapeHtml;
104+
99105
line?.nodeList?.forEach(({ node, wrapper }) => {
100106
template += `<span data-start="${node.startIndex}" data-end="${node.endIndex}" class="${(
101107
wrapper?.properties?.className || []
102-
)?.join(" ")}" style="${wrapper?.properties?.style || ""}">${doAfterTransform(escapeHtml(doPreTransform(node.value)))}</span>`;
108+
)?.join(" ")}" style="${wrapper?.properties?.style || ""}">${transform(node.value)}</span>`;
103109
});
104110

105111
return template;
@@ -108,7 +114,9 @@ export const getSyntaxLineTemplate = (line: SyntaxLine) => {
108114
export const getPlainLineTemplate = (line: string) => {
109115
if (!line) return "";
110116

111-
const template = doAfterTransform(escapeHtml(doPreTransform(line)));
117+
const transform = isTransformEnabled() ? processTransformContent : escapeHtml;
118+
119+
const template = transform(line);
112120

113121
return template;
114122
};

0 commit comments

Comments
 (0)