developit / snarkdown

:smirk_cat: A snarky 1kb Markdown parser written in JavaScript
http://jsfiddle.net/developit/828w6t1x/
MIT License
2.28k stars 110 forks source link

Nested formatting causing weird issues #77

Open Andarist opened 5 years ago

Andarist commented 5 years ago

input

***hello***

actual

<em>hello<strong><em></em></strong></em>

expected https://babelmark.github.io/?text=***hello***

<strong><em>hello</em></strong>

And one other, even weirder, case (heading being nested inside formatting from the previous line)

input

***hello***

## Heading

actual

<em>hello<strong><em><h2>Heading</h2></em></strong></em>

expected https://babelmark.github.io/?text=***hello***%0A%0A%23%23+Heading

<strong><em>hello</em></strong>

<h2>
  Heading
</h2>
fffed commented 5 years ago

If function tag could be changed to something like this

function tag(token) {
        var desc = TAGS[token.replace(/\*/g,'_')[1] || ''],
          index = context.indexOf(token),
          end = ~index && 1;
        if (!desc) return token;
        if (!desc[1]) return desc[0];

        end ? context.splice(index, 1) : context.push(token)

        return desc[end];
      }

it helps in the case.

As a result for an input ***hello*** it would be <strong><em>hello</strong></em> and browsers are smart enough to resolve such cases.