markdown-to-jsx
markdown-to-jsx copied to clipboard
First line indentation ignored / whole indented string erroneously considered codeblock
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;
});
@jshado1 Something like this might be useful: https://github.com/zspecza/common-tags#stripindent
@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.