Skip to content
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

Add line numbers to code blocks (use existing script) #410

Open
SmartManoj opened this issue Jun 26, 2019 · 4 comments
Open

Add line numbers to code blocks (use existing script) #410

SmartManoj opened this issue Jun 26, 2019 · 4 comments

Comments

@SmartManoj
Copy link
Contributor

Not maintained 5 years old,
https://greasyfork.org/en/scripts/1781-so-code-line-nums

Updated one
https://raw.githubusercontent.com/SmartManoj/SmartUserScripts/master/SO_Lines.user.js

@shu8
Copy link
Member

shu8 commented Jun 26, 2019

I'd be happy to accept a pull request for this, if you want to include it in SOX :)

@shu8 shu8 changed the title Merge SO - Code line nums Add line numbers to code blocks (use existing script) Jun 26, 2019
@SmartManoj
Copy link
Contributor Author

SmartManoj commented Sep 16, 2020

Any reason for the removal of that label? @mezmi

@double-beep
Copy link
Contributor

double-beep commented May 29, 2021

Does this look good? Partly inspired from Stacks.

[...document.querySelectorAll('.s-prose pre')].forEach(codeBlock => {
    codeBlock.classList.add('s-code-block'); // add the stacks class in case it doesn't already exist
    // the last item of the array is just a newline and not an actual line of code
    const lineNumber = codeBlock.innerText.split(/\r?\n/).length - 1;
    // index is zero-based, so we need to increase by 1
    const lineDivs = [...Array(lineNumber).keys()].map(number => `<div>${number + 1}</div>`).join('');
    codeBlock.insertAdjacentHTML('afterbegin', `<code class="s-code-block--line-numbers">${lineDivs}</code>`);
});

@shu8
Copy link
Member

shu8 commented May 29, 2021

@double-beep that looks perfect! would you mind making a quick PR to add that in as a feature? :) /cc @SmartManoj!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants