thlorenz / cardinal

Syntax highlights JavaScript code with ANSI colors to be printed to the terminal.
MIT License
175 stars 19 forks source link

Problem targeting a custom theme #13

Open felipenmoura opened 8 years ago

felipenmoura commented 8 years ago

I'm trying to customize it, but when I simply add the theme option, it stops highlighting!

For example:

// works with default theme
cardinal.highlight(codeString,  {
    linenos: true
});

But:

// do not work!
// nothing gets lighlighted
cardinal.highlight(codeString,  {
    linenos: true,
    theme: 'default'
});

Am I doing something wrong?

My idea is to use it like this:

cardinal.highlight(codeString,  {
    linenos: true,
    theme: __dirname + '/sh-theme.js'
});

Should that work? Also, it would be interesting if we could send the theme object itself, like this:

cardinal.highlight(codeString,  {
    linenos: true,
    theme: require('./sh-theme.js')
});
adamgruber commented 7 years ago

@felipenmoura Your last example should work so long as you are passing a valid theme object.

The problem seems to be that the theme is properly resolved only when using the CLI.

Cardinal uses redeyed to do the highlighting here. Essentially, this is the call:

redeyed(code, opts.theme || theme)

It is expected that theme is an object. However, when you pass options to cardinal.highlight(), as in your second example, you are passing theme as a string and redeyed barfs.

This library should be fixed to handle theme options the same whether from the CLI or used programmatically.