Skip to content

Commit 7e4dc7b

Browse files
committed
fix: Allow $ in diff
1 parent 018aad8 commit 7e4dc7b

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

src/__tests__/utils-tests.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,17 @@ describe("utils", () => {
1010

1111
expect(stringToCompare).toBe(contentRead);
1212
});
13+
14+
test("should execute command in shell", () => {
15+
const echoedValue = "echoed string";
16+
const result = utils.execute(`echo "${echoedValue}"`);
17+
18+
expect(result).toBe(`${echoedValue}\n`);
19+
});
20+
21+
test("should replace exactly string", () => {
22+
const result = utils.replaceExactly("my long and nice text", "long", "$&beautiful");
23+
24+
expect(result).toBe("my $&beautiful and nice text");
25+
});
1326
});

src/cli.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,31 @@ function prepareHTML(diffHTMLContent: string, config: Configuration): string {
3232
const jsUiFilePath = path.resolve(diff2htmlPath, "dist", "diff2html-ui.min.js");
3333
const jsUiContent = utils.readFile(jsUiFilePath);
3434

35-
return template
36-
.replace("<!--diff2html-css-->", `<style>\n${cssContent}\n</style>`)
37-
.replace("<!--diff2html-js-ui-->", `<script>\n${jsUiContent}\n</script>`)
38-
.replace("//diff2html-fileListCloseable", `diff2htmlUi.fileListCloseable("#diff", ${config.showFilesOpen});`)
39-
.replace("//diff2html-synchronisedScroll", `diff2htmlUi.synchronisedScroll("#diff", ${config.synchronisedScroll});`)
40-
.replace("//diff2html-highlightCode", config.highlightCode ? `diff2htmlUi.highlightCode("#diff");` : "")
41-
.replace("<!--diff2html-diff-->", diffHTMLContent);
35+
/* HACK:
36+
* Replace needs to receive a function as the second argument to perform an exact replacement.
37+
* 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
38+
*/
39+
return [
40+
{ searchValue: "<!--diff2html-css-->", replaceValue: `<style>\n${cssContent}\n</style>` },
41+
{ searchValue: "<!--diff2html-js-ui-->", replaceValue: `<script>\n${jsUiContent}\n</script>` },
42+
{
43+
searchValue: "//diff2html-fileListCloseable",
44+
replaceValue: `diff2htmlUi.fileListCloseable("#diff", ${config.showFilesOpen});`
45+
},
46+
{
47+
searchValue: "//diff2html-synchronisedScroll",
48+
replaceValue: `diff2htmlUi.synchronisedScroll("#diff", ${config.synchronisedScroll});`
49+
},
50+
{
51+
searchValue: "//diff2html-highlightCode",
52+
replaceValue: config.highlightCode ? `diff2htmlUi.highlightCode("#diff");` : ""
53+
},
54+
{ searchValue: "<!--diff2html-diff-->", replaceValue: diffHTMLContent }
55+
].reduce(
56+
(previousValue, replacement) =>
57+
utils.replaceExactly(previousValue, replacement.searchValue, replacement.replaceValue),
58+
template
59+
);
4260
}
4361

4462
/**

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ export function writeFile(filePath: string, content: string): void {
3131
export function execute(cmd: string): string {
3232
return childProcess.execSync(cmd).toString("utf8");
3333
}
34+
35+
export function replaceExactly(value: string, searchValue: string, replaceValue: string): string {
36+
return value.replace(searchValue, () => replaceValue);
37+
}

0 commit comments

Comments
 (0)