Get difference between the two files

This commit is contained in:
friebetill 2023-01-28 18:20:40 +08:00
parent 6ff3d6cf50
commit 1318e87032
3 changed files with 38 additions and 4 deletions

View file

@ -10,11 +10,17 @@
"coverage": "vitest run --coverage",
"release": "./scripts/release.sh"
},
"keywords": ["obsidian"],
"keywords": [
"obsidian"
],
"author": "Till Friebe",
"authorUrl": "https://friebetill.github.io/",
"license": "MIT",
"dependencies": {
"diff": "^5.1.0"
},
"devDependencies": {
"@types/diff": "^5.0.2",
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",

View file

@ -1,4 +1,6 @@
import { Editor, Plugin, TFile } from "obsidian";
import { createTwoFilesPatch } from "diff";
import { SelectFileModal } from "./select_file_modal";
export default class FileDiffPlugin extends Plugin {
@ -15,12 +17,28 @@ export default class FileDiffPlugin extends Plugin {
const selectableFiles = this.app.vault.getFiles();
selectableFiles.remove(activeFile);
await this.showSelectOtherFileModal({
const fileToCompare = await this.showSelectOtherFileModal({
selectableFiles: selectableFiles,
});
if (fileToCompare == null) {
return;
}
// Install library to calculate the difference between two files
// Open new file where the differences are shown
const activeFileContent = await this.app.vault.read(activeFile);
const fileToCompareContent = await this.app.vault.read(
fileToCompare
);
const patch = createTwoFilesPatch(
activeFile.path,
fileToCompare.path,
activeFileContent,
fileToCompareContent
);
console.log(patch);
// Open viewer to show patch
},
});
}

View file

@ -140,6 +140,11 @@
dependencies:
"@types/tern" "*"
"@types/diff@^5.0.2":
version "5.0.2"
resolved "https://registry.yarnpkg.com/@types/diff/-/diff-5.0.2.tgz#dd565e0086ccf8bc6522c6ebafd8a3125c91c12b"
integrity sha512-uw8eYMIReOwstQ0QKF0sICefSy8cNO/v7gOTiIy9SbwuHyEecJUm7qlgueOO5S1udZ5I/irVydHVwMchgzbKTg==
"@types/estree@*":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.0.tgz#5fb2e536c1ae9bf35366eed879e827fa59ca41c2"
@ -266,6 +271,11 @@ debug@^4.3.4:
dependencies:
ms "2.1.2"
diff@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-5.1.0.tgz#bc52d298c5ea8df9194800224445ed43ffc87e40"
integrity sha512-D+mk+qE8VC/PAUrlAU34N+VfXev0ghe5ywmpqrawphmVZc1bEfn56uo9qpyGp1p4xpzOHkSW4ztBd6L7Xx4ACw==
dir-glob@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f"