Syntax highlighting indented code


#1

Please see this issue for the context surrounding this discussion.

I believe it should be possible to syntax highlight indented code. Presently, this does not appear to be possible.

Ideally, somehow, it would be possible to have raw code blocks (indented by tabs or 2+ spaces) and also specify which language should be used for syntax highlighting.


#2

I believe this issue warrants further discussion.


#3

What would the syntax look like?


#4

Hello, ioquatix.

Can you elaborate on what your use case is? I’ve looked back through the linked issue, but I’m not sure what you’re trying to accomplish that you can’t already. Is there some reason you can’t use backticks? Or is there some other reason?


#5

I am not sure exactly what the syntax should look like, but using the existing mechanism might make sense.

The problem I’m trying to address is to make sure that code blocks are easily readable both as plain text as well as marked up (e.g. to HTML).

The problem right now IMHO is that when you write code, to syntax highlight that code you must do the following:

Paragraph paragraph paragraph

```{language}
code code code
code code code
```

Paragraph paragraph paragraph

In large documents, because you have a lot of paragraphs and code, because code is not indented, it becomes hard to read.

My preference is to have something like this:

Paragraph paragraph paragraph

    code code code
    code code code

Paragraph paragraph paragraph

i.e. the code is indented and is easily distinguishable from the surrounding text.

However, as it currently stands, there is no way to highlight this code.

My suggestion is to use the existing code fencing mechanism, but allow it to be indented.

Does that make sense?


#6

You can still indent fenced code and use as many backticks or tildes as you like, as long as it’s more than 3 and the ending fence has at least as many as the starting one.

Anyhow, markdown processors and their code highlighting modules should support heuristics to guess code languages or settings for the default language outside of the markdown document itself. This should cover most cases and the rest can just use the existing fenced syntax.

One could also adopt the convention to reuse the info-string from fences, but put it into a comment in the first line of the code, using a comment syntax supported by the highlighted language.

 // JS
 document.write("foo");

#7

I don’t know if I agree with this – the way we do it, indented blocks are for “code” that you don’t want syntax highlighted, and fenced code blocks are for code that does need syntax highlighting.

So it’s a feature from my perspective.

We used to syntax highlight everything by default on Stack Overflow (we still might actually) and that was acceptable since it was a site for programmers. But I doubt it is a good idea at all for a general audience.


#8

Pandoc has a command-line option that allows you to set a default class for all indented code blocks. And I have nothing against implementations adding something like this, but it doesn’t seem like something that should be part of the spec. (Ditto for the initial comment suggestion.)


#9

Surely using a fenced code block without specifying a language should be the proper way to say “I don’t want syntax highlighting”? Anyway, it would be nice to have the ability to enable syntax highlighting for indented code blocks. Perhaps something like this:

Paragraph paragraph paragraph

   `bash
    echo "Hello, world!"

Paragraph paragraph paragraph

Here the language name is indented by 3 spaces and a single backtick, while the code is indented by 4 spaces.

(Maybe highlighting could even be enabled for inline code spans using this syntax: `code`(language), which is quite similar to the link syntax: [text](link).)

In the meantime, there is a workaround for enabling syntax highlighting of indented code. Simply add the backticks and indent by three spaces instead of 4. For example,

This:

Paragraph paragraph paragraph

   ```bash
   echo "Hello, world!"
   ```
Paragraph paragraph paragraph

Produces this:

Paragraph paragraph paragraph

echo "Hello, world!"

Paragraph paragraph paragraph


#10

Not exactly; sometimes you just want monospace font and it has zip to do with any programming language.

Remember GitHub, a programming source control site essentially invented the 3 backticks. Guess why they invented it? :wink:

Anyway I propose this as a helpful rule of thumb, not an ironclad law that must be adhered to under penalty of death, or encoded into the spec, or anything.