Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Each line of content wrapped in a div

I'm having trouble troubleshooting an issue on provemath where the content is not rendering as desired.

The problem is that each line (except for the first) in written content is wrapped in a <div>. For example, the content

line1.
line2.

as you can see through the Safari web inspector is actually rendered as <p>line1.<div>line2.</div></p>. The DESIRED output is <p>line1.<br>line2.</p>.

There are a few things that could be coming into play here...

  1. I'm not sure how the browser takes in typed newlines.
  2. My content rendering function (included below) runs Markdown (actually, marked) and some regexs.
  3. CSS (included below) manipulates how some things are displayed, and I'm wondering if it can cause changes in the HTML itself. I previously thought the use of flexbox was causing this, but I changed the CSS to no avail.

Content rendering happens as follows:

Content is typed by user (hitting RETURN for a new line) and captured with jQuery's .html function

blind.value = $value.html()

to get the content, and then processed with

function render_content(string) {
    // make all \ into \\ instead, so that they will be \ again when marked is done. This is for MathJax postrender compatability.
    string = string.replace(/\\/g, '\\\\')
    string = string.replace(/\n/g, '<br />') // not doing anything AFAIK
    string = marked(string)

    string = string.replace(/img(\d+)/g, '<img src="image/$1.jpg" />')
    string = string.replace(/\\includegraphics\{(.*?)\}/g, '<img src="image/$1.jpg" />')

    return string
}

The CSS is as follows:

    .value {
        // display: inline-flex;
        display: inline;
        // flex-direction: column;
        vertical-align: top;
        width: 85%;
    }

you can see the old CSS commented out.

like image 892
mareoraft Avatar asked Aug 15 '17 22:08

mareoraft


People also ask

How do I wrap content inside a div?

If you've faced the situation when you need to wrap words in a <div>, you can use the white-space property with the "pre-wrap" value to preserve whitespace by the browser and wrap the text when necessary and on line breaks. Also, you'll need the word-wrap property.

How do I wrap text in a box CSS?

You have to set 'display:inline-block' and 'height:auto' to wrap the content within the border. Show activity on this post. Two ways are there. No need to mention height in this it will be auto by default.

How do I stop text wrapping in CSS?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).


2 Answers

Why are you processing 'backslash' before 'newline'? Have you tried this?

string = string.replace(/\n/g, '<br />')
string = string.replace(/\\/g, '\\\\')
string = marked(string)
like image 142
kjonach Avatar answered Nov 10 '22 11:11

kjonach


try wrapping both lines in a div, then using css grid to render them as you like. Here is a link documenting the css grid.

like image 27
Ronny Dark Team Avatar answered Nov 10 '22 11:11

Ronny Dark Team