Skip to content

Commit

Permalink
Del: Js regex highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Jan 14, 2017
1 parent 63834c0 commit fb7fc34
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/lib/highlight.es6
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ export default function highlight(str, lang)
str = str.replace(val.re, '___' + key + '___$1___end' + key + '___');
});

var lvls = [];
var levels = [];

str = str.replace(/___(?!subtmpl)\w+?___/g, function($0)
{
var end = $0.substr(3, 3) === 'end',
tag = (!end? $0.substr(3) : $0.substr(6)).replace(/_/g,''),
lastTag = lvls.length > 0 ? lvls[lvls.length - 1] : null;
tag = (!end ? $0.substr(3) : $0.substr(6)).replace(/_/g,''),
lastTag = levels.length > 0 ? levels[levels.length - 1] : null;

if(!end && (lastTag == null || tag == lastTag || (lastTag != null && lang[lastTag] && lang[lastTag].embed != undefined && lang[lastTag].embed.indexOf(tag) > -1)))
{
lvls.push(tag);
levels.push(tag);

return $0;
} else if(end && tag == lastTag)
{
lvls.pop();
levels.pop();

return $0;
}
Expand Down Expand Up @@ -87,7 +87,6 @@ language.js = {
comment: {re: /(\/\/.*|\/\*([\s\S]*?)\*\/)/g, style: 'comment'},
string: {re: /(('.*?')|(".*?"))/g, style: 'string'},
numbers: {re: /(\-?(\d+|\d+\.\d+|\.\d+))/g, style: 'number'},
regex: {re: /([^\/]\/[^\/].+\/(g|i|m)*)/g, style: 'number'},
keywords: {re: /(?:\b)(function|for|foreach|while|if|else|elseif|switch|break|as|return|this|class|self|default|var|false|true|null|undefined)(?:\b)/gi, style: 'keyword'},
operators: {re: /(\+|\-|\/|\*|%|=|<|>|\||\?|\.)/g, style: 'operators'}
};
Expand Down
11 changes: 11 additions & 0 deletions test/sources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var tool = eruda.get('sources'),
$tool = $('.eruda-sources');

describe('highlight code', function ()
{
it('js', function ()
{
tool.set('js', '/* test */');
expect($tool.find('.eruda-content')).toContainHtml('<span style="color:#63a35c;">/* test */</span>');
});
});

0 comments on commit fb7fc34

Please sign in to comment.