Spec issue with annotations and underscores

Here are the conditions to reproduce:

  • Use _ or __
    • Finish the underscored text with ), followed by 1+ white space characters _) (in this case, no closing . is necessary)
    • Add a . right after the closing underscores, _).` _(in this case, you can have any amount of whitespace before the closing underscore)

Failing to convert:

  • _) _
  • __) __
  • _)_.
  • __)__.
  • _) _.

Succeeding:

  • _)_
  • __)__

In my opinion * and _ should behave identically at all times (both single and doubles). It’s really strange that these corner cases behave one way using underscores and another using asterisks. Particularly when the rules for the parsing bug seem so arbitrary.

[See dingus][1]

[1]: http://spec.commonmark.org/dingus/?text=Failing%20to%20convert%3A -%20`_)%20_%0A-%20%60__)%20__%60%0A-%20%60_).%60%0A-%20%60__)__.%60%0A-%20%60)%20%20%20%20%20%20_.%60%0A%0AIn%20markdown%3A%0A%0A_)%20_%0A__)%20__%0A_).%0A__)__.%0A)%20%20%20%20%20%20_.%0A%0ASucceeding%3A%0A%0A-%20%60_)%60%0A-%20%60__)__%60%0A%0AIn%20markdown%3A%0A%0A)_%0A__)__%0A

This behavior can be explained in terms of these rules:

  1. A single * character can close emphasis iff it is part of a right-flanking delimiter run.
  2. A single _ character can close emphasis iff it is part of a right-flanking delimiter run and not part of a left-flanking delimiter run.

When you have _(hai)_ by itself, the second _ is right- but not left-flanking.

However, when you have _(hai)_., the second _ is now both left- and right-flanking.

_ and * behave differently because we want to disallow the use of _ for intraword emphasis.

What your examples show is that these rules don’t quite capture that. We could perhaps make the rule a bit more complicated (though I liked its simplicity):