7

I'm using React Syntax Highlighter to highlight code when I render a markdown file to HTML.

I see that I have to set wrapLines to true so that there's a span parent for each line. However, I'm confused on what I should be passing to lineProps to enable line wrap?

For an example, you can look at this screenshot.

enter image description here

I'd like to preserve the line numbers too.

Thank you very much for the help!

4 Answers 4

20

Update: as of react-syntax-highlighter 14.0.0, you can use the prop wrapLongLines in order to wrap the lines to the next line. See details here.

For versions before 14: Here's what worked for me – wrapping each line it's own with wrapLines prop and then adding a custom style to each line with lineProps. Hat tip to a Nitesh's previous answer. Note that showLineNumbers won't work correctly with text-wrapping in this way.

<SyntaxHighlighter
  lineProps={{style: {wordBreak: 'break-all', whiteSpace: 'pre-wrap'}}}
  wrapLines={true} 
  language="jsx" 
  style={a11yDark}
>
Sign up to request clarification or add additional context in comments.

3 Comments

Worked for me! Just a comment: for boolean props, you don't need to set ={true}, just passing it is enough: ` <SyntaxHighlighter lineProps={{style: {wordBreak: 'break-all', whiteSpace: 'pre-wrap'}}} wrapLines language="jsx" style={a11yDark} /> `
My NPM downloaded 13.5.3 version for an unknow reason, that made me so confused.
im on version 15, but the solution for <v14 worked best for me. The wrapLongLines prop broke the long lines of text in a really weird way by removing spaces and stacking the text on top of eachother
2

This was also really confusing to me.

The wrapLines is NOT about wrapping lines of code at a pre-defined length or the container width.

It's about surrounding each line in a containing DOM element ("wrapping" the line in a DOM element).

I suppose the name of the option should probably be changed.

Sources

Comments

1

As per https://www.npmjs.com/package/react-syntax-highlighter docs you can use :

lineProps - props to be passed to the span wrapping each line if wrapLines is true. Can be either an object or a function that receives current line number as argument and returns props object.

try this :

<SyntaxHighlighter
  lineProps={{style: {paddingBottom: 8}}}
  wrapLines={true}
  showLineNumbers={true}
</SyntaxHighlighter>

sandbox snippet : https://codesandbox.io/s/syntax-highlighter-demo-skhkw

Comments

0

I'm using v15 and these properties worked for me.

<SyntaxHighlighter
  wrapLines
  wrapLongLines
>
  {code}
</SyntaxHighlighter>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.