Skip to content

Preserve new lines (\n) within a <code> block #645

Closed

Description

@reggi

How do I preserve new lines (\n) within a <code> block?

var marked = require('marked')
var Promise = require('bluebird')
var markedAsync = Promise.promisify(marked)

var markdown = [
  '```js',
  'if (true) {',
  '  console.log(\'word spaces\')',
  '}',
  '',
  '',
  '',
  '```'
].join('\n')

console.log(markdown)
markedAsync(markdown)
.then(console.log)

And the output:

```js
if (true) {
  console.log('word spaces')
}



```
<pre><code class="lang-js">if (true) {
  console.log(&#39;word spaces&#39;)
}
</code></pre>

However if there are spaces then a character, the empty lines are available.

var markdown = [
  '```js',
  'if (true) {',
  '  console.log(\'word spaces\')',
  '}',
  '',
  '',
  '',
  '```'
].join('\n')

console.log(markdown)
markedAsync(markdown)
.then(console.log)

Here's the output

```js
if (true) {
  console.log('word spaces')
}


x
```
<pre><code class="lang-js">if (true) {
  console.log(&#39;word spaces&#39;)
}


x
</code></pre>

Activity

  1. chrisdrackett commented on Sep 2, 2015

    @chrisdrackett

    I'm not sure if this is the same problem, but I'm also not getting new lines when using a code block with gym on:

    input:

    input

    output:

    <div> <span>test</span> </div>

    expected output:

    <div>
        <span>test</span>
    </div>
    
  2. ChiChou commented on Dec 8, 2016

    @ChiChou
  3. joshbruce commented on Dec 25, 2017

    @joshbruce
    Member
  4. Feder1co5oave commented on Jan 14, 2018

    @Feder1co5oave
    Contributor

    Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions