Skip to content

Commit f1fd78a

Browse files
template config
1 parent dbb7ea8 commit f1fd78a

File tree

9 files changed

+107
-30
lines changed

9 files changed

+107
-30
lines changed

packages/core/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ declare class File$1 {
2626
highlighterName?: DiffHighlighter["name"];
2727
highlighterType?: DiffHighlighter["type"];
2828
maxLineNumber: number;
29+
enableTemplate: boolean;
2930
static createInstance(data: File$1): File$1;
3031
constructor(row: string, lang: DiffHighlighterLang, fileName?: string);
3132
constructor(row: string, lang: string, fileName?: string);
@@ -88,6 +89,9 @@ export declare class DiffFile {
8889
registerHighlighter?: Omit<DiffHighlighter, "getHighlighterEngine">;
8990
}): void;
9091
init(): void;
92+
enableTemplate(): void;
93+
disableTemplate(): void;
94+
getIsEnableTemplate(): boolean;
9195
buildSplitDiffLines(): void;
9296
buildUnifiedDiffLines(): void;
9397
getSplitLeftLine: (index: number) => SplitLineItem;
@@ -166,6 +170,7 @@ export declare class DiffFile {
166170
};
167171
version: string;
168172
theme: "light" | "dark";
173+
enableTemplate: boolean;
169174
isFullMerge: boolean;
170175
};
171176
mergeBundle: (data: ReturnType<DiffFile["getBundle"]>, notifyUpdate?: boolean) => void;
@@ -223,6 +228,7 @@ export declare class DiffFile {
223228
};
224229
version: string;
225230
theme: "light" | "dark";
231+
enableTemplate: boolean;
226232
};
227233
_mergeFullBundle: (data: ReturnType<DiffFile["_getFullBundle"]>, notifyUpdate?: boolean) => void;
228234
_destroy: () => void;

packages/core/src/diff-file.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ export class DiffFile {
145145

146146
#composeByMerge: boolean = false;
147147

148+
#enableTemplate: boolean = true;
149+
148150
#composeByFullMerge: boolean = false;
149151

150152
#highlighterName?: string;
@@ -294,6 +296,7 @@ export class DiffFile {
294296
this._oldFileName,
295297
this.uuid ? this.uuid + "-old" : undefined
296298
);
299+
this.#oldFileResult.enableTemplate = this.#enableTemplate;
297300
}
298301

299302
if (this._newFileContent) {
@@ -304,6 +307,7 @@ export class DiffFile {
304307
this._newFileName,
305308
this.uuid ? this.uuid + "-new" : undefined
306309
);
310+
this.#newFileResult.enableTemplate = this.#enableTemplate;
307311
}
308312
}
309313

@@ -374,13 +378,15 @@ export class DiffFile {
374378
this._oldFileName,
375379
this.uuid ? this.uuid + "-old" : undefined
376380
);
381+
this.#oldFileResult.enableTemplate = this.#enableTemplate;
377382
this.#newFileResult = getFile(
378383
this._newFileContent,
379384
this._newFileLang,
380385
this.#theme,
381386
this._newFileName,
382387
this.uuid ? this.uuid + "-new" : undefined
383388
);
389+
this.#newFileResult.enableTemplate = this.#enableTemplate;
384390
this.#oldFilePlaceholderLines = oldFilePlaceholderLines;
385391
this.#newFilePlaceholderLines = newFilePlaceholderLines;
386392
// all of the file just compose by diff, so we can not do the expand action
@@ -415,6 +421,7 @@ export class DiffFile {
415421
this._newFileName,
416422
this.uuid ? this.uuid + "-new" : undefined
417423
);
424+
this.#newFileResult.enableTemplate = this.#enableTemplate;
418425
} else if (this.#newFileResult) {
419426
let oldLineNumber = 1;
420427
let newLineNumber = 1;
@@ -445,6 +452,7 @@ export class DiffFile {
445452
this._oldFileName,
446453
this.uuid ? this.uuid + "-old" : undefined
447454
);
455+
this.#oldFileResult.enableTemplate = this.#enableTemplate;
448456
}
449457

450458
this.#composeRaw();
@@ -706,6 +714,18 @@ export class DiffFile {
706714
this.initSyntax();
707715
}
708716

