-
-
Notifications
You must be signed in to change notification settings - Fork 382
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
Line numbers #3
Comments
It's possible, just need to add that to the HTML renderer. You can do it too if you have time: const shiki = require('shiki')
shiki.getHighlighter({
theme: 'nord'
}).then(highlighter => {
const tokens = highlighter.codeToThemedTokens(`console.log('shiki');`, 'js')
const html = shiki.renderToHtml(tokens) // default renderer, replace with yours
}) |
As an alternative to writing a custom code {
counter-reset: step;
counter-increment: step 0;
}
code .line::before {
content: counter(step);
counter-increment: step;
width: 1rem;
margin-right: 1.5rem;
display: inline-block;
text-align: right;
color: rgba(115,138,148,.4)
} |
Building further upon @alexpeattie's code: using Custom Properties you can set a different start number. code {
counter-reset: step;
counter-increment: step calc(var(--start, 1) - 1);
} <code style="--start: 13;">…</code> If no Demo: https://codepen.io/bramus/pen/mdmmdQB (You might want to opt for a more unique name for the Custom Property) |
Given that line numbers is pretty much a normal expectation of syntax highlighting and that this issue has been open for three and a half years now, could one of the above solutions be built in? |
I'd also love to see code diff highlighting, so for example showing added and removed lines of code (with highlighting for whatever lang). |
prefer the style of shiki, but compared with other similar plugins, such as Prism and highlight, they all have the line number function, hope shiki can also support it |
Consider the solution #3 (comment) is quite solid and working well with the existing ecosystem, I think we could consider this is resolved. Otherwise, the newly introduced transformers API should allow this to be implemented on user land. |
#3 (comment) looks good. While with astro, I found there is always a trailing Not sure if it's an issue of Shiki or astro. In index.mdx file -
```rust
let x = 42;
Ok(x)
``` Rendered: <pre class="astro-code github-light" style="background-color:#fff;color:#24292e;overflow-x:auto" tabindex="0"><code><span class="line"><span style="color:#D73A49">let</span><span style="color:#24292E"> x </span><span style="color:#D73A49">=</span><span style="color:#005CC5"> 42</span><span style="color:#24292E">;</span></span>
<span class="line"><span style="color:#6F42C1">Ok</span><span style="color:#24292E">(x)</span></span>
<span class="line"></span></code></pre> Result: |
Same dangling line here. You can filter it out: code > .line:not(:last-child)::before {
/* styling */
} |
@antfu Would love to see this solution on the Shiki docs. |
PR welcome :) |
You can also checkout https://expressive-code.com/ which uses Shiki and has a line number option |
Extending on @alexpeattie's answer, if you're looking to get rid of the last line that this code adds, feel free to use the following code. code .line:last-child::before {
content: 'none';
} |
@seancheung another way of getting rid of that extra line: code .line:last-child:empty::before {
content: none;
counter-increment: none;
} |
You my day :-) Thank you. |
Regarding the dangling new line when used in Astro: You can remove it like so in your import { defineConfig } from 'astro/config'
// https://astro.build/config
export default defineConfig({
markdown: {
syntaxHighlight: 'shiki',
shikiConfig: {
transformers: [
{
preprocess(code) {
if (code.endsWith('\n')) {
code = code.slice(0, -1)
}
return code
},
},
],
},
},
}) |
@antfu do you have any reservations on how the transformer for this should be implemented? |
Would it be possible to add support for line numbers?
Bonus points for not restricting you to auto-generate them either. For example, when showing a Git diff, the line numbers will change.
The text was updated successfully, but these errors were encountered: