PanDiff

repository·master·Indexed 18 days ago

https://github.com/davidar/pandiff

A tool for generating prose-based diffs for any document format supported by Pandoc. Unlike standard line-based diffs, PanDiff respects document structure and can output results in formats such as CriticMarkup, HTML, PDF, and Microsoft Word with Track Changes enabled. It can be used as a CLI tool, integrated as a Git difftool, or run via Docker and CI/CD pipelines like GitHub Actions and GitLab CI.

Tokens
3.2K
Snippets
15
Records
17
Agent score
63%

What's inside pandiff

  1. Run PanDiff via Docker

    master

    You can run PanDiff in a containerized environment by mounting your local documents directory as a volume. This avoids the need to install Pandoc and npm locally.

    docker run -v ~/documents/my-md-documents:/data davidar/pandiff -o diff.pdf old.md new.md
  2. Integrate PanDiff with Git

    master

    You can configure Git to use PanDiff as a difftool. This allows you to use git pandiff to view prose diffs instead of the standard line-based diff.

    git config --global difftool.pandiff.cmd 'pandiff "$LOCAL" "$REMOTE"'
    git config --global alias.pandiff 'difftool -t pandiff -y'
  3. How pandiff handles different output formats

    master

    Pandiff applies specific 'critic' styles depending on the requested output format to ensure the diff is visually represented correctly in the target medium:

    • LaTeX: Uses \color{Maroon} for deletions, \color{OliveGreen} for insertions, and \color{RedOrange} for substitutions.
    • DOCX: Uses standard Word Track Changes styling (via HTML tags that Pandoc converts to track changes).
    • HTML: Uses <span class="del"> and <span class="ins"> tags, often wrapped in a github-markdown-css styled container if standalone is enabled.
    • Markdown: Uses custom syntax like {--deletion--}, {++insertion++}, and {~~subst1~>subst2~~}.
  4. Use the pandiff CLI to compare files

    master

    The pandiff CLI tool allows you to compare files and generate a diff. It supports three primary modes of operation based on the number and type of files provided:

    1. Compare two files: Provide two files as positional arguments to generate a diff between them. pandiff FILE1 FILE2 [OPTIONS]

    2. Track changes in a Word document: Provide a single .docx file to extract track changes. pandiff FILE.docx [OPTIONS]

    3. Normalise a Markdown file: Provide a single .md file to normalise its content. pandiff FILE.md [OPTIONS]

    Output is written to stdout.

    # Compare two files
    pandiff file1.md file2.md
    
    # Track changes in a docx file
    pandiff document.docx
    
    # Normalise a markdown file
    pandiff input.md
  5. Use PanDiff in GitLab CI

    master

    To use PanDiff in GitLab CI, use the davidar/pandiff image and set the entrypoint to /bin/sh to allow running the pandiff command directly.

    stages:
      - diff
    
    compile_markdown_to_pdf:
      stage: compile
      image:
        name: davidar/pandiff
        entrypoint: ["/bin/sh", "-c"]
    
      script:
        - pandiff -o diff.pdf old.md new.md
      artifacts:
        paths:
          - diff.pdf
  6. Generate Word (.docx) with Track Changes

    master

    PanDiff can output a .docx file that utilizes Microsoft Word's native 'Track Changes' feature. This is useful for collaborative editing.

    # Diffing two files into a docx
    pandiff old.md new.md -o diff.docx
    
    # Processing a single file (e.g. to convert existing docx to track changes format)
    pandiff test/track_changes_move.docx
  7. Use PanDiff in GitHub Actions

    master

    You can use the davidar/pandiff Docker image within a GitHub Actions workflow to automatically generate PDF diffs when Markdown files are pushed.

    name: Markdown Diff
    
    on:
      push:
        paths:
          - '**.md'
      workflow_dispatch:
    
    jobs:
      compile-markdown-to-pdf:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout repository
            uses: actions/checkout@v4
    
          - name: Run pandiff
            run: |
              docker run --rm \
                -v ${{ github.workspace }}:/work \
                -w /work \
                davidar/pandiff \
                pandiff -o diff.pdf old.md new.md
    
          - name: Upload artifact
            uses: actions/upload-artifact@v4
            with:
              name: diff-pdf
              path: diff.pdf
  8. Configure pandiff options

    master

    When calling pandiff, you can pass an Options object to control the Pandoc conversion and the diff rendering process.

    OptionTypeDescription
    tostringTarget format (e.g., 'html', 'latex', 'docx', 'markdown').
    outputstringPath to the output file. If provided, the function returns null after writing.
    metadata'old' | 'new' | 'none'Whether to include the YAML metadata block from the old file, the new file, or neither.
    thresholdnumberA similarity threshold (0-1). If the similarity is below this, the process logs an error and returns null.
    standalonebooleanIf true, produces a standalone document (e.g., with full HTML headers/footers).
    columnsnumberNumber of columns for word wrapping (used when wrap is not 'none').
    wrap'auto' | 'none' | 'preserve'Controls text wrapping behavior.
    bibliographystring[]List of bibliography files.
    filterstring[]List of Pandoc filters to apply.
    lua-filterstring[]List of Lua filters to apply.
    resource-pathstringPath to search for resources.
    pdf-enginestringThe engine to use for PDF generation.
    reference-docstringA reference document for DOCX output.