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?


Discourse Security Guide
(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


(Jamie Matthews) #10

Awesome! Thanks @zogstrip! So how do I get this change, just wait for the next discourse release?


(Régis Hanol) #11

If you’re using the docker_manager plugin, you head over to /admin/docker and click the update buttons :wink:


(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
   ```