GithubHelp home page GithubHelp logo

marcosc90 / node-jsdifflib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cemerick/jsdifflib

2.0 2.0 0.0 36 KB

A javascript library for diffing text and generating corresponding HTML views

JavaScript 90.27% CSS 9.73%

node-jsdifflib's Introduction

node-jsdifflib

A diff library to compare text differences between two texts.

This is a fork of cemerick/jsdifflib which has been adapted to render the HTML on the server.

Installation

npm install node-jsdifflib

Overview

jsdifflib is a Javascript library that provides:

  1. A partial reimplementation of Python’s difflib module (specifically, the SequenceMatcher class)
  2. A visual diff view generator, that offers side-by-side as well as inline formatting of file data

API

diff(baseText, newText[, options])

options

  • baseTextName: [string] the title to be displayed above the base text listing in the diff view [default="Base Text"]
  • newTextName: [string] the title to be displayed above the new text listing in the diff view [default="New Text"]
  • contextSize: [int] the number of lines of context to show around differences; by default, all lines are shown
  • inline: [boolean] if false, a side-by-side diff view is generated (default); if true, an inline diff view is generated
  • colgroup: [object/boolean] whether to add colgroup tag to table [default = false]
    • th: [int] The width (%) of th elements (lines) [default=5]
    • td: [int] The width (%) of td elements (content) [default=45]

Style

jsdifflib comes with a ready to use CSS file (assets/jsdifflib.css) or you can use a custom one.

Example

const http = require("http");
const diff = require('node-jsdifflib');

const baseFile = "console.log(5);"
const newFile = "\"use strict\";\nconsole.log(5);";

http.createServer(function(req, res){ 
  
    if (req.url === '/favicon.ico') {
      res.writeHead(200, {'Content-Type': 'image/x-icon'} );
      return res.end();
    }

    var output = diff(baseFile, newFile);

    res.writeHead(200, {'Content-Type': 'text/html'});

    res.end(`<html>
      <head>
        <link rel="stylesheet" href="https://cdn.rawgit.com/marcosc90/node-jsdifflib/8838a6401c6933ca3faa1085bc1ec9b8174a6db8/assets/jsdifflib.css" />
      </head>
      <body>
        ${output}
      </body>
    </html>`);

}).listen(8081, function(){
  console.log("Server started at: http://localhost:8081");
});

License

BSD

node-jsdifflib's People

Contributors

cemerick avatar marcosc90 avatar mmv avatar splewako avatar yonatan avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.