Comments (8)
I'm working on an eleventy based starter and I was able to get line numbers working with these two steps:
- Enable
alwaysWrapLineHighlights
in the eleventy syntax highlighting plugin options - Add CSS
Step 1
// .eleventy.js
// ...
eleventyConfig.addPlugin(pluginSyntaxHighlight, {
alwaysWrapLineHighlights: true,
});
Step 2
/* styles.css */
pre {
counter-reset: lineNumber;
}
code .highlight-line:before {
-webkit-user-select: none;
border-right: 1px solid #404040;
color: #858585;
content: counter(lineNumber);
counter-increment: lineNumber;
display: inline-block;
font-variant-numeric: tabular-nums;
margin-right: 1.2em;
padding-right: 1.2em;
text-align: right;
width: 2.4em;
}
Screenshot
Your mileage may vary, remove any styles you don't need. Hope this helps someone! ✌️
from eleventy-plugin-syntaxhighlight.
Wondering the same thing. Is there some way to utilize https://prismjs.com/plugins/line-numbers/ currently?
from eleventy-plugin-syntaxhighlight.
@joshbuchea, FYI, I'm seeing issues with this approach. I've documented them here: falldowngoboone/falldowngoboone-com#116.
from eleventy-plugin-syntaxhighlight.
I'd love to use line numbers as well, any word on this? Let me know if I can help in any way.
from eleventy-plugin-syntaxhighlight.
Just to add a little extra sugar on top of @joshbuchea contribution for cases when you need horizontal scrolling. (Although this might break when you force line wrapping).
code .highlight-line:before {
position: sticky; /*added*/
left: 0; /*added*/
background: var(--code-bg); /*added*/
user-select: none;
border-right: 1px solid hsla(0, 0%, 0%, 0.2);
color: hsla(0, 100%, 100%, 0.6);
content: counter(lineNumber);
counter-increment: lineNumber;
display: inline-block;
font-variant-numeric: tabular-nums;
margin-right: 1.2rem;
text-align: center;
width: 3em;
}
from eleventy-plugin-syntaxhighlight.
After a bit of testing this, I can assume it's not a stable solution. For example, in PHP code you will have a lot of troubles:
- The
<?php
at the beginning is not wrapped insidehighlight-line
and you have to "whitelist" it along withcode .highlight-line:before
, that's fine. - The PHPDocs are... just weird. The whole comment is wrapped inside
.token.comment
, the opening tag (/**
) and closing tag (*/
) are not wrapped inside.highlight-line
but the other lines are, generating an undesired result:
Adding .token.comment
to code .highlight-line:before
does not solve: the style is messed up, but above all the closing tag is not even counted:
I saw there's a PR from 2020 to implement line numbers without the messy JS code provided by PrismJS, I hope it can be considered: #24
from eleventy-plugin-syntaxhighlight.
the solution of @joshbuchea works for the most part but it's not reliable and stable. There are issues with the plugin in rendering span
as mentioned by @falldowngoboone.
from eleventy-plugin-syntaxhighlight.
I'm working on an eleventy based starter and I was able to get line numbers working with these two steps:
- Enable
alwaysWrapLineHighlights
in the eleventy syntax highlighting plugin options- Add CSS
Step 1
// .eleventy.js // ... eleventyConfig.addPlugin(pluginSyntaxHighlight, { alwaysWrapLineHighlights: true, });Step 2
/* styles.css */ pre { counter-reset: lineNumber; } code .highlight-line:before { -webkit-user-select: none; border-right: 1px solid #404040; color: #858585; content: counter(lineNumber); counter-increment: lineNumber; display: inline-block; font-variant-numeric: tabular-nums; margin-right: 1.2em; padding-right: 1.2em; text-align: right; width: 2.4em; }Screenshot
Your mileage may vary, remove any styles you don't need. Hope this helps someone! ✌️
Maybe this will help someone, as I've spend quite a bit of time trying to figure out how to enable showing line numbers only for specific code blocks, not site-wide (so without "step 1" that sets the alwaysWrapLineHighlights
to true
).
I went through different solutions including enabling markdown-it-attrs (which didn't work, because python {.myclass}
wrapped in a code fence - three backticks - doesn't work and myclass
is removed by this plugin) and markdown-it-container (where I wanted to wrap the whole <pre><code>
in a div with a custom class, but that also didn't work, as inside <pre><code>
I didn't have a separate class for each line of code because I didn't enable the alwaysWrapLineHighlights
option).
Turns out this issue can be fixed with a small hack - suffix the language in the code block with "/" (but don't pass any number, unless you want to highlight some code). This enables highlighting lines and highlighting lines in turn enables the line wrapping. And don't forget to add the CSS from step 2 to make this work.
This markdown:
from eleventy-plugin-syntaxhighlight.
Related Issues (20)
- Providing a default language for fenced code blocks HOT 8
- Add prismjs alias for `nunjucks` mapped to `jinja2` HOT 2
- Unsupported Languages cause Build Failures HOT 3
- Inline code support HOT 1
- Compatibility with markdown-it-attrs HOT 3
- Upgrade to use Torchlight? HOT 6
- Using the diff function, output is not escaped. HOT 3
- Cannot mix `diff` and `diff-lang` in one app HOT 1
- Feature request: config to add wrapping div HOT 3
- Syntax highlighting can fail if another plugin uses Prism too
- diff-javascript code block generates invalid HTML HOT 1
- Remove character wrap feature.
- Change default `lineSeparator` option to `\n` instead of `<br>` HOT 1
- possibility of adding plugins
- possibility of adding plugins
- possibility of adding Prismjs plugins to a config
- possibility of adding Prismjs plugins to a config
- [Discussion] Allowing users to override Prism instance
- If I'm using Ghost as a data source and it provides article content in HTML code, how can I do code highlighting?
- VS Code HTML highlight doesn't know WebC's @-prefixed properties
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 eleventy-plugin-syntaxhighlight.