Skip to content

Commit ee65860

Browse files
authored
Merge pull request #131 from pazner/set-title
Allow setting page title and header from command line
2 parents 923d848 + 0c23576 commit ee65860

File tree

6 files changed

+23
-2
lines changed

6 files changed

+23
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Usage: diff2html [ flags and/or options ] -- [git diff passthrough flags and opt
8686
| | --maxLineSizeInBlockForComparison | Maximum number of characters of the bigger line in a block to apply comparison | | `200` |
8787
| | --maxLineLengthHighlight | Maximum number of characters in a line to apply highlight | | `10000` |
8888
| --hwt | --htmlWrapperTemplate | Path to custom template to be rendered when using the `html` output format | `[string]` |
89+
| -t | --title | Page title for `html` output | `[string]` |
8990
| -f | --format | Output format | `html`, `json` | `html` |
9091
| -i | --input | Diff input source | `file`, `command`, `stdin` | `command` |
9192
| -o | --output | Output destination | `preview`, `stdout` | `preview` |

src/cli.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ function prepareHTML(diffHTMLContent: string, config: Configuration): string {
4949
const jsUiFilePath = path.resolve(diff2htmlPath, 'bundles', 'js', 'diff2html-ui-slim.min.js');
5050
const jsUiContent = utils.readFile(jsUiFilePath);
5151

52+
const pageTitle = config.pageTitle;
53+
const pageHeader = config.pageHeader;
54+
5255
/* HACK:
5356
* Replace needs to receive a function as the second argument to perform an exact replacement.
5457
* 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
5558
*/
5659
return [
60+
{ searchValue: '<!--diff2html-title-->', replaceValue: pageTitle },
5761
{ searchValue: '<!--diff2html-css-->', replaceValue: `<style>\n${cssContent}\n</style>` },
5862
{ searchValue: '<!--diff2html-js-ui-->', replaceValue: `<script>\n${jsUiContent}\n</script>` },
5963
{
@@ -68,6 +72,7 @@ function prepareHTML(diffHTMLContent: string, config: Configuration): string {
6872
searchValue: '//diff2html-highlightCode',
6973
replaceValue: config.highlightCode ? `diff2htmlUi.highlightCode();` : '',
7074
},
75+
{ searchValue: '<!--diff2html-header-->', replaceValue: pageHeader },
7176
{ searchValue: '<!--diff2html-diff-->', replaceValue: diffHTMLContent },
7277
].reduce(
7378
(previousValue, replacement) =>

src/configuration.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export function parseArgv(argv: Argv): [Diff2HtmlConfig, Configuration] {
3333
maxLineLengthHighlight: argv.maxLineLengthHighlight,
3434
};
3535

36+
const defaultPageTitle = 'Diff to HTML by rtfpessoa';
37+
const defaultPageHeader = 'Diff to HTML by <a href="https://github.com/rtfpessoa">rtfpessoa</a>';
3638
const defaultWrapperTemplate = path.resolve(__dirname, '..', 'template.html');
3739
const configuration: Configuration = {
3840
showFilesOpen: argv.summary === 'open' || false,
@@ -44,6 +46,8 @@ export function parseArgv(argv: Argv): [Diff2HtmlConfig, Configuration] {
4446
inputSource: argv.input,
4547
diffyType: argv.diffy,
4648
htmlWrapperTemplate: argv.htmlWrapperTemplate || defaultWrapperTemplate,
49+
pageTitle: argv.pageTitle || defaultPageTitle,
50+
pageHeader: argv.pageTitle || defaultPageHeader,
4751
ignore: argv.ignore || [],
4852
};
4953

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ export type Configuration = {
1717
inputSource: InputType;
1818
diffyType?: DiffyType;
1919
htmlWrapperTemplate: string;
20+
pageTitle: string;
21+
pageHeader: string;
2022
ignore: string[];
2123
};

src/yargs.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type Argv = {
3131
diffy?: DiffyType;
3232
file?: string;
3333
htmlWrapperTemplate?: string;
34+
pageTitle?: string;
3435
ignore?: string[];
3536
extraArguments: string[];
3637
};
@@ -54,6 +55,7 @@ const defaults: Argv = {
5455
diffy: undefined,
5556
file: undefined,
5657
htmlWrapperTemplate: undefined,
58+
pageTitle: undefined,
5759
extraArguments: [],
5860
};
5961

@@ -206,6 +208,13 @@ export function setup(): Argv {
206208
type: 'string',
207209
default: defaults.htmlWrapperTemplate,
208210
})
211+
.option('title', {
212+
alias: 't',
213+
describe: 'Page title for HTML output',
214+
nargs: 1,
215+
type: 'string',
216+
default: defaults.pageTitle,
217+
})
209218
.option('ignore', {
210219
alias: 'ig',
211220
describe: 'ignore a file',

template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
5-
<title>Diff to HTML by rtfpessoa</title>
5+
<title><!--diff2html-title--></title>
66

77
<!--
88
Diff to HTML (template.html)
@@ -26,7 +26,7 @@
2626
</script>
2727
</head>
2828
<body style="text-align: center; font-family: 'Source Sans Pro', sans-serif">
29-
<h1>Diff to HTML by <a href="https://github.com/rtfpessoa">rtfpessoa</a></h1>
29+
<h1><!--diff2html-header--></h1>
3030

3131
<div id="diff">
3232
<!--diff2html-diff-->

0 commit comments

Comments
 (0)