Emphasis/strong emphasis corner cases

Fletcher Penney pointed out to me that the current reference implementations give unintuitive results in a few cases.

The first kind of case is an unexpected symmetry violation:


Intuitively, the second case should yield


The second kind of case involves unnatural groupings:

**a b*b*b c**
<p><em><em>a b</em>b</em>b c**</p>

Here you’d intuitively expect:

**a b*b*b c**
<p><strong>a b<em>b</em>b c</strong></p>

There are two problems here. One is that the current declarative spec for emph/strong doesn’t say quite enough to determine the interpretations of the reference implementations in every case. The other is that the algorithm used by these implementations is giving fairly counterintuitive results, at least in these cases.

In the newemph5 branch of jgm/cmark I have made a small tweak to the parsing algorithm that gives much better results in these cases. The change is this: when considering matches between an interior delimiter run (one that can open and can close) and another delimiter run, we require that the sum of the lengths of the two delimiter runs is not a multiple of 3.

Thus, for example, in

1 23 4

delimiter 1 cannot match 2, since the sum of the lengths of the first delimiter run (1) and the second (1,2) == 3. Thus we get <em>a**b</em> instead of <em>a</em><em>b</em>.

This gives better behavior on things like


which previously got parsed as


and now would be parsed as


With this change we get four spec test failures, but in each case the output seems more “intuitive”:

Example 386 (lines 6490-6494) Emphasis and strong emphasis

--- expected HTML
+++ actual HTML
@@ -1 +1 @@

Example 389 (lines 6518-6522) Emphasis and strong emphasis

--- expected HTML
+++ actual HTML
@@ -1 +1 @@

Example 401 (lines 6620-6624) Emphasis and strong emphasis

--- expected HTML
+++ actual HTML
@@ -1 +1 @@

Example 442 (lines 6944-6948) Emphasis and strong emphasis

--- expected HTML
+++ actual HTML
@@ -1 +1 @@

So I’d like to propose making this change to the parsing algorithm, and adjusting the declarative spec accordingly (as well as perhaps adding some language to rule out ambiguities). Comments welcome.

1 Like

That looks indeed a lot more intuitive (though I have never feel the need to use nested emphasis), so good if it makes into the specs

I have implemented this change in the dev version of the spec, cmark, and commonmark.js.


Great, I have replicated the changes to markdig as well.

FWIW, i thought things like the following were a bug, until i skimmed the complex [emphasis and strong emphasis] rules:
Hello *Super-*man

Since it appears to not be a bug (or at least it’s spec-compliant behavior), i added it to a PR as a suggestion for improving the spec:

P.S. But please do let me know if it’s a bug, then i’d revert the commit, & log a bug.

Perhaps some ZWS-requiring things like that could be simplified Beyond MD as well… E.g., catching such stuff in auto transformation from HTML is tricky, to say the least.

1 Like