717+
enableTemplate() {
718+
this.#enableTemplate = true;
719+
}
720+
721+
disableTemplate() {
722+
this.#enableTemplate = false;
723+
}
724+
725+
getIsEnableTemplate() {
726+
return this.#enableTemplate;
727+
}
728+
709729
buildSplitDiffLines() {
710730
if (this.#hasBuildSplit) return;
711731
let oldFileLineNumber = 1;
@@ -1458,6 +1478,7 @@ export class DiffFile {
14581478

14591479
const version = this._version_;
14601480
const theme = this.#theme;
1481+
const enableTemplate = this.#enableTemplate;
14611482

14621483
return {
14631484
hasInitRaw,
@@ -1496,6 +1517,8 @@ export class DiffFile {
14961517

14971518
theme,
14981519

1520+
enableTemplate,
1521+
14991522
isFullMerge: false,
15001523
};
15011524
};
@@ -1537,6 +1560,8 @@ export class DiffFile {
15371560

15381561
this.#theme = data.theme;
15391562

1563+
this.#enableTemplate = data.enableTemplate;
1564+
15401565
// mark this instance as a merged instance
15411566
this.#composeByMerge = true;
15421567

packages/core/src/file.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export class File {
5454

5555
maxLineNumber: number = 0;
5656

57+
enableTemplate: boolean = true;
58+
5759
static createInstance(data: File) {
5860
const file = new File(data?.raw, data?.lang, data?.fileName);
5961

@@ -79,6 +81,8 @@ export class File {
7981

8082
file.maxLineNumber = data?.maxLineNumber;
8183

84+
file.enableTemplate = data?.enableTemplate ?? true;
85+
8286
return file;
8387
}
8488

@@ -137,10 +141,12 @@ export class File {
137141

138142
const { syntaxFileObject, syntaxFileLineNumber } = supportEngin.processAST(this.ast);
139143

140-
// get syntax template
141-
Object.values(syntaxFileObject).forEach((line: SyntaxLineWithTemplate) => {
142-
line.template = getSyntaxLineTemplate(line);
143-
});
144+
if (this.enableTemplate) {
145+
// get syntax template
146+
Object.values(syntaxFileObject).forEach((line: SyntaxLineWithTemplate) => {
147+
line.template = getSyntaxLineTemplate(line);
148+
});
149+
}
144150

145151
this.syntaxFile = syntaxFileObject;
146152

@@ -174,10 +180,12 @@ export class File {
174180

175181
for (let i = 0; i < rawArray.length; i++) {
176182
this.rawFile[i + 1] = i < rawArray.length - 1 ? rawArray[i] + "\n" : rawArray[i];
177-
this.plainFile[i + 1] = {
178-
value: this.rawFile[i + 1],
179-
template: getPlainLineTemplate(this.rawFile[i + 1]),
180-
};
183+
if (this.enableTemplate) {
184+
this.plainFile[i + 1] = {
185+
value: this.rawFile[i + 1],
186+
template: getPlainLineTemplate(this.rawFile[i + 1]),
187+
};
188+
}
181189
}
182190

183191
this.hasDoRaw = true;

packages/core/src/parse/template.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type { SyntaxLineWithTemplate } from "../file";
88
import type { DiffLine } from "./diff-line";
99
import type { SyntaxLine } from "@git-diff-view/lowlight";
1010

11+
const defaultTransform = (content: string) => escapeHtml(content).replace(/\n/g, "").replace(/\r/g, "");
12+
1113
export const getPlainDiffTemplate = ({
1214
diffLine,
1315
rawLine,
@@ -23,7 +25,7 @@ export const getPlainDiffTemplate = ({
2325

2426
if (!changes || !changes.hasLineChange || !rawLine) return;
2527

26-
const transform = isTransformEnabled() ? processTransformTemplateContent : escapeHtml;
28+
const transform = isTransformEnabled() ? processTransformTemplateContent : defaultTransform;
2729

2830
const range = changes.range;
2931
const str1 = rawLine.slice(0, range.location);
@@ -59,7 +61,7 @@ export const getSyntaxDiffTemplate = ({
5961

6062
if (!changes || !changes.hasLineChange) return;
6163

62-
const transform = isTransformEnabled() ? processTransformTemplateContent : escapeHtml;
64+
const transform = isTransformEnabled() ? processTransformTemplateContent : defaultTransform;
6365

6466
const range = changes.range;
6567

@@ -100,7 +102,7 @@ export const getSyntaxDiffTemplate = ({
100102
export const getSyntaxLineTemplate = (line: SyntaxLine) => {
101103
let template = "";
102104

103-
const transform = isTransformEnabled() ? processTransformTemplateContent : escapeHtml;
105+
const transform = isTransformEnabled() ? processTransformTemplateContent : defaultTransform;
104106

105107
line?.nodeList?.forEach(({ node, wrapper }) => {
106108
template += `<span data-start="${node.startIndex}" data-end="${node.endIndex}" class="${(
@@ -114,7 +116,7 @@ export const getSyntaxLineTemplate = (line: SyntaxLine) => {
114116
export const getPlainLineTemplate = (line: string) => {
115117
if (!line) return "";
116118

117-
const transform = isTransformEnabled() ? processTransformTemplateContent : escapeHtml;
119+
const transform = isTransformEnabled() ? processTransformTemplateContent : defaultTransform;
118120

119121
const template = transform(line);
120122

packages/file/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ declare class File$1 {
2626
highlighterName?: DiffHighlighter["name"];
2727
highlighterType?: DiffHighlighter["type"];
2828
maxLineNumber: number;
29+
enableTemplate: boolean;
2930
static createInstance(data: File$1): File$1;
3031
constructor(row: string, lang: DiffHighlighterLang, fileName?: string);
3132
constructor(row: string, lang: string, fileName?: string);
@@ -87,6 +88,9 @@ export declare class DiffFile {
8788
registerHighlighter?: Omit<DiffHighlighter, "getHighlighterEngine">;
8889
}): void;
8990
init(): void;
91+
enableTemplate(): void;
92+
disableTemplate(): void;
93+
getIsEnableTemplate(): boolean;
9094
buildSplitDiffLines(): void;
9195
buildUnifiedDiffLines(): void;
9296
getSplitLeftLine: (index: number) => SplitLineItem;
@@ -165,6 +169,7 @@ export declare class DiffFile {
165169
};
166170
version: string;
167171
theme: "light" | "dark";
172+
enableTemplate: boolean;
168173
isFullMerge: boolean;
169174
};
170175
mergeBundle: (data: ReturnType<DiffFile["getBundle"]>, notifyUpdate?: boolean) => void;
@@ -222,6 +227,7 @@ export declare class DiffFile {
222227
};
223228
version: string;
224229
theme: "light" | "dark";
230+
enableTemplate: boolean;
225231
};
226232
_mergeFullBundle: (data: ReturnType<DiffFile["_getFullBundle"]>, notifyUpdate?: boolean) => void;
227233
_destroy: () => void;

packages/react/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ declare class File$1 {
2626
highlighterName?: DiffHighlighter["name"];
2727
highlighterType?: DiffHighlighter["type"];
2828
maxLineNumber: number;
29+
enableTemplate: boolean;
2930
static createInstance(data: File$1): File$1;
3031
constructor(row: string, lang: DiffHighlighterLang, fileName?: string);
3132
constructor(row: string, lang: string, fileName?: string);
@@ -87,6 +88,9 @@ export declare class DiffFile {
8788
registerHighlighter?: Omit<DiffHighlighter, "getHighlighterEngine">;
8889
}): void;
8990
init(): void;
91+
enableTemplate(): void;
92+
disableTemplate(): void;
93+
getIsEnableTemplate(): boolean;
9094
buildSplitDiffLines(): void;
9195
buildUnifiedDiffLines(): void;
9296
getSplitLeftLine: (index: number) => SplitLineItem;
@@ -165,6 +169,7 @@ export declare class DiffFile {
165169
};
166170
version: string;
167171
theme: "light" | "dark";
172+
enableTemplate: boolean;
168173
isFullMerge: boolean;
169174
};
170175
mergeBundle: (data: ReturnType<DiffFile["getBundle"]>, notifyUpdate?: boolean) => void;
@@ -222,6 +227,7 @@ export declare class DiffFile {
222227
};
223228
version: string;
224229
theme: "light" | "dark";
230+
enableTemplate: boolean;
225231
};
226232
_mergeFullBundle: (data: ReturnType<DiffFile["_getFullBundle"]>, notifyUpdate?: boolean) => void;
227233
_destroy: () => void;

0 commit comments

Comments
 (0)