2

Synopsis: In ReactJS, I have a string of HTML code that I've formatted using js-beautify and styled using react-syntax-highlighter:

import SyntaxHighlighter from "react-syntax-highlighter";
import { a11yDark } from "react-syntax-highlighter/dist/esm/styles/hljs";
let beautify = require("js-beautify").html;

export default function App() {
  const beautifyHTML = () => {
    const code =
      "<div><h1>Small Piece of Text.</h1><div><h2>AReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongPieceOfText.</h2></div><h3>AnotherReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongPieceOfText.</h3></div>";
    const beautifiedHTML = beautify(code, {
      indent_size: 2,
      brace_style: "expand"
    });
    return beautifiedHTML;
  };

  return (
    <div className="App">
      <SyntaxHighlighter language="html" style={a11yDark}>
        {beautifyHTML()}
      </SyntaxHighlighter>
    </div>
  );
}

On the UI, this is what I get: enter image description here

Here's the Code Sandbox to see it all in action and for you to edit.

Problem: What I would like to have happen is two things:

  1. Wrap the line at the width of the div, so that the horizontal scroll does not show up.
  2. When the line wraps to the next line, the overflowing text should not start at the beginning of the line, but right below where the original line started.

To try wrapping the text (Problem 1), I tried adding the following as suggested in this post, but it seems to only work when the code is wrapped in a pre tag, which I do not have here...

.App code {
  white-space: pre-wrap;
  word-wrap: break-word;
}
penguin
  • 143
  • 3
  • 11
  • [There's a CSS value for it](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Text/Wrapping_Text#breaking_long_words). I'm not sure about question 2. – code May 17 '22 at 01:05
  • Yup, I edited my post to say that I tried that and it did not work... – penguin May 17 '22 at 01:08
  • The wrapping part is easy, starting indented on the new line gives me a headache xD – Zach Jensz May 17 '22 at 01:11
  • 1
    @TheAmateurCoder I used VSCode as my IDE and Code Sandbox to demo. Both have the same results when I try to wrap the text using that last bit of code in my post. – penguin May 17 '22 at 01:17
  • 1
    [This is my hack](https://codesandbox.io/s/mystifying-haslett-ynozee) so far... only works for one set indentation – Zach Jensz May 17 '22 at 01:33
  • 1
    https://stackoverflow.com/questions/62492403/enabling-line-wrap-with-react-syntax-highlighter might also help – Zach Jensz May 17 '22 at 01:36
  • Also, do these help?: https://stackoverflow.com/questions/36284453/react-native-text-going-off-my-screen-refusing-to-wrap-what-to-do, https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap and https://developer.mozilla.org/en-US/docs/Web/CSS/max-width – The Amateur Coder May 17 '22 at 01:40
  • @TheAmateurCoder Thanks for these links. I'm not able to figure out how to do problem 2 still. – penguin May 20 '22 at 16:09
  • @Zach Jensz Thanks for these links. I'm not able to figure out how to do problem 2 still. – penguin May 20 '22 at 16:09

0 Answers0