node.js support? #7

Closed
tipiirai opened this Issue Mar 27, 2012 · 5 comments

Comments

Projects
None yet
4 participants

Looks like a fine and compact syntax package.

Any plans to support node.js so that you can do syntax hilite on the server too?

Also makes testing more easy.

Keep up the good, minimal work!

Any plans for this?

Owner

ccampbell commented Apr 28, 2012

I'm not sure exactly what is required to add node support. I am thinking it should just work because you can call:

Rainbow.color('var foo = true;', 'javascript', function(highlighted_code) {
    console.log(highlighted_code);
});

And that should not require access to the DOM.

I don't have a node environment set up at the moment, but I can look into it at some point.

philc commented Apr 30, 2012

@ccampbell it mostly does work today, although a few painful workarounds are required. These changes would make Rainbow would be more friendly in Node:

  • instead of window["Rainbow"] = ..., use simply Rainbow = ... (they're equivalent in a browser).
  • don't assume the presence of window, in particular when running window.addEventListener. Don't add event listeners if window doesn't exist.
  • Provide a synchronous API (see #43).
  • If you had a version of this project packaged using the commonJS module format, that would also be nice, but the three fixes above would allow most people interesting in using Rainbow on the server to get up-and-running with much less confusion.

For reference, this is what you'll need to do to run Rainbow using d8 (the V8 debug console):

window = this;
window.attachEvent = function() {};
window.setTimeout = function(block, timeout) { return block(); };

load("./rainbow.js");
load("./language/generic.js");
load("./language/javascript.js");

Rainbow.color('var foo = true;', 'javascript', function(highlighted_code) { print(highlighted_code); });

And this is what you'll need to do to run it using node:

window = this;
window.attachEvent = function() {};
window.setTimeout = function(block, timeout) { return block(); };

require("./rainbow.js");
require("./language/generic.js");
require("./language/javascript.js");

Rainbow.color('var foo = true;', 'javascript', function(highlighted_code) { console.log(highlighted_code); });

nopnop commented Jul 20, 2012

I suggest this pull request to review :
#79

bjb568 referenced this issue Jun 17, 2015

Closed

publish to npm #184

Owner

ccampbell commented Jul 2, 2016

Support for node.js and Rainbow.colorSync was added in version 2.0 which finally was released.

npm install rainbow-code
var rainbow = require('rainbow-code');
var highlightedCode = rainbow.colorSync('var finally = true;', 'javascript');

ccampbell closed this Jul 2, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment