Code formatting changes while typing the code

Regarding this post of mine.

I noticed the different code formatting of my post compared to the OP.
I then played around a little bit, trying to match the formatting of my code to the one in the OP, and I noticed that the code formatting changes while I change the code. Watch the following gif-animations:

firefox_C1edwtkncg
firefox_wUnJCVaEN2

I expect that the formatting of code only changes if I change/specify the programming language, for example by starting the code block with:

```php

Am I doing something wrong?

1 Like

I haven’t experimented much with code blocks, this is the official doc about how to use code blocks:

1 Like

Thanks!

The official doc says:

By default, Discourse will try to automatically detect what language you are typing and style your code accordingly. This might be nice for sharing code, but tends to produce strange results when sharing other preformatted text. To avoid the automatic styling, declare your code to be “raw text” by typing text after the first 3 back ticks.

In addition to text, you can choose a number of other languages. The default list can be found on GitHub . Site admins can add or remove from the list to customize it for their site.

The default list consists of the following entries:
apache, bash, cs, cpp, css, coffeescript, diff, xml, http, ini, json, java, javascript, makefile, markdown, nginx, objectivec, ruby, perl, php, python, sql, handlebars

So Discourse tried to recognize the programming language in my code by itself and therefore sometimes switched back and forth between different formattings.

Formatting with “cpp” oder “java” seems to work best for Overpass-Queries because comments are recognized correctly:

[out:json][timeout:25];
// gather results
(
  // query part for: “"tiger:county"="Calvert, MD" and "tiger:reviewed"=*”
  way["tiger:county"="Calvert, MD"]["tiger:reviewed"];
);
// print results
out geom;

@Map_HeRo Maybe include this in your Discourse-Formatting-Guide in the chapter about codeblocks?

@nukeador Maybe a custum entry could be added to the list of possible languages, something like “overpass” or “ql”. Or the default format could be changed to something like this. Overpass-Queries will be by far the most shared code in this forum. But I guess this is more something for a minor feature-request rather than a site-issue… Anyways, thanks again.

1 Like

I see this setting

In the list of languages I don’t see overpass or ql, not sure what happens if we add that manually, which language will autodetect?

Well does the syntax highlighter discourse uses have support for overpass or ql? I doubt it does, so adding them is unlikely to do anything…

Changing this setting to “cpp” would already help a lot I think and is a simple solution. Then most of the code published in this forum would look good and also consistent, even without having to explicitly specify the language first. With the “auto”-setting every block of code might be formatted different.

If you want I can create a feature request to discuss it with some other people too.

Of course if you were to add “overpass” or “ql” to the list, some script with formatting instructions would have to be created, so this would be a much more difficult solution. And then again people would have to know about the possibility to even use this.

Example overpass-query formatted with 'cpp'
// Find all bridges and tunnels that have an inner node connected to another way that is not a bridge or tunnel.
// By Shaun das Schaf

[timeout:600];

{{geocodeArea:"Freiburg"}}->.searchArea;

// BRIDGES
way[highway][bridge=yes](area.searchArea); // Select all bridges

foreach ->.b
{
  node(w.b)->.n;
  if (n.count(nodes) > 2) // Only process bridges with at least one inner node
  {
    (.n; - node(w.b:1,-1);); // Select inner nodes
    foreach
    {
      way(bn)[highway][bridge!=yes]->.p; // For each node select the parent ways
      if (p.count(ways) >= 1)
      {
        (.b; .p;); // Output bridge and connected way
        out;
        >;
        out skel;
      }
    }
  }
}

// TUNNELS
way[highway][tunnel=yes](area.searchArea); // Select all tunnels

foreach ->.t
{
  node(w.t)->.n;
  if (n.count(nodes) > 2) // Only process tunnels with at least one inner node
  {
    (.n; - node(w.t:1,-1);); // Select inner nodes
    foreach
    {
      way(bn)[highway][tunnel!=yes]->.p; // For each node select the parent ways
      if (p.count(ways) >= 1)
      {
        (.t; .p;); // Output tunnel and connected way
        out;
        >;
        out skel;
      }
    }
  }
}



{{style:

way
{ color:red; fill-color:red; }

way[tunnel=yes]
{ color:green; fill-color:green; }
  
way[bridge=yes]
{ color:blue; fill-color:blue; }

}}

Discourse seems to use highlight.js. Ovepass/ql is not in supported languages:

I think list of languages available in Discourse is limited to BUNDLED_LANGS discourse/highlight_js.rb at 64171730827c58df26a7ad75f0e58f17c2add118 · discourse/discourse · GitHub

So basically in order to make one use case (overpass/ql script examples) work without the user having to specify a language you propose to remove auto detection from all the other languages where it would work.

So instead of you having to add cpp to your code blocks everybody adding a code block that isn’t overpass or C++ has to instead.

Does that really sound like a reasonable, non-selfish, solution to you?

I’m just thinking about possible solutions. For me it is clear now how to format overpass-queries the way I want them to. And I don’t have any problem with adding the cpp to a codeblock.

I just find it a pity that exactly for the language that is probably posted here in the forum by far the most often, the automatic detection fails. :confused:
But I get your point.