Skip to content

JSON formatting is broken #10

@cowboy

Description

@cowboy

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.

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