endent icon indicating copy to clipboard operation
endent copied to clipboard

JSON formatting is broken

Open cowboy opened this issue 4 years ago • 0 comments

If I do this:

console.log(endent`
      hello

      \`\`\`json
      ${endent.pretty({ a: 1, b: 2, c: { foo: 3, bar: 4 } })}
      \`\`\`
      
      world
`)

Or this:

const myJson = JSON.stringify({ a: 1, b: 2, c: { foo: 3, bar: 4 } })
console.log(endent`
      hello

      \`\`\`json
      ${myJson}
      \`\`\`
      
      world
`)

I get this poorly-formatted output:

hello

    ```json
    {
"a": 1,
"b": 2,
"c": {
  "foo": 3,
  "bar": 4
}
}
    ```

    world

But I expect this:

hello

```json
{
  "a": 1,
  "b": 2,
  "c": {
    "foo": 3,
    "bar": 4
  }
}
```

world

However, if I put some text before my object:

console.log(endent`
      hello

      \`\`\`json
      REMOVE_ME${endent.pretty({ a: 1, b: 2, c: { foo: 3, bar: 4 } })}
      \`\`\`
      
      world
`)

It formats everything correctly (but then I need to remove the REMOVE_ME bit in a separate step):

hello

```json
REMOVE_ME{
  "a": 1,
  "b": 2,
  "c": {
    "foo": 3,
    "bar": 4
  }
}
```

world

I was going to file this over at https://github.com/zhouhanseng/endent but you have issues disabled there.

cowboy avatar Oct 01 '21 02:10 cowboy