Skip to content

Commit

Permalink
Merge pull request #658 from donejs/line-highlight
Browse files Browse the repository at this point in the history
Move line highlighting into last document ready callback
  • Loading branch information
daffl committed May 2, 2016
2 parents 611b48f + acf3570 commit 852918d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
70 changes: 34 additions & 36 deletions docs/theme/static/js/line-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,48 @@ var getLines = function (lineString) {
};

module.exports = function() {
$(function() {
$('span[line-highlight]').each(function(i, el){
var $el = $(el);
var lines = getLines($el.attr('line-highlight'));
var codeBlock = $el.parent().prev('pre').children('code');
$('span[line-highlight]').each(function(i, el){
var $el = $(el);
var lines = getLines($el.attr('line-highlight'));
var codeBlock = $el.parent().prev('pre').children('code');

var lineMap = { 0: [] };
var k = 0;
var lineMap = { 0: [] };
var k = 0;

codeBlock.children().each(function(i, el){
var nodeText = $(el).text();
codeBlock.children().each(function(i, el){
var nodeText = $(el).text();

if (/\n/gm.test(nodeText)) {
var cNames = $(el).attr('class');
var str = nodeText.split('\n');
var l = str.length
if (/\n/gm.test(nodeText)) {
var cNames = $(el).attr('class');
var str = nodeText.split('\n');
var l = str.length

for (var j = 0; j < l; j++) {
var text = j === (l - 1) ? str[j] : str[j] + '\n';
var newNode = document.createElement('span');
newNode.className = cNames;
$(newNode).text(text);
lineMap[k].push(newNode);

if(j !== (l - 1)) {
k++;
lineMap[k] = [];
}
for (var j = 0; j < l; j++) {
var text = j === (l - 1) ? str[j] : str[j] + '\n';
var newNode = document.createElement('span');
newNode.className = cNames;
$(newNode).text(text);
lineMap[k].push(newNode);

if(j !== (l - 1)) {
k++;
lineMap[k] = [];
}
} else {
lineMap[k].push(el);
}
});
} else {
lineMap[k].push(el);
}
});

codeBlock.empty();
codeBlock.empty();

for (var key in lineMap) {
if (lineMap.hasOwnProperty(key)) {
var newNode = document.createElement('span');
newNode.className = lines[key] ? 'line highlight': 'line' ;
$(newNode).append(lineMap[key]);
codeBlock.append(newNode);
}
for (var key in lineMap) {
if (lineMap.hasOwnProperty(key)) {
var newNode = document.createElement('span');
newNode.className = lines[key] ? 'line highlight': 'line' ;
$(newNode).append(lineMap[key]);
codeBlock.append(newNode);
}
});
}
});
}
3 changes: 2 additions & 1 deletion docs/theme/static/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ steal(
}
}
prettyPrint();
Highlighter();

new ContentList(".contents");
new FrameHelper(".docs");
Expand Down Expand Up @@ -563,6 +562,8 @@ steal(
}
});
}

Highlighter();
})


Expand Down

0 comments on commit 852918d

Please sign in to comment.