Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show invisibles inside tokens #1610

Merged
merged 5 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugins/show-invisibles/prism-show-invisibles.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
.token.cr:before,
.token.lf:before,
.token.space:before {
color: inherit;
opacity: 0.4;
color: #808080;
opacity: 0.6;
position: absolute;
}

Expand Down
99 changes: 79 additions & 20 deletions plugins/show-invisibles/prism-show-invisibles.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,80 @@
(function(){

if (
typeof self !== 'undefined' && !self.Prism ||
typeof global !== 'undefined' && !global.Prism
) {
return;
}

Prism.hooks.add('before-highlight', function(env) {
var tokens = env.grammar;

if (!tokens) return;

tokens.tab = /\t/g;
tokens.crlf = /\r\n/g;
tokens.lf = /\n/g;
tokens.cr = /\r/g;
tokens.space = / /g;
});
(function () {

if (
typeof self !== 'undefined' && !self.Prism ||
typeof global !== 'undefined' && !global.Prism
) {
return;
}


var invisibles = {
'tab': /\t/,
'crlf': /\r\n/,
'lf': /\n/,
'cr': /\r/,
'space': / /
};


/**
* Handles the recursive calling of `addInvisibles` for one token.
*
* @param {Object|Array} tokens The grammar or array which contains the token.
* @param {string|number} name The name or index of the token in `tokens`.
*/
function handleToken(tokens, name) {
var value = tokens[name];

var type = Prism.util.type(value);
switch (type) {
case 'RegExp':
var inside = {};
tokens[name] = {
pattern: value,
inside: inside
};
addInvisibles(inside);
break;

case 'Array':
for (var i = 0, l = value.length; i < l; i++)
handleToken(value, i);
break;
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved

default: // 'Object'
var inside = value.inside || (value.inside = {});
addInvisibles(inside);
break;
}
}

/**
* Recursively adds patterns to match invisible characters to the given grammar (if not added already).
*
* @param {Object} grammar
*/
function addInvisibles(grammar) {
if (!grammar || grammar['tab'])
return;

// assign invisibles here to "mark" the grammar in case of self references
for (var name in invisibles) {
if (invisibles.hasOwnProperty(name))
grammar[name] = invisibles[name];
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
}

for (var name in grammar) {
if (grammar.hasOwnProperty(name) && !invisibles[name]) {
if (name === 'rest')
addInvisibles(grammar['rest']);
else
handleToken(grammar, name);
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Prism.hooks.add('before-highlight', function (env) {
addInvisibles(env.grammar);
});
})();
2 changes: 1 addition & 1 deletion plugins/show-invisibles/prism-show-invisibles.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.