Confused about highlighting in code blocks


(dougalcorn) #1

I have some chat logs I’m trying to paste into discourse:

[ 2013.07.27 03:03:37 ] a PATRIOT > grrr we still haven't finished our fitting
[ 2013.07.27 03:03:43 ] a PATRIOT > sorry it's gonna be a few minutes
[ 2013.07.27 03:03:45 ] Bronya Boga > we can wait
[ 2013.07.27 03:03:48 ] a PATRIOT > k
[ 2013.07.27 03:04:13 ] Zed Vudu > Oh, it's gonna be so BROUGTEN you'll be remembering the brang for a long time!
[ 2013.07.27 03:04:37 ] Cethion > Parse error
[ 2013.07.27 03:13:38 ] a PATRIOT > i literally have
[ 2013.07.27 03:13:45 ] a PATRIOT > 12 thorax and 6 vexor hulls sitting in SMA
[ 2013.07.27 03:13:46 ] a PATRIOT > unfit
[ 2013.07.27 03:26:56 ] Sir Lomax > 7o

I’ve tagged the block as irc, but there seems to be some auto-detection going wrong. In this preview the code block has class='irc matlab'. On my forum, it has class='irc applescript'. It causes what I’m assuming are applescript keywords to show up as bold and numbers to be highlighted.

I’m not sure how to tackle this problem. How does the highlighting work? What library is it? How can I extend it to handle the markup for these chat logs?


(Jeff Atwood) #2

I would not recommend using fenced code blocks for things that are not code. Try indenting the lines by 4 spaces, or select the text and use the preformatted toolbar button, to insert 4 spaces before each line and indicate a preformatted block instead.

Irc is cool, and all, but code it ain’t. :wink:


(dougalcorn) #3

I hear what you’re saying. Can you help me understand a bit better though how the code block highlighting works? My understanding is that github syntax highlighting uses linguist. When I paste the above chat log into a gist and mark it as “irc logs”, it’s basically the same as saying no formatting (as you can see on this gist).

I thought discourse was also using linguist, so I thought the triple-backtick with the irc label would work. It seems likely there will be a lot of chat logs pasted into one of my forums. It’d be nice to figure out some way to format it nicely. I’d take a whack at formatting it if I knew how it was getting done.


(Régis Hanol) #4

Even though Discourse supports GitHub style fenced code, it uses highlight.js for syntax highlighting:

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/components/syntax_highlighting.js


(Jamie Matthews) #5

I have a site up where I post almost exclusinvely Go code. I see here that highligh.js doesnt support Go by default. Any idea how I could add this into the highlighting?


(Sam Saffron) #6

Golang is supported, see: highlight.js demo

You can set the default syntax highlighting language in site settings “default code lang”


(Régis Hanol) #7

Actually I think we use a custom build of highlight.js that doesn’t support Go. I’ll check that tomorrow.


(Jamie Matthews) #8

@zogstrip thanks for looking into it.

package main

import (
    "fmt"
    "os"
)

const (
    Sunday = iota
    numberOfDays  // this constant is not exported
)

As you guys can see here, the highlighting doesnt match the highlight.js example (import, const not bolded, comments not greyed)


(Régis Hanol) #9

Just updated our highlight.js version with support for the Go language

https://github.com/discourse/discourse/commit/2aa907e34e0ff237e37d5e79f0f56042ba3978b5


(Marcin Rataj) #12

Guys, I think I’ve found a regression: is golang is not supported in 1.3.0.beta5 release?
This post looks like this:

I checked https://meta.discourse.org/raw/8708/8 and it has explicit syntax definition:

```go

but the comment is not greyed out…


(Marcin Rataj) #13

Nevermind, I’ve found that now we can pick supported languages in /admin:


(Christoph) #14

I’m also confused about code block highlighting. We have autohighlight all code turned off but when someone pastes a log file, for example, it is still being highlighted in weird ways (similar to the OP). My assumption was that with autohighlighting turned off, highlighting would only occur if a language is explicitly defined (i.e. ~~~ruby etc). Is that assumption wrong or is there a bug?


(Eli the Bearded) #15

I’m going to guess this changed with the new Markdown engine. Among other things, now highlighting happens even before / without a closing codefence:

int main(int argc, char**argv) {

(Sam Saffron) #16

The default for this setting is off.

When it is changed to on this happens:

Or … in English…

Markdown like this:

def not_treated_like_code
   hello = 1
end
    def not_treated_like_code
       hello = 1
    end

Is highlighted.


(Christoph) #17

Okay, that’s what I thought. But I have two fora with autohighlight all code set to off, i.e. the default setting but codeblocks is nevertheless being highlighted. So is this a bug?


(cpradio) #18

No, I don’t think you understand. The difference is between using spaces when crafting the code blocks versus backticks.

The setting applies code highlighting when spaces is used. backticks highlights regardless.


(Christoph) #19

Okay, now I understand.

In that case, I wonder if highlighting could also be turned off for “real” code blocks, i.e. those with ``` or ~~~?

The reason for this is that I have always found it a pain to indent log files I pasted, especially on mobile, but also on desktop, so that I was relieved to find that I can basically achieve the same with ~~~ before and after the pasted text.

So it would really be excellent to be able to turn off highlighting in code blocks.

Actually, I just figured it out: the solution is to set default code lang to text.


(Eli the Bearded) #20

You can also name the highlighter to be used on the code fence:

   ```text
   copious log output here
   even more log output here
   ```

(Christoph) #21

Yes, but I’m already more than happy when our users actually use the ~~~, I don’t want to complicate things :wink:


(Christoph) #22

A year later, I stumbled across this setting again and was happy to see that I asked about it before. And now I also found why this setting is so confusing. Compare the solution here:

with what the help text says:

Force apply code highlighting to all preformatted code blocks even when they didn’t explicitly specify the language

Note that the “even…” part suggests that this setting is about enforcing highlighting when no language is selected. It doesn’t say anything about the method with which the code block was generated (spaces vs backticks vs tildes).

So I’m still not sure I understand the logic behind this 100 percent, but maybe it should say something like

Force apply code highlighting to all preformatted code blocks even when indentation is used instead of code fences.

Or is it the other way around?

Force apply code highlighting to all preformatted code blocks even when code fencing is used.

I have omitted the “even when they didn’t explicitly specify the language” part because that is already obvious when saying “force apply”.