[Tex/LaTex] How to display JavaScript and HTML code in latex

codeline-breaking

I am trying to insert sections of HTML and JavaScript code in my latex document in overleaf but the lstlisting does not work as my code spans longer than the page width. I have tried various different suggestions of how to do this from forums but none of them work. Some of the suggestions work but the code into large boxes which does not look right. Manually separating the code is not an option as it does not look right when displayed. Any suggestions of how to get this to work and look a presentable would be greatly appreciated.

            {reviews_as_buyer.length > 0 && 
            <div>
                <p><span className='text-bold'>As buyer:</span> {avg_rating_as_buyer.toFixed(2)} ({reviews_as_buyer.length} reviews)</p>
                {reviews_as_buyer.map((r, i) => <div style={{paddingBottom: '2rem'}} key={i}>
                    <ReactStars size={25} edit={false} value={r.rating} />
                    <p style={{fontSize: '1rem'}}>By: <a href={`/reviews/${r.by}`}>{r.by}</a>
                    <br />
                    {r.text}</p>
                </div>)}
                <hr />
            </div>

Best Answer

Overleaf have a tutorial for code listing properly, like in this post.

Like in the example:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\usepackage{xcolor}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\lstset{style=mystyle}

\begin{document}
The next code will be directly imported from a file

\lstinputlisting[language=Octave]{BitXorMatrix.m}
\end{document}

enter image description here

Also I recommend this post to enable easy copy code.