Takes JavaScript code, along with a config and returns the original code with tokens wrapped as configured.
npm install redeyedAdd color to your JavaScript!
!frog
Red Eyed Tree Frog (Agalychnis callidryas)
Takes JavaScript code, along with a config and returns the original code with tokens wrapped and/or replaced as configured.
- server side using nodejs
- in the browser
One usecase is adding metadata to your code that can then be used to apply syntax highlighting.
- copy the config.js and edit it in order to specify how
certain tokens are to be surrounded/replaced
- replace the undefined of each token you want to configure with one of the following
'before:after'
wraps the token inside before/after
{ _before: 'before', _after: 'after' }
wraps token inside before/after
#### Missing before and after resolution for {String} and {Object} config
For the {String} and {Object} configurations, 'before' or 'after' may be omitted:
- {String}:
- 'before:' (omitting 'after')
- ':after' (omitting 'before')
- {Object}:
- { _before: 'before' } (omitting '_after')
- { _after: 'after' } (omitting '_before')
In these cases the missing half is resolved as follows:
- from the parent._default (i.e., Keyword._default) if found
- otherwise from the config._default if found
- otherwise '' (empty string)
function (tokenString, info) { return {String}|{Object}; }
#### Inputs
- tokenString: the content of the token that is currently being processed
- info: an object with the following structure
``js
{
// {Int}
// the index of the token being processed inside tokens
tokenIndex
// {Array}
// all tokens that are being processed including comments
// (i.e. the result of merging esprima tokens and comments)
, tokens
// {Object}
// the abstract syntax tree of the parsed code
, ast
// {String}
// the code that was parsed (same string as the one passed to redeyed(code ..)
, code
}
`
In most cases the tokenString is all you need. The extra info object is passed in case you need to gather moretoken
information about the 's surroundings in order to decide how to transform it.
See: replace-log-example
#### Output
You can return a {String} or an {Object} from a {Function} config.
- when returning a {String}, the token value will be replaced with it
- when returning an {Object}, it should be of the following form:
`js
{
// {String}
// the string that should be substituted for the value of the current and all skipped tokens
replacement
// {Object} (Token)
// the token after which processing should continue
// all tokens in between the current one and this one inclusive will be ignored
, skipPastToken
}
`
redeyed(code, config[, opts])
Invoke redeyed with your configuration, a code snippet and maybe opts as in the below example:
`javascript
var redeyed = require('redeyed')
, config = require('./path/to/config')
, code = 'var a = 3;'
, result;
// redeyed will throw an error (caused by the esprima parser) if the code has invalid javascript
try {
result = redeyed(code, config);
console.log(result.code);
} catch(err) {
console.error(err);
}
`
opts:
`jsresult.ast
{ // {Boolean}
// if true property contains the abstract syntax tree of the coderesult.ast
// if false (default) is not assigned and therefore undefinedtrue
buildAst: true|false
// {Boolean}
// if , jsx syntax is supported, default falsetrue
// due to how esprima works, the AST is built when this option is , even ifbuildAST
// is falseresult.code
, jsx: true|false
// {Boolean}
// if true is not assigned and therefore undefinedresult.code
// if false (default) property contains the result of split.joinesprima-fb
nojoin: true|false
// {Object}
// overrides default parser and needs to be compatible with it`
parser: require('esprima')
}
return value:
`js`
{ ast
, tokens
, comments
, splits
, code
}
- ast {Array}: abstract syntax tree as returned by esprima{Array}
parse
- tokens : tokens) provided by esprima (excluding{Array}
comments)
- comments : block and line comments as provided by esprima{Array}
- splits : code pieces split up, some of which where transformed as configured{String}
- code : transformed code, same as splits.join('') unless this step has been skipped (see opts)
Ensure to include esprima as one of your dependencies
`js`
define(['redeyed'], function (redeyed) {
[ .. ]
});
The redeyed {Function} will be exposed globally as window.redeyed - big surprise!
`html`
- cardinal: Syntax highlights JavaScript code with ANSI colors to be printed to
the terminal
- peacock: JavaScript syntax highlighter that generates html that is compatible
with pygments styles.
- npm explore redeyed; npm demo will let you try the browser examplenpm explore redeyed; npm demo-log` will let you try the replace log example
-