Search code examples
markdown

Bold does not display correctly


Is there a rule of Markdown syntax that does not allow some punctuation as the last character in a bolded string? For example, this input

No punctuation **A**C, with colon **A:**C, with dash **A-**C, with space **A **B, using undercores __A:__C

Gives this output
No punctuation AC, with colon **A:**C, with dash A-C, with space **A **B, using undercores __A:__C

When the last character is not punctuation then the text is bolded. If the last character is punctuation then I see the Markdown formatting characters in the output.

Working input

**A:** C

Gives
A: C

But I don't really want the space before the 'C'.

(I'm using MarkdownIt but I notice that whatever the flavour of Markdown used by StackOverflow works the same way)

Is there some way to workaround this behaviour (maybe something zero width could go between the bolding characters and the following text instead of the thinspace?)


Solution

  • In short, the closing token needs to be followed by whitespace or a punctuation character.

    Commonmark implementations are much more complex than the original Markdown rules. It would appears that you have run into one of those complications. As the spec states:

    First, some definitions. A delimiter run is either a sequence of one or more * characters that is not preceded or followed by a non-backslash-escaped * character, or a sequence of one or more _ characters that is not preceded or followed by a non-backslash-escaped _ character.

    A left-flanking delimiter run is a delimiter run that is (1) not followed by Unicode whitespace, and either (2a) not followed by a Unicode punctuation character, or (2b) followed by a Unicode punctuation character and preceded by Unicode whitespace or a Unicode punctuation character. For purposes of this definition, the beginning and the end of the line count as Unicode whitespace.

    A right-flanking delimiter run is a delimiter run that is (1) not preceded by Unicode whitespace, and either (2a) not preceded by a Unicode punctuation character, or (2b) preceded by a Unicode punctuation character and followed by Unicode whitespace or a Unicode punctuation character. For purposes of this definition, the beginning and the end of the line count as Unicode whitespace.

    ...

    The following rules define emphasis and strong emphasis:

    1. A single * character can open emphasis iff (if and only if) it is part of a left-flanking delimiter run.

    2. A single _ character can open emphasis iff it is part of a left-flanking delimiter run and either (a) not part of a right-flanking delimiter run or (b) part of a right-flanking delimiter run preceded by a Unicode punctuation character.

    3. A single * character can close emphasis iff it is part of a right-flanking delimiter run.

    4. A single _ character can close emphasis iff it is part of a right-flanking delimiter run and either (a) not part of a left-flanking delimiter run or (b) part of a left-flanking delimiter run followed by a Unicode punctuation character.

    5. A double ** can open strong emphasis iff it is part of a left-flanking delimiter run.

    6. A double __ can open strong emphasis iff it is part of a left-flanking delimiter run and either (a) not part of a right-flanking delimiter run or (b) part of a right-flanking delimiter run preceded by a Unicode punctuation character.

    7. A double ** can close strong emphasis iff it is part of a right-flanking delimiter run.

    8. A double __ can close strong emphasis iff it is part of a right-flanking delimiter run and either (a) not part of a left-flanking delimiter run or (b) part of a left-flanking delimiter run followed by a Unicode punctuation character.

    9. Emphasis begins with a delimiter that can open emphasis and ends with a delimiter that can close emphasis, and that uses the same character (_ or *) as the opening delimiter. The opening and closing delimiters must belong to separate delimiter runs. If one of the delimiters can both open and close emphasis, then the sum of the lengths of the delimiter runs containing the opening and closing delimiters must not be a multiple of 3 unless both lengths are multiples of 3.

    10. Strong emphasis begins with a delimiter that can open strong emphasis and ends with a delimiter that can close strong emphasis, and that uses the same character (_ or *) as the opening delimiter. The opening and closing delimiters must belong to separate delimiter runs. If one of the delimiters can both open and close strong emphasis, then the sum of the lengths of the delimiter runs containing the opening and closing delimiters must not be a multiple of 3 unless both lengths are multiples of 3.

    11. A literal * character cannot occur at the beginning or end of *-delimited emphasis or **-delimited strong emphasis, unless it is backslash-escaped.

    12. A literal _ character cannot occur at the beginning or end of _-delimited emphasis or __-delimited strong emphasis, unless it is backslash-escaped.