admin管理员组文章数量:1201372
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.
I'd like to preserve the line numbers too.
Thank you very much for the help!
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.
I'd like to preserve the line numbers too.
Thank you very much for the help!
Share Improve this question edited Sep 13, 2020 at 17:11 James Moore 9,0266 gold badges76 silver badges93 bronze badges asked Jun 20, 2020 at 22:24 Somya AgrawalSomya Agrawal 3751 gold badge3 silver badges15 bronze badges4 Answers
Reset to default 20Update: 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}
>
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
- Original issue: https://github.com/react-syntax-highlighter/react-syntax-highlighter/issues/44
- Pull Request: https://github.com/react-syntax-highlighter/react-syntax-highlighter/issues/46
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
I'm using v15 and these properties worked for me.
<SyntaxHighlighter
wrapLines
wrapLongLines
>
{code}
</SyntaxHighlighter>
本文标签: javascriptEnabling line wrap with ReactSyntaxHighlighterStack Overflow
版权声明:本文标题:javascript - Enabling line wrap with React-Syntax-Highlighter? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738554499a2098016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论