question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add line breaks for newlines?

See original GitHub issue

Is there an option to add line breaks for newlines when the user presses “enter” in the markdown? I tried converting all newlines \n to <br> but it seems to break parsing in other places.

marked has a breaks option to allow this: https://github.com/markedjs/marked/

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:11
  • Comments:6

github_iconTop GitHub Comments

2reactions
AJamesPhillipscommented, Mar 31, 2021

I’d toyed with just adding a trailing double space which also works. For now I am passing the content through the following function before passing to markdown-to-jsx:


const regexp_list_line = /^[ \t]*(\*|\d+\.)/gm
const regexp_text_line = /^[ \t]*[^\s]+/gm
export function add_newlines_to_markdown (text: string): string
{
    const new_lines: string[] = []

    const lines = text.split("\n")
    lines.forEach((line, i) =>
    {
        if (i > 0)
        {
            const previous_line = lines[i - 1]
            if (previous_line.match(regexp_text_line) && !previous_line.match(regexp_list_line))
            {
                if (line.match(regexp_list_line))
                {
                    new_lines[i - 1] = previous_line + "\n"
                }
                else if (line.match(regexp_text_line))
                {
                    new_lines[i - 1] = previous_line + "<br>"
                }
            }
        }
        new_lines.push(line)
    })

    const new_text = new_lines.join("\n")

    return new_text
}



function run_tests ()
{
    test(add_newlines_to_markdown(`test 1
test2`), `test 1<br>
test2`)

    const bulleted_items = `* Test bullet 1
* Test bullet 2`
    test(add_newlines_to_markdown(bulleted_items), bulleted_items)



    const input_text = `Test 1
Test
  Test 3${"  "}
\tTest 4
* Test bullet 1
* Test bullet 2

Test 5
1. Test number 1
2. Test number 2

`

    const prepared_text = add_newlines_to_markdown(input_text)
    const expected_text = `Test 1<br>
Test<br>
  Test 3  <br>
\tTest 4\n
* Test bullet 1
* Test bullet 2

Test 5\n
1. Test number 1
2. Test number 2

`

    test(prepared_text, expected_text)
}

run_tests()

So that this text:

Test 1
Test
  Test 3  // <- note two trailing spaces
	Test 4
* Test bullet 1
* Test bullet 2

Renders as:

Test 1 Test Test 3
Test 4

  • Test bullet 1
  • Test bullet 2

Instead of as:

Test 1 Test Test 3 Test 4 * Test bullet 1 * Test bullet 2

1reaction
joebnbcommented, Oct 12, 2021

I’d toyed with just adding a trailing double space which also works. For now I am passing the content through the following function before passing to markdown-to-jsx:

const regexp_list_line = /^[ \t]*(\*|\d+\.)/gm
const regexp_text_line = /^[ \t]*[^\s]+/gm
export function add_newlines_to_markdown (text: string): string
{
    const new_lines: string[] = []

    const lines = text.split("\n")
    lines.forEach((line, i) =>
    {
        if (i > 0)
        {
            const previous_line = lines[i - 1]
            if (previous_line.match(regexp_text_line) && !previous_line.match(regexp_list_line))
            {
                if (line.match(regexp_list_line))
                {
                    new_lines[i - 1] = previous_line + "\n"
                }
                else if (line.match(regexp_text_line))
                {
                    new_lines[i - 1] = previous_line + "<br>"
                }
            }
        }
        new_lines.push(line)
    })

    const new_text = new_lines.join("\n")

    return new_text
}



function run_tests ()
{
    test(add_newlines_to_markdown(`test 1
test2`), `test 1<br>
test2`)

    const bulleted_items = `* Test bullet 1
* Test bullet 2`
    test(add_newlines_to_markdown(bulleted_items), bulleted_items)



    const input_text = `Test 1
Test
  Test 3${"  "}
\tTest 4
* Test bullet 1
* Test bullet 2

Test 5
1. Test number 1
2. Test number 2

`

    const prepared_text = add_newlines_to_markdown(input_text)
    const expected_text = `Test 1<br>
Test<br>
  Test 3  <br>
\tTest 4\n
* Test bullet 1
* Test bullet 2

Test 5\n
1. Test number 1
2. Test number 2

`

    test(prepared_text, expected_text)
}

run_tests()

So that this text:

Test 1
Test
  Test 3  // <- note two trailing spaces
	Test 4
* Test bullet 1
* Test bullet 2

Renders as: Test 1 Test Test 3 Test 4

  • Test bullet 1
  • Test bullet 2

Instead of as: Test 1 Test Test 3 Test 4 * Test bullet 1 * Test bullet 2

in codeblock not as expected


const regexp_list_line = /^[ \t]*(\*|\d+\.)/gm;
const regexp_text_line = /^[ \t]*[^\s]+/gm;
const regexp_code_line = /^[ \t]*(\`{3})/gm;
let isInCodeBlock = false;

export function add_newlines_to_markdown(text: string): string {
    const new_lines: string[] = [];

    const lines = text.split('\n');
    lines.forEach((line, i) => {
        if (i > 0) {
            const previous_line = lines[i - 1];
            if (regexp_code_line.test(line)) isInCodeBlock = !isInCodeBlock;
            if (previous_line.match(regexp_text_line) && !previous_line.match(regexp_list_line) && !isInCodeBlock) {
                if (line.match(regexp_list_line)) {
                    new_lines[i - 1] = previous_line + '\n';
                } else if (line.match(regexp_text_line) && isInCodeBlock) {
                    new_lines[i - 1] = previous_line + '<br>';
                }
            }
        }
        new_lines.push(line);
    });

    const new_text = new_lines.join('\n');

    return new_text;
}

code block situation

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add line breaks - Line tools - Unit conversion
Add line breaks where there are none by following these steps. See the example following. 1. Enter or paste information into the window...
Read more >
Add or move line breaks - adjust the line length online
Online tool for adding line breaks to change the length of lines with many setting options - fast and flexible.
Read more >
The Dos and Don'ts of Adding an HTML Line Break
To do a line break in HTML, use the <br> tag. Simply place the tag wherever you want to force a line break....
Read more >
Handle line breaks (newlines) in Python - nkmk note
This article describes how to handle strings including line breaks (line feeds, new lines) in Python. Create a string containing line breaks.
Read more >
Line breaks and blank spaces - Overleaf, Online LaTeX Editor
The most standard way how to break lines is to create a new paragraph. This is done by leaving an empty line in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found