Document Line Numbers Snippet
See original GitHub issueHey there!
Would be cool to add a small stub to the Readme to document adding line numbers either via either of the following methods:
- the CodeSandbox demo -
src/basic.js
- something like the code below - @kitten: “I have another snippet somewhere that has line numbers which I can upload later”
Would the prism-react-renderer
team be open to this?
CodeSandbox version
import React from 'react'
import styled from 'styled-components'
import Highlight, { defaultProps } from 'prism-react-renderer'
import theme from 'prism-react-renderer/themes/nightOwl'
const exampleCode = `
(function someDemo() {
var test = "Hello World!";
console.log(test);
})();
return () => <App />;
`.trim()
const LineNo = styled.span`
display: inline-block;
width: 2em;
user-select: none;
opacity: 0.3;
`
const Pre = styled.pre`
text-align: left;
margin: 1em 0;
padding: 0.5em;
& .token-line {
line-height: 1.3em;
height: 1.3em;
}
`
const Basic = () => (
<Highlight {...defaultProps} theme={theme} code={exampleCode} language="jsx">
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<Pre className={className} style={style}>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
<LineNo>{i + 1}</LineNo>
{line.map((token, key) => <span {...getTokenProps({ token, key })} />)}
</div>
))}
</Pre>
)}
</Highlight>
)
export default Basic
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Formatting code with line numbers - microsoft word - Super User
Formatting code with line numbers · Go to "insert" tab, click "object" button (it's on the right) · Choose "openDocument Text": it will...
Read more >Add or remove line numbers - Microsoft Support
Click Select in the Editing group on the Home tab, and then click Select All. Or press CTRL+A.
Read more >Adding Line Numbers in MS Word to Specific Sections of Text
To apply line numbers to a specific section, insert section breaks in the document. Place the cursor at the beginning sentence/word of text...
Read more >Code Snippets — Display Line Numbers - Elegant
reStructuredText and Markdown have directives that generate line numbers for code blocks. Use them to display line numbers in code snippets.
Read more >5.7 Line numbers for code blocks (*) | R Markdown Cookbook
5.7 Line numbers for code blocks (*) ... For revealjs's revealjs_presentation output format (El Hattab and Allaire 2017), you may also need to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I tried @karlhorky’s solution and encounter a problem. It looks like the last element of
tokens
is an empty line that looks like this:If you don’t add line numbering, this last line is not visible. However, the addition of the
<LineNo/>
React element causes this line to always be visible, leading to one extra empty line always showing up in every code block. My solution was to add a conditional check for for this emptyline
and also remove the styling for.token-line
inside thePre
styled-component, so here’s how my solution looks (without the dynamic line number offset):I didn’t add it here but it’s also helpful to change the
LineNo
todisplay: inline-flex; justify-content: flex-end; padding-right: 0.5rem;
to make line numbers right-align, which looks better instead of the jagged right edge with the original formatting.Working on a PR now.