rtfpessoa / diff2html-cli

Pretty diff to html javascript cli (diff2html-cli)
https://diff2html.xyz
MIT License
524 stars 50 forks source link

Cannot make to work API from imported library #114

Closed fraballi closed 3 years ago

fraballi commented 3 years ago

I'm using:

var diff2html = require('diff2html');
...
        const parsed = diff2html.parse(changes);
        const html = diff2html.html(parsed, diff2html.defaultDiff2HtmlConfig);

Note: Diff content is already stored in variable: changes.

But cannot export html with diff content, only obtained empty template such as:

<div class="d2h-file-list-wrapper">
    <div class="d2h-file-list-header">
        <span class="d2h-file-list-title">Files changed (0)</span>
        <a class="d2h-file-switch d2h-hide">hide</a>
        <a class="d2h-file-switch d2h-show">show</a>
    </div>
    <ol class="d2h-file-list">

    </ol>
</div><div class="d2h-wrapper">

</div>

Also, tried docs example (with a .diff file exported) without success: diff2html -i file -- my-file-diff.diff

Kindly, can point to any direction?

rtfpessoa commented 3 years ago

Can you provide and example of what is your changes content? If you want to just inject something in the dom you can use Diff2HtmlUI directly

Keep in mind that this repository is just the CLI, if you want to use the parser and generator directly you should check Diff2Html

fraballi commented 3 years ago

I need both: parser and htmlUI, for sending an email with a Diff2Html file attached (zipped html) but also replacing message body content with html output.

Tried this also without success: package.json

dependencies:{
"git-diff": "^2.0.6",
"diff2html": "^3.2.0"
}

index.js

   const gitDiff = require('git-diff');
   const diff2html = require('diff2html');

   // Git-Diff Basic Example
   const oldStr = 'fred\nis\nfunny\n'
   const newStr = 'paul\nis\nfunny\n'
   const diff2 = gitDiff(oldStr, newStr, { forceFake: true })

  // diff2:   '-fred\n+paul\n is\n funny\n'

   const parsed = diff2html.parse(diff2);   // [], length = 0

  // Nothing to pass to:  
   const html = diff2html.html(parsed);
rtfpessoa commented 3 years ago

diff2 is not a valid unified diff (which is a subset of git diff) Check https://www.gnu.org/software/diffutils/manual/html_node/Example-Unified.html#Example-Unified for more details.

fraballi commented 3 years ago

Here is an example, checking postman collections. Could only put a fragment, due to privacy issues. ;-) Should this format work, also using git-diff npm? Kindly, can suggest any npm superset library, for comparing text instead of files as: git diff does?

@@ -1,4 +1,4 @@
-{"status":true,"collection":{"item":[{
...

Same output for:

const parsed = diff2html.parse(diff2);   // [], length = 0
rtfpessoa commented 3 years ago

That still seems invalid. The minimum you need is the names of the old file and the new file prefixed by --- and +++ respectively, then you have the hunks that start with the line numbers @@ and then the changed lines prefixed with `,-,+`.

You are at least missing the file names.

fraballi commented 3 years ago

So, the solution is aimed to files (git diff) instead of text-only. Then, could add dummy section to the top:

--- dummy1  2002-02-21 23:30:39.942229878 -0800
+++ dummy2  2002-02-21 23:30:50.442260588 -0800
@@ -1,4 +1,4 @@
-{"status":true,"collection":{"item":[{
...

Which time format is: 2002-02-21 23:30:50.442260588 -0800? Perhaps:

Format Example
yyyy-MM-dd HH:mm:ss.SSSZZZZ 2017-03-12 13:11:34.222-0700

From: Timestamps, Time Zones, Time Ranges, and Date Formats

rtfpessoa commented 3 years ago

So, the solution is aimed to files (git diff) instead of text-only.

As explained in the readme this tool supports parsing the unified diff format which git diff builds over.

The data format is described here although I don't think it is used for anything.

fraballi commented 3 years ago

Many thanks, already accomplished all the objectives. Made a test project and compared git diff output with the article on GNU diff command (dates weren't there).

Next, using git-diff superset library with a prepended mocked header as:

index 0001fb1..c00002e 000003
--- a/collection.json
+++ b/collection.json
@@ -1,4 +1,4 @@
-{"status":true,"collection":{"item":[{

Appreciate all the help and resources posted. Great project by the way ;-)

rtfpessoa commented 3 years ago

Glad you got it working