Distinct Markdown syntaxes for code macros, verbatim and monospace in Markdown 1.2

Hi devs,

Right now the CommonMark Markdown 1.2 syntax renders the code macro, verbatim ({{{...}}}) and monospace (##...##) all as Markdown code spans or code blocks, and the Markdown parser reads those back as code macros. So after a round trip (e.g. XWiki syntax → Markdown → XWiki syntax, or a WYSIWYG edit of a Markdown page):

  • verbatim and monospace become code macros (and the formatting inside monospace is lost)
  • a code macro without language gets language="none"

I’d like to propose using a distinct syntax for each of them so that nothing is lost.

Code macro and monospace

XWiki Markdown 1.2 Parsed back as
inline code macro (language="none") `x` inline code macro (unchanged)
standalone code macro with a language ```java fenced block (no info string for none) code macro (unchanged)
code macro without language, or with other parameters {{code ...}}...{{/code}} (XWiki macro syntax, already supported in Markdown 1.2) code macro
monospace ##x## <code>x</code> (Markdown inside is still parsed, so formatting is kept) monospace
  • Backticks and fences without language keep meaning “code macro with language="none"”, since Markdown code isn’t highlighted unless a language is given (see Loading...).
  • <code> ↔ monospace is aligned with what we agreed for HTML in HTML <code>...</code> and XWiki 2.1 monospace ##...## (Loading...).
  • Using the code macro for Markdown code stays a stopgap until Loading... (a core code block).
  • Other Markdown readers (GitHub, IDEs, etc.) display backticks, fences and <code> correctly. They show {{code}}...{{/code}} as plain text, but that’s already the case for all the XWiki macros used in Markdown 1.2 pages.

Verbatim: 3 options

Example XWiki 2.1 content (inline and standalone verbatim, with characters that are special in Markdown and HTML):

a {{{**not bold** <b>x</b> [[link]]}}} b

(% class="box" %)
{{{
**not bold**
<b>x</b> & {{info}}y{{/info}}
}}}

Option A: {{verbatim}} (XWiki macro syntax, read as verbatim)

a {{verbatim}}**not bold** <b>x</b> [[link]]{{/verbatim}} b

{{verbatim class="box"}}
**not bold**
<b>x</b> & {{info}}y{{/info}}
{{/verbatim}}
  • The Markdown parser reads it directly as verbatim, not as a macro call, so no macro needs to be installed.
  • No escaping at all in the content (only a content containing an unbalanced {{/verbatim}} would need a fallback).
  • Parameters supported, and it reuses the existing macro parsing of Markdown 1.2.
  • Other Markdown readers: they show the {{verbatim}} tags as plain text, and they interpret the content as Markdown (e.g. **not bold** is displayed in bold, <b>x</b> as bold HTML). This is the same as for any XWiki macro in a Markdown 1.2 page today, so nothing really new.
  • Downside: a macro named verbatim couldn’t be called from Markdown pages.

Option B: XWiki’s own syntax, (% ... %){{{...}}}

a {{{**not bold** <b>x</b> [[link]]}}} b

(% class="box" %)
{{{
**not bold**
<b>x</b> & {{info}}y{{/info}}
}}}
  • Identical to XWiki 2.1, no escaping in the content (except for }}}).
  • Other Markdown readers: same as option A (the {{{, }}} and (% ... %) shown as plain text, content interpreted as Markdown).
  • But it adds 2 new syntaxes to Markdown 1.2 ((% ... %) doesn’t exist in it today): the literal text {{{ and (% elsewhere in a page would need to be escaped.
  • Supporting (% ... %) for all elements could be a separate proposal (it would also keep paragraph, group and table parameters, which are lost or turned into HTML today).

Option C: HTML <tt> and <pre>

a <tt>\*\*not bold\*\* &lt;b&gt;x&lt;/b&gt; \[\[link]]</tt> b

<pre class="box">
**not bold**
&lt;b&gt;x&lt;/b&gt; &amp; {{info}}y{{/info}}
</pre>
  • Other Markdown readers: they display it correctly (monospace, content shown as is). That’s the main advantage of this option.
  • But the Markdown inside <tt> is parsed (it’s inline HTML), so the content needs both Markdown escaping (\*, \[, …) and HTML escaping (&lt;, &amp;), and the content of <pre> needs HTML escaping. The source becomes hard to read and edit as soon as the content has special characters, which is precisely when verbatim is used.

I’m +1 for option A.

Impact on existing XWiki pages written in Markdown

  • The existing Markdown source of the pages isn’t modified; only the way some constructs are parsed changes.
  • Backticks and fenced code blocks: no change (still code macros with language="none" when no language is given).
  • Hand-written <code>...</code> (without attributes): it’s an HTML macro today and would become monospace. It renders almost the same; until XRENDERING-780 is done, the XHTML renderer outputs monospace as <span class="monospace">, so the exact styling depends on the skin’s CSS.
  • Verbatim:
    • Option A: no change, unless a wiki has its own macro named verbatim used in Markdown pages (it would be read as verbatim instead).
    • Option B: pages containing literal {{{ or (% ... %) text would change meaning.
    • Option C: hand-written <tt> and <pre> would become verbatim instead of HTML macros. A <pre> containing HTML markup (e.g. <pre><b>x</b></pre>) would have to stay an HTML macro to keep displaying the same.
  • Editing an existing Markdown page in the WYSIWYG editor: monospace, verbatim and code macros saved from now on keep their type. Content that was already turned into code macros by a previous edit stays as code macros (no migration).

Jira issue: Loading...

WDYT?

Thanks