Skip to content

Commit

Permalink
Check for possible pre-existing marker strings in Handlebars
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitgeist87 committed May 13, 2017
1 parent 36bc560 commit 7a1a404
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
17 changes: 13 additions & 4 deletions components/prism-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@

env.backupCode = env.code;
env.code = env.code.replace(handlebars_pattern, function(match) {
env.tokenStack.push(match);
var i = env.tokenStack.length;
// Check for existing strings
while (env.backupCode.indexOf('___HANDLEBARS' + i + '___') !== -1)
++i;

return '___HANDLEBARS' + env.tokenStack.length + '___';
// Create a sparse array
env.tokenStack[i] = match;

return '___HANDLEBARS' + i + '___';
});
});

Expand All @@ -72,9 +78,12 @@
return;
}

for (var i = 0, t; t = env.tokenStack[i]; i++) {
for (var i = 0, keys = Object.keys(env.tokenStack); i < keys.length; ++i) {
var k = keys[i];
var t = env.tokenStack[k];

// The replace prevents $$, $&, $`, $', $n, $nn from being interpreted as special patterns
env.highlightedCode = env.highlightedCode.replace('___HANDLEBARS' + (i + 1) + '___', Prism.highlight(t, env.grammar, 'handlebars').replace(/\$/g, '$$$$'));
env.highlightedCode = env.highlightedCode.replace('___HANDLEBARS' + k + '___', Prism.highlight(t, env.grammar, 'handlebars').replace(/\$/g, '$$$$'));
}

env.element.innerHTML = env.highlightedCode;
Expand Down
2 changes: 1 addition & 1 deletion components/prism-handlebars.min.js

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

3 changes: 2 additions & 1 deletion tests/languages/handlebars/handlebars_in_markup_feature.js

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

0 comments on commit 7a1a404

Please sign in to comment.