@@ -13,9 +13,52 @@ import { put } from './http-utils.js';
13
13
import * as log from './logger.js' ;
14
14
import { Configuration , InputType , DiffyType } from './types.js' ;
15
15
import * as utils from './utils.js' ;
16
+ import { ColorSchemeType } from 'diff2html/lib/types.js' ;
16
17
17
18
const defaultArgs = [ '-M' , '-C' , 'HEAD' ] ;
18
19
20
+ const lightGitHubTheme = `<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css" />` ;
21
+ const darkGitHubTheme = `<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css" />` ;
22
+ const autoGitHubTheme = `<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css" media="screen and (prefers-color-scheme: light)" />
23
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css" media="screen and (prefers-color-scheme: dark)" />` ;
24
+
25
+ const lightBaseStyle = `<style>
26
+ body {
27
+ background-color: var(--d2h-bg-color);
28
+ }
29
+ h1 {
30
+ color: var(--d2h-light-color);
31
+ }
32
+ </style>` ;
33
+
34
+ const darkBaseStyle = `<style>
35
+ body {
36
+ background-color: rgb(13, 17, 23);
37
+ }
38
+ h1 {
39
+ color: var(--d2h-dark-color);
40
+ }
41
+ </style>` ;
42
+
43
+ const autoBaseStyle = `<style>
44
+ @media screen and (prefers-color-scheme: light) {
45
+ body {
46
+ background-color: var(--d2h-bg-color);
47
+ }
48
+ h1 {
49
+ color: var(--d2h-light-color);
50
+ }
51
+ }
52
+ @media screen and (prefers-color-scheme: dark) {
53
+ body {
54
+ background-color: rgb(13, 17, 23);
55
+ }
56
+ h1 {
57
+ color: var(--d2h-dark-color);
58
+ }
59
+ }
60
+ </style>` ;
61
+
19
62
function generateGitDiffArgs ( gitArgsArr : string [ ] , ignore : string [ ] ) : string [ ] {
20
63
const gitDiffArgs : string [ ] = [ 'diff' ] ;
21
64
@@ -41,7 +84,7 @@ function runGitDiff(gitArgsArr: string[], ignore: string[]): string {
41
84
return utils . execute ( 'git' , gitDiffArgs ) ;
42
85
}
43
86
44
- function prepareHTML ( diffHTMLContent : string , config : Configuration ) : string {
87
+ function prepareHTML ( diffHTMLContent : string , config : Configuration , colorScheme ?: ColorSchemeType ) : string {
45
88
const template = utils . readFile ( config . htmlWrapperTemplate ) ;
46
89
47
90
const diff2htmlPath = path . join ( path . dirname ( require . resolve ( 'diff2html' ) ) , '..' ) ;
@@ -55,13 +98,21 @@ function prepareHTML(diffHTMLContent: string, config: Configuration): string {
55
98
const pageTitle = config . pageTitle ;
56
99
const pageHeader = config . pageHeader ;
57
100
101
+ const gitHubTheme =
102
+ colorScheme === 'light' ? lightGitHubTheme : colorScheme === 'dark' ? darkGitHubTheme : autoGitHubTheme ;
103
+
104
+ const baseStyle = colorScheme === 'light' ? lightBaseStyle : colorScheme === 'dark' ? darkBaseStyle : autoBaseStyle ;
105
+
58
106
/* HACK:
59
107
* Replace needs to receive a function as the second argument to perform an exact replacement.
60
108
* This will avoid the replacements from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter
61
109
*/
62
110
return [
63
111
{ searchValue : '<!--diff2html-title-->' , replaceValue : pageTitle } ,
64
- { searchValue : '<!--diff2html-css-->' , replaceValue : `<style>\n${ cssContent } \n</style>` } ,
112
+ {
113
+ searchValue : '<!--diff2html-css-->' ,
114
+ replaceValue : `${ baseStyle } \n${ gitHubTheme } \n<style>\n${ cssContent } \n</style>` ,
115
+ } ,
65
116
{ searchValue : '<!--diff2html-js-ui-->' , replaceValue : `<script>\n${ jsUiContent } \n</script>` } ,
66
117
{
67
118
searchValue : '//diff2html-fileListToggle' ,
@@ -118,7 +169,7 @@ export function getOutput(options: Diff2HtmlConfig, config: Configuration, input
118
169
switch ( config . formatType ) {
119
170
case 'html' : {
120
171
const htmlContent = html ( diffJson , { ...options } ) ;
121
- return prepareHTML ( htmlContent , config ) ;
172
+ return prepareHTML ( htmlContent , config , options . colorScheme ) ;
122
173
}
123
174
case 'json' : {
124
175
return JSON . stringify ( diffJson ) ;
0 commit comments