Skip to content

Commit ff3a86d

Browse files
committed
Add colorscheme variable to wrapper
1 parent 5dae945 commit ff3a86d

File tree

6 files changed

+100
-5
lines changed

6 files changed

+100
-5
lines changed

src/__tests__/diff2html-tests.ts

+63-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22

33
import { parse, html } from '../diff2html';
4-
import { DiffFile, LineType, OutputFormatType } from '../types';
4+
import { ColorSchemeType, DiffFile, LineType, OutputFormatType } from '../types';
55

66
const diffExample1 =
77
'diff --git a/sample b/sample\n' +
@@ -1258,5 +1258,67 @@ describe('Diff2Html', () => {
12581258
`);
12591259
/* eslint-enable no-irregular-whitespace */
12601260
});
1261+
1262+
describe('with dark mode flag', () => {
1263+
it('should return a html diff with dark mode', () => {
1264+
const result = html(diffExample1, {
1265+
drawFileList: false,
1266+
colorScheme: ColorSchemeType.DARK,
1267+
});
1268+
expect(result).toMatchInlineSnapshot(`
1269+
"<div class="d2h-wrapper dark">
1270+
<div id="d2h-675094" class="d2h-file-wrapper" data-lang="">
1271+
<div class="d2h-file-header">
1272+
<span class="d2h-file-name-wrapper">
1273+
<svg aria-hidden="true" class="d2h-icon" height="16" version="1.1" viewBox="0 0 12 16" width="12">
1274+
<path d="M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z"></path>
1275+
</svg> <span class="d2h-file-name">sample</span>
1276+
<span class="d2h-tag d2h-changed d2h-changed-tag">CHANGED</span></span>
1277+
<label class="d2h-file-collapse">
1278+
<input class="d2h-file-collapse-input" type="checkbox" name="viewed" value="viewed">
1279+
Viewed
1280+
</label>
1281+
</div>
1282+
<div class="d2h-file-diff">
1283+
<div class="d2h-code-wrapper">
1284+
<table class="d2h-diff-table">
1285+
<tbody class="d2h-diff-tbody">
1286+
<tr>
1287+
<td class="d2h-code-linenumber d2h-info"></td>
1288+
<td class="d2h-info">
1289+
<div class="d2h-code-line">@@ -1 +1 @@</div>
1290+
</td>
1291+
</tr><tr>
1292+
<td class="d2h-code-linenumber d2h-del d2h-change">
1293+
<div class="line-num1">1</div>
1294+
<div class="line-num2"></div>
1295+
</td>
1296+
<td class="d2h-del d2h-change">
1297+
<div class="d2h-code-line">
1298+
<span class="d2h-code-line-prefix">-</span>
1299+
<span class="d2h-code-line-ctn"><del>test</del></span>
1300+
</div>
1301+
</td>
1302+
</tr><tr>
1303+
<td class="d2h-code-linenumber d2h-ins d2h-change">
1304+
<div class="line-num1"></div>
1305+
<div class="line-num2">1</div>
1306+
</td>
1307+
<td class="d2h-ins d2h-change">
1308+
<div class="d2h-code-line">
1309+
<span class="d2h-code-line-prefix">+</span>
1310+
<span class="d2h-code-line-ctn"><ins>test1</ins></span>
1311+
</div>
1312+
</td>
1313+
</tr>
1314+
</tbody>
1315+
</table>
1316+
</div>
1317+
</div>
1318+
</div>
1319+
</div>"
1320+
`);
1321+
});
1322+
});
12611323
});
12621324
});

src/line-by-line-renderer.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export default class LineByLineRenderer {
5252
})
5353
.join('\n');
5454

55-
return this.hoganUtils.render(genericTemplatesPath, 'wrapper', { content: diffsHtml });
55+
return this.hoganUtils.render(genericTemplatesPath, 'wrapper', {
56+
colorScheme: renderUtils.colorSchemeToCss(this.config.colorScheme),
57+
content: diffsHtml,
58+
});
5659
}
5760

5861
makeFileDiffHtml(file: DiffFile, diffs: string): string {

src/render-utils.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import * as jsDiff from 'diff';
22

33
import { unifyPath, hashCode } from './utils';
44
import * as rematch from './rematch';
5-
import { LineMatchingType, DiffStyleType, LineType, DiffLineParts, DiffFile, DiffFileName } from './types';
5+
import {
6+
ColorSchemeType,
7+
DiffFile,
8+
DiffFileName,
9+
DiffLineParts,
10+
DiffStyleType,
11+
LineMatchingType,
12+
LineType,
13+
} from './types';
614

715
export type CSSLineClass =
816
| 'd2h-ins'
@@ -37,13 +45,15 @@ export interface RenderConfig {
3745
matchWordsThreshold?: number;
3846
maxLineLengthHighlight?: number;
3947
diffStyle?: DiffStyleType;
48+
colorScheme?: ColorSchemeType;
4049
}
4150

4251
export const defaultRenderConfig = {
4352
matching: LineMatchingType.NONE,
4453
matchWordsThreshold: 0.25,
4554
maxLineLengthHighlight: 10000,
4655
diffStyle: DiffStyleType.WORD,
56+
colorScheme: ColorSchemeType.LIGHT,
4757
};
4858

4959
const separator = '/';
@@ -76,6 +86,17 @@ export function toCSSClass(lineType: LineType): CSSLineClass {
7686
}
7787
}
7888

89+
export function colorSchemeToCss(colorScheme: ColorSchemeType): string {
90+
switch (colorScheme) {
91+
case ColorSchemeType.DARK:
92+
return ' dark';
93+
case ColorSchemeType.AUTO:
94+
return ' auto-color-scheme';
95+
default:
96+
return '';
97+
}
98+
}
99+
79100
/**
80101
* Prefix length of the hunk lines in the diff
81102
*/

src/side-by-side-renderer.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export default class SideBySideRenderer {
5252
})
5353
.join('\n');
5454

55-
return this.hoganUtils.render(genericTemplatesPath, 'wrapper', { content: diffsHtml });
55+
return this.hoganUtils.render(genericTemplatesPath, 'wrapper', {
56+
colorScheme: renderUtils.colorSchemeToCss(this.config.colorScheme),
57+
content: diffsHtml,
58+
});
5659
}
5760

5861
makeFileDiffHtml(file: DiffFile, diffs: FileHtml): string {
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<div class="d2h-wrapper">
1+
<div class="d2h-wrapper{{colorScheme}}">
22
{{{content}}}
33
</div>

src/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,9 @@ export const DiffStyleType: { [_: string]: DiffStyleType } = {
9191
WORD: 'word',
9292
CHAR: 'char',
9393
};
94+
95+
export enum ColorSchemeType {
96+
AUTO = 'auto',
97+
DARK = 'dark',
98+
LIGHT = 'light',
99+
}

0 commit comments

Comments
 (0)