Currently, Stack Exchange’s Markdown parser only allows four-space indents to represent code blocks:
// some code
// another line of code
GitHub Flavored Markdown and other Markdown implementations allow for an alternative syntax, that doesn’t require indenting each code line:
```
// some code
// another line of code
```
This is much more convenient to type out. It would be super useful if Stack Exchange could support this syntax.
By extension, this syntax also allow you to specify the source language right after the opening ```
:
```js
// some code
// another line of code
```
...which would then enable syntax highlighting for that specific language. Although it’s interesting metadata, I don’t think this feature is needed on Stack Overflow, as the syntax highlighting library works pretty well for various languages.
So, even if you would allow this syntax but ignore the ```language
, this would greatly improve my productivity on Stack Exchange.
What do you think?
The moderators are currently collecting feedback regarding this feature, so please post a comment with your thoughts. Good idea? Bad idea? Don’t really care? What do you think are the benefits/drawbacks? Experiences? Let them know! Thanks!
10 Answers 10
Code fences work now.
function DeepThought(task) {
this.task = task;
}
DeepThought.prototype.answer = function () {
switch (this.task) {
case "life, universe, and everything": return 42;
default: throw "not implemented";
}
}
As much as that's possible for a partial feature, they are compliant with CommonMark 0.28. That means:
- A code fence consists of three or more backticks
```
or tildes~~~
, indented by zero to three spaces. - The opening fence can be followed by a syntax indicator. On Stack Exchange, this can be a Prettify module name (e.g.
```lang-cxx
) or a tag for which syntax highlighting has been configured (e.g.```c++
).```none
disables syntax highlighting. - The closing code fence needs to use the same character, and at least the same number as the opening code fence.
The code block will be de-dented by as many spaces as the opening code fence was indented with. So this:
See this: ~~~ foo bar ~~~
turns into this:
See this:
foo bar
An unclosed fenced codeblock continues to the end of the block (e.g. block quote) or post.
That last point is also the reason for the only real edge case breakage that this causes with existing content. If somebody created a horizontal line in a post with something like ~~~~~~~~~~~~~~
, that is now going to open a code block. As always when we make markdown changes, the question & answer view is not going to be impacted by this unless the post is edited, and it's extremely rare, so I'm not too worried about this.
-
38Wow, cool! You folks are on fire this week.jscs– jscs01/08/2019 17:17:24Commented Jan 8, 2019 at 17:17
-
4@JoshCaswell 10th Anniversary affect.Nog Shine– Nog Shine01/08/2019 18:39:01Commented Jan 8, 2019 at 18:39
-
Hi balpha. When are we going to change help information? E.g. the "How to format" on the right side of asking a question etc.iBug says Reinstate Monica– iBug says Reinstate Monica01/09/2019 05:56:59Commented Jan 9, 2019 at 5:56
-
3I have just updated How do I format my code blocks?.Sebastian Simon– Sebastian Simon01/09/2019 14:47:08Commented Jan 9, 2019 at 14:47
-
We should also update the "indent all code with 4 spaces" error. It likely won’t trigger on triple-backtick fences, but will it trigger on
~~~
fences? Also, it should mention fenced code blocks as an alternative.Sebastian Simon– Sebastian Simon01/09/2019 15:06:56Commented Jan 9, 2019 at 15:06 -
2@Xufox Good call on the "unformatted code" detector. Fix for that is in the pipeline. As for the other thing, there are a bunch of places where indenting is recommended. Once code fences have been working for a bit, we may revisit which way of creating code blocks should be the recommended one.01/10/2019 10:45:55Commented Jan 10, 2019 at 10:45
-
11I just told someone that triple backticks don't work on main, then had to eat my words when I tried and it worked. Could you guys please feature this post or fire up a separate announcement? Considering the large previously-malformed-but-now-valid post rate, this is important information. Also thanks.Andras Deak -- Слава Україні– Andras Deak -- Слава Україні01/10/2019 18:04:58Commented Jan 10, 2019 at 18:04
-
3Why did you decide to require e.g.
lang-js
instead of justjs
like other common implementations of fenced code blocks?Mathias Bynens– Mathias Bynens01/11/2019 21:06:54Commented Jan 11, 2019 at 21:06 -
4@MathiasBynens That's how it's always been when you specify the syntax highlighting language on SO, so that's what I went with for my example. You can leave off the
lang-
, that's going to work as well.01/11/2019 21:10:13Commented Jan 11, 2019 at 21:10 -
I just noticed this accidentally—7 days after the fact. For a moment I thought I had gone crazy—"why is my GFM suddenly valid on SO!?!?"binki– binki01/16/2019 19:16:11Commented Jan 16, 2019 at 19:16
-
Another victim. (Someone trying to change working formatting to equivalent working formatting; can't be due to anything other than lack of information.)Andras Deak -- Слава Україні– Andras Deak -- Слава Україні01/17/2019 10:20:36Commented Jan 17, 2019 at 10:20
Benefits
- Compatibility with GitHub-Flavored Markdown and other Markdown libraries that support this de facto standard (and possibly its variants). This enables copy-pasting from gists or Markdown files within repositories, which can be useful on programming-related sites like Stack Overflow.
- The triple backtick syntax is easier and faster to type than indenting each line of code with four spaces.
- Saves some storage in the database, too! ;)
All the above benefits apply to the new syntax, even without the additional functionality to specify the programming language. With that functionality, I’ll add another (huge) benefit:
- As it’s much easier to specify the language of code blocks using this syntax (compared to the current syntax), I wouldn’t be surprised if this change caused more people to explicitly declare the programming language, so that the syntax highlighting can be more accurate. This is useful for answers that include, e.g. separate HTML and CSS code blocks.
Drawbacks
I honestly can’t think of any. It’s very likely no one is using the triple backtick-syntax at the moment as it doesn’t work; so I doubt it will break anything. If you don’t like the new syntax, feel free to stick to the old one.
Experiences
Ever since GitHub enabled this syntax, I’ve grown accustomed to it. I ♥ triple backtick syntax, and every time I’m forced to go back to the four-space-indent syntax, it feels awkward.
-
15159 places have a triple-backtick (I was scared to search for a double-backtick, though): data.stackexchange.com/stackoverflow/query/edit/76842 As far as I can tell, none of them would be broken by implementing this.Ry-– Ry-08/09/2012 15:20:26Commented Aug 9, 2012 at 15:20
-
3@minitech Thanks for the query. I see a few that might cause problems, but with just 159 results it's not beyond the realm of possibility to manually verify that each one is correct, or is fixed, if this were implemented.Servy– Servy08/09/2012 15:23:44Commented Aug 9, 2012 at 15:23
-
3@Servy I would even volunteer to manually verify these if it meant getting this feature!Michael Gaskill– Michael Gaskill05/21/2016 01:52:57Commented May 21, 2016 at 1:52
-
1
I don't really care either way whether this gets implemented: it's a superfluous extension to Markdown (unlike, say, inline HTML, spoiler markdown, or the as-yet-unimplemented-on-Stack-Exchange table syntax), but it is true the fenced code block has been added to pretty much every Markdown library that doesn't stick to just the original spec.
However, the triple backtick syntax is a GitHub extension, and it's not exactly the "de facto standard" Mathias Bynens portrays it as.
Fenced code blocks, as far as I understand it, were first introduced in the PHP Markdown Extra library as the following:
Fenced code blocks are like Markdown’s regular code blocks, except that they’re not indented and instead rely on a start and end fence lines to delimit the code block. The code block start with a line containing three or more tilde ~ characters, and ends with the first line with the same number of tilde ~. For instance:
This is a paragraph introducing: ~~~~~~~~~~~~~~~~~~~~~ a one-line code block ~~~~~~~~~~~~~~~~~~~~~
Other implementations followed suit, by either supporting only 3+ tildes or supporting both 3+ tildes and 3+ backticks. I ran through the implementations I know of, and Wikipedia's (incomplete) list of implementations (before GitHub went down), and found:
Supports only the 3+ tilde syntax:
Supports only the triple backtick syntax:
- ByWord
- Elements
- PEG Markdown Highlight (and derivative Qarkdown)
- R Markdown (only documents triple backtick, but based on Sundown, which supports both)
- markdown-clj
- Showdown
Supports both:
- The CommonMark specification
- Marked App
- Sundown (and derivative Redcarpet)
- GitHub (only documents the triple backtick, but parses both just fine)
- Blackfriday
- Doxygen
- Pandoc
- Parsedown
- Markdown-it
Supports neither, or is unspecified:
- mm2pdf
- peg-markdown and peg-multimarkdown
- MarkdownSharp
- libupskirt
- MarkdownJ
- MarkdownPapers
- Txtmark
- Pagedown
- Dillinger (treats triple backtick code blocks as inline code)
Based on this, I think Stack Exchange should support the tilde syntax, the triple backtick syntax, and the 3+ backtick syntax. Bonus: support of either the tilde syntax or the 3+ backtick syntax should solve the deadkey problem.
The syntax toolbar should continue to indent code with 4 spaces as per the original Markdown spec, or if that's not feasible, it should use the tilde syntax, which has the benefits of being the original version, compatible with GitHub, and marginally more supported by existing implementations.
-
4Excellent research! Good point about supporting all variants of the syntax to eliminate the dead key problem.Mathias Bynens– Mathias Bynens08/16/2012 08:09:06Commented Aug 16, 2012 at 8:09
-
5Thanks for the great research. I'm still not 100% sure we should do it, but this answer contains excellent information to base a decision on.08/17/2012 09:49:12Commented Aug 17, 2012 at 9:49
-
5«Bonus: support of either the tilde syntax or the 3+ backtick syntax should solve the deadkey problem.»: while I agree with you say, this last one is false. In my standard Portuguese keyboard, both `` ` `` and
~
are deadkeys.JMCF125– JMCF12502/16/2014 18:49:01Commented Feb 16, 2014 at 18:49 -
5Both tilde and backtick is dead keys on Swedish keyboards too.Emil Vikström– Emil Vikström04/02/2014 18:00:43Commented Apr 2, 2014 at 18:00
-
1@JMCF125: And what happens if you press the dead key twice? Yep, it acts as if it was not dead, twice.Deduplicator– Deduplicator11/03/2015 01:11:08Commented Nov 3, 2015 at 1:11
-
9@user149432, The thing is that Github is 90% of the population, so if Github supports it....Pacerier– Pacerier02/18/2016 20:22:38Commented Feb 18, 2016 at 20:22
-
5Yeah I have to agree with @Pacerier. Listing the libraries like this makes it seem like
~
and ``` are equally popular. In reality Github is huge, and not all libraries are equally popular.Timmmm– Timmmm09/25/2017 14:06:48Commented Sep 25, 2017 at 14:06 -
Doxygen supports the tilde and the backtick syntaxalbert– albert10/02/2018 08:51:44Commented Oct 2, 2018 at 8:51
-
2The deadkey problem links back to this answer. intended?lucidbrot– lucidbrot11/19/2018 13:58:10Commented Nov 19, 2018 at 13:58
-
the dead key shouldn't be too much of a problem unless the kb layout is b0rken - typing the dead key twice should result in one character. And yes, the Finnish layouts share these 2 with the Swedish ones (but we've got one layout with gazillion dead keys more as well)Antti Haapala– Antti Haapala01/14/2019 06:53:50Commented Jan 14, 2019 at 6:53
On Stack Overflow, all the time I'm seeing this happen to people who don't know the syntax.
class Foo {
public static void main(String[] args) {
// y my code no is working
Sytem.out.println(qrew3r);
}
}
Some people stop there and never fix it because it's close enough. Others figure out that the first and last lines aren't part of the code block because they're not indented. So they indent those two lines - only those two lines - because the editor doesn't provide an apparent way to indent the whole block.
class Foo {
public static void main(String[] args) {
// y my code no is working
Sytem.out.println(qrew3r);
}
}
The result is that we have a site that systematically causes newbs to post questions with poorly-formatted code. Whether or not you like fenced code blocks, when you look at all of the mistakes that users are making, you've got to appreciate that the current UX is broken.
-
9..."the editor doesn't provide an apparent way to indent the whole block". There is, though not apparent:
Ctrl
+k
after you select the block. (On Mac it's⌘
+k
)Franklin Yu– Franklin Yu09/17/2016 23:53:44Commented Sep 17, 2016 at 23:53 -
1@FranklinYu ooh, I did not know that. Thanks.ArchNoob– ArchNoob04/13/2017 17:49:23Commented Apr 13, 2017 at 17:49
-
13It’s even worse in Python, where you often literally cannot tell what the code is supposed to do. In brace-delimited languages you can work past the bad formatting.Daniel H– Daniel H12/19/2017 01:12:20Commented Dec 19, 2017 at 1:12
-
yes, this is really annoying and unintuitive. I have to google on how to fix this and goes back to StackOverflow to write an answer. The problem will grow bigger when combining with a list and long lines of code.Aldo Wachyudi– Aldo Wachyudi01/23/2018 02:28:47Commented Jan 23, 2018 at 2:28
-
That is what Reject and edit is for.CPlus– CPlus04/21/2024 18:00:25Commented Apr 21, 2024 at 18:00
Another reason to include this syntax: Some people are already using it.
However, it makes their posts look kind of strange, since it becomes inline code spread over multiple lines:
This is what it looks like
when you put triple backticks above and below
multiple lines of text.
By contrast, this is
a multi-line code block
created by indenting text four spaces.
-
1On 10 million questions the numbers are rather low: data.stackexchange.com/stackoverflow/query/488893?year=-105/20/2016 20:15:04Commented May 20, 2016 at 20:15
-
4@rene What if you add old revisions that people have fixed in the meantime?Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'05/20/2016 20:44:00Commented May 20, 2016 at 20:44
-
@Gilles uuugghh, that will probably kill SEDE ;) I'll give it a try later05/20/2016 20:45:39Commented May 20, 2016 at 20:45
-
1@rene Does your query account for every version in a post's history? I find and fix triple-backtick posts to proper syntax all the time, and I wonder if the "low" number is only low due to human effort. Personally, I don't mind doing it, but if I and others no longer needed to fix these posts, we could use that time for something more useful.Dan Lowe– Dan Lowe01/14/2017 22:01:06Commented Jan 14, 2017 at 22:01
-
@DanLowe no that query didn't take posthistory into account. This one does but I limited both the date range as well as the number of rows because the full table scan on the posthistory table is killing the performance and it timesout if I fetch much more rows.01/14/2017 23:03:05Commented Jan 14, 2017 at 23:03
You can currently specify a language to be syntax-highlighted.
Example:
<!-- language: lang-js -->
//JS code...
This should not discourage the question, it is even a plus because they don't need to code this feature anymore.
The only thing left to do is to implement the ```` code blocks system (which I believe, is not that hard)
-
2Good to know — thanks! Sounds like a regular expression that converts the triple backtick-syntax into the existing syntax behind the curtains could be a quick fix.Mathias Bynens– Mathias Bynens03/10/2012 08:14:09Commented Mar 10, 2012 at 8:14
-
31This is not as nice than the triple-backtick syntax though.ThiefMaster– ThiefMaster03/23/2012 07:26:52Commented Mar 23, 2012 at 7:26
-
2github supports the same using ```php for examplepocesar– pocesar04/11/2013 20:35:58Commented Apr 11, 2013 at 20:35
-
@ThiefMaster, Exactly. This is a bother to type (or even to use the UI), often I just don't bother and do it the "old way".Pacerier– Pacerier02/18/2016 20:22:31Commented Feb 18, 2016 at 20:22
-
4This
<!-- language: lang-js -->
is horrific! I always need to go Google to remember the exact syntaxYves M.– Yves M.05/09/2018 09:16:48Commented May 9, 2018 at 9:16
Another major benefit is when marking down code within lists. Creating block codes within list is often a major annoyance, contrast:
- Bullet 1
Lorem Ipsum (paragraph 1).
Lorem Ipsum (paragraph 2).
var foo1 = bar1;
var foo2 = bar2;
var foo3 = bar3;
Lorem Ipsum (paragraph 3).
to:
- Bullet 1
Lorem Ipsum (paragraph 1).
Lorem Ipsum (paragraph 2).
```javascript
var foo1 = bar1;
var foo2 = bar2;
var foo3 = bar3;
```
Lorem Ipsum (paragraph 3).
Bulk of the annoyance comes from the editor not being able to do 8-space indents as CtrlK un-indents instead of converting four spaces to eight. "Also, is that blank gap 7 spaces or 9 spaces?"
Of course, the problem is magnified when trying to put block codes within nested lists. Contrast:
- Bullet 1
- Bullet 1.1
Lorem Ipsum (paragraph 1).
Lorem Ipsum (paragraph 2).
var foo1 = bar1;
var foo2 = bar2;
var foo3 = bar3;
Lorem Ipsum (paragraph 3).
to:
- Bullet 1
- Bullet 1.1
Lorem Ipsum (paragraph 1).
Lorem Ipsum (paragraph 2).
```javascript
var foo1 = bar1;
var foo2 = bar2;
var foo3 = bar3;
```
Lorem Ipsum (paragraph 3).
It gets worse with double nested lists. Contrast:
- Bullet 1
- Bullet 1.1
- Bullet 1.1.1
Lorem Ipsum (paragraph 1).
Lorem Ipsum (paragraph 2).
var foo1 = bar1;
var foo2 = bar2;
var foo3 = bar3;
Lorem Ipsum (paragraph 3).
to:
- Bullet 1
- Bullet 1.1
- Bullet 1.1.1
Lorem Ipsum (paragraph 1).
Lorem Ipsum (paragraph 2).
```javascript
var foo1 = bar1;
var foo2 = bar2;
var foo3 = bar3;
```
Lorem Ipsum (paragraph 3).
Hopefully inserting block codes within list will be less time-consuming after the update.
Another (very) minor benefit is that you can use this syntax to break up code blocks. For example, this code (rendered in an engine) will create three separate blocks with very tight breaks:
```
block1
```
```
block2
```
```
block3
```
I'm not aware of any way to do this using the indent style as unindented lines (no matter how many you interject) don't break the boxes up:
block1
block2
block3
-
8Just interject
<!-->
between the code blocks. I'm tempted to edit your answer to show it, but it's probably better you try for yourself.08/02/2017 20:16:17Commented Aug 2, 2017 at 20:16 -
3
<!>
is enough. Also<?>
(which is three adjacent keys, but out of order) and many other variants. I like</>
.Scott - Слава Україні– Scott - Слава Україні02/13/2019 07:00:18Commented Feb 13, 2019 at 7:00 -
2I've always used an empty comment
<!-- -->
or a horizontal line---
depending on the situationphuclv– phuclv05/24/2019 05:01:40Commented May 24, 2019 at 5:01
Only drawback I could see is being unclear as to which is the 'right way'. But on a whole I think this would be a great feature to add for someone like me who doesn't want to be using either the button above or spacing in.
-
5But there is not "right way". All ways that works are correct.Emil Vikström– Emil Vikström04/02/2014 18:01:09Commented Apr 2, 2014 at 18:01
The main benefit I see coming from this is that you'll be able to see a little more of the code in the edit box without wrapping. Making it a little easier to specify language is nice too, but I find that the tags take care of it in a large majority of cases.
I'm certainly not opposed, but I don't think it will make fixing code formatting that much easier. You can already select the lines and hit the Code Sample button to add the first level of indent. From there on it's the same amount of work to add additional indents--either copy four spaces and use CTRL-V as a tab key for each line, or copy into editor, fix, and copy back. I think being able to select multiple lines and indent/unindent multiple times would be a lot more helpful than this.
On the downside, ```
would be one of the least intuitive bits of markdown. Doesn't really have the same WYSIWYG feel, at least for me. Using the same sequence to start and end the formatted section could also be a bit confusing, particularly when there are multiple code blocks in a post.
Something like {{{
to start and }}}
to end would be clearer, or a custom html tag like, <codeblock></codeblock>
, or just allow the <code>
tag to span multiple lines to create a block. Or, combine with specifying the language, for example: <java></java>
.
-
1It's a big help when you have mixed tabs and spaces, too.Ry-– Ry-08/09/2012 15:36:25Commented Aug 9, 2012 at 15:36
-
2@bemace I know you can select the lines and then hit the Code button, but as a heavy keyboard user, I find typing three backticks, a line break,
Cmd
+V
, another line break, and three closing backticks is much faster.Mathias Bynens– Mathias Bynens08/09/2012 15:36:43Commented Aug 9, 2012 at 15:36 -
1I like the thought of the language tags
<java></java>
.gobernador– gobernador08/09/2012 17:35:54Commented Aug 9, 2012 at 17:35 -
8But why invent a new syntax when several existing Markdown implementations already support the triple backtick syntax? Interoperability (and copy-pastability) with other Markdown flavors is a big plus IMHO!Mathias Bynens– Mathias Bynens08/09/2012 19:05:36Commented Aug 9, 2012 at 19:05
-
@MathiasBynens as I explained, with multiple code blocks delimited by ``` separated by a line or two of text (as is common on Stack Overflow) it could get very confusing trying to determine which ``` are starting a code block, and which are ending a code block-- or rather, which lines are code and which are text.Brad Mace– Brad Mace08/09/2012 19:08:46Commented Aug 9, 2012 at 19:08
-
3@bemace There would be no confusion if the existing Markdown implementations that support this syntax are followed. Triple backtick code blocks need two line breaks before and after (unless they’re at the start or end of the input).Mathias Bynens– Mathias Bynens08/16/2012 08:06:47Commented Aug 16, 2012 at 8:06
-
4@BradMace, It's actually even more confusing to invent a New Syntax if there's no substantial improvement. There's more people using Github than SE, no harm just following.Pacerier– Pacerier02/18/2016 20:22:03Commented Feb 18, 2016 at 20:22
You must log in to answer this question.
Explore related questions
See similar questions with these tags.
`
is a single key where Ctrl+K is not; as a resultclick ``` click ```
is faster to perform.Ctrl+V, DownArrow, Home
to indent the entire block.