markdown-to-jsx icon indicating copy to clipboard operation
markdown-to-jsx copied to clipboard

First line indentation ignored / whole indented string erroneously considered codeblock

Open JakobJingleheimer opened this issue 4 years ago • 2 comments

Hiya,

I'm using JSON6 and have some strings of text containing markdown like this:

{
    description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

    ## Ut porttitor leo a diam sollicitudin tempor id eu nisl

    Sem nulla pharetra diam sit amet nisl. Tortor id aliquet lectus proin. Egestas tellus rutrum tellus pellentesque eu tincidunt tortor aliquam. Tellus elementum sagittis vitae et.",
}

I read your "gotcha" note about codeblocks, and tried adding an empty line at the start

{
    description: "
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

      ## Ut porttitor leo a diam sollicitudin tempor id eu nisl

      Sem nulla pharetra diam sit amet nisl. Tortor id aliquet lectus proin. Egestas tellus rutrum tellus pellentesque eu tincidunt tortor aliquam. Tellus elementum sagittis vitae et.
    ",
}

but the text was still rendered as a codeblock.

Fortunately, I don't and likely won't need to support code blocks like this in the data, so as a workaround, I added a reviver to strip out the leading whitespace characters:

JSON6.parse(str, (key, val) => {
  if (typeof val === 'string') return val.replace(/\n(\t| )+/g, '\n');

  return val;
});

JakobJingleheimer avatar Dec 13 '20 20:12 JakobJingleheimer

@jshado1 Something like this might be useful: https://github.com/zspecza/common-tags#stripindent

quantizor avatar Jan 09 '21 17:01 quantizor

@probablyup thanks. The reviver is working fine as a workaround (no need to add a whole library for something to small). Just seemed like a bug in this library since the gotcha seemed to indicate my change should have fixed the problem.

JakobJingleheimer avatar Jan 09 '21 18:01 JakobJingleheimer