How to get the total number of lines with SyntaxHighlighter

61 Views Asked by At

Is it possible to have the total number of lines of a sourcecode using SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/) please ?

I could use the technique defined here : How to get the number of lines in a textarea? But perhaps SyntaxHighlighter can do it more easily.

Thank you.

1

There are 1 best solutions below

0
On

I don't believe there's a built-in solution, but here's a function that should do the trick. It uses the getElementsByClassName method so I don't think it works on IE8 or below. Use your favorite DOM query library if you wish.

/**
 * Returns the number of lines in a SyntaxHighlighter code block.
 *
 * @param {Element} node The top-level DOM element containing the code block.
 * @return {Number} The number of code lines, or 0 if not found.
 */
function getLineCount(node) {
    var codeNode;
    var containerNode;

    if (node && typeof node.getElementsByClassName === 'function') {
        codeNode = node.getElementsByClassName('code');

        if (codeNode.length) {              
            containerNode = codeNode[0].getElementsByClassName('container');

            if (containerNode.length) {
                return containerNode[0].children.length;
            }
        }
    }

    return 0;
}

jQuery version, because apparently that's a thing.

function getLineCount(node) {
    return $(node).find('.code .container').children().length;
}