Skip to content

Commit

Permalink
Merge branch 'complete-hook' of https://github.com/manfer/prism
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Aug 13, 2015
2 parents be57365 + c89bbdb commit e58b6c0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion plugins/line-highlight/prism-line-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function applyHash() {

var fakeTimer = 0; // Hack to limit the number of times applyHash() runs

Prism.hooks.add('after-highlight', function(env) {
Prism.hooks.add('complete', function(env) {
var pre = env.element.parentNode;
var lines = pre && pre.getAttribute('data-line');

Expand Down
7 changes: 6 additions & 1 deletion plugins/line-numbers/prism-line-numbers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Prism.hooks.add('after-highlight', function (env) {
Prism.hooks.add('complete', function (env) {
// works only for <code> wrapped inside <pre> (not inline)
var pre = env.element.parentNode;
var clsReg = /\s*\bline-numbers\b\s*/;
Expand All @@ -10,6 +10,11 @@ Prism.hooks.add('after-highlight', function (env) {
return;
}

if ( env.element.querySelector(".line-numbers-rows") ) {
// Abort if line numbers already exists
return;
}

if (clsReg.test(env.element.className)) {
// Remove the class "line-numbers" from the <code>
env.element.className = env.element.className.replace(clsReg, '');
Expand Down
12 changes: 8 additions & 4 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ var _ = _self.Prism = {
parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
}

if (!grammar) {
return;
}

var code = element.textContent;

if(!code) {
Expand All @@ -196,6 +192,11 @@ var _ = _self.Prism = {
code: code
};

if (!grammar) {
_.hooks.run('complete', env);
return;
}

_.hooks.run('before-highlight', env);

if (async && _self.Worker) {
Expand Down Expand Up @@ -228,6 +229,9 @@ var _ = _self.Prism = {

_.hooks.run('after-highlight', env);
}

_.hooks.run('complete', env);

},

highlight: function (text, grammar, language) {
Expand Down

0 comments on commit e58b6c0

Please sign in to comment.