Comments (7)
As an alternative to writing a custom renderToHtml
function: since v0.2.3 each line is wrapped in a <span class='line'>
which makes it quite easy to do line numbering using CSS counters. I added some CSS like this:
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)
}
from shiki.
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).
from shiki.
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 --start
is set, it will fall back to start numbering at 1.
Demo: https://codepen.io/bramus/pen/mdmmdQB
(You might want to opt for a more unique name for the Custom Property)
from shiki.
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
})
from shiki.
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?
from shiki.
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
from shiki.
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.
from shiki.
Related Issues (20)
- Hope to add support for ArkTS(*.ets) HOT 1
- [BUG] Bug with vitepress integration HOT 5
- Spellcheck styles are visible in the docs HOT 1
- @shikijs/monaco how to use editor default theme? HOT 15
- Languages not included when using a custom theme HOT 3
- Support for non-default shikijs themes is broken HOT 1
- `Vue SFC`: add a different highlight color to vue components in Upper Camel Case HOT 1
- [Bug] `Notation` transfomers remove empty annotated lines HOT 5
- Cannot load WASM with Cloudflare Worker HOT 1
- Shell script comment wrong color HOT 9
- Decorations: Allow intersections HOT 2
- [QUESTION] How to use shiki and twoslash with vitepress in vue component HOT 1
- Monaco Editor: Normalize Hex code HOT 1
- [BUG] // ^? unable to take effect
- ansi highlighting not working with theme dark-plus HOT 2
- Comparison with Prism.js and Highlight.js
- docs: Update Bundle Size
- [Question] Usage for colorReplacements option HOT 2
- Support inline code/lang? HOT 1
- Cannot work with hono + Cloudflareworkers HOT 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shiki.