-
Notifications
You must be signed in to change notification settings - Fork 10
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
possibility to add line numbers to code blocks #101
Comments
What about the new block |
IMO, we should have a single switch in nimib that all the official |
Yes I think one way to implement a global switch for all code like blocks would be to make sure that |
notes from call nimib speaking hours: proc addLineNumbers(doc: var NbDoc, blk: var NbBlock) =
if blk.context["enableLineNumbers].castBoolor doc.context["enableLineNumbers].castBool:
blk.context["codeHighlighted"] = addLineNumbersToHIghlightedCode(blk.context["codeHighlighted"].castStr)
template enableLineNumbersDoc =
nb.context["enableLineNumber"] = true
template enableLineNumbersBlock =
nb.blk.context["enableLineNumber"] = true
assert addLineNumbersToHIghlightedCode("""
func</span> decode(secret: openArray[<span class="hljs-built_in">int</span>]): <span class="hljs-built_in">string</span> =
<span class="hljs-comment">## classified by NSA as <strong>TOP SECRET</strong></span>
<span class="hljs-keyword">for</span> c <span class="hljs-keyword">in</span> secret:
<span class="hljs-literal">result</span>.add <span class="hljs-built_in">char</span>(c)
""") == """
<span class="hljs-comment">1</span> func</span> decode(secret: openArray[<span class="hljs-built_in">int</span>]): <span class="hljs-built_in">string</span> =
<span class="hljs-comment">2</span> <span class="hljs-comment">## classified by NSA as <strong>TOP SECRET</strong></span>
<span class="hljs-comment">3</span> <span class="hljs-keyword">for</span> c <span class="hljs-keyword">in</span> secret:
<span class="hljs-comment">4</span> <span class="hljs-literal">result</span>.add <span class="hljs-built_in">char</span>(c)
""" |
I noticed that the example you gave to me has one issue. If someone tries to select the code, he would also select the line numbers. HTML<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Renderer</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<button id="copyButton">Copy to Clipboard</button>
<pre id="code"><code>// Here is your code snippet
function helloWorld() {
console.log('Hello, world!');
}</code></pre>
<script src="script.js"></script>
</body>
</html> JSdocument.addEventListener("DOMContentLoaded", function() {
const codeElement = document.getElementById('code');
const copyButton = document.getElementById('copyButton');
const codeText = codeElement.innerText;
const lineCount = codeText.split('\n').length;
let lineNumberHTML = '';
for (let i = 1; i <= lineCount; i++) {
lineNumberHTML += i + '\n';
}
const lineNumberElement = document.createElement('code');
lineNumberElement.className = 'line-numbers';
lineNumberElement.innerText = lineNumberHTML;
codeElement.insertBefore(lineNumberElement, codeElement.firstChild);
copyButton.addEventListener('click', function() {
navigator.clipboard.writeText(codeText).then(function() {
console.log('Code copied to clipboard');
}).catch(function(err) {
console.error('Could not copy text: ', err);
});
});
}); CSSbody {
font-family: 'Courier New', Courier, monospace;
}
pre {
position: relative;
border: 1px solid #ccc;
padding-left: 30px;
}
code {
display: block;
white-space: pre;
}
.line-numbers {
position: absolute;
left: 0;
top: 0;
padding: 0 10px;
user-select: none; /* Prevent text selection */
color: #888;
} It encloses the code inside |
Hello, template nbCode*(body: untyped) =
newNbCodeBlock("nbCode", body):
addLineNumbers(nb, nb.blk)
captureStdout(nb.blk.output):
body This does absolutely nothing. Would you have any hint on how to proceed? |
We have something we call Line 48 in e256687
A renderPlan consists of a list of renderProc s that are run in order. So that would be the correct place to add this feature.
I think the reason your example doesn't work is because the Let me know if you want a more thorough explaination, but it might take some time. I'm quite busy at the moment 😄 |
To flesh out my previous answer, you could say nimib has 3 phases when it generates a document:
So the problem you had was that you tried to add the line numbers in step 2 while the highlighted code wasn't available until step 3. But if you add it as a renderProc and add it to the renderPlan och nbCode it should be available to you. |
Thank you for the explanations. I was able to move forward and debug the code. There are still discussions to have on the I wish you all the best for your work. |
Awesome, thanks 😄 regarding the copy-ability of the code blocks without including the line numbers, is wrapping the code in a table the usual solution to this? If so I'd say we could try it out and see how it works out 👍 |
If I believe the above JS generated by ChatGPT4 (thus highly not reliable), it's first approach is a div container. <div class="code-container">
<button id="copyButton" class="copy-button">Copy</button>
<pre id="code"><code class="line-numbers">1
2
3
4</code><code>// Here is your code snippet
function helloWorld() {
console.log('Hello, world!');
}</code></pre>
</div> I am unsure of what is the “usual” method here. It could be interpreted as table values as well as element of a div. I'll give in a second post a table solution. |
from a google search, it appears there are alternative approaches that do not even need to add numbers in text but automatically generate them through css, it seems something we could explore if it works for us too: https://stackoverflow.com/a/41354764 |
Hello, Both approaches, CSS and HTML should be tested, as some CSS styling might turn off line numbers. |
we were discussing about this with @HugoGranstrom during speaking hours, it is not clear to us how we can help? do you plan to work on the CSS one and we should discard the HTML one? to you want a review on the PR for the HTML approach (#220) and see if we can merge that? Let us know and thanks for working on this! |
This css has an interesting recipe based on simple css and html manipulation: https://css.winterveil.net/ |
we should have the ability to add line numbers to code blocks, both those generate by
nbCode
andnbFile
. Ideally it should be something customizable by single block and one could set a global option. defaults would probably be to have it disabled in code blocks produce bynbCode
and maybe enabled for blocks produce bynbFile
.The text was updated successfully, but these errors were encountered: