Skip to content

Added event "ready" when line numbering task complete #79

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions samples/example.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.hljs-ln-highlight {
background: #FFFAE3;
width: 100%;
}

.hljs-ln-numbers:hover {
color: #2badd4;
}
111 changes: 111 additions & 0 deletions samples/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
$(function() {

function scrollToLineNumber(lineNumber) {
var selector = ".hljs-ln-line[data-line-number='" + lineNumber + "']";
var element_to_scroll_to = document.querySelector(selector);
element_to_scroll_to.scrollIntoView();
}


function extractLineNumbersFromHash(){
var str = location.hash.substring(1);
var matches = str.match(/L\d+/g); // Regex match multiple events (/g) of the regex "L" + number
if (matches.length == 0){
return {
first: null,
second: null,
};
}
else if(matches.length == 1){
return {
first: parseInt(matches[0].substring(1)),
second: null,
};
}
else{
return {
first: parseInt(matches[0].substring(1)),
second: parseInt(matches[1].substring(1)),
};
}
}


function colorLineNumbers(lineNumberBegin, lineNumberEnd){
// clean current highlighted lines first
var highlightedList = document.querySelectorAll(".hljs-ln-highlight");
var idx;
for (idx = 0; idx < highlightedList.length ; idx++) {
highlightedList[idx].classList.remove("hljs-ln-highlight");
}

if (!lineNumberBegin) return;
if (!lineNumberEnd) lineNumberEnd=lineNumberBegin;

var lineNumber;
for (lineNumber = lineNumberBegin; lineNumber < lineNumberEnd+1 ; lineNumber++) {
var selector = ".hljs-ln-code[data-line-number='" + lineNumber + "']";
var element = document.querySelector(selector);
if (element){
element.classList.add('hljs-ln-highlight');
}
}
}


if (typeof hljs != "undefined")
{
/////////////////////////////
// classic highlight js
/////////////////////////////
hljs.configure({
languages: []
})
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});




/////////////////////////////
// line numbers highlight js - activate only on class "do-hljs-ln"
/////////////////////////////
$('pre.do-hljs-ln code.hljs').each(function(i, block) {
hljs.lineNumbersBlock(block);
});


// Add multi line coloring, click, and hashchange,
// Assumes hash is similar to github format. Can be a range (#L35-L68) or single line(#L128).
var preList = document.querySelectorAll("pre.do-hljs-ln code.hljs");
var idx;
for (idx = 0; idx < preList.length ; idx++) {
preList[idx].addEventListener('ready', function(e) {

// scroll to hash
if (location.hash){
var res = extractLineNumbersFromHash();
scrollToLineNumber(res.first);
colorLineNumbers(res.first, res.second);
e.preventDefault();
}

// add event listener scroll to hash change
$(window).on('hashchange', function() {
var res = extractLineNumbersFromHash();
scrollToLineNumber(res.first);
colorLineNumbers(res.first, res.second);
e.preventDefault();
});

// add event listener scroll to click
$(".hljs-ln-n").click(function(){
var lineNumber = parseInt( $(this).attr('data-line-number'));
var new_path = window.location.pathname + window.location.search + '#L' + lineNumber;
history.replaceState(null, null, new_path); // change hash without triggering hashchange event
colorLineNumbers(lineNumber, lineNumber)
e.preventDefault();
});
}, false);
};
}
});
4 changes: 4 additions & 0 deletions src/highlightjs-line-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@

async(function () {
element.innerHTML = lineNumbersInternal(element, options);

// event line numbering task complete
const event = new Event('ready');
element.dispatchEvent(event);
});
}

Expand Down