I posted this answer using text from this python docs page.
I tried using block quotes for the text, but if I indent the code, it breaks the block quote, and if I don't indent it, it doesn't display as code.
I posted this answer using text from this python docs page.
I tried using block quotes for the text, but if I indent the code, it breaks the block quote, and if I don't indent it, it doesn't display as code.
The blockquote Markdown syntax allows for an optional space after >
, so in order to have code formatted inside a blockquote, you will need a >
followed by five spaces instead of the usual four, to give the code formatting the four spaces it needs and let the blockquote formatting eat up its optional space.
To illustrate:
> Some code:
>
> // code goes here
renders as
Some code:
// code goes here
Apparently this also works:
> Some code:
>
> ```lang-js
> const a = 0
> ```
Demo:
Some code:
const a = 0
Useful because the accepted answer doesn't work on Github or on my Jekyll blog.
Here's another option: Since Markdown is a superset of HTML, you can use the <blockquote>
element to wrap the code block (note the blank lines after the opening & before the closing tag):
<blockquote>
```lua
print"Hello World!"
```
</blockquote>
is rendered as:
print"Hello World!"
The advantages of this approach are that (1) it's supposed to work everywhere where CommonMark is supported (which goes beyond SO) and (2) you don't need to prefix every line of code with >
.
You can also add syntax highlighting when you use blockquotes:
Code:
> Some code:
>
> <!-- language: lang-js -->
>
> const a = 0
Demo:
Some code:
const a = 0