Page MenuHomePhabricator

Support backticks (`) for code samples (as in Markdown)
Open, Needs TriagePublicFeature

Description

In Markdown, text surrounded by backticks becomes monospace text. Since inline monospaced code is used very frequently in Wikipedia pages (and undoubtedly pages on other wikis), we should support similar syntax.

Specifically:

  • Text inside single backticks becomes inline monospace.
  • Text inside triple backticks becomes block monospace. Additionally, if there is a word {lang} immediately after the opening backticks, that enables syntax highlighting as if it were enclosed in <syntaxhighlight lang="{lang}"> tags.
`example text` becomes <code>example text</code>

```java
System.out.println("Hello");
```​

is treated the same as

<syntaxhighlight lang="java">
System.out.println("Hello");
</syntaxhighlight>

Event Timeline

Aklapper renamed this task from Backticks for code samples (as in Markdown) to Support backticks (`) for code samples (as in Markdown).Mar 9 2019, 9:08 AM

I think there are too many languages where a single backtick is going to cause issues. Double backticks might be feasible.

@Izno Do you have any examples offhand? I tried looking earlier, and again just now, and couldn't find anything (other than how some people insist on using backticks for opening quotes). The only thing I can think of, and can find, is the grave accent, which the backtick is descended from but semantically distinct and encoded separately from; I don't think grave accents ever appear except as diacritics on letters, but could be wrong.

Don't know about anything orthographic, but this symbol is way too common in all kinds of texts to make this change at this point. Search for insource:/`/ on a reasonably big wiki and you'll get thousands of results (despite a timeout).

@Izno Do you have any examples offhand? I tried looking earlier, and again just now, and couldn't find anything (other than how some people insist on using backticks for opening quotes). The only thing I can think of, and can find, is the grave accent, which the backtick is descended from but semantically distinct and encoded separately from; I don't think grave accents ever appear except as diacritics on letters, but could be wrong.

As qwer says, a representative search that times out on en.wp. I'm sure you can refine things if you want to see if you can dodge the timeout, and/or review on other wikis in other languages. You can decide whether those are errors. I take on faith that they're not.

OTOH the similar search for double backticks times out with many fewer results and looking at a few are clear errors that should either be removed or replaced.

Looking through some of those search results, everything (that wasn't a Template:Tree chart parameter) looked like a misuse of the backtick where a standard apostrophe or double quote (or in a few cases, stuff like an ayin) should be used instead. That said, the extant corpus consisting mostly of misuses (assuming my extremely limited sample size is actually representative of the larger corpus, especially since I only looked on a single wiki) doesn't magically make it disappear or correct editor behavior. If the implementation was limited to matching pairs of backticks on the same line, that would limit breakage, but there would still be some; if this was to be seriously considered for implementation now, it seems the direction to go would be a linter report or some equivalent.

Change #1015469 had a related patch set uploaded (by Novem Linguae; author: Novem Linguae):

[mediawiki/extensions/SyntaxHighlight_GeSHi@master] Add <sh> and <shi> parser tags

https://gerrit.wikimedia.org/r/1015469

zdev changed the subtype of this task from "Task" to "Feature Request".Mar 11 2025, 4:08 PM

As an alternative, you could add ` as a shortcut to VE to trigger <code> and triple-backquote to trigger <syntaxhighlight> without changing wikitext at all. That would be consistent with how (eg) Slack's UX handles these keys, and I believe I already added a similar shortcut-keystroke to VE at some point years ago (I don't remember exactly what, any more) and it wasn't very difficult w/r/t the VE codebase.

You would probably want to make this wiki-specific or sensitive to input encoding method to ensure we don't screw up folks using backquotes to input accents etc.

Like the idea! I think it it's important to clarify though if what is meant is that the Parser replaces backticks with <code> or <syntaxhighlight> HTML tags, or creates "monospaced text" as is mentioned in the main task which would be more like: <span style="font-family: monospaced;"> so there'll be no background, kind of like the enwiki template {{mono}